Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Thursday, September 15, 2011

new keyword in C#

In C# the “new” keyword can be used in 3 different ways, as operator, modifier and constraint. We all are familiar with new operator usage, i.e. for creating object and invoking constructor.  But other two usages are not much popular in normal coding.

new” Operator
It is used for instantiating objects and invoking constructors. For example,
BaseClass baseClass = new BaseClass();

new” Modifier
The new keyword can be used for hiding the inherited member of base class. If the base class and derived class having the member with same name and type, and we need to hide the base class member in child class, then we can use the “new” keyword to hide the base class member.
public class BaseClass
{
    public int GetValue() { return 10; }
}

public class DerrivedClass : BaseClass
{
    public new int GetValue() { return 20; }
}

Here the BaseClass and DerrivedClass have the same method and we are hiding the BaseClass member in DerrivedClass using “new”.

new” Constraint
Here new keyword is using as part of generic class definition.  If the generic class needs to create any instances of the specified type, then we need to specify the new constraint. As an example,
public class MyFormatter
    where T : IFormattable
    where F : IMyFormatProvider, new()
{
    public T Value { get; set; }

    public MyFormatter(T Value)
    {
        this.Value = Value;
    }

    public string Format()
    {
        F formatter = new F();
        return Value.ToString(formatter.GetFormatter(), null);
    }
}

In this scenario we need to make sure that the specified class having Parameter Less constructor. In this example, we are instantiating new class instance of type F, so we need to specify “new()” over the generic class definition. If we missed to specify “new”, then we will get the build error like” Cannot create an instance of the variable type 'F' because it does not have the new() constraint”.

Tuesday, June 15, 2010

C# 3.0 - Features

1. Object and Collection Initializers
Object initializers let you assign values to any accessible fields or properties of an object at creation time without having to explicitly invoke a constructor.
http://msdn.microsoft.com/en-us/library/bb384062.aspx

2. Anonymous types - (var)
Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to first explicitly define a type. The type name is generated by the compiler and is not available at the source code level. The type of the properties is inferred by the compiler.
http://msdn.microsoft.com/en-us/library/bb397696.aspx

3. Auto Implemented properties.

Auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors. They also enable client code to create objects
http://msdn.microsoft.com/en-us/library/bb384054.aspx

4. Lambda Expression
A lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types.
http://msdn.microsoft.com/en-us/library/bb397687.aspx

5. Extension method
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. For client code written in C# and Visual Basic, there is no apparent difference between calling an extension method and the methods that are actually defined in a type
http://msdn.microsoft.com/en-us/library/bb383977.aspx