Preface
Start TimeI learned C #2.0 and didn't fully learn it. After work, I am either busy with projects, or have a taste of various technologies, but I have no progress in language learning. It wasn't until six months ago that I started getting started with Python (purely a hobby, and I learned it intermittently) that I could do the same for programming! OriginalCodeYou can also write it like this!
Indulge in various design patterns, frameworks, and new technologies
Looking back
Suddenly found
She
Still so elegant
Even
Better than ever!
Target
Complete the new features of C #2.0 3.0, and then conduct a review (generic) on the generic type of C # In study 4.0 ). This article describes the anonymity in C #, including: anonymous method, anonymous type, and implicit type local variable.
Reference
Read these in detailArticleYou don't have to read what I wrote! If you have any questions, please go down or leave a message for further discussion.
C # features-object set Initialization
Automatically implemented attributes
How to Use object initial values
C #3.0 object initiators and collectors
Anonymous implicit local variables
C # features-Anonymous type
Object initialization
Before going anonymous, let's talk about object initialization. C # provides the object initializing tool, set initializing tool, and automatic attributes for us to initialize an object.
Class Program
{
Static Void Main ( String [] ARGs)
{
Cat Mili = New Cat {birthday = New Datetime ( 2010 , 3 , 3 ), Name = " Mili " };
Cat Kaka = New CAT ( " Kaka " ) {Birthday = New Datetime ( 2008 , 3 , 3 )};
Cat Tutu = New CAT ( " Tutu2 " ) {Birthday = New Datetime ( 2010 , 4 , 3 ), Name = " Tutu " };
Console. writeline ( " Name: {0}, age: {1} " , Mili. Name, Mili. Age );
Console. writeline ( " Name: {0}, age: {1} " , Tutu. Name, tutu. Age );
List < Cat > Catshop = New List < Cat > {
New Cat {birthday = New Datetime ( 2010 , 6 , 3 ), Name = " Lulu " },
New Cat {birthday = New Datetime ( 2010 , 8 , 3 ), Name = " Xixi " },
New Cat {birthday = New Datetime ( 2010 , 9 , 4 ), Name = " FAFA " }
};
Console. Read ();
}
}
Class Cat
{
Public String Name
{
Get ;
Set ;
}
Public Datetime birthday
{
Get ;
Set ;
}
Public Int Age
{
Get
{
Return Datetime. Now. Year - Birthday. Year;
}
}
/// <Summary>
/// If you need to use the constructor, You need to manually add a non-argument constructor.
/// In fact, constructor can be omitted in object and set initialization.
/// </Summary>
Public CAT (){}
Public CAT (string name ){}
}
If no other logic is required, the automatically implemented attributes make the attribute Declaration very concise, and we no longer need to define a private variable in the class as a container. In fact, the compiler will create a private anonymous backup field, which can only be passed through the attributeGetAndSetThe accessors perform access (the compiler helped us with what we did previously ).Initialize an object of the type declared (new cat {}) without explicitly calling the constructor of this type (in this example, the constructor is called during initialization, from the print results, we can see that the constructor is executed before initialization), and each attribute of the object can be assigned a value directly. However, make sure that the class has no constructor.
Anonymous and implicit local variables
VaR -- a local variable of the implicit type, which can be a variable within the method range. The implicit type is actually a strongly typed variable (as if you have already declared this type), which is determined by the compiler. It is different from the weak type of JavaScript. The compiler (the previous features are also what it secretly does) will help us determine the VaR variable type based on our assignment.
VaR nameI = New Cat {name = " NameI " , Birthday = New Datetime ( 2005 , 4 , 3 )};
VaR I = 13 ;
Console. writeline ( " Name: {0}, age: {1}, No: {2} " , NameI. Name, nameI. Age, I );
// In fact, we often use VaR in this way.
VaR Lulu = New {Name = " Lulu " , Birthday = New Datetime ( 2005 , 4 , 3 )};
Console. writeline ( " Name: {0}, age: {1} " , Lulu. Name, datetime. Now. Year - Lulu. Birthday. year );
The anonymous type is used to associate a groupRead-OnlyAttributes are encapsulated in a single object without explicitly defining a type. The type name is generated by the compiler and cannot beSource codeLevel. So in the code above
VaR Lulu = new {}. Because there is no name, we cannot use it in the code. But it can be read through var. Note the anonymityAll attributes in the type are read-only!
Although the applicationProgramThe anonymous type cannot be accessed, but the compiler still provides a name for it. From the perspective of public language runtime, the anonymous type is no different from any other reference type. The only difference is that the anonymous type cannot be forcibly converted to any type other than the object.
Anonymous Method
C #2.0 has provided the anonymous method in the version. In the past, we always needed to create a separate method to use the delegate. Now we can declare the delegate using the anonymous method. Note that the jump statement cannot be used in the anonymous method to jump to the outside of the anonymous method or to the inside of the method.
Public Delegate VoidPrint (CAT );
......
Print youname= Delegate(Cat) {console. writeline ("Name: {0}, age {1}", Cat. Name, Cat. Age );};
Youname (Mili );
Define a print delegate type, and then declare the youname delegate object. This delegate is used to print the information of the passed cat object. Use delegate (type) to determine the parameter type accepted by the anonymous method. Here, it must be the same as the defined delegate and then define the logic of the method in braces. The semantics of the anonymous method is hard to understand, so lambda expressions are introduced in C #3.0.