Sunday, February 20, 2011

Generic Constraints

class SortedList where T: IComparable , new()
{...}

In C# we have generic constraints like this to indicate a where clause to which the class must comply.

Here sortedlist is using a type T. This type must implement the Icomparable interface. The new() states that the type T must have a constructor.

Thursday, February 17, 2011

C# Const and Read Only Difference

We have const variables and read only variables. Is there a difference between them?
There is.

When you declare a const variable it has to be given a value at the time of declaration.

public const int a = 5;

When you declare read only variables you can give a value at the run time. You don’t have to give the value at the time of declaration. But the thing is you have to give the value in the constructor. You cannot give a value anywhere else.

Calss read
{
public readonly int b;

public read(int a)
{
  b = a ;
}

}

Const variables are by default static variables.

Some differences between abstract classes and interfaces



One major difference is that you don’t have to implement all the methods in the abstract classes, unless all the methods are declared abstract.

In an interface all the methods are abstract hence they would have to be implemented in the class that implements the interface. Also there can be no variables and method bodies where as in an abstract class you can have methods with method bodies(implementation)

Sealed keyword in C#


Sealed keyword will be used when you don’t need the class to be inherited furthermore.

e.g : - string class in c#

Virtual and Override methods in C#

You can have methods in your base class with virtual signature and if you want to use the same method in your child class you need to use the term override.

public class a
{
public virtual int methodA()
{}
}

public class B : a
{
public override int methodA()
{}
}





Member Shadowing in C#


You have a the same method in both base and derived class and when you compile the compiler would give you an error saying one method has to overridden. By putting new keyword infront of the child class method you can do this and this is called member shadowing.

Monday, February 14, 2011

Which constructor runs first? Base or Child

public class A
{
public A()
{}
}


Public class B :A
{
public B()
{}
}


B b = new B();

What do you think? Will A constructor run before or B Constructor run?
It would be the base class constructor(A) that would be running before the child constructor(B). Not sure? Run the above code yourself and find out.

Retrieving Calendar of a Bookable Resource in Dynamics

There are occasions where we need to retrieve working days and working times of a resource in Dynamics grammatically. This is quite possible...