# C# 3.0 Features

# Implicitly typed variables (var)

The var keyword allows a programmer to implicitly type a variable at compile time. var declarations have the same type as explicitly declared variables.

var squaredNumber = 10 * 10;
var squaredNumberDouble = 10.0 * 10.0;
var builder = new StringBuilder();
var anonymousObject = new
{ 
    One = SquaredNumber,
    Two = SquaredNumberDouble,
    Three = Builder
}

The types of the above variables are int, double, StringBuilder, and an anonymous type respectively.

It is important to note that a var variable is not dynamically typed. SquaredNumber = Builder is not valid since you are trying to set an int to an instance of StringBuilder

# Language Integrated Queries (LINQ)

//Example 1
int[] array = { 1, 5, 2, 10, 7 };

// Select squares of all odd numbers in the array sorted in descending order
IEnumerable<int> query = from x in array
                         where x % 2 == 1
                         orderby x descending
                         select x * x;
// Result: 49, 25, 1

Example from wikipedia article on C# 3.0, LINQ sub-section (opens new window)

Example 1 uses query syntax which was designed to look similar to SQL queries.

//Example 2
IEnumerable<int> query = array.Where(x => x % 2 == 1)
    .OrderByDescending(x => x)
    .Select(x => x * x);
// Result: 49, 25, 1 using 'array' as defined in previous example

Example from wikipedia article on C# 3.0, LINQ sub-section (opens new window)

Example 2 uses method syntax to achieve the same outcome as example 1.

It is important to note that, in C#, LINQ query syntax is syntactic sugar (opens new window) for LINQ method syntax. The compiler translates the queries into method calls at compile time. Some queries have to be expressed in method syntax. From MSDN (opens new window) - "For example, you must use a method call to express a query that retrieves the number of elements that match a specified condition."

# Lambda expresions

Lambda Expresions are an extension of anonymous methods (opens new window) that allow for implicitly typed parameters and return values. Their syntax is less verbose than anonymous methods and follows a functional programming style.

using System;
using System.Collections.Generic;
using System.Linq;
                    
public class Program
{
    public static void Main()
    {
        var numberList = new List<int> {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        var sumOfSquares = numberList.Select( number => number * number )
            .Aggregate( (int first, int second) => { return first + second; } );
        Console.WriteLine( sumOfSquares );
    }
}

The above code will output the sum of the squares of the numbers 1 through 10 to the console.

The first lambda expression squares the numbers in the list. Since there is only 1 parameter parenthesis may be omitted. You can include parenthesis if you wish:

.Select( (number) => number * number);

or explicitly type the parameter but then parenthesis are required:

.Select( (int number) => number * number);

The lambda body is an expression and has an implicit return. You can use a statement body if you want as well. This is useful for more complex lambdas.

.Select( number => { return number * number; } );

The select method returns a new IEnumerable with the computed values.

The second lambda expression sums the numbers in list returned from the select method. Parentheses are required as there are multiple parameters. The types of the parameters are explicitly typed but this is not necessary. The below method is equivalent.

.Aggregate( (first, second) => { return first + second; } );

As is this one:

.Aggregate( (int first, int second) => first + second );

# Anonymous types

Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first. The type name is generated by the compiler and is not available at the source code level. The type of each property is inferred by the compiler.

You can make anonymous types by using the new keyword followed by a curly brace ({). Inside the curly braces, you could define properties like on code below.

var v = new { Amount = 108, Message = "Hello" };

It's also possible to create an array of anonymous types. See code below:

var a = new[] { 
    new { 
        Fruit = "Apple", 
        Color = "Red" 
    },
    new {
        Fruit = "Banana",
        Color = "Yellow"
    }
};

Or use it with LINQ queries:

var productQuery = from prod in products
                   select new { prod.Color, prod.Price };

# Remarks

C# version 3.0 was released as part of .Net version 3.5. Many of the features added with this version were in support of LINQ (Language INtegrated Queries).

List of added features:

  • LINQ
  • Lambda expressions
  • Extension methods
  • Anonymous types
  • Implicitly typed variables
  • Object and Collection Initializers
  • Automatically implemented properties
  • Expression trees