C#结构体定义的详解

来源:互联网 发布:网络教育毕业考试 编辑:程序博客网 时间:2024/05/16 11:15
    C#结构体定义是什么样子呢?也可以象类一样可以单独定义.C#结构体定义也可以在名字前面加入控制访问符,本文向你详细介绍C#结构体定义方面的内容。

     

    C#结构体定义的情况:

    C#结构体定义也可以象类一样可以单独定义.

    1. class  a{};  
    2. struct a{}; 

    C#结构体定义也可以在名字前面加入控制访问符.

    1. public struct student{};  
    2. internal struct student{}; 

    如果结构体student没有publice或者internal的声明 类program就无法使用student结构定义 obj对象

    如果结构体student的元素没有public的声明,对象obj就无法调用元素x

    因为默认的结构体名和元素名是private类型

    C#结构体定义之程序:

    1. using System;  
    2. public struct student  
    3. {  
    4.    public int x;  
    5. };  
    6.  
    7. class program  
    8. {  
    9. public static void Main()  
    10. {  
    11.  student obj=new student();  
    12.  obj.x=100;     
    13. }  
    14.  
    15. }; 

    在结构体中也可以定义静态成员与类中一样,使用时必须用类名,或结构名来调用不属于实例,声明时直接定义.

    C#结构体定义程序:

    1. using System;  
    2. public struct student  
    3. {  
    4.  public static int a = 10;  
    5. };  
    6. class exe  
    7. {  
    8.  public static void Main()  
    9. {  
    10.  Console.WriteLine( student.a = 100);  
    11. }  
    12. }; 

    1. using System;  
    2. class base 
    3. {  
    4. public struct student  
    5. {  
    6.  public static int a = 10;  
    7. };  
    8. }  
    9. class exe  
    10. {  
    11.  public static void Main()  
    12. {  
    13.  Console.WriteLine( base.student.a = 100);  
    14. }  
    15. }; 

    在结构体中可以定义构造函数以初始化成员,但不可以重写默认无参构造函数和默认无参析构函数

    C#结构体定义程序:

    1. public struct student  
    2. {  
    3.    public int x;  
    4.    public int y;  
    5.    public static int z;  
    6.    public student(int a,int b,int c)  
    7. {  
    8. x=a;  
    9. y=b;  
    10.  student.z=c;  
    11. }  
    12.  
    13. }; 

    在结构体中可以定义成员函数。

    C#结构体定义程序:

    1. public struct student  
    2. {  
    3.    public void list()  
    4. {  
    5. Console.WriteLine("这是构造的函数");  
    6. }  
    7.  
    8.  }; 

    结构体的对象使用new运算符创建(obj)也可以直接创建单个元素赋值(obj2)这是与类不同的因为类只能使用new创建对象

    C#结构体定义程序:

    1. public struct student  
    2. {  
    3.    public int x;  
    4.    public int y;  
    5.    public static int z;  
    6.    public student(int a,int b,int c)  
    7. {  
    8. x=a;  
    9. y=b;  
    10.  student.z=c;  
    11. }  
    12.  
    13. };  
    14. class program  
    15. {  
    16.  public static void Main()  
    17. {  
    18.   student obj=new student(100,200,300);  
    19.   student obj2;  
    20.   obj2.x=100;  
    21.   obj2.y=200;  
    22.   student.z=300;  
    23. }  

    在使用类对象和函数使用时,使用的是引用传递,所以字段改变

    在使用结构对象和函数使用时,是用的是值传递,所以字段没有改变

    C#结构体定义程序:

    1. using System;  
    2. class class_wsy  
    3. {  
    4. public int x;  
    5. }  
    6. struct struct_wsy  
    7. {  
    8. public int x;  
    9. }  
    10. class program  
    11. {  
    12. public static void class_t(class_wsy obj)  
    13. {  
    14. obj.x = 90;  
    15. }  
    16. public static void struct_t(struct_wsy obj)  
    17. {  
    18. obj.x = 90;  
    19. }  
    20. public static void Main()  
    21. {  
    22. class_wsy obj_1 = new class_wsy();  
    23. struct_wsy obj_2 = new struct_wsy();  
    24. obj_1.x = 100;  
    25. obj_2.x = 100;  
    26. class_t(obj_1);  
    27. struct_t(obj_2);  
    28. Console.WriteLine("class_wsy obj_1.x={0}",obj_1.x);  
    29. Console.WriteLine("struct_wsy obj_2.x={0}",obj_2.x);  
    30. Console.Read();  
    31. }  

    C#结构体定义程序运行结果为:

    1. class_wsy obj_1.x=90  
    2. struct_wsy obj_2.x=100 

    C#结构体定义的基本内容就向你介绍到这里,希望对你了解C#结构体定义有所帮助。