结构,是程序员自定义的数据类型。结构与类相似,包含数据成员和函数成员。
结构,是值类型;类,是引用类型。
结构,是隐式密封的,因此,结构不支持继承,不支持派生。
系统为结构提供一个隐式的构造函数。
定义结构的关键字:struct。
语法:
struct xxx
{
xxx;
xxx;
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 结构
{
//定义一个结构
struct my_struct
{
//数据成员
public int x;
public int y;
//函数成员
public int add()
{
return x + y;
}
}
class Program
{
static void Main(string[] args)
{
//使用new运算符,声明结构的变量。
my_struct st = new my_struct();
//为结构成员赋值
st.x = 3;
st.y = 4;
//访问结构的函数
int z = st.add();
//输出结果
Console.WriteLine(z.ToString());
Console.Read();
}
}
}