Some of the new stuff in .Net 3.x simply/examplyfied.
Just thought I put together a small class that shows some of the new features in .Net 3.x
I have no intention of trying to explain how it works in detail (there is good documentation out there), you should see this more as a reference.
Also, this is not a full list of all that is new, no LINQ for example; it is more to show the ones that may save you typing J
In this class:
Auto-Implemented Properties
Anonymous Types
Object and Collection Initializer
Extension Methods
Lambda Expressions
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SomeDemoStuff
{
class Program
{
/*********************************************************
Auto-Implemented Properties
https://msdn.microsoft.com/en-us/library/bb384054.aspx
*********************************************************/
// Oldschool
private int oldAge;
public int OldAge
{
get { return oldAge; }
set { oldAge = value; }
}
// Newschool
public int NewAge{get; set;}
// Note, you can't mix old/new, ie:
//public int NewAge { get; set{value + 4}; }
static void Main(string[] args)
{
/*********************************************************
Anonymous Types
https://msdn.microsoft.com/en-us/library/bb397696.aspx
*********************************************************/
for (int i = 20; i < 25; i++)
{
var anonymousPerson = new { Age = i, AgeInTenYears = i + 10 };
Console.WriteLine("Age now {0}, age in ten years {1}", anonymousPerson.Age, anonymousPerson.AgeInTenYears);
}
// Can only access it within the scope, so the following fails since it is outside scope:
//int i = person.Age;
/*********************************************************
Object and Collection Initializers
https://msdn.microsoft.com/en-us/library/bb384062.aspx
*********************************************************/
// *** Object Initializer ***
// This uses the Person class below. Which uses auto implemented properties
// Old way
Person oldPerson = new Person();
oldPerson.Age = 29;
oldPerson.Name = "Michael";
// New way
Person newPerson = new Person { Age = 30, Name = "Michael" };
// *** Collection Initializer ***
List<string> names = new List<string> { "Michael", "John", "Steve", "Charles" };
/*********************************************************
Extension Methods
https://msdn.microsoft.com/en-us/library/bb383977.aspx
*********************************************************/
// The short story, this gives you the ability to add methods to existing types.
int age = 29;
// Old way
int newAge = AddFive(age);
Console.WriteLine("Current age {0}, age in 5 years {1}", age, newAge);
// New way - adding method to type.
Console.WriteLine("Current age {0}, age in 10 years {1}", age, age.AddTen());
/*********************************************************
Lambda Expressions
https://msdn.microsoft.com/en-us/library/bb397687.aspx
*********************************************************/
// The lambda expression reads as "goes to".
// The leftsid is the input parameter, the right one is the expression.
// In the case below, persons over 30 goes into the var variable called personsOver30
// (This example uses Object/List intitializers and auto matic properties as well as Extension methods (persons.Where))
List<Person> persons = new List<Person>
{
new Person{Age = 10, Name="Pete"},
new Person{Age = 20, Name="John"},
new Person{Age = 30, Name="Steve"},
new Person{Age = 40, Name="Michael"}
};
var personsOver30 = persons.Where(p => p.Age >= 30);
foreach (Person p in personsOver30)
Console.WriteLine(p.ToString());
}
// Old method
public static int AddFive(int age)
{
return age + 5;
}
}
class Person
{
public int Age { get; set; }
public string Name { get; set; }
public override string ToString()
{
return string.Format("Name: {0}, Age: {1}", Name, Age);
}
}
static class ExtensionMethods
{
public static int AddTen(this int inValue)
{
return inValue + 10;
}
}
}
Comments
- Anonymous
June 09, 2009
PingBack from http://cellulitecreamsite.info/story.php?id=552