C# 3.0 New Features Using Visual Studio 2008

About this Tutorial –

Objectives –

Delegates will learn to develop applications using C #3.0 After completing this course, delegates will be able to:

  • Use Visual Studio 2008 effectively
  • Use new features from Visual Studio 2008

Audience

This course has been designed primarily for programmers new to the .Net development platform. Delegates experience solely in Windows application development or earlier versions of ASP.Net will also find the content beneficial.

Prerequisites

Before attending this workshop, students must:

  • Be able to manage a solution environment using the Visual Studio 2008 IDE and tools
  • Be able to program an application using a .NET Framework 3.0 compliant language

Contents

Copyright 20/12/12 – David Ringsell

Download Solutions

Java tutorial

Lab 1 – Implicit Typing
Declare a variable as an unspecified type. The compilers infers the type from the value assigned to the variable. Then output the inferred type.

  1. Open Visual Studio.Net and create a new C# Console Application project.
  2. In the Main method insert the following lines:
  3. //The compiler determines the variable type
    var a = 5;
    Console.WriteLine("a");
    Console.WriteLine   ("Variable a is of type "+  a.GetType());
    Console.ReadLine();
  4. Run the application to test it then save your work.
  5. View code file.

Lab 2 – Anonymous Types
An object is created without a full definition of its underlying class. The object’s properties are assigned inline, with inferred data types. This employs implicit typing for the variables.

  1. Open Visual Studio.Net and create a new C# Console Application project.
  2. In the Main method insert the following lines:
  3. //The compiler infers the class definition
    var captain = new { FirstName = "Jamie", LastName = "Cooke" };    Console.WriteLine(captain.FirstName + " " + captain.LastName);
    //Instantiate an object of the same type
    var doctor = new { FirstName = "Peter", LastName = "Watson" };
    //New object is assigned to original since the type is the same
    captain = doctor;    Console.WriteLine(captain.FirstName + " " + captain.LastName);
    Console.ReadLine();
  4. Run the application to test it then save your work.

Lab 3 – Object Initialiser
Create an object and set its properties in a single statement. The properties are set in the braces.

  1. Open Visual Studio.Net and create a new C# Console Application project.
  2. In the Main method insert the following lines:
  3. //A Person class with two properties
    public class Person
    {
    public string Name { get; set; }
    public string Age { get; set; }
    }
    class ObjectInitialiser
    {
    static void Main(string[] args)
    {
    //The properties for the Person object are set inline
    Person p = new Person{ Name = "Jamie", Age = 50 };
    Console.WriteLine(p.Name + " "+ p.Age);
    Console.ReadLine();
    }
    }

Lab 4 – Collection Initialiser
Initialise a collection that holds objects.

  1. Open Visual Studio.Net and create a new C# Console Application project.
  2. First in the Main method create a Person class:
  3. //A Person class with two properties
    public class Person
    {
    public string    Name { get; set; }
    public string Age { get; set; }
    }
  4. Next initialise a collection that holds objects. The objects themselves are initialized by using the concise object initialiser syntax. So a collection initialiser is just lots of object initialisers.
  5. class CollectionInitialisers
    {
    static void Main(string[] args)
    {
    //Collection initialiser - lots of object initialiser
    Person[] people = {
    new Person { Name="Allen Frances", Age=11},
    new Person { Name="Burke Madison", Age=50},
    new Person { Name="Connor Morgan", Age=59},
    new Person { Name="David Charles", Age=33},
    new Person { Name="Everett Frank", Age=16}
    };
    //Show items in the collection
    foreach (Person item in people)    Console.WriteLine(item.Name);
    Console.ReadLine();
    }
    }
  6. Run the application to test it then save your work.

Lab 5 – Partial Methods
Partial methods can be created in partial classes. A partial class is a class with a declaration that is split, for example between two files. The call to partial method is in one part of the class, but method’s implementation is in another part.

  1. Open Visual Studio.Net and create a new C# Console Application project.
  2. In the Main method, create the first part of a partial class, with the call to the partial Do() method.
  3. //First part of partial class
    partial class PartialMethods
    {
    static void Main()
    {
    Do();
    }
    static partial void Do();
    }
  4. In the Main method, now create the second part of partial class with the Do() method’s implementation.
  5. //Second part of partial class - could be in a seperate file
    partial class PartialMethods
    {
    //Partial method implementation
    static partial void Do()
    {
    Console.WriteLine("Do some work");
    Console.ReadLine();
    }
    }
  6. Run the application to test it then save your work.

Lab 6 – Extension Methods
Extension methods are implemented not in the methods underlying class, but a related class. This allows methods to be added to classes even if there is no access to the source code. Extension methods were introduced to provide the syntax for LINQ queries, for example a .Select() extension method can be called on a collection, in a LINQ query. Extension methods also have other uses.

  1. Open Visual Studio.Net and create a new C# Console Application project.
  2. In the Main method create the Money class and its members.
  3. //The Money class has a property & a method
    public class Money
    {
    public string Amount { get; set; }
    public override string ToString()
    {
    return "£" + Amount.ToString();
    }
    }
  4. In the Main now create a related class, MoneyExtension, with an extension method. The first parameter of the extension method is of type Money.
  5. //The MoneyExtension class has extension method for Money class
    public static class MoneyExtension
    {
    //An extension methods, the 1st parameter’s type is the Money class
    public static void AddToAmount(this Money money, decimal amountToAdd)
    {
    money.Amount += amountToAdd;
    }
    }
  6. Test the Money class by calling its methods, then save your work.
  7. class ExtensionMethods
    {
    static void Main(string[] args)
    {
    Money m = new Money();
    m.Amount = 100;
    //Call the extension method
    m.AddToAmount(10);
    Console.WriteLine("The new amount is £"+ m.Amount);
    Console.ReadLine();
    }
    }

Lab 7 – Lambda Expressions
Lambda expression are a new concise syntax for creating methods.

  1. Open Visual Studio.Net and create a new C# Console Application project.
  2. Let’s start by creating an anonymous method in the Main method. A delegate is instantiated with a method, but the method is only declared in a code block. That is, the method has no name.
  3. static void Main()
    {
    string mid = ", middle part,";
    //Instantiate the delegate with an anonymous method
    DelegateTest anonDel = delegate(string param)
    { //The method declaratiopn is a code block
    param += mid;
    param += " and this was added to the string.";
    return param;
    };
    }
  4. Next the delegate is again instantiated with the same method, but using the Lambda expression syntax. This includes the new => operator.
  5. //Instantiate the delegate using the lambda expression syntax
    DelegateTest anonDel2 = param =>
    {
    param += mid;
    param += " and this was added to the string.";
    return param;
    };
  6. Test the delegate.
    Console.WriteLine(anonDel2("Start of string"));
    Console.ReadLine();
  7. Next create a function using the Lambda expression syntax. The function f, returns true if its parameter is less than 5. The Func syntax means that the functions takes an integer and returns a bool. Func is a keyword.
  8. //Declare a lambda expression
    Func f = n => n < 5; //Like anonymous method
  9. Test the function.
  10. //Call a lambda expression
    bool isSmall = f(2); // isSmall is now true
  11. You completed code may look like this:
  12. class LambdaExpressions
    {
    //Declare a delegate that returns a string
    delegate string DelegateTest(string val);
    static void Main()
    {
    string mid = ", middle part,";
    //Instantiate the delegate with an anonymous method
    DelegateTest anonDel = delegate(string param)
    { //The method declaratiopn is a code block
    param += mid;
    param += " and this was added to the string.";
    return param;
    };
    //Instantiate the delegate using the lambda expression syntax
    DelegateTest anonDel2 = param =>
    {
    param += mid;
    param += " and this was added to the string.";
    return param;
    };
    Console.WriteLine(anonDel2("Start of string"));
    Console.ReadLine();
    //Declare a lambda expression
    Func f = n => n < 5; //Like anonymous method
    //Call a lambda expression
    bool isSmall = f(2); // isSmall is now true
    Console.WriteLine("The LAMDA EXPRESSIONS returns: {0}", isSmall);
    Console.ReadLine();
    }
    }
  13. Run the application to test it then save your work.
If you liked this post, please comment with your suggestions to help others.
If you would like to see more content like this in the future, please fill-in our quick survey.
Scroll to Top