C # anonymous Class

Source: Internet
Author: User

What is an anonymous class , in fact, is essentially the same as a common definition class, except that it is done by the compiler of the system, first for example.

General situation

//declare a class that contains more than one pasted field Public classPerson () { Public stringname{Set; ge;}  Public intage{Set;Get;}                .                                      .                .                . .}//Instantiation and initializationPerson person=NewPerson ();p Erson. Name="Xi Yang sheep";p Erson. Age="Lazy"; 。 。 。 。 

As you can see, when there are so many fields, it's too cumbersome, so c#3.0 provides anonymous classes. Syntax isvar name =new {field Assignment};
where Var is different in C # and in JavaScript, C # is simply inferred, based on the assignment to infer the type,Implicitly-typed var. An implicitly typed local variable is a strongly typed variable (as if you have declared the type), but the type is determined by the compiler.

1) The value of the Var type must be a local variable or a static variable

2) var variable must be initialized before declaring

3) The value of a variable cannot be null (so the compiler cannot determine the type)

4) var variable cannot be a method or method group

5) cannot be used to declare parameter types

The Var type in JavaScript does not make judgments, what types can be, what types of variables (such as string,int,double, etc.) are not strictly limited.

// anonymous class; Declares a class #3.0, which is equivalent to putting class initialization and object declaration together            var New {title=" Super Good news ", author=" Sunset Eye ", postdate="  3013-10-9", msg=" announced tonight "};

C # anonymous Class

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.