C # advanced Programming var type

Source: Internet
Author: User

The var variable keyword, called inferred type, was introduced in C # after version 3.5.

So what type of VAR is this type? Below I based on my own information and learning experience summed up:


1.Var is a weakly typed keyword.

var declared variables can be substituted for any kind of variable depending on the context. (Personal feeling a bit like object type, but actually not the same)


2. How to define var variables and the characteristics of VAR variables.


var number;

Number = 10;

A. The above approach is wrong, and you must initialize the variable at the same time as stated, and the correct practice is as follows:

var number = 10;

-----------------------------------------------------------------

var number = 10;

.

.

Number = 10.23;

B. This is wrong, once the Var variable is initialized, the type of data you initialize is the type of the variable, and you will not be able to assign a different type of data to it later.


C.var requirements are local variables.

D.var is different from object, and is the same in terms of efficiency and use of strong types.

Summarize:

The VAR keyword is a new feature in C # 3.5, called inferred type.
A local variable can be given the inferred "type" var instead of an explicit type. The VAR keyword instructs the compiler to infer the type of the variable based on the expression on the right side of the initialization statement. The inferred type can be a built-in type, an anonymous type, a user-defined type, a type defined in a. NET Framework class Library, or any expression. Use this var (inferred type) When you are not sure what type of data you will use. This allows you to determine what type of data is in the context of the incoming value.

This article is from the "Love Coffee" blog, please be sure to keep this source http://4837471.blog.51cto.com/4827471/1565858

C # advanced Programming var type

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.