关于c#结构体(struct)定义详解

本文详细介绍了C#结构体的定义方式、访问控制、静态成员、构造函数、成员函数、对象创建及引用传递的区别。通过示例程序,深入理解C#结构体的基本概念和应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C#结构体定义的情况:

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

1
2
class a { };
struct a { };
C#结构体定义也可以在名字前面加入控制访问符。
1
2
public struct student { };
internal struct student { };
如果结构体student没有publice或者internal的声明 类program就无法使用student结构定义 obj对象。
如果结构体student的元素没有public的声明,对象obj就无法调用元素x,因为默认的结构体名和元素名是private类型。

C#结构体定义之程序:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
using System;
 
public struct student
{
    public int x; 
}
 
class program {
    public static void Main()
    {
        student obj = new student();
        obj.x = 100;
    }
};
在结构体中也可以定义静态成员与类中一样,使用时必须用类名,或结构名来调用不属于实例,声明时直接定义。

C#结构体定义程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
using System;
 
public struct Student
{
    public static int A = 10;
}
 
class Exe
{
    public static void Main()
    {
        Console.WriteLine(Student.A = 100);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System;
  
class Base
{
    public struct Student 
    
        public static int A = 10; 
    
 
class Exe
{  
    public static void Main()
    {  
        Console.WriteLine(Base.Student.A = 100);
    
}
在结构体中可以定义构造函数以初始化成员,但不可以重写默认无参构造函数和默认无参析构函数。

C#结构体定义程序

1
2
3
4
5
6
7
8
9
10
11
12
13
public struct Student
{
    public int x;
    public int y;
    public static int z;
     
    public Student(int a, int b, int c)
    {
        x = a;
        y = b;
        Student.z = c;
    }
}
在结构体中可以定义成员函数。

C#结构体定义程序

1
2
3
4
5
6
7
public struct Student
{
    public void list()
    {
        Console.WriteLine("这是构造的函数");
    }
}
结构体的对象使用new运算符创建(obj)也可以直接创建单个元素赋值(obj2)这是与类不同的因为类只能使用new创建对象。

C#结构体定义程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public struct Student
{
    public int x;
    public int y;
    public static int z;
     
    public Student(int a, int b, int c)
    {
        x = a;
        y = b;
        Student.z = c;
    }
}
 
class Program
{
    public static void Main()
    {
        Student obj = new Student(100, 200, 300);
        Student obj2;
        obj2.x = 100;
        obj2.y = 200;
        Student.z = 300;
    }
}
在使用类对象和函数使用时,使用的是引用传递,所以字段改变。在使用结构对象和函数使用时,是用的是值传递,所以字段没有改变。

C#结构体定义程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System;
 
class Class_Test
{
    public int x;
}
 
struct Struct_Test
{
    public int x; 
}
 
class Program
{
    public static void class_t(Class_Test obj)
    {
        obj.x = 90;
    }
 
    public static void struct_t(Struct_Test obj)
    {
        obj.x = 90;
    }
 
    public static void Main()
    {
        Class_Test obj_1 = new Class_Test();
        Struct_Test obj_2 = new Struct_Test();
 
        obj_1.x = 100; obj_2.x = 100;
        class_t(obj_1); struct_t(obj_2);
 
        Console.WriteLine("Class_Test obj_1.x={0}", obj_1.x);
        Console.WriteLine("Struct_Test obj_2.x={0}", obj_2.x);
        Console.Read();
    }
}
C#结构体定义程序运行结果为:
1
Class_Test obj_1.x=90  Struct_Test obj_2.x=100

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值