Search This Blog

Monday, October 28, 2013

.NET Framework

The .NET Framework is a development framework build by Microsoft for building apps for Windows, Windows Phone, Windows Server, and Windows Azure.

Components
  • common language runtime (CLR)
  • .NET Framework class library
The .NET Framework is a managed execution environment.





NET Framework Garbage Collector

.NET Framework > Garbage Collector

The .NET Framework garbage collector manages the allocation and release of memory for applications.
When you create a new object the CLR allocates memory for the object in a managed heap. 
The runtime continues to allocate space for new objects.
Because the memory is not infinite the garbage collector performs a collection in order to free some memory. 

Advantages:
  • don't have to free memory
  • effectively allocate objects  on the managed heap
  • clears memory of the objects no longer being used
  •  memory safety

Friday, October 25, 2013

Float type in C#

C# > Types > Float

Float is a simple type that stores 32-bit floating-point values.

By default, a float on the right side of the assignment operator is treated as double.
If you do not use the suffix F, you will get a compilation error because you are trying to store a double value into a float variable:

float x = 7.5; // Error Literal of type double cannot be implicitly converted to type 'float'; use an 'F' suffix to create a literal of this type

To initialize a float variable, use the suffix f or F:

float x = 7.5F; // Ok





Char type in C #

Types in C# > Char

Char type in C# declares a Unicode character.
Unicode characters are 16-bit characters that are used to represent most of the known written languages throughout the world.

Example of char:

1.
char[] chs = new char[2];
chs[0] = 'U'; // Character literal
chs[1] = (char)85; // Cast from integral type
foreach (char c in chs)
   MessageBox.Show(c.ToString());

2. Null Char




Thursday, October 24, 2013

Bool Type C Sharp

C# > Types > Bool

bool type in C# it is used to declare variables to store the Boolean values, true and false.

Examples:

bool b = true;
MessageBox.Show(b.ToString()); // Display True

b = null; //Error: Cannot convert null to 'bool' because it is a non-nullable value type

If you need a Boolean variable that can also have a value of null, use bool?

bool? b = true;
b = null; //Ok






Types in C#

C# > Types





Difference between Value types and Reference types

Value types
Reference types
Variable content
Value
Reference
Stored
Stack
Heap
Assignment
Value
Reference
Initial value
0, false,’\0’
null






Ranges

Type
Range
byte
0 .. 255
sbyte
-128 .. 127
short
-32,768 .. 32,767
ushort
0 .. 65,535
int
-2,147,483,648 .. 2,147,483,647
uint
0 .. 4,294,967,295
long
-9,223,372,036,854,775,808 .. 9,223,372,036,854,775,807
ulong
0 .. 18,446,744,073,709,551,615
float
-3.402823e38 .. 3.402823e38
double
-1.79769313486232e308 .. 1.79769313486232e308
decimal
-79228162514264337593543950335 .. 79228162514264337593543950335
char
Unicode character.
string
String of Unicode characters.
bool
True or False.
object
An object.

ICloneable system interface

C# > System > Interfaces > IEquatable

The ICloneable interface enables you to provide a customized implementation that creates a copy of an existing object.

Definition:

public interface ICloneable
    {
        // Summary:
        //     Creates a new object that is a copy of the current instance.
        //
        // Returns:
        //     A new object that is a copy of this instance.
        object Clone();
    }

Example:
        public class Customer : ICloneable
        {
            public string Name { get; set; }
            public Address CustomerAddress { get; set; }
            public object Clone()
            {
                Customer newCustomer = (Customer)this.MemberwiseClone();
                newCustomer.CustomerAddress = (Address)this.CustomerAddress.Clone();
                 return newCustomer;
            }
        }
        public class Address : ICloneable
        {
            public string StreetName { get; set; }
            public int StreetNumber { get; set; }
            public object Clone()
            {
                return this.MemberwiseClone();
            }
        }

        Customer c1 = new Customer();
        c1.Name = "c1";
        Address a1 = new Address();
         a1.StreetName = "Street1";
        a1.StreetNumber = 1;
        c1.CustomerAddress = a1;

        Customer custClone = (Customer)c1.Clone();
        custClone.Name = "c2";