Jump to content

C Sharp 2.0

From Wikipedia, the free encyclopedia

Thecomputer programming language,C#,introduces several new features in version 2.0 (corresponding to the 3rd edition of the ECMA-334 standard and the.NET Framework 2.0). These include:

C# 2.0 Features

[edit]

Partial class

[edit]

Partial classesallow implementation of a class to be spread between several files, with each file containing one or more class members. It is useful primarily when parts of a class are generated automatically. For example, the feature is heavily used by code-generating user interface designers inVisual Studio.

file1.cs:

publicpartialclassMyClass
{
publicvoidMyMethod1()
{
// Manually written code
}
}

file2.cs:

publicpartialclassMyClass
{
publicvoidMyMethod2()
{
// Automatically generated code
}
}

Generics

[edit]

Generics,or parameterized types, orparametric polymorphismis a.NET 2.0 feature supported by C# and Visual Basic. Unlike C++ templates,.NET parameterized types are instantiated at runtime rather than by the compiler; hence they can be cross-language whereas C++ templates cannot. They support some features not supported directly by C++ templates such as type constraints on generic parameters by use of interfaces. On the other hand, C# does not support non-type generic parameters. Unlike generics in Java,.NET generics usereificationto make parameterized typesfirst-class objectsin theCLIVirtual Machine, which allows for optimizations and preservation of the type information.[1]

Static classes

[edit]

Static classes are classes that cannot be instantiated or inherited from, and that only allow static members. Their purpose is similar to that ofmodulesin many procedural languages.

Generator functionality

[edit]

The.NET 2.0 Framework allowed C# to introduce aniteratorthat providesgeneratorfunctionality, using ayield returnconstruct similar toyieldinPython.[2]With ayield return,the function automatically keeps its state during the iteration.

// Method that takes an iterable input (possibly an array)
// and returns all even numbers.
publicstaticIEnumerable<int>GetEven(IEnumerable<int>numbers)
{
foreach(intiinnumbers)
{
if(i%2==0)
yieldreturni;
}
}

There is also ayield breakstatement, in which control is unconditionally returned to the caller of the iterator. There is an implicityield breakat the end of each generator method.

Anonymous delegates

[edit]

As a precursor to thelambda functionsintroduced in C# 3.0, C#2.0 added anonymous delegates. These provideclosure-like functionality to C#.[3]Code inside the body of an anonymous delegate has full read/write access to local variables, method parameters, and class members in scope of the delegate, exceptingoutandrefparameters. For example:-

intSumOfArrayElements(int[]array)
{
intsum=0;
Array.ForEach(array,
delegate(intx)
{
sum+=x;
}
);
returnsum;
}

Unlike some closure implementations, each anonymous delegate instance has access to the same relative memory location for each bound variable, rather than to the actual values at each creation. See afuller discussionof this distinction.

Delegate covariance and contravariance

[edit]

Conversions from method groups todelegate typesarecovariant and contravariantin return and parameter types, respectively.[4]

The accessibility of property accessors can be set independently

[edit]

Example:

stringstatus=string.Empty;

publicstringStatus
{
get{returnstatus;}// anyone can get value of this property,
protectedset{status=value;}// but only derived classes can change it
}

Nullable value types

[edit]

Nullable value types(denoted by a question mark, e.g.int? i = null;) which addnullto the set of allowed values for any value type. This provides improved interaction with SQL databases, which can have nullable columns of types corresponding to C# primitive types: an SQLINTEGER NULLcolumn type directly translates to the C#int?.

Nullable value types received an improvement at the end of August 2005, shortly before the official launch, to improve theirbo xingcharacteristics: a nullable variable which is assigned null is not actually a null reference, but rather an instance ofstruct Nullable<T>with propertyHasValueequal tofalse.When boxed, theNullableinstance itself is boxed, and not the value stored in it, so the resulting reference would always be non-null, even for null values. The following code illustrates the corrected flaw:

int?i=null;
objecto=i;
if(o==null)
System.Console.WriteLine("Correct behaviour - runtime version from September 2005 or later");
else
System.Console.WriteLine("Incorrect behaviour - pre-release runtime (from before September 2005)");

When copied into objects, the official release boxes values fromNullableinstances, so null values and null references are considered equal. The late nature of this fix caused some controversy[5] , since it required core-CLRchanges affecting not only.NET2, but all dependent technologies (including C#, VB, SQL Server 2005 and Visual Studio 2005).

Null-coalescing operator

[edit]

The??operator is called thenull coalescing operatorand is used to define a default value for nullable value types as well as reference types. It returns the left-hand operand if it is not null; otherwise it returns the right operand.[6]

objectnullObj=null;
objectobj=newObject();
returnnullObj??obj;// returns obj

The primary use of this operator is to assign a nullable type to a non-nullable type with an easy syntax:

int?i=null;
intj=i??0;// If i is not null, initialize j to i. Else (if i is null), initialize j to 0.

References

[edit]
  1. ^"An Introduction to C# Generics".Microsoft.January 2005.RetrievedJune 18,2009.
  2. ^"yield".C# Language Reference.Microsoft.Retrieved2009-04-26.
  3. ^"Anonymous Methods (C#)".C# Programming Guide.Microsoft.RetrievedJune 18,2009.
  4. ^"Covariance and Contravariance in Delegates (C#)".C# Programming Guide.Microsoft.RetrievedJune 18,2009.
  5. ^Somasegar (August 11, 2005)."Nulls not missing anymore".Somasegar's WebLog.MSDN.Retrieved2008-11-05.
  6. ^"?? Operator".C# Reference.Microsoft.Retrieved2008-11-23.