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.

devx-admin

devx-admin

Share the Post:
Software Development

Top Software Development Companies

Looking for the best in software development? Our list of Top Software Development Companies is your gateway to finding the right tech partner. Dive in

India Web Development

Top Web Development Companies in India

In the digital race, the right web development partner is your winning edge. Dive into our curated list of top web development companies in India,

USA Web Development

Top Web Development Companies in USA

Looking for the best web development companies in the USA? We’ve got you covered! Check out our top 10 picks to find the right partner

Clean Energy Adoption

Inside Michigan’s Clean Energy Revolution

Democratic state legislators in Michigan continue to discuss and debate clean energy legislation in the hopes of establishing a comprehensive clean energy strategy for the

Chips Act Revolution

European Chips Act: What is it?

In response to the intensifying worldwide technology competition, Europe has unveiled the long-awaited European Chips Act. This daring legislative proposal aims to fortify Europe’s semiconductor

Revolutionized Low-Code

You Should Use Low-Code Platforms for Apps

As the demand for rapid software development increases, low-code platforms have emerged as a popular choice among developers for their ability to build applications with

Software Development

Top Software Development Companies

Looking for the best in software development? Our list of Top Software Development Companies is your gateway to finding the right tech partner. Dive in and explore the leaders in

India Web Development

Top Web Development Companies in India

In the digital race, the right web development partner is your winning edge. Dive into our curated list of top web development companies in India, and kickstart your journey to

USA Web Development

Top Web Development Companies in USA

Looking for the best web development companies in the USA? We’ve got you covered! Check out our top 10 picks to find the right partner for your online project. Your

Clean Energy Adoption

Inside Michigan’s Clean Energy Revolution

Democratic state legislators in Michigan continue to discuss and debate clean energy legislation in the hopes of establishing a comprehensive clean energy strategy for the state. A Senate committee meeting

Chips Act Revolution

European Chips Act: What is it?

In response to the intensifying worldwide technology competition, Europe has unveiled the long-awaited European Chips Act. This daring legislative proposal aims to fortify Europe’s semiconductor supply chain and enhance its

Revolutionized Low-Code

You Should Use Low-Code Platforms for Apps

As the demand for rapid software development increases, low-code platforms have emerged as a popular choice among developers for their ability to build applications with minimal coding. These platforms not

Cybersecurity Strategy

Five Powerful Strategies to Bolster Your Cybersecurity

In today’s increasingly digital landscape, businesses of all sizes must prioritize cyber security measures to defend against potential dangers. Cyber security professionals suggest five simple technological strategies to help companies

Global Layoffs

Tech Layoffs Are Getting Worse Globally

Since the start of 2023, the global technology sector has experienced a significant rise in layoffs, with over 236,000 workers being let go by 1,019 tech firms, as per data

Huawei Electric Dazzle

Huawei Dazzles with Electric Vehicles and Wireless Earbuds

During a prominent unveiling event, Huawei, the Chinese telecommunications powerhouse, kept quiet about its enigmatic new 5G phone and alleged cutting-edge chip development. Instead, Huawei astounded the audience by presenting

Cybersecurity Banking Revolution

Digital Banking Needs Cybersecurity

The banking, financial, and insurance (BFSI) sectors are pioneers in digital transformation, using web applications and application programming interfaces (APIs) to provide seamless services to customers around the world. Rising

FinTech Leadership

Terry Clune’s Fintech Empire

Over the past 30 years, Terry Clune has built a remarkable business empire, with CluneTech at the helm. The CEO and Founder has successfully created eight fintech firms, attracting renowned

The Role Of AI Within A Web Design Agency?

In the digital age, the role of Artificial Intelligence (AI) in web design is rapidly evolving, transitioning from a futuristic concept to practical tools used in design, coding, content writing

Generative AI Revolution

Is Generative AI the Next Internet?

The increasing demand for Generative AI models has led to a surge in its adoption across diverse sectors, with healthcare, automotive, and financial services being among the top beneficiaries. These

Microsoft Laptop

The New Surface Laptop Studio 2 Is Nuts

The Surface Laptop Studio 2 is a dynamic and robust all-in-one laptop designed for creators and professionals alike. It features a 14.4″ touchscreen and a cutting-edge design that is over

5G Innovations

GPU-Accelerated 5G in Japan

NTT DOCOMO, a global telecommunications giant, is set to break new ground in the industry as it prepares to launch a GPU-accelerated 5G network in Japan. This innovative approach will

AI Ethics

AI Journalism: Balancing Integrity and Innovation

An op-ed, produced using Microsoft’s Bing Chat AI software, recently appeared in the St. Louis Post-Dispatch, discussing the potential concerns surrounding the employment of artificial intelligence (AI) in journalism. These

Savings Extravaganza

Big Deal Days Extravaganza

The highly awaited Big Deal Days event for October 2023 is nearly here, scheduled for the 10th and 11th. Similar to the previous year, this autumn sale has already created

Cisco Splunk Deal

Cisco Splunk Deal Sparks Tech Acquisition Frenzy

Cisco’s recent massive purchase of Splunk, an AI-powered cybersecurity firm, for $28 billion signals a potential boost in tech deals after a year of subdued mergers and acquisitions in the

Iran Drone Expansion

Iran’s Jet-Propelled Drone Reshapes Power Balance

Iran has recently unveiled a jet-propelled variant of its Shahed series drone, marking a significant advancement in the nation’s drone technology. The new drone is poised to reshape the regional

Solar Geoengineering

Did the Overshoot Commission Shoot Down Geoengineering?

The Overshoot Commission has recently released a comprehensive report that discusses the controversial topic of Solar Geoengineering, also known as Solar Radiation Modification (SRM). The Commission’s primary objective is to

Remote Learning

Revolutionizing Remote Learning for Success

School districts are preparing to reveal a substantial technological upgrade designed to significantly improve remote learning experiences for both educators and students amid the ongoing pandemic. This major investment, which

Revolutionary SABERS Transforming

SABERS Batteries Transforming Industries

Scientists John Connell and Yi Lin from NASA’s Solid-state Architecture Batteries for Enhanced Rechargeability and Safety (SABERS) project are working on experimental solid-state battery packs that could dramatically change the

Build a Website

How Much Does It Cost to Build a Website?

Are you wondering how much it costs to build a website? The approximated cost is based on several factors, including which add-ons and platforms you choose. For example, a self-hosted

Battery Investments

Battery Startups Attract Billion-Dollar Investments

In recent times, battery startups have experienced a significant boost in investments, with three businesses obtaining over $1 billion in funding within the last month. French company Verkor amassed $2.1