Thursday, March 27, 2008
Visual Studio 2008 IDE C# - UK Launch Notes
- VS08 allows seamless (re)targeting of apps to .NET 2.0, 3.0 and 3.5. This provides graceful upgrading of existing applications. Applications initially targeted for 3.x can also be targeted to .NET 2.0 as long as features of 3.x have not been used.
- Properties have now been improved so the implementation of the get and set methods do not need to be provided if the default implementation is all that is required. Eg./
public string FirstName { get; set; }This creates a hidden private member variable. - It is now possible to use braces to initialise multiple object properties:
Thread myThread = new Thread(MyThreadMethod)
{
Name="my thread",
IsBackground=true
}; - Anonymous types can be created using the 'var' keyword. This is NOT weak typing - C# knows what the type is and it cannot change. Anonymous types are used in instances where the developer does not know the type, the primary instance of this being LINQ. Eg./
var anonobject = new {Name = "Bob", Age = "40"}; - Anonymous methods are best described in detail here:
http://msdn.microsoft.com/msdnmag/issues/04/05/C20/#S5 - Partial methods can be used as a lightweight event system in cases where a large number of objects are held in memory (IE. LINQ) or where the method may be optionally implemented. Eg./
partial class MyClass
{
MyClass()
{
DisplayMessage(); // Only called if definition exists
}
static partial DisplayMessage(); // Not implemented
}
partial class MyClass
{
static partial DisplayMessage() // Optional
{
Console.WriteLine("MyClass created");
}
} - Extension methods allow you to 'tack on' functionality to classes that you would otherwise not be able to due to inheritance being prevented (IE. .NET classes). Eg./
public static class MyExtensions
{
public static bool
IsValidEmailAddress(this string s) // "this" keyword
{
Regex regex =
new Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");
return regex.IsMatch(s);
}
}
public class MyClass
{
public void MyMethod()
{
string email = "paul@here.com";
// Extension method used like a standard string method
if (email.IsValidEmailAddress())
{
Console.WriteLine("Valid!");
}
}
}
Subscribe to Posts [Atom]