c#3.0 Notes (iii) var keyword and extension method

Source: Internet
Author: User
Tags foreach static class


From this blog began to really start to care about the characteristics of 3.0, this article mainly introduces the var keyword and extension methods.



First knowledge of the VAR keyword



C#3.0 provides us with the Var keyword to define implicit local variables, defined by:



var i = 0;
var  myBool = true;
var myString =  "Henllyee";



In fact, when using the var keyword, the compiler infers the data type of the variable based on the variable value of the initialization variable, which we can look at after compiling the following code:



.locals init ([0] int32 i,
[1] bool myBool,
[2] string myString)



We can see clearly that the compiler infers the type of the variable based on the value, so the VAR keyword does not fundamentally change anything. The VAR keyword can also be used in a foreach phrase such as:



var lists  = new List<int>() { 1, 2, 4, 100 };
foreach (var  i in lists)
{
Console.Write(i + ",",  i.ToString());
}



Note points using the var keyword



1 The implicit type variable definition must be defined as an initial value;



2) cannot be initialized with null;



3 cannot use Var as the return value or parameter type of the method;



4 cannot define the members of the class with Var;



The origin and definition of extension methods



When a type is defined and compiled into an assembly, its definition work is over, and when we add a new method, we can only modify the code to recompile (or modify it through the reflection mechanism). In c#3.0, we provide an extension method for such things. The extension method is a good choice when we need to add functionality to a type, but without the source code. Here we define an extension method for the object type:



public static string  DisplayDefinigAssembly(this object obj)
  {
    return String.Format("{0} lives here :\n\t {1}\n", obj.GetType().Name,
      Assembly.GetAssembly(obj.GetType()));
  }



Be aware of three points when defining an extension method:



1 The method must be defined in the static class, which means that the method must also be static;



2 The first parameter of all methods must be added with this keyword;



3 The call to the extension method can be invoked only in an in-memory instance or through a static class.



The extension method above is to look at the type of object and the Assembly through a radiological mechanism. We can look at the next call example:






static void Main(string[] args)
  {

    DataSet ds = new DataSet();
    Console.Write(ds.DisplayDefinigAssembly());
    Console.Read();

  }





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.