C # anonymous class anonymous learning,

Source: Internet
Author: User

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:

 

 

  1. Using System;
  2. Using System. Collections. Generic;
  3. Using System. Linq;
  4. Using System. Text;
  5. Namespace ConsoleApplication1
  6. {
  7. /// <Summary>
  8. /// Author: it Xiaojin
  9. /// Role: Use of the anonymous type
  10. /// Description: var keyword, used to indicate implicitly typed variables. When using var with the new Keyword, you can create an anonymous type.
  11. /// </Summary>
  12. Class Program
  13. {
  14. Static void Main (string [] args)
  15. {
  16. 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.
  17. // 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.
  18. String B = a. name. ToString ();
  19. Int c = a. age;
  20. Console. WriteLine (B );
  21. Console. WriteLine (c );
  22. Console. Read ();
  23. }
  24. }
  25. }
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.

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.