字段是一种特殊的变量,字段在结构中使用。- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace ConsoleApplication1
- {
- public struct Person
- {
- public string _name;
- public int _age;
- public char _gender;
- }
- class Program
- {
- static void Main(string[] args)
- {
- //声明一个结构类型的变量
- Person zhangsan;
- zhangsan._name = "张三";
- zhangsan._age = 20;
- zhangsan._gender = '男';
- //声明另外一个结构类型的变量
- Person lisi;
- lisi._name = "李四";
- lisi._age = 19;
- lisi._gender = '女';
- //声明变量并赋值
- string name = "张三";
- int age = 20;
- char gender = '男';
- //给变量重新赋值
- name = "李四";
- age = 19;
- gender = '女';
- Console.WriteLine("姓名:{0},年龄:{1},性别:{2}", zhangsan._name, zhangsan._age, zhangsan._gender);
- Console.WriteLine("姓名:{0},年龄:{1},性别:{2}", lisi._name, lisi._age, lisi._gender);
- Console.WriteLine("姓名:{0},年龄:{1},性别:{2}", name, age, gender);
- Console.ReadKey();
- }
- }
- }
复制代码 |