The 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:
Partial class
Partial classes allow 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 in Visual Studio.
file1.cs:
public partial class MyClass
{
public void MyMethod1
{
// Manually written code
}
}
file2.cs:
public partial class MyClass
{
public void MyMethod2
{
// Automatically generated code
}
}
In a broad definition, generic programming is a style of computer programming in which algorithms are written in terms of to-be-specified-later types that are then instantiated when needed for specific types provided as parameters...
, or parameterized types, or parametric polymorphism is 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 use reification
Reification (computer science)
Reification is the process by which an abstract idea about a computer program is turned into an explicit data model or other object created in a programming language. A computable/addressable object — a resource — is created in a system as a proxy for a non computable/addressable object...
In programming language design, a first-class citizen , in the context of a particular programming language, is an entity that can be constructed at run-time, passed as a parameter, returned from a subroutine, or assigned into a variable...
The Common Language Infrastructure is an open specification developed by Microsoft and standardized by ISO and ECMA that describes the executable code and runtime environment that form the core of the Microsoft .NET Framework and the free and open source implementations Mono and Portable.NET...
Virtual Machine, which allows for optimizations and preservation of the type information.
Static classes
Static classes are classes that cannot be instantiated or inherited from, and that only allow static members. Their purpose is similar to that of modules in many procedural languages.
Generator functionality
The .NET 2.0 Framework allowed C# to introduce an iterator
Iterator
In computer programming, an iterator is an object that enables a programmer to traverse a container. Various types of iterators are often provided via a container's interface...
In computer science, a generator is a special routine that can be used to control the iteration behaviour of a loop. A generator is very similar to a function that returns an array, in that a generator has parameters, can be called, and generates a sequence of values...
functionality, using a yield return construct similar to yield in Python. With a yield return, the function automatically keeps its state during the iteration.
// Method that takes an iterable input (possibly an array)
// and returns all even numbers.
public static IEnumerable GetEven(IEnumerable numbers)
{
foreach (int i in numbers)
{
if (i % 2 0)
yield return i;
}
}
There is also a yield break statement, in which control is unconditionally returned to the caller of the iterator. There is an implicit yield break at the end of each generator method.
Anonymous delegates
As a precursor to the lambda functions introduced in C# 3.0, C#2.0 added anonymous delegates. These provide closure
Closure (computer science)
In computer science, a closure is a function together with a referencing environment for the non-local variables of that function. A closure allows a function to access variables outside its typical scope. Such a function is said to be "closed over" its free variables...
-like functionality to C#. 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, excepting out and ref parameters. For example:-
int SumOfArrayElements(int[] array)
{
int sum = 0;
Array.ForEach(array,
delegate(int x)
{
sum += x;
}
);
return sum;
}
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 a fuller discussion of this distinction.
A delegate is a form of type-safe function pointer used by the .NET Framework. Delegates specify a method to call and optionally an object to call the method on. They are used, among other things, to implement callbacks and event listeners....
Within the type system of a programming language, covariance and contravariance refers to the ordering of types from narrower to wider and their interchangeability or equivalence in certain situations ....
in return and parameter types, respectively.
The accessibility of property accessors can be set independently
Example:
string status = string.Empty;
public string Status
{
get { return status; } // anyone can get value of this property,
protected set { status = value; } // but only derived classes can change it
}
In programming, nullable types are a feature of some statically-typed programming languages which allows a data type to be set to the special value NULL instead of their common range of possible values....
(denoted by a question mark, e.g. int? i = null;) which add null to 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 SQL INTEGER NULL column type directly translates to the C# int?.
Nullable types received an improvement at the end of August 2005, shortly before the official launch, to improve their boxing characteristics: a nullable variable which is assigned null is not actually a null reference, but rather an instance of struct Nullable with property HasValue equal to false. When boxed, the Nullable instance 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;
object o = 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 from Nullable instances, so null values and null references are considered equal. The late nature of this fix caused some controversy
, since it required core-CLR
Common Language Runtime
The Common Language Runtime is the virtual machine component of Microsoft's .NET framework and is responsible for managing the execution of .NET programs. In a process known as just-in-time compilation, the CLR compiles the intermediate language code known as CIL into the machine instructions...
changes affecting not only .NET2, but all dependent technologies (including C#, VB, SQL Server 2005 and Visual Studio 2005).
Null-Coalesce operator
The ?? operator is called the null-coalescing operator and 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.