C # anonymous class anonymous learning,
Thanks http://blog.csdn.net/jjx0224/article/details/5887589
Thanks http://hi.baidu.com/guodong828/blog/item/cc53404ef40af002b3de0500.html
C # code on the anonymous class:
- Using System;
- Using System. Collections. Generic;
- Using System. Linq;
- Using System. Text;
- Namespace ConsoleApplication1
- {
- /// <Summary>
- /// Author: it Xiaojin
- /// Role: Use of the anonymous type
- /// Description: var keyword, used to indicate implicitly typed variables. When using var with the new Keyword, you can create an anonymous type.
- /// </Summary>
- Class Program
- {
- Static void Main (string [] args)
- {
- Var a = new {name = "it Xiaojin", age = 24}; // The anonymous type is only a class that inherits the Object and has no name. The definition of this class is inferred from the initiator, similar to an implicitly typed variable.
- // A. name = "hh"; this is incorrect. You cannot assign a value to the attribute because name is an attribute in a and is read-only.
- String B = a. name. ToString ();
- Int c = a. age;
- Console. WriteLine (B );
- Console. WriteLine (c );
- Console. Read ();
- }
- }
- }
When using anonymous classes, you will inevitably encounter the problem of Anonymous class conversion. The code above:
public T CastAnonymous<T>(object anonymous, T anonymousType){ return (T)anonymous;}class User{ public string Name { get; set; }}public static void Main(){ var u = new User{ Name = "Lucifer" }; var a = new { a = 26, Name = u.Name, b = false }; Print(a);}public void Print(object anonymous){ var a = CastAnonymous(anonymous, new { a=0, Name = "", b = false }); Console.WriteLine{"{0} - {1} - {2}", a.a, a.Name, a.b};}
In the C language, what is the symbol (->) and how to use it?
This is a symbol in the struct pointer. Write a program to explain it, for example:
# Include <stdio. h>
Struct STU // define a struct
{
Int num;
} Stu;
Int main ()
{
Struct STU * p; // defines a struct pointer.
P = stu; // p points to the struct variable stu.
Stu. num = 100; // attaches an initial value to the struct member num.
Printf ("% d", p-> num); // output the num value in stu
Return;
}
As you can see, the-> method is to reference the variable in the struct !!
Format: p-> struct member (such as p-> num)
The function is equivalent to stu. num or (* p). num.
I don't know. You don't understand, and don't understand call me. O (∩ _ ∩) O ~
Hope to adopt it.
In the C language, what is the symbol (->) and how to use it?
This is a symbol in the struct pointer. Write a program to explain it, for example:
# Include <stdio. h>
Struct STU // define a struct
{
Int num;
} Stu;
Int main ()
{
Struct STU * p; // defines a struct pointer.
P = stu; // p points to the struct variable stu.
Stu. num = 100; // attaches an initial value to the struct member num.
Printf ("% d", p-> num); // output the num value in stu
Return;
}
As you can see, the-> method is to reference the variable in the struct !!
Format: p-> struct member (such as p-> num)
The function is equivalent to stu. num or (* p). num.
I don't know. You don't understand, and don't understand call me. O (∩ _ ∩) O ~
Hope to adopt it.