C#结构体变量学习浅谈

开发 后端
C#结构体变量学习中的一点体会和大家一起分享下,C#结构体变量学习会遇到些什么问题呢?本文向你一一介绍。

C#结构体变量的内涵:一个结构体可以作为另一个结构体的成员类型:

(1)C#结构体变量示例:

  1. struct phoneBook  
  2. {  
  3.   public string name;  
  4.   public uint age;  
  5.   public string phone;  
  6.   public struct address  
  7.   {   
  8.   public string city;  
  9.   public string street;  
  10.   public uint no;  
  11.   }  

但是由一个问题:

如何对结构的结构体的成员赋值?

有如下C#结构体变量示例:

  1. phoneBook M_1;  
  2. M_1.name=Console.ReadLine();  
  3. ……  
  4. M_1.address.city=Console.ReadLine();  
  5. …… 

这样访问的话,编译报错:

error CS0572:“address”:无法通过表达式引用类型,请尝试“phoneBook.address”。

在这里M_1.address这种引用方式应该是错误的,因为address仅仅是一个类型名,而不是phoneBook的成员名,所以说无法引用。

(2)C#结构体变量示例

还尝试另外一种方式给他赋值:

  1. phoneBook M_1;  
  2. M_1.name=Console.ReadLine();  
  3. ……  
  4. phoneBook.address ad;  
  5. ad.city=Console.ReadLine();  
  6. …… 

这样赋值倒是没问题,但ad和M_1有什么关系?基本是两个不想干的对象。

这样看来,目前还无法解决,只能在结构体本身想办法了。

(3)C#结构体变量示例

那么,结构体这样定义如何?

  1. struct phoneBook  
  2. {  
  3.   public string name;  
  4.   public uint age;  
  5.   public string phone;  
  6.   public struct address  
  7.   {   
  8.   public string city;  
  9.   public string street;  
  10.   public uint no;  
  11.   } ad;  

注意:在声明address类型的时候同时声明他的对象ad作为phoneBook的成员,这在C++里应该是没问题的。

但还是无法通过编译:类、结构或接口成员声明中的标记“;”无效

不知道为什么,可能c#不支持

(4)C#结构体变量示例

最后一个办法:把结构体里的结构体拿出来,其对象作为另一个结构体的成员:

  1. struct phoneBook  
  2. {  
  3.   public string name;  
  4.   public uint age;  
  5.   public string phone;  
  6.   public address ad;  
  7. }  
  8. struct address  
  9. {  
  10.   public string city;  
  11.   public string street;  
  12.   public uint no;  

这样肯定就没问题了,但是已经跟原来问题不大相关了。

(5)C#结构体变量示例

经请教,得知可以这样写:

  1. struct phoneBook  
  2. {  
  3.   public string name;  
  4.   public uint age;  
  5.   public string phone;  
  6.   public address ad;  
  7.   public struct address  
  8.   {  
  9.   public string city;  
  10.   public string street;  
  11.   public uint no;  
  12.   }  
  13. }  

其实,这就是方式(3)所要做的

只是方式(3)是c++的方法在C#里不起作用而已,其实质和方式(4)却相同。

下面是所有代码,作为参考:

C#结构体变量示例之版本2代码

  1. using System;  
  2.  
  3. struct phoneBook  
  4. {  
  5.   public string name;  
  6.   public uint age;  
  7.   public string phone;  
  8.   public address ad;  
  9. }  
  10. struct address  
  11. {  
  12.   public string city;  
  13.   public string street;  
  14.   public uint no;  
  15. }  
  16.  
  17. class Welcome  
  18. {  
  19.   static void Main()  
  20.   {  
  21.      
  22.   try 
  23.   {  
  24.   phoneBook M_1;  
  25.   while (true)  
  26.   {  
  27.   Console.WriteLine("请输入你的姓名:");  
  28.   M_1.name = Console.ReadLine();  
  29.   if (M_1.name != "")  
  30.   break;  
  31.   else 
  32.   {  
  33.   Console.WriteLine("请输入!");  
  34.   }  
  35.   }  
  36.  
  37.   while (true)  
  38.   {  
  39.   try 
  40.   {  
  41.   Console.WriteLine("请输入你的年龄:");  
  42.   M_1.age = Convert.ToUInt32(Console.ReadLine());  
  43.   break;  
  44.   }  
  45.   catch (FormatException e)  
  46.   {  
  47.   Console.WriteLine("请输入-9的数字!");  
  48.   }  
  49.   }  
  50.  
  51.   while (true)  
  52.   {  
  53.   Console.WriteLine("请输入你的电话:");  
  54.   M_1.phone = Console.ReadLine();  
  55.   if (M_1.phone != "")  
  56.   break;  
  57.   else 
  58.   {    
  59. Console.WriteLine("请输入!");  
  60.   }  
  61.   }  
  62.  
  63.   Console.WriteLine("请输入你的住址:");    
  64.   while (true)  
  65.   {  
  66.   Console.WriteLine("请输入你的城市:");  
  67.   M_1.ad.city = Console.ReadLine();  
  68.   //M_1.address.city=Console.ReadLine();  
  69. //通过此种方式无法直接访问结构体address的成员    
  70.   if (M_1.ad.city != "")  
  71.   break;  
  72.   else 
  73.   {  
  74.   Console.WriteLine("请输入!");  
  75.   }  
  76.   }  
  77.  //C#结构体变量
  78.   while (true)  
  79.   {  
  80.   Console.WriteLine("请输入你的街道:");  
  81.   M_1.ad.street = Console.ReadLine();  
  82.   if (M_1.ad.street != "")  
  83.   break;  
  84.   else 
  85.   {  
  86.   Console.WriteLine("请输入!");  
  87.   }  
  88.   }  
  89.  
  90.   while (true)  
  91.   {  
  92.   try 
  93.   {  
  94.   Console.WriteLine("请输入你的门牌号:");  
  95.   M_1.ad.no = Convert.ToUInt32(Console.ReadLine());    
  96.   break;  
  97.   }  
  98.   catch (FormatException e)  
  99.   {  
  100.   Console.WriteLine("请输入-9的数字!");  
  101.   }  
  102.   }  
  103.   Console.WriteLine("OK now……"); Console.WriteLine(  
  104. "------------------------------------------  
  105. ---------------------------------");  
  106.   Console.WriteLine("你的信息如下:\n");  
  107.   Console.WriteLine(" 姓名: {0}",M_1.name);  
  108.   Console.WriteLine(" 年龄: {0}",M_1.age);  
  109.   Console.WriteLine(" 电话: {0}",M_1.phone);  
  110.   Console.WriteLine(" 住址: {0}市{1}街{2}号",  
  111. M_1.ad.city,M_1.ad.street,M_1.ad.no);  
  112.   }  
  113.   catch(FormatException e/*,IOException i*/)  
  114.   {  
  115.   string msg = "\n--------------  
  116. --------------------------------------  
  117. ---------------------\n错误:\n 源 :"+e.Source +  
  118. "\n 消息:"+ e.Message+"\n 追踪:\n"+e.StackTrace;  
  119.   Console.WriteLine(msg);  
  120.   }  
  121.  }   

C#结构体变量示例之版本3代码:

  1. using System;  
  2. using System.IO;  
  3.  
  4. struct phoneBook  
  5. {  
  6.   public string name;  
  7.   public uint age;  
  8.   public string phone;  
  9.   public address ad;  
  10.   public struct address  
  11.   {  
  12.   public string city;  
  13.   public string street;  
  14.   public uint no;  
  15.   }  
  16. }  
  17.  
  18. class Welcome  
  19. {  
  20.   static phoneBook PB;  
  21.   static string info="见到这条消息说明你是笨蛋";  
  22.   static string write_info = "CUG来客";  
  23.   static string fileName = "信息记录.txt";  
  24.  
  25.   static void outMsg(int flag)//输出信息到控制台  
  26.   {  
  27.   Console.ForegroundColor = ConsoleColor.Green;  
  28.   switch (flag)  
  29.   {  
  30.   case 0:  
  31.   {  
  32.   Console.ForegroundColor = ConsoleColor.Red;  
  33.   //Console.BackgroundColor = ConsoleColor.White;  
  34. Console.WriteLine("\n\n 2008世纪通讯录 ");  
  35.   Console.WriteLine("_________________________  
  36. _____________________________________________________\n");  
  37.   Console.WriteLine("录入信息(I):");  
  38.   Console.WriteLine("查看记录(V):");  
  39.   Console.WriteLine("退出系统(E):");  
  40.   break;  
  41.   }  
  42.   case 1:  
  43.   {  
  44.   Console.WriteLine("请输入!");  
  45.   break;  
  46.   }  
  47.   case 2:  
  48.   {  
  49.   Console.WriteLine("请输入-9的数字!");  
  50.   break;  
  51.   }  
  52.   case 3:  
  53.   {  
  54.   Console.ForegroundColor = ConsoleColor.Magenta;  
  55.   Console.WriteLine("-------  
  56. --------------------------------------------");  
  57.   Console.WriteLine(info);  
  58.   break;  
  59.   }  
  60.   case 4:  
  61.   {  
  62.   Console.WriteLine("读取文件错误!");  
  63.   break;  
  64.   }  
  65.   case 5:  
  66.   {  
  67.   Console.WriteLine("写文件错误!");  
  68.   break;  
  69.   }  
  70.   case 6:  
  71.   {  
  72.   Console.WriteLine("非法字符!");  
  73.   break;  
  74.   }  
  75.   case 7:  
  76.   {  
  77.   Console.WriteLine("操作成功!");  
  78.   break;  
  79.   }  
  80.   default:  
  81.   break;  
  82.   }  
  83.   Console.BackgroundColor = ConsoleColor.Black;  
  84.   Console.ForegroundColor = ConsoleColor.White;  
  85.   }   
  86.   static void writeFile(phoneBook pb) //将信息输出到文件保存  
  87.   {  
  88.   try 
  89.   {  
  90.   StreamWriter w = File.AppendText(fileName);  
  91.   /*FileStream f_stream=new   
  92. FileStream(filename,FileMode.OpenOrCreate);  
  93.   StreamWriter swd = new StreamWriter(f_stream);  
  94.   swd.WriteLine(info);*/ 
  95.   /*BinaryWriter bwd = new BinaryWriter(f_stream);  
  96.   bwd.WriteString(info);*/ 
  97.   w.WriteLine(Environment.NewLine); w.WriteLine("///  
  98. ////////////////////////////////////////////////////////////////");  
  99.   w.Write(write_info);  
  100.   w.WriteLine(Environment.NewLine);  
  101.   w.Close();  
  102.   outMsg(7);  
  103.   }  
  104.   catch (IOException e)  
  105.   {  
  106.   outMsg(4);  
  107.   string msg = "\n----------  
  108. ---------------------------------------------------------------  
  109. \n错误:\n 源 :" + e.Source + "\n 消息:" +  
  110.  e.Message + "\n 追踪:\n" + e.StackTrace;  
  111.   Console.WriteLine(msg);  
  112.   }  
  113.   }  
  114.   static void inPut(ref phoneBook pb)//从控制台输入信息  
  115.   {  
  116.   try 
  117.   {  
  118.   while (true)  
  119.   {  
  120.   Console.WriteLine("请输入你的姓名:");  
  121.   pb.name = Console.ReadLine();  
  122.   if (pb.name != "")  
  123.   break;  
  124.   else 
  125.   {  
  126.   outMsg(1);  
  127.   }  
  128.   }  
  129.  
  130.   while (true)  
  131.   {  
  132.   try 
  133.   {  
  134.   Console.WriteLine("请输入你的年龄:");  
  135.   pb.age = Convert.ToUInt32(Console.ReadLine());  
  136.   break;  
  137.   }  
  138.   catch   
  139.   {  
  140.   outMsg(2);  
  141.   }  
  142.   }  
  143.  
  144.   while (true)  
  145.   {  
  146.   Console.WriteLine("请输入你的电话:");  
  147.   pb.phone = Console.ReadLine();  
  148.   if (pb.phone != "")  
  149.   break;  
  150.   else 
  151.   {  
  152.   outMsg(1);  
  153.   }  
  154.   }  
  155.  
  156.   Console.WriteLine("请输入你的住址:");    
  157.   while (true)  
  158.   {  
  159.   Console.WriteLine("请输入你的城市:");  
  160.   pb.ad.city = Console.ReadLine();  
  161.   if (pb.ad.city != "")  
  162.   break;  
  163.   else 
  164.   {  
  165.   outMsg(1);  
  166.   }  
  167.   }  
  168.  
  169.   while (true)  
  170.   {  
  171.   Console.WriteLine("请输入你的街道:");  
  172.   pb.ad.street = Console.ReadLine();  
  173.   if (pb.ad.street != "")  
  174.   break;  
  175.   else 
  176.   {  
  177.   outMsg(1);  
  178.   }  
  179.   }  
  180.  //C#结构体变量
  181.   while (true)  
  182.   {  
  183.   try 
  184.   {  
  185.   Console.WriteLine("请输入你的门牌号:");  
  186.   pb.ad.no = Convert.ToUInt32(Console.ReadLine());    
  187.   break;  
  188.   }  
  189.   catch   
  190.   {  
  191.   outMsg(2);  
  192.   }  
  193.   }  
  194.  //C#结构体变量
  195. info = "你的信息如下:\r\n" +  
  196.  " 姓名: " + pb.name + "\r\n 年龄: " + pb.age + "\r\n    
  197. 电话: " + pb.phone + "\r\n 住址:   
  198. " + pb.ad.city + "市" + pb.ad.street +  
  199. "街" + pb.ad.no + "号";  
  200.   write_info = "姓名: " + pb.name +   
  201. "\r\n年龄: " + pb.age + "\r\n电话: " + pb.phone +   
  202. "\r\n住址: " + pb.ad.city + "市" +   
  203.  
  204. pb.ad.street + "街" + pb.ad.no + "号";  
  205.   }  
  206.   catch (FormatException e/*,IOException i*/)  
  207.   {  
  208.   string msg = "\n---------------  
  209. ----------------------------------------------------------\n  
  210. 错误:\n 源 :" + e.Source + "\n 消息:" +   
  211. e.Message + "\n 追踪:\n" + e.StackTrace;  
  212.   Console.WriteLine(msg);  
  213.   }  
  214.   }   
  215.   static void readFile(phoneBook pb)//从文件读取信息  
  216.   {  
  217.   try 
  218.   {  
  219.   StreamReader srd = File.OpenText(fileName);  
  220.   string str;  
  221.   int count=0;//记录信息的条数  
  222.   while(srd.Peek()>=0)  
  223.   {  
  224.   str = srd.ReadLine();  
  225.   if (str.Contains("姓名"))//以出现姓名为标志,处理条数记录  
  226.   {  
  227.   count++;  
  228.   }  
  229.   Console.WriteLine(str);  
  230. //在此处仅仅是将读取的字符串输出,  
  231.  
  232. //其实还可以做进一步的处理,得到一个phoneBook  
  233. //结构做其他用处,这也是最初的目的,但由于时间原因,也就罢了。  
  234.   }  
  235.   Console.WriteLine(Environment.NewLine);  
  236.   Console.WriteLine("-----------------  
  237. -----------------------------------------");  
  238.   Console.WriteLine("目前共有{0}条记录", count);  
  239.   srd.Close();  
  240.   }  
  241.   catch 
  242.   {  
  243.   outMsg(6);  
  244.   }  
  245.   }  
  246.   static void Main()  
  247.   {  
  248.   do 
  249.   {  
  250.   outMsg(0);    
  251.   char choice = Console.ReadKey().KeyChar;  
  252.   Console.WriteLine("\r\n");  
  253.   switch (choice)  
  254.   {  
  255.   case 'i':  
  256.   case 'I'//输入信息  
  257.   {  
  258.   inPut(ref PB);  
  259.   outMsg(3);  
  260.   writeFile(PB);  
  261.   break;  
  262.   }  
  263.   case 'v':  
  264.   case 'V'//查看记录  
  265.   {  
  266.   //Console.WriteLine("查看记录");  
  267.   readFile(PB);  
  268.   break;  
  269.   }  
  270.   case 'e':  
  271.   case 'E'//退出系统  
  272.   {  
  273.   Console.WriteLine("机器名 :{0} \r\n  
  274. OS版本 :{1} \r\n运行时间:{2}毫秒",   
  275. Environment.MachineName, Environment.OSVersion,  
  276.  Environment.TickCount);  
  277.   Console.WriteLine("退出系统");  
  278.   Environment.Exit(0);  
  279.   //Application.Exit();  
  280.   break;  
  281.   }  
  282.   default:  
  283.   {  
  284.   outMsg(6);  
  285.   break;  
  286.   }  
  287.   }  
  288.   } while (true);  
  289.   }  

C#结构体变量的基恩内容就向你介绍到这里,希望对你学习和了解C#结构体变量有所帮助。

【编辑推荐】

  1. C#构造函数介绍及分类浅析
  2. C#结构体和类的区别浅析
  3. C#结构体构造函数的应用
  4. C#结构体定义的详解
  5. C#的结构体使用实例浅析
责任编辑:仲衡 来源: 百度空间
相关推荐

2011-09-21 10:56:31

C#结构

2009-08-07 11:26:53

C#数组结构

2009-08-13 13:29:04

C#结构体使用

2009-08-14 11:05:28

C#语言的结构体

2009-08-31 15:02:22

C#解析结构体指针

2009-08-13 11:18:50

C#结构体

2009-08-13 14:46:03

C#结构体定义

2009-08-27 14:44:11

C# interfac

2009-08-27 16:18:47

C#类C#结构体

2009-08-13 14:24:44

C#结构体构造函数

2009-08-13 14:56:46

C#的结构体使用

2009-08-03 17:07:31

C#固定的和活动的变量

2009-08-13 14:06:37

C#结构体结构体和类的区别

2009-08-13 13:03:52

C#结构体数组

2009-08-13 13:17:10

C#结构体数组

2009-09-04 17:44:35

2009-08-20 18:30:33

C# ReaderWr

2009-08-13 15:41:50

C#结构体指针

2009-08-13 14:36:40

C#结构体构造函数

2009-08-19 17:12:18

C# Connecti
点赞
收藏

51CTO技术栈公众号