|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionWell, in this article I'll illustrate some of the C# 3.0 new language and compiler features and I'll illustrate the rest of the features in the second part. First of all, let's define all the new features:
In this article, I will define the first four features with code samples to make it clear. Implicitly Typed Local Variables and ArraysInstead of using the explicit type, now we can use the inferred type which means declaring any local variable as
Now let's see how local variables can be declared with var int_variable = 6; // int_variable is compiled as an int
var string_variable = "Mony"; // string_variable is compiled as a string
var int_array = new[] { 0, 1, 2 }; // int_array is compiled as int[]
// Query is compiled as IEnumerable
Restrictions when using implicitly-typed variables are as follows :
Object InitializersSometimes you spend a lot of time writing a lot of redundant code to declare constructors that do the same job. Object initializers can be used to initialize types without writing explicit constructors. Code Example 1private class Person
{
// Auto-implemented properties
public int Age { get; set; }
public string Name { get; set; }
}
static void Test()
{
// Object initializer
Person per = new Person { Age = 22, Name = "Mony" };
}
Code Example 2class Point
{
int x, y;
public int X
{
get { return x; }
set { x = value; }
}
public int Y
{
get { return y; }
set { y = value; }
}
}
When you instantiate this class, you normally write the following code: Point p = new Point();
p.X = 10;
p.Y = 20;
Instead, you can create and initialize a Point p = new Point { X = 10, Y = 20 }; // object initializer
Or even like this: var p = new Point { X = 10, Y = 20 }; // object initializer
With complex fields, such as a square or a rectangle whose corners are located at the points public class Rectangle
{
Point p1;
Point p2;
public Point ULcorner { get { return p1; } set { p1 = value; } }
public Point LRcorner { get { return p2; } set { p2 = value; } }
}
You can create and initialize the var rectangle = new Rectangle { ULcorner = new Point { X = 0, Y = 0 },
LRcorner = new Point { X = 10, Y = 20 } };
Collection InitializersEnables initialization of collections with an initialization list rather than specific calls to public class Person
{
string _Name;
List _Intersets = new List();
public string Name { get { return _Name; } set { _Name =value; } }
public List Interests { get { return _Intersets; } }
}
class Test
{
static void Main(string[] args)
{
List
In C# 3.0, you can write less code to express the same concept: static void Main(string[] args)
{
var PersonList = new List
Extension MethodsExtension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. To create an extension method, declare it as a The following is an example of an extension method to convert the temperature from Fahrenheit to Celsius. namespace MyNameSpace
{
public static class MyClass
{
public static double ConvertToCelsius(this double fahrenheit)
{
return ((fahrenheit – 32) / 1.8); }
}
}
}
Now it is possible to invoke the extension method, double fahrenheit = 98.7;
double Celsius = fahrenheit.ConvertToCelsius();
So it adds a method called Hope this simple article makes the C# 3.0 new language features quite clear. In the next article, I'll discuss the other five features. Waiting for your feedback.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||