The anonymous class is like this word. It is a class without a name. It is a bit confusing to see this anonymous class, if you think that a class has no name, how can we use it to create an instance? The answer is that we can use VaR to declare an anonymous class. The created format is:
New anonymousclassname {Field 1, Field 2 ,........}
Note:
All fields in the anonymous class are public, which means that we can access this so-called Anonymous class at will;
There are many restrictions on this anonymous class, for example:
1. You must Initialize an anonymous class;
2. Only public fields can be included;
3. static fields cannot be declared;
4. It cannot be declared or implemented in it;
A simple example of an anonymous class:
VaR perinfo = new mytest {name = "Barney", age = 21 };
Console. writeline ("Name: {0}, age: {1}", perinfo. Name, perinfo. Age );
Console. Readline ();
/// // Result ////////////////////// ///////////////////////////////
Name: Barney, age: 21
........
......