Thursday, 21 July 2011

Generics in c#


Generics introduce to the .NET Framework the concept of type parameters, which make it possible to design classes and methods that defer the specification of one or more types until the class or method is declared and instantiated by client code.
For example, by using a generic type parameter T you can write a single class that other client code can use without incurring the cost or risk of runtime casts or boxing operation.

// Declare the generic class
public class GenericList<T>
{
    void Add(T input) { }
}

class TestGenericList
{
    private class ExampleClass { }
    static void Main()
    {
        // Declare a list of type int
        GenericList<int> list1 = new GenericList<int>();

        // Declare a list of type string
        GenericList<string> list2 = new GenericList<string>();

        // Declare a list of type ExampleClass
        GenericList<ExampleClass> list3 = new GenericList<ExampleClass>();
    }
}

Generics Overview

  • Use generic types to maximize code reuse, type safety, and performance.
  • The most common use of generics is to create collection classes.
  • The .NET Framework class library contains several new generic collection classes in the System.Collections.Generic namespace. These should be used whenever possible in place of classes such as ArrayList in the System.Collections namespace.
  • You can create your own generic interfaces, classes, methods, events and delegates.
  • Generic classes may be constrained to enable access to methods on particular data types.
  • Information on the types used in a generic data type may be obtained at run-time by means of reflection.

Difference between struct and class in c#


Feature
Struct
Class
Notes
Is a reference type?
No
Yes

Is a value type?
Yes
No

Can have nested Types (enum, class, struct)?
Yes
Yes

Can have constants?
Yes
Yes

Can have fields?
Yes
Yes
Struct instance fields cannot be initialized, will automatically initialize to default value.
Can have properties?
Yes
Yes

Can have indexers
Yes
Yes

Can have methods?
Yes
Yes

Can have events?
Yes
Yes
Structs, like classes, can have events, but care must be taken that you don’t subscribe to a copy of a struct instead of the struct you intended.
Can have static members (constructors, fields, methods, properties, etc.)?
Yes
Yes

Can inherit?
No
Yes
Classes can inherit from other classes (or object by default). Structs always inherit from System.ValueType and are sealed implicitly
Can implement interfaces?
Yes
Yes

Can overload constructor?
Yes
Yes
Struct overload of constructor does not hide default constructor.
Can define default constructor?
No
Yes
The struct default constructor initializes all instance fields to default values and cannot be changed.
Can overload operators?
Yes
Yes

Can be generic?
Yes
Yes

Can be partial?
Yes
Yes

Can be sealed?
Always
Yes
Structs are always sealed and can never be inherited from.
Can be referenced in instance members using this keyword?
Yes
Yes
In structs, this is a value variable, in classes, it is a readonly reference.
Needs new operator to create instance?
No
Yes
C# classes must be instantiated using new. However, structs do not require this. While new can be used on a struct to call a constructor, you can elect not to use new and init the fields yourself, but you must init all fields and the fields must be public!

Wednesday, 20 July 2011

Difference between Destructor and Finalize() method in C#


Finalize() Method of Object class
Each class in C# is automatically (implicitly) inherited from the Object class which contains a method Finalize(). This method is guaranteed to be called when your object is garbage collected (removed from memory). You can override this method and put here code for freeing resources that you reserved when using the object.

Example of Finalize Method
Protected override void Finalize()
{
          try
          {
                   Console.WriteLine(“Destructing Object….”);//put some code here.
          }
          finally
          {
                   base.Finalize();
          }
}

Destructor
1) A destructor is just opposite to constructor.
2) It has same as the class name, but with prefix ~ (tilde).
3) They do not have return types, not even void and therefore they cannot return values.
4) Destructor is invoked whenever an object is about to be garbage collected.

Example of Destructor in .Net
class person
{
          //constructor
          person()
          {
          }

          //destructor
          ~person()
          {
                   //put resource freeing code here.
          }
}

Difference between the destructor and the Finalize() method
Finalize() corresponds to the .Net Framework and is part of the System.Object class. Destructors are C#'s implementation of the Finalize() method.
The functionality of both Finalize() and the destructor is the same, i.e., they contain code for freeing the resources when the object is about to be garbage collected.
In C#, destructors are converted to the Finalize() method when the program is compiled.
The Finalize() method is called by the .Net Runtime and we cannot predict when it will be called. It is guaranteed to be called when there is no reference pointing to the object and the object is about to be garbage collected.