C # What is the keyword var, and under what circumstances,

Source: Internet
Author: User

C # What is the keyword var, and under what circumstances,

Starting from. NET 3.0, you can use the var keyword to declare local variables within the method. What is the var keyword? Under what circumstances?
 

□Var keyword is used to implicitly declare a data type. The variable type is determined during compilation rather than during runtime.

Declare a var variable a, assign the integer value 10 to a, and assign a string to variable. Error reported during running: the type of string cannot be implicitly converted to int.

This Note: When a variable is declared with var, although it is not explicitly declared, the type of the variable has been determined during the compilation period.

 

The above inference can also be proved through decompilation.

→ Remove the = "abc"; line of code and generate a solution by pressing F6.
→ Open "VS2012 developer command prompt" and enter the following command

→ In Solution Explorer, right-click the project name, select Open folder in file explorer, click the bin and Debug folders, and assign the path of the Debug folder to the address bar.
→ Click the "il dasm" file to open it. Enter the debug folder of the created file, select the executable file suffixed with ".exe", and double-click "Main: void (string [])". In the IL code:

It can be seen that variable a is already of the Int type and a local variable.

 

□Var keyword for shorter Encoding

For some classes with long names, when creating a class instance, use the var keyword to look shorter. Of course, this reason is far-fetched ~~

     class Program
    {
        static void Main(string[] args)
        {
            HelloIHaveALongLongName<string, string> obj = new HelloIHaveALongLongName<string, string>();
        }
    }
    public class HelloIHaveALongLongName<T, T>
    {
        public string Name;
    }

 

If the var keyword is used, it becomes:

var obj = new HelloIHaveALongLongName<string, string>();
obj.Name = "hello";

It can be seen that the var keyword is used to make the Writing Method shorter, and the var variable is a strong type.

 

□Var keyword and anonymous type

Returns an anonymous type using a LINQ expression.

        static void Main(string[] args)
        {
            string[] strs = {"hello", "world", "nice", "to", "meet", "you"};
            object o = from s in strs
                where s.Length > 3
                select new {s.Length, s};
        }

The object type variable o is not a strongly typed variable.


If we use a strongly typed class to receive the collection returned by LINQ.

    class Program
    {
        static void Main(string[] args)
        {
            string[] strs = {"hello", "world", "nice", "to", "meet", "you"};
            IEnumerable<SomeData> o = from s in strs
                where s.Length > 3
                select new SomeData() {Key = s.Length, Value = s};
            foreach (SomeData item in o)
            {
                Console.WriteLine(item.Key);
            }
        }
    }
    public class SomeData
    {
        public int Key;
        public string Value;
    }

Above, in the IEnumerable <SomeData> type set, each set element is strongly typed.

 

If the var keyword is used to receive the anonymous set returned by LINQ.

    class Program
    {
        static void Main(string[] args)
        {
            string[] strs = { "hello", "world", "nice", "to", "meet", "you" };
            var o = from s in strs
                    where s.Length > 3
                    select new { Key = s.Length, Value = s };
            foreach (var item in o)
            {
                Console.WriteLine(item.Key);
            }
        }
    }

Above, the var type variable o receives an anonymous type set, and each element of this set is also strongly typed. That is to say, the compiler automatically infers the type during compilation.

 

Summary:
○ Variables declared using the var keyword are determined during the compilation period, rather than at runtime.
○ For the anonymous type returned by the LINQ expression, you can use the variable declared by the var keyword to receive
○ For classes with long names, you can use the variables declared by the var keyword to receive them during class instantiation.

 

 

 

 


& In C Language

& Can be used as the bitwise AND or address fetch Operator
The following describes two usage methods:
1. bitwise and operation bitwise AND operator "&" are binary operators. Its function is the binary phase corresponding to the two numbers involved in the operation. The result bit is 1 only when the two binary numbers are 1. Otherwise, the result bit is 0. The number of involved operations is supplemented.
For example, 9 & 5 can be written as follows: 00001001 (Binary complement of 9) & 00000101 (Binary complement of 5) 00000001 (Binary complement of 1) Visible 9 & 5 = 1.
Bitwise AND operations are usually used to clear some bits or retain some bits. For example, if a clears the high eight bits of 0 and retains the low eight bits, it can be used as a & 255 operation (255 of the binary number is 0000000011111111 ).
2. Get the address
& As The unary operator, the result is the address of the right operation object.
For example, & x returns the address of x.
The address itself is an abstract concept used to indicate the logical location of an object in the memory.

& In C Language

& Can be used as the bitwise AND or address fetch Operator
The following describes two usage methods:
1. bitwise and operation bitwise AND operator "&" are binary operators. Its function is the binary phase corresponding to the two numbers involved in the operation. The result bit is 1 only when the two binary numbers are 1. Otherwise, the result bit is 0. The number of involved operations is supplemented.
For example, 9 & 5 can be written as follows: 00001001 (Binary complement of 9) & 00000101 (Binary complement of 5) 00000001 (Binary complement of 1) Visible 9 & 5 = 1.
Bitwise AND operations are usually used to clear some bits or retain some bits. For example, if a clears the high eight bits of 0 and retains the low eight bits, it can be used as a & 255 operation (255 of the binary number is 0000000011111111 ).
2. Get the address
& As The unary operator, the result is the address of the right operation object.
For example, & x returns the address of x.
The address itself is an abstract concept used to indicate the logical location of an object in the memory.

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.