C # What is the keyword VaR and under what circumstances?

Source: Internet
Author: User

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.

 

 

 

 

C # What is the keyword VaR and under what circumstances?

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.