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
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.
- Open Visual Studio.Net and create a new C# Console Application project.
- In the Main method insert the following lines:
- Run the application to test it then save your work.
- View code file.
//The compiler determines the variable type
var a = 5;
Console.WriteLine("a");
Console.WriteLine ("Variable a is of type "+ a.GetType());
Console.ReadLine();
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.
- Open Visual Studio.Net and create a new C# Console Application project.
- In the Main method insert the following lines:
- Run the application to test it then save your work.
//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();
Lab 3 – Object Initialiser
Create an object and set its properties in a single statement. The properties are set in the braces.
- Open Visual Studio.Net and create a new C# Console Application project.
- In the Main method insert the following lines:
//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.
- Open Visual Studio.Net and create a new C# Console Application project.
- First in the Main method create a Person class:
- 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.
- Run the application to test it then save your work.
//A Person class with two properties
public class Person
{
public string Name { get; set; }
public string Age { get; set; }
}
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();
}
}
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.
- Open Visual Studio.Net and create a new C# Console Application project.
- In the Main method, create the first part of a partial class, with the call to the partial Do() method.
- In the Main method, now create the second part of partial class with the Do() method’s implementation.
- Run the application to test it then save your work.
//First part of partial class
partial class PartialMethods
{
static void Main()
{
Do();
}
static partial void Do();
}
//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();
}
}
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.
- Open Visual Studio.Net and create a new C# Console Application project.
- In the Main method create the Money class and its members.
- In the Main now create a related class, MoneyExtension, with an extension method. The first parameter of the extension method is of type Money.
- Test the Money class by calling its methods, then save your work.
//The Money class has a property & a method
public class Money
{
public string Amount { get; set; }
public override string ToString()
{
return "£" + Amount.ToString();
}
}
//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;
}
}
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.
- Open Visual Studio.Net and create a new C# Console Application project.
- 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.
- Next the delegate is again instantiated with the same method, but using the Lambda expression syntax. This includes the new => operator.
- Test the delegate.
Console.WriteLine(anonDel2("Start of string"));
Console.ReadLine();
- 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.
- Test the function.
- You completed code may look like this:
- Run the application to test it then save your work.
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;
};
//Declare a lambda expression
Func f = n => n < 5; //Like anonymous method
//Call a lambda expression
bool isSmall = f(2); // isSmall is now true
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();
}
}
If you would like to see more content like this in the future, please fill-in our quick survey.