What’s New in C# 3.0? Part 1

What’s New in C# 3.0? Part 1

ith the release of Visual Studio 2008, Microsoft has updated the C# language to its latest version, 3.0. C# 3.0, contains several key language enhancements that support the recently-announced Language Integrated Query (LINQ) feature. This article, the first of a two-part series, will walk you through each of these new, time-saving language enhancements and provide a couple of code examples illustrating how to use them.

Implicit Typing
In the previous version of C#, all variables must be explicitly typed/declared. For example, suppose you want to declare a string variable. You’d do this:

   string str = "Hello World";

In C# 3.0, this is not mandatory. You can use the new var keyword to implicitly declare a variable. Consider the following statements:

   var str = "Hello world!";   var pt = this.Size;

Here, str is implicitly declared as a string variable, while pt is implicitly declared as a Size variable. The type of variable declared is based on the value with which it is initialized. This method of variable declaration is known as Implicit Typing. Note that implicitly-type variables must be initialized when they are declared. The following statement will not compile:

   var str;  //---missing initializer---

Also notice that IntelliSense will automatically know the type of the variable declared, as you can see in Figure 1.

Figure 1. IntelliSense Knows: IntelliSense automatically infers the type of variable from the initialized value.12a5.

You can also use implicit typing on arrays. For example, the following statement declares points to be an array containing two Point objects:

var points = new[] { new Point(1, 2), new Point(3, 4) };

When you use implicit typing on arrays, make sure all the members in the array are of the same type. The following won?t compile since its members are of different types?string and Boolean:

var arr = new[] { "hello", true, "world" };

Implicit typing is useful in cases where you do not know the exact type of data you’re manipulating and you need the compiler to determine for you. Do not confuse the Object type with implicit typing. Variables declared as Object types need to be casted during runtime and IntelliSense does not know this type during development time. On the other hand, implicitly-typed variables are statically typed during design time?hence, IntelliSense is able to provide detailed information about the type. In terms of performance, an implicitly typed variable is no different from a normal typed variable.

Automatic Properties
In object-oriented programming, it’s good practice not to expose your member variables as publicly accessible. Instead, wrap them using properties so that you can impose business rules on them. For example, Listing 1 shows the definition of the Contact class containing two properties?Name and YearofBirth.

There are some conditions to check for when setting the YearofBirth property, while there are none for setting the Name property. In fact, most of the time, you probably won’t have any rules for setting the properties. When this happens, defining the get and set statements is superfluous. In C# 3.0, properties that don’t need any rules can simply be defined using a feature known as automatic properties. The following is a rewrite of the Contact class using this feature:

public class Contact{    uint _YearofBirth;    public string Name {get; set;}    public uint YearofBirth    {        get { return _YearofBirth; }        set        {            if (value >= 1900 && value <= 2008)                _YearofBirth = value;            else                _YearofBirth = 0;        }    }}

Now, there's no need for the private member variable _Name. The C# compiler will automatically generate its own private member variable to store the name. One advantage to using this feature is that, in the future, when you need to apply your business rules to the setting of Name properties, you can simply modify the get and set statements directly without affecting the external components using this class.

Object Initializers
Generally, there are two ways in which you can initialize an object?through its constructor during instantiation, or by setting its properties individually after instantiation. Using the Contact class defined in the previous section, here is one example of how to initialize a Contact object:

   Contact c1 = new Contact();   c1.Name = "John";   c1.YearofBirth = 1980;

C# 3.0 provides a third way to initialize objects?when they are instantiated. Here's an example:

   Contact c1 = new Contact { Name = "John", YearofBirth = 1980 };

In the code above, instantiating the Contact class also directly sets its properties. Remember not to confuse the object initializer with a class's constructor(s). You should continue to use the constructor (if it has one) to initialize the object.

Listing 2 the following modification of the Contact class which has a constructor that takes in a string.

During instantiation, you can pass a string to the constructor while simultaneously using the object initializer to initialize the YearofBirth property:

    Contact c1 = new Contact("Wei-Meng Lee") { YearofBirth=1980 };

The object initializer is useful when you want to initialize an object's properties at the time of instantiation.

Collection Initializers
Besides initializing objects at time of instantiation, you can also instantiate a collection of objects. For example, suppose you have a collection of Contact objects and want to keep them in a generic List object. In that case, you'd do this:

   List Contacts = new List ();   Contacts.Add(new Contact { Name = "John", YearofBirth = 1980 });   Contacts.Add(new Contact { Name = "Mary", YearofBirth = 1986 });   Contacts.Add(new Contact { Name = "Richard", YearofBirth = 1948 });

Instead of using the Add() method multiple times, you can simply use the collection initializers, like this:

   List Contacts = new List() {      new Contact { Name = "John", YearofBirth = 1980 },      new Contact { Name = "Mary", YearofBirth = 1986 },      new Contact { Name = "Richard", YearofBirth = 1948 }    };

Anonymous Types
A new feature in C# 3.0 is anonymous types. Anonymous types allow you to define data types without having to formally define a class. Consider the following example:

            var contact1 = new            {                id = "54321",                Name = "Wei-Meng Lee",                email = "[email protected]"            };

Here, contact1 is an object with three properties: id, Name, and email (see Figure 2).

Figure 2. IntelliSense Knows: IntelliSense automatically knows the properties contained with the contact1 object.
Figure 3. The Property Name: The property name will take the name of the variable name in an anonymous type.

Notice that there's no need to define a class containing the three properties. Instead, the object is created and its properties are initialized with their respective values.

Author's Note: In C#, anonymous types are immutable, which means that all their properties are read-only, unlike in VB 9.0, which requires the use of the Key keyword to denote immutable properties.

You can use variables when assigning values to properties in an anonymous type, like this:

            var Name = "Wei-Meng Lee";            var Email = "[email protected]";            var contact1 = new { id = "54321", Name, Email };

In this case, the name of the properties will take the name of the variable, as shown in Figure 3.

However, the following is not allowed:

   var contact1 = new {"54321", "Wei-Meng Lee",                        "[email protected]" };

Hence, when assigning a literal value to a property in an anonymous type, you must use an identifier, like this:

            var contact1 = new            {                id = "54321",                Name = "Wei-Meng Lee",                email = "[email protected]"            };

So, how are you supposed to use anonymous types? Well, they allow you to shape your data from one type to another. You'll learn about this in Part 2, when we'll discuss LINQ support.

Lambda Expressions
In C# 3.0, Microsoft has further extended the concept of anonymous methods, which were introduced in C# 2.0. To understand what a Lambda Expression is, you'll need to first understand anonymous methods.

Suppose you have just added a Button control to a Windows Form. In C# 1.0/1.1, you'd need to wire up the event handler for the control using the following statements:

        private void Form1_Load(object sender, EventArgs e)        {            button1.Click += new EventHandler(button1_Click);        }        private void button1_Click(object sender, EventArgs e)        {            Console.WriteLine("Button clicked!");        }

Essentially, you added an event handler by using the "+=" operator and then defined the event handler explicitly. In C# 2.0, you can rewrite the above statements using anonymous method, like this:

        button1.Click += delegate        {            Console.WriteLine("Button clicked!");        };

Instead of defining the event handler explicitly, you embed the code in the event handler inside an anonymous method. Now, in C# 3.0, you can further shorten the anonymous method using a Lambda expression, like this:

        private void Form1_Load(object sender, EventArgs e)        {            button1.Click +=               (_sender, _args)=> Console.WriteLine("Button clicked!") ;        }

Lambda expressions are compact functions that can be passed as arguments to another method. In the above example, the Lambda expression is:

   (_sender, _args)=> Console.WriteLine("Button clicked!")

Lambda expressions have the following format:

params => expression(s)

Note that in this example, both _sender and _args are parameters of the Lambda expression (they've been prefixed with an underscore character to differentiate them from the variable used in the Form1_Load event handler). It's not mandatory to define the type of parameter(s) you are passing into the Lambda expression; they are inferred automatically based on the context in which they are defined. Also, you can have multiple statements in the expression section of the lambda expression. Here's an example:

        button1.Click += (object _sender,EventArgs _args) =>        {            Console.WriteLine("Button clicked!");            Console.WriteLine("Button clicked!");        };

More to Come!
Now that you've gotten a taste of the new features available in C# 3.0, you can understand how they help save you time and hassle. Stay tuned for Part 2 and find out how C#'s Language Integrated Query (LINQ) support does the same.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several