C #3.0 var declaration syntax

Source: Internet
Author: User

The VaR declaration syntax is the anonymous type declaration,

Like the VaR syntax in JavascriptCodeYou do not need to determine its type,

However, note that VaR in C #3.0 is different from VAR in JavaScript,

Javascript is a weak language and does not matter. C # is a strong language,

Various types are strictly differentiated,

For JavaScript, the data type can only be determined at runtime, that is, execution,

That is, only when you execute this part of code or need to use this data type,

The data type will be determined,

This is not true for C #. C # determines the data type during compilation,

Therefore, if you use the VaR syntax to declare an array, which contains both int and string,

If the compilation fails, an error is reported. This is because during compilation,

C # determines the type of the array, and the array contains both int and string,

Therefore, the basic data type cannot be used to determine the array type declared by VAR during compilation, so an error is reported,

Therefore, in the end, the VaR type is still a strong syntax,

It is just an uncertain type when writing code.

Why does Microsoft invent this var statement syntax,

In fact, it is used in LINQ,

Its combination with LINQ can bring greater potential to play.

Although it is very convenient to use the VaR declaration syntax,

But in fact, there are still many restrictions in use,

The main items are as follows:

It cannot be used at the class/struct/interface level,

Global variables cannot be declared,

Cannot be the return value type of a method,

It cannot be used as a parameter type of a method,

However, it is widely used,

It can be used in foreach,

Can be used in,

It can be used in the using statement,

It can be used in the LINQ technology,

For the basic syntax of VaR, let's look at the following example.

Using system;
Using system. LINQ;

Namespace csharplanguage
{
Public partial class demo _ 1: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Methodone ();
Methodtwo ();
}
// Use the VaR declaration syntax
Public void methodone ()
{
// Declare an implicit Array Using the VaR declaration syntax and the data is of the Double Type
// When defined here, the content in the array must be of the same data type,
// Multiple data types cannot be defined at the same time in the array declared by the same VaR
// For example, arrays such as {12, "baobeime", 13,434} cannot be defined.
// If this definition is used, the array type cannot be determined during compilation.
// This will fail during compilation
VaR mynumarray = new [] {12, 23, 43.5, 49.3, 32, 4, 0.76 };
Response. Write ("<br/> traverse using mynumarray [I]: <br/> ");
For (INT I = 0; I <mynumarray. length; I ++)
{
Response. Write (mynumarray [I] + "----");
}
Response. Write ("<br/> traverse using mynumarray. elementat (I): <br/> ");
For (INT I = 0; I <mynumarray. length; I ++)
{
Response. Write (mynumarray. elementat (I) + "----");
}
Response. Write ("<br/> use the foreach Method for traversal: <br/> ");
Foreach (VaR value in mynumarray)
{
Response. Write (Value + "----");
}
Response. Write ("<br/> use the LINQ to object method for traversal: <br/> ");
// In this example, the content in the array is queried using the LINQ to object method.
VaR result = from P in mynumarray
Select P;
Foreach (var p in result)
{
Response. Write (p + "----");
}
// Obtain the type of the array declared using the VaR syntax through the reflection operation,
// In this way, we can see that, in essence, this var array is still a basic type array.
// Only the type is determined during compilation
Response. Write ("<br/> mynumarray will be converted to a strong type during compilation:" +
Mynumarray. GetType ());
}

Public void methodtwo ()
{
// All data in this anonymous person is read-only.
// You cannot write any data in the subsequent code.
// For example, the person. Name = "baobeime" cannot be performed ";
VaR person = new
{
Name = "Xiaozhen ",
Sex = "male ",
Age = "20 ",
Blogname = "baobeime"
};
Response. Write ("<br/>" + String. Format ("Name: {0}, Gender: {1}," +
"Age: {2}, blog name: {3 }",
Person. Name, person. Sex, person. Age, person. blogname ));
}
}
}

The above is all. aspx. CS code-behind of this demo.

The result of the example is very easy, I think.

However, we still provide one to ensure the integrity of the demo.

2010-2-01

 

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.