Posts

Showing posts from June, 2009

C# 2.0 syntax reference

readonly - member variable of class where can only be initialized once in constructor.

yield - contruct return for IEnumerable

e.g.

IEnumerable GetNumbers()
{
yield return 1;
yield return 2;
yield return 3;
yield return 4;
yield break;
}

This is return a 1,2,3,4 list.





this() - calling class constructor



this[] - [] operator override

e.g

public int this[int i]
{
get
{
return 1;
}

}



generic programming

//where TbaseClass is use for T calling function
class C1<T>:D1 where T:TbaseClass

class B1 : C1<myInherittedTbaseClass>

C# pass-by-value and reference type

If you are coming from C/C++ programming background. You would be accustomed with heavy use of pass by reference (or pass by pointer) as a way to improve the performance, update the variable passed in and reducing memory consumption. It is so common, that most C/C++ function written to be efficient would be passing by constant reference (if they do not wish to allow variable passed in get updated)

Like

void myFunction(const MyClass & param)


So, you are rest assured that variable pass in would not get updated.


For C#, there is no such thing as const reference. But, there is pass-by-ref, unsafe pointer and pass-by-value.
So, to ensure that your variable passed in to function not get updated. You would naturally think of using pass-by-value.

However, there is a catch for pass-by-value if your object is of reference type (class, interface, delegate, objet, string)

Value type variable store value but reference type variable store references to object. So, even the reference is passed b…