C#结构体定义的情况:
C#结构体定义也可以象类一样可以单独定义。
1
2
|
class
a { };
struct
a { };
|
1
2
|
public
struct
student { };
internal
struct
student { };
|
如果结构体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(
"这是构造的函数"
);
}
}
|
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();
}
}
|
1
|
Class_Test obj_1.x=90 Struct_Test obj_2.x=100
|
C#结构体定义的基本内容就向你介绍到这里,希望对你了解C#结构体定义有所帮助。