Extension methods enable you to “add” methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type. Continue reading
“Fluent interfaces simplify your object consumption code by making your code more simple, readable and discoverable.”
“Method chaining” is a common technique where each method returns an object and all these methods can be chained together to form a single statement.
The core idea behind building a fluent interface is one of readability – someone reading the code should be able to understand what is being achieved without having to dig into the implementation to clarify details. Continue reading
Interfaces can’t be instantiated by definition. You always instantiate a concrete class. When you use interface variables in your code, it allows you to more easily switch out the concrete implementation, which makes your code more flexible.That’s the power of an interface. Continue reading
What are covariance and contravariance?
In C#, covariance and contravariance enable implicit reference conversion for array types, delegate types, and generic type arguments.Covariance preserves assignment compatibility and contravariance reverses it. In C#, covariance and contravariance are supported only for reference types.
http://www.codeproject.com/Articles/42863/Covariance-and-Contravariance-in-C Continue reading
C# 4.0 now supports using optional parameters with methods, constructors, and indexers (note: VB has supported optional parameters for awhile).
Parameters are optional when a default value is specified as part of a declaration. For example, the method below takes two parameters – a “category” string parameter, and a “pageIndex” integer parameter. The “pageIndex” parameter has a default value of 0, and as such is an optional parameter: Continue reading
The ‘And'(&) operator evaluates both sides, where ‘AndAlso'(&&) only evaluates the right side if the left side is true.
If mystring IsNot Nothing And mystring.Contains("Foo") Then ' bla bla End If
The above throws an exception if
mystring = Nothing
If mystring IsNot Nothing AndAlso mystring.Contains("Foo") Then ' bla bla End If
This one does not throw an exception. Continue reading
int? is shorthand for
int? allows you to have “null” values in your int.
The ?? operator can be very useful in these scenarios. Continue reading
The ?? operator is called the null-coalescing operator and is used to define a default value for nullable value types or reference types. It returns the left-hand operand if the operand is not null; otherwise it returns the right operand.
A nullable type can contain a value, or it can be undefined. The ?? operator defines the default value to be returned when a nullable type is assigned to a non-nullable type. If you try to assign a nullable value type to a non-nullable value type without using the ?? operator, you will generate a compile-time error. If you use a cast, and the nullable value type is currently undefined, an InvalidOperationException exception will be thrown. Continue reading
C# has two type of data types. Value type and reference types. String is reference type and int is value type. A reference type can be assigned with a null value like string s = null; But you can not assign a null value direcly to a inteer. like int a = null . So to make the value type accept a null value, nullable types are used.
Nullable types can represent all the values of an underlying type, and an additional null value.
To represent null in a value type, you must use a special construct called a nullable type. It is represented using ? symbol.
Int32? I=null; //now its ok Continue reading