C# 3.0 New Language Features (Part 1)
By Mony Hamza
- Implicitly Typed Local Variables and Arrays
- Object Initializers
- Collection Initializers
- Extension Methods
- Anonymous Types
- Lambda Expressions
- Query Keywords
- Auto-Implemented Properties
- Partial Method Definitions
In this article, I will define the first four features with code samples to make it clear.
Implicitly Typed Local Variables and Arrays
- Built-in type
- Anonymous type (will be discussed later)
- User-defined type
- Type defined in the .NET Framework class library
Now let's see how local variables can be declared with var:
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
// anonymous_variable is compiled as an anonymous type
var anonymous_variable = new { Name =
var list = new List"Mony", Job = "Web Developer" };
var int_array = new[] { 1, 10, 100, 1000 }; // int[]
var string_array = new[] { "hello", null, "world" }; // string[]
Restrictions when using implicitly-typed variables are as follows :
- var can only be used when you are to declare and initialize the local variable in the same statement.
- The variable cannot be initialized to null.
- var cannot be used on fields at class scope.
- Variables declared by using var cannot be used in the initialization expression. In other words, var i = i++; produces a compile-time error.
- Multiple implicitly-typed variables cannot be initialized in the same statement.
- If a type named var is in scope, then you will get a compile-time error if you try to initialize a local variable with the var keyword.
// Auto-implemented properties
public string Name { get; set; }
Person per = new Person { Age = 22, Name = "Mony" };
When you instantiate this class, you normally write the following code:
Instead, you can create and initialize a Point object like this:
Point p = new Point { X = 10, Y = 20 }; // object initializer
var p = new Point { X = 10, Y = 20 }; // object initializer
public Point ULcorner { get { return p1; } set { p1 = value; } }
public Point LRcorner { get { return p2; } set { p2 = value; } }
You can create and initialize the Rectangle object like this:
var rectangle = new Rectangle { ULcorner = new Point { X = 0, Y = 0 },
LRcorner = new Point { X = 10, Y = 20 } };
public string Name { get { return _Name; } set { _Name =value; } }
public List Interests { get { return _Intersets; } }
static void Main(string[] args)
In C# 3.0, you can write less code to express the same concept:
static void Main(string[] args)
new Person{ Name = "Mony Hamza", Interests = { "Reading", "Running" } },
new Person { Name = "John Luke", Interests = { "Swimming"} };
public static double ConvertToCelsius(this double fahrenheit)
return ((fahrenheit – 32) / 1.8); }
Now it is possible to invoke the extension method, ConvertToCelsius, as if it is an instance method:
double Celsius = fahrenheit.ConvertToCelsius();
So it adds a method called ConvertToCelisius to an existing type which is double here.