C#结构体变量的内涵:一个结构体可以作为另一个结构体的成员类型:
(1)C#结构体变量示例:
- struct phoneBook
- {
- public string name;
- public uint age;
- public string phone;
- public struct address
- {
- public string city;
- public string street;
- public uint no;
- }
- }
但是由一个问题:
如何对结构的结构体的成员赋值?
有如下C#结构体变量示例:
- phoneBook M_1;
- M_1.name=Console.ReadLine();
- ……
- M_1.address.city=Console.ReadLine();
- ……
这样访问的话,编译报错:
error CS0572:“address”:无法通过表达式引用类型,请尝试“phoneBook.address”。
在这里M_1.address这种引用方式应该是错误的,因为address仅仅是一个类型名,而不是phoneBook的成员名,所以说无法引用。
(2)C#结构体变量示例
还尝试另外一种方式给他赋值:
- phoneBook M_1;
- M_1.name=Console.ReadLine();
- ……
- phoneBook.address ad;
- ad.city=Console.ReadLine();
- ……
这样赋值倒是没问题,但ad和M_1有什么关系?基本是两个不想干的对象。
这样看来,目前还无法解决,只能在结构体本身想办法了。
(3)C#结构体变量示例
那么,结构体这样定义如何?
- struct phoneBook
- {
- public string name;
- public uint age;
- public string phone;
- public struct address
- {
- public string city;
- public string street;
- public uint no;
- } ad;
- }
注意:在声明address类型的时候同时声明他的对象ad作为phoneBook的成员,这在C++里应该是没问题的。
但还是无法通过编译:类、结构或接口成员声明中的标记“;”无效
不知道为什么,可能c#不支持
(4)C#结构体变量示例
最后一个办法:把结构体里的结构体拿出来,其对象作为另一个结构体的成员:
- struct phoneBook
- {
- public string name;
- public uint age;
- public string phone;
- public address ad;
- }
- struct address
- {
- public string city;
- public string street;
- public uint no;
- }
这样肯定就没问题了,但是已经跟原来问题不大相关了。
(5)C#结构体变量示例
经请教,得知可以这样写:
- struct phoneBook
- {
- public string name;
- public uint age;
- public string phone;
- public address ad;
- public struct address
- {
- public string city;
- public string street;
- public uint no;
- }
- }
其实,这就是方式(3)所要做的
只是方式(3)是c++的方法在C#里不起作用而已,其实质和方式(4)却相同。
下面是所有代码,作为参考:
C#结构体变量示例之版本2代码
- using System;
- struct phoneBook
- {
- public string name;
- public uint age;
- public string phone;
- public address ad;
- }
- struct address
- {
- public string city;
- public string street;
- public uint no;
- }
- class Welcome
- {
- static void Main()
- {
- try
- {
- phoneBook M_1;
- while (true)
- {
- Console.WriteLine("请输入你的姓名:");
- M_1.name = Console.ReadLine();
- if (M_1.name != "")
- break;
- else
- {
- Console.WriteLine("请输入!");
- }
- }
- while (true)
- {
- try
- {
- Console.WriteLine("请输入你的年龄:");
- M_1.age = Convert.ToUInt32(Console.ReadLine());
- break;
- }
- catch (FormatException e)
- {
- Console.WriteLine("请输入-9的数字!");
- }
- }
- while (true)
- {
- Console.WriteLine("请输入你的电话:");
- M_1.phone = Console.ReadLine();
- if (M_1.phone != "")
- break;
- else
- {
- Console.WriteLine("请输入!");
- }
- }
- Console.WriteLine("请输入你的住址:");
- while (true)
- {
- Console.WriteLine("请输入你的城市:");
- M_1.ad.city = Console.ReadLine();
- //M_1.address.city=Console.ReadLine();
- //通过此种方式无法直接访问结构体address的成员
- if (M_1.ad.city != "")
- break;
- else
- {
- Console.WriteLine("请输入!");
- }
- }
- //C#结构体变量
- while (true)
- {
- Console.WriteLine("请输入你的街道:");
- M_1.ad.street = Console.ReadLine();
- if (M_1.ad.street != "")
- break;
- else
- {
- Console.WriteLine("请输入!");
- }
- }
- while (true)
- {
- try
- {
- Console.WriteLine("请输入你的门牌号:");
- M_1.ad.no = Convert.ToUInt32(Console.ReadLine());
- break;
- }
- catch (FormatException e)
- {
- Console.WriteLine("请输入-9的数字!");
- }
- }
- Console.WriteLine("OK now……"); Console.WriteLine(
- "------------------------------------------
- ---------------------------------");
- Console.WriteLine("你的信息如下:\n");
- Console.WriteLine(" 姓名: {0}",M_1.name);
- Console.WriteLine(" 年龄: {0}",M_1.age);
- Console.WriteLine(" 电话: {0}",M_1.phone);
- Console.WriteLine(" 住址: {0}市{1}街{2}号",
- M_1.ad.city,M_1.ad.street,M_1.ad.no);
- }
- catch(FormatException e/*,IOException i*/)
- {
- string msg = "\n--------------
- --------------------------------------
- ---------------------\n错误:\n 源 :"+e.Source +
- "\n 消息:"+ e.Message+"\n 追踪:\n"+e.StackTrace;
- Console.WriteLine(msg);
- }
- }
- }
C#结构体变量示例之版本3代码:
- using System;
- using System.IO;
- struct phoneBook
- {
- public string name;
- public uint age;
- public string phone;
- public address ad;
- public struct address
- {
- public string city;
- public string street;
- public uint no;
- }
- }
- class Welcome
- {
- static phoneBook PB;
- static string info="见到这条消息说明你是笨蛋";
- static string write_info = "CUG来客";
- static string fileName = "信息记录.txt";
- static void outMsg(int flag)//输出信息到控制台
- {
- Console.ForegroundColor = ConsoleColor.Green;
- switch (flag)
- {
- case 0:
- {
- Console.ForegroundColor = ConsoleColor.Red;
- //Console.BackgroundColor = ConsoleColor.White;
- Console.WriteLine("\n\n 2008世纪通讯录 ");
- Console.WriteLine("_________________________
- _____________________________________________________\n");
- Console.WriteLine("录入信息(I):");
- Console.WriteLine("查看记录(V):");
- Console.WriteLine("退出系统(E):");
- break;
- }
- case 1:
- {
- Console.WriteLine("请输入!");
- break;
- }
- case 2:
- {
- Console.WriteLine("请输入-9的数字!");
- break;
- }
- case 3:
- {
- Console.ForegroundColor = ConsoleColor.Magenta;
- Console.WriteLine("-------
- --------------------------------------------");
- Console.WriteLine(info);
- break;
- }
- case 4:
- {
- Console.WriteLine("读取文件错误!");
- break;
- }
- case 5:
- {
- Console.WriteLine("写文件错误!");
- break;
- }
- case 6:
- {
- Console.WriteLine("非法字符!");
- break;
- }
- case 7:
- {
- Console.WriteLine("操作成功!");
- break;
- }
- default:
- break;
- }
- Console.BackgroundColor = ConsoleColor.Black;
- Console.ForegroundColor = ConsoleColor.White;
- }
- static void writeFile(phoneBook pb) //将信息输出到文件保存
- {
- try
- {
- StreamWriter w = File.AppendText(fileName);
- /*FileStream f_stream=new
- FileStream(filename,FileMode.OpenOrCreate);
- StreamWriter swd = new StreamWriter(f_stream);
- swd.WriteLine(info);*/
- /*BinaryWriter bwd = new BinaryWriter(f_stream);
- bwd.WriteString(info);*/
- w.WriteLine(Environment.NewLine); w.WriteLine("///
- ////////////////////////////////////////////////////////////////");
- w.Write(write_info);
- w.WriteLine(Environment.NewLine);
- w.Close();
- outMsg(7);
- }
- catch (IOException e)
- {
- outMsg(4);
- string msg = "\n----------
- ---------------------------------------------------------------
- \n错误:\n 源 :" + e.Source + "\n 消息:" +
- e.Message + "\n 追踪:\n" + e.StackTrace;
- Console.WriteLine(msg);
- }
- }
- static void inPut(ref phoneBook pb)//从控制台输入信息
- {
- try
- {
- while (true)
- {
- Console.WriteLine("请输入你的姓名:");
- pb.name = Console.ReadLine();
- if (pb.name != "")
- break;
- else
- {
- outMsg(1);
- }
- }
- while (true)
- {
- try
- {
- Console.WriteLine("请输入你的年龄:");
- pb.age = Convert.ToUInt32(Console.ReadLine());
- break;
- }
- catch
- {
- outMsg(2);
- }
- }
- while (true)
- {
- Console.WriteLine("请输入你的电话:");
- pb.phone = Console.ReadLine();
- if (pb.phone != "")
- break;
- else
- {
- outMsg(1);
- }
- }
- Console.WriteLine("请输入你的住址:");
- while (true)
- {
- Console.WriteLine("请输入你的城市:");
- pb.ad.city = Console.ReadLine();
- if (pb.ad.city != "")
- break;
- else
- {
- outMsg(1);
- }
- }
- while (true)
- {
- Console.WriteLine("请输入你的街道:");
- pb.ad.street = Console.ReadLine();
- if (pb.ad.street != "")
- break;
- else
- {
- outMsg(1);
- }
- }
- //C#结构体变量
- while (true)
- {
- try
- {
- Console.WriteLine("请输入你的门牌号:");
- pb.ad.no = Convert.ToUInt32(Console.ReadLine());
- break;
- }
- catch
- {
- outMsg(2);
- }
- }
- //C#结构体变量
- info = "你的信息如下:\r\n" +
- " 姓名: " + pb.name + "\r\n 年龄: " + pb.age + "\r\n
- 电话: " + pb.phone + "\r\n 住址:
- " + pb.ad.city + "市" + pb.ad.street +
- "街" + pb.ad.no + "号";
- write_info = "姓名: " + pb.name +
- "\r\n年龄: " + pb.age + "\r\n电话: " + pb.phone +
- "\r\n住址: " + pb.ad.city + "市" +
- pb.ad.street + "街" + pb.ad.no + "号";
- }
- catch (FormatException e/*,IOException i*/)
- {
- string msg = "\n---------------
- ----------------------------------------------------------\n
- 错误:\n 源 :" + e.Source + "\n 消息:" +
- e.Message + "\n 追踪:\n" + e.StackTrace;
- Console.WriteLine(msg);
- }
- }
- static void readFile(phoneBook pb)//从文件读取信息
- {
- try
- {
- StreamReader srd = File.OpenText(fileName);
- string str;
- int count=0;//记录信息的条数
- while(srd.Peek()>=0)
- {
- str = srd.ReadLine();
- if (str.Contains("姓名"))//以出现姓名为标志,处理条数记录
- {
- count++;
- }
- Console.WriteLine(str);
- //在此处仅仅是将读取的字符串输出,
- //其实还可以做进一步的处理,得到一个phoneBook
- //结构做其他用处,这也是最初的目的,但由于时间原因,也就罢了。
- }
- Console.WriteLine(Environment.NewLine);
- Console.WriteLine("-----------------
- -----------------------------------------");
- Console.WriteLine("目前共有{0}条记录", count);
- srd.Close();
- }
- catch
- {
- outMsg(6);
- }
- }
- static void Main()
- {
- do
- {
- outMsg(0);
- char choice = Console.ReadKey().KeyChar;
- Console.WriteLine("\r\n");
- switch (choice)
- {
- case 'i':
- case 'I': //输入信息
- {
- inPut(ref PB);
- outMsg(3);
- writeFile(PB);
- break;
- }
- case 'v':
- case 'V': //查看记录
- {
- //Console.WriteLine("查看记录");
- readFile(PB);
- break;
- }
- case 'e':
- case 'E': //退出系统
- {
- Console.WriteLine("机器名 :{0} \r\n
- OS版本 :{1} \r\n运行时间:{2}毫秒",
- Environment.MachineName, Environment.OSVersion,
- Environment.TickCount);
- Console.WriteLine("退出系统");
- Environment.Exit(0);
- //Application.Exit();
- break;
- }
- default:
- {
- outMsg(6);
- break;
- }
- }
- } while (true);
- }
- }
C#结构体变量的基恩内容就向你介绍到这里,希望对你学习和了解C#结构体变量有所帮助。
【编辑推荐】