C# v3.0 - Visual Studio 2008

The name Anonymous Functions is often used to describe the following two features: Anonymous Methods (introduced in .NET 2.0) and the Lambda Expressions (introduced in .NET 3.5)


Implicitly Typed Variables - var

The var keyword is short for variable and does not mean variant.
The var keyword does not indicate that the variable is loosely typed, or late-bound.
The compiler automatically replaces "var" with the correct data type.
More Details

var student = new Company.Namespace.People.Person(); 

instead of

Company.Namespace.People.Person student = new Company.Namespace.People.Person(); 

Auto Implemented Properties

This was added to VB.Net in Visual Studio 2010.
The compiler automatically creates a private field to store the property variable in addition to creating the associated get and set procedures.
Makes property declaration more concise when no extra logic is required in the property accessors.

public string Property_Name { get; set; } 

instead of

private string m_sName; 
public string Property_Name
{
   get { return m_sName; }
   set { m_sName = value; }
}

Anonymous Types

This provides a quick way to create a single read-only object.
Provides a way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first
This is achieved by using implicitly typed local variables.
The name/value pairs declare properties with initial values.
Although anonymous types are classes they do not implement the IDisposable interface

var myType = new { Property_Name="Simon", Property_Age=30 }; 

instead of

public class ReadOnly_Object { 
   public string Property_Name { get; }
   public int Property_Age { get; }
}
var myType = new ReadOnly_Object();
myType.Property_Name = "Simon";
myType.Property_Age = 30;

If you do not specify member names in the anonymous type, the compiler gives the anonymous type members the same name as the property being used to initialize them.
It is derived from System.Object class and it is also a sealed class.


Lamda Expressions

A lambda expression is an anonymous function that you can use to create delegates.
This lets you write local functions that can be passed as arguments or returned from functions.
A lambda expression is a block of code (an expression or a statement block) that is treated as an object. It can be passed as an argument to methods, and it can also be returned by method calls.
To create a lambda expression you specify the input parameters on the left of the lambda operator with an expression or statement block on the right
Multi-line lambda expressions were added to VB.Net in Visual Studio 2010.

string Person = peoplelist.Find(name => name.Contains("m") 

instead of

public string FindPerson(string contains, List<string> peoplelist) 
{
   for each (string name in peoplelist)
   {
      if (name.Contains("m"))
      {
         return name;
      }
   }
   return null;
}

System.Func Delegate

The Func delegate is used for methods that accept one or more arguments and return a value.
An anonymous function is an inline statement or expression that can be used wherever a delegate type is expected.
A delegate with a specific signature can be declared using the Func type.
The last parameter of the Func delegate is the output parameter or result parameter.

System.Func<string, string> delegate_variable = delegate(string param1) 
{ return parameter.ToUpper(); }

instead of

delegate string Delegate_Type(string x); 

Delegate_Type delegate_variable delegate(string param1)
{ return parameter.ToUpper(); }

System.Action Delegate

The Action delegate can be used for methods that accept one or more arguments and do not return a value.


System.Action<string, string> delegate_variable = delegate(string param1, string param2); 
{ //do something }

instead of

delegate void Delegate_Type(string x, string y); 

Delegate_Type delegate_variable delegate(string param1, string param2)
{ // do something }

System.Predicate Delegate

This is a special case of a Func, in that it takes only a single parameter and always returns a bool.



Object Initializers

Allows you to instantiate and initialize objects in one compound statement
Let you assign values to any accessible fields or properties at creation without having to explicitely invoke a constructor

MyClass instance = new MyClass { MyField=10, MyProperty="text" }; 

instead of

MyClass instance = new MyClass(); 
instance.MyField = 10;
instance.MyProperty = "text";

Partial Methods

A partial method is a method whose signature is declared in one class but its implementation is in a different class.
There are several restrictions though:
partial methods must have a return type of void
partial methods cannot accept out parameters
partial methods cannot be extern
partial methods cannot be virtual
partial methods are implicitely private


Collection Initializers

This provides a shortened syntax that enables you to create a collection and populate it with an initial set of values

var myChildren = new List<Person> 
{
   new Person { Name = "Lisa"; }
   new Person { Name = "Richard"; }
}
List<int> mylist = new List<int> {0,1,2,3,4,5}; 

This was added to VB.Net in Visual Studio 2010.


Extension Methods

Classes > Extension Methods


Query Expressions

Make frequent use of anonymous types


Expression Trees

These represent code in a tree-like data structure where each node is an expression (for example a method call or a binary operation).
You can instruct the compiler to create an expression tree for you based on an anonymous lambda expression.
You can also create expression trees manually using the System.Linq.Expressions namespace.


HashSet Collection

Another useful collection class has been added called HashSet
System.Collections.Generic.HashSet
Generics > HashSet


Language Intergrated Query (LINQ)

This is a name that incorporates a wide range of general purpose querying that can be down.
There can often be a lot of complexity around the actual data.
The common sources are relational databses and XML.
LINQ provides a standardised means to query information from many different data sources.
Rather than try and add specific (relational or XML) features to help with this complexity the approach has been more general.


Language integrated query allows a single general purpose declarative query facility to be applied to not just relational or XML based data but in-memory data as well.
LINQ defines a set of general purpose standard query operators that allow the traversal, filter and projection of information to be expressed in a direct and declarative way.
These standard query operators can be found in the System.Linq namespace.
LINQ can be divided into the following categories:
Collections > LINQ - Refers to extension methods that are available for all the classes that implement the IEnumerable or IEnumerable<T> interfaces.
XML > LINQ to XML - Refers to the use of the standard query operators as well as tree specific operators.


Office Interop API Extensions

These are a set of additional interop assemblies that can be referenced to make working with COM easier.
They provide the following additional functionality:
Method Overloads
Optional Parameters
Parameterised Properties
More Details



© 2022 Better Solutions Limited. All Rights Reserved. © 2022 Better Solutions Limited TopPrevNext