15&16. C# 2.0 Features and C# 3.0 Features


Here is a list of new features added to C# version 2.0.
  1. Partial Classes
  2. Static Classes
  3. Property Accessor Accessibility Modifier
  4. Nullable Types
  5. Generics
  6. Anonymous Methods
  7. Iterators
  8. Friend Assemblies

1. Partial Classes
Partial classes is one of the coolest feature added to C# language. Having partial classes in the language provides programmers to write cleaner interfaces. A partial class means "A single class can be defined in multiple physical files.". For example, I can have a ClassA, which is defined in three different AFile.cs, BFile.cs, and CFile.cs physical files.  
Now you may ask why would I want that? Do you remember ASP.NET or Windows Forms applications, where designer (Visual Studio IDE) generated code was written in a region and your code will be added after that. When writing larger user interface driven applications (for example Windows UI or Web UI), you may want to have user interfaces related code in a file, some logic in a separate file and so on. Now using partial classes, we can have designer generated code in a separate file, control event handlers in a separate file, and rest of the code in a separate file.
2. Static Classes
C# 2.0 now supports static classes. Here are static classes properties.
  • A static class cannot be instantiated. That means you cannot create an instance of a static class using new operator.
  • A static class is a sealed class. That means you cannot inherit any class from a static class.
  • A static class can have static members only. Having non-static member will generate a compiler error.
  • A static class is less resource consuming and faster to compile and execute.
Public static class MyStaticClass
{
Private static int myStaticVariable;
Public static int StaticVariable;
{
Get
{
   return myStaticVariable;
}
Set
{
   myStaticVariable = value;
}
}
Public static void Function()
{
}
}
3. Property Accessor Accessibility Modifier
C# 2.0 supports access modifiers for property accessor, which means now you can set access levels to get and set accessors of a property. For example, I can write a property something like this:
public string LoginName
{
  get { return loginName; }
  protected set { loginName = value; }
}

So now I can set LoginName property from any class derived from the class that has this property but I won't be able to set this property from other classes. This feature was not supported in previous version of C#.
I create a class called ABaseClass, which has LoginName property:
class ABaseClass
{
///
///
Property Access Modifiers
///
private string loginName;
///
///
Login Name
///
public string LoginName
{
get { return loginName; }
protected set { loginName = value; }
}
}

Now I create a new class, which is derived from ABaseClass and in this class I set set LoginName property.
class ADerivedClass : ABaseClass
{
public void SetPrivateProperty()
{
base.LoginName = "mcb";
}
}

If I try to set the property value from other classes, I get the following error:



4. Nullable Types
One of the new features of C# 2.0 is nullable types. Now C# 2.0 allows you to assign null values to premitive types such as int, long, and bool.
The following code shows how to define an integer as nullable type and checks if the value of the integer is null or not.
///
///
Nullable types
///
public static void TestNullableTypes()
{
// Syntax to define nullable types
int? counter;
counter = 100;
if (counter != null)
{
// Assign null to an integer type
counter = null;
Console.WriteLine("Counter assigned null.");
}
else
{
Console.WriteLine("Counter cannot be assigned null.");
Console.ReadLine();
}

If you remove the "?" from int? counter, you will see a warning as following: 








For  C# 3.0 Features 


Updates will be in next version.


 
 
Summary

This article offered an overview of the new Microsoft language, C#. C# takes the best features of many present day programming languages. You became familiar with some of the language’s syntax. You learned how to write and compile your first command-line program with the “Hello, C# World!” example. You also became familiar with classes and their members, their scopes, and how to use them. You learned about some unique features, such as events the indexers, which were not available in languages such as C++. In the end of this article, you also saw the new advanced features added to version 2.0 and 3.0.
 


Previous           Back to Introduction