C#結(jié)構(gòu)體變量學(xué)習(xí)淺談
C#結(jié)構(gòu)體變量的內(nèi)涵:一個(gè)結(jié)構(gòu)體可以作為另一個(gè)結(jié)構(gòu)體的成員類型:
(1)C#結(jié)構(gòu)體變量示例:
- struct phoneBook
- {
- public string name;
- public uint age;
- public string phone;
- public struct address
- {
- public string city;
- public string street;
- public uint no;
- }
- }
但是由一個(gè)問(wèn)題:
如何對(duì)結(jié)構(gòu)的結(jié)構(gòu)體的成員賦值?
有如下C#結(jié)構(gòu)體變量示例:
- phoneBook M_1;
- M_1.name=Console.ReadLine();
- ……
- M_1.address.city=Console.ReadLine();
- ……
這樣訪問(wèn)的話,編譯報(bào)錯(cuò):
error CS0572:“address”:無(wú)法通過(guò)表達(dá)式引用類型,請(qǐng)嘗試“phoneBook.address”。
在這里M_1.address這種引用方式應(yīng)該是錯(cuò)誤的,因?yàn)閍ddress僅僅是一個(gè)類型名,而不是phoneBook的成員名,所以說(shuō)無(wú)法引用。
(2)C#結(jié)構(gòu)體變量示例
還嘗試另外一種方式給他賦值:
- phoneBook M_1;
- M_1.name=Console.ReadLine();
- ……
- phoneBook.address ad;
- ad.city=Console.ReadLine();
- ……
這樣賦值倒是沒(méi)問(wèn)題,但ad和M_1有什么關(guān)系?基本是兩個(gè)不想干的對(duì)象。
這樣看來(lái),目前還無(wú)法解決,只能在結(jié)構(gòu)體本身想辦法了。
(3)C#結(jié)構(gòu)體變量示例
那么,結(jié)構(gòu)體這樣定義如何?
- 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類型的時(shí)候同時(shí)聲明他的對(duì)象ad作為phoneBook的成員,這在C++里應(yīng)該是沒(méi)問(wèn)題的。
但還是無(wú)法通過(guò)編譯:類、結(jié)構(gòu)或接口成員聲明中的標(biāo)記“;”無(wú)效
不知道為什么,可能c#不支持
(4)C#結(jié)構(gòu)體變量示例
最后一個(gè)辦法:把結(jié)構(gòu)體里的結(jié)構(gòu)體拿出來(lái),其對(duì)象作為另一個(gè)結(jié)構(gòu)體的成員:
- 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;
- }
這樣肯定就沒(méi)問(wèn)題了,但是已經(jīng)跟原來(lái)問(wèn)題不大相關(guān)了。
(5)C#結(jié)構(gòu)體變量示例
經(jīng)請(qǐng)教,得知可以這樣寫:
- 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;
- }
- }
其實(shí),這就是方式(3)所要做的
只是方式(3)是c++的方法在C#里不起作用而已,其實(shí)質(zhì)和方式(4)卻相同。
下面是所有代碼,作為參考:
C#結(jié)構(gòu)體變量示例之版本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("請(qǐng)輸入你的姓名:");
- M_1.name = Console.ReadLine();
- if (M_1.name != "")
- break;
- else
- {
- Console.WriteLine("請(qǐng)輸入!");
- }
- }
- while (true)
- {
- try
- {
- Console.WriteLine("請(qǐng)輸入你的年齡:");
- M_1.age = Convert.ToUInt32(Console.ReadLine());
- break;
- }
- catch (FormatException e)
- {
- Console.WriteLine("請(qǐng)輸入-9的數(shù)字!");
- }
- }
- while (true)
- {
- Console.WriteLine("請(qǐng)輸入你的電話:");
- M_1.phone = Console.ReadLine();
- if (M_1.phone != "")
- break;
- else
- {
- Console.WriteLine("請(qǐng)輸入!");
- }
- }
- Console.WriteLine("請(qǐng)輸入你的住址:");
- while (true)
- {
- Console.WriteLine("請(qǐng)輸入你的城市:");
- M_1.ad.city = Console.ReadLine();
- //M_1.address.city=Console.ReadLine();
- //通過(guò)此種方式無(wú)法直接訪問(wèn)結(jié)構(gòu)體address的成員
- if (M_1.ad.city != "")
- break;
- else
- {
- Console.WriteLine("請(qǐng)輸入!");
- }
- }
- //C#結(jié)構(gòu)體變量
- while (true)
- {
- Console.WriteLine("請(qǐng)輸入你的街道:");
- M_1.ad.street = Console.ReadLine();
- if (M_1.ad.street != "")
- break;
- else
- {
- Console.WriteLine("請(qǐng)輸入!");
- }
- }
- while (true)
- {
- try
- {
- Console.WriteLine("請(qǐng)輸入你的門牌號(hào):");
- M_1.ad.no = Convert.ToUInt32(Console.ReadLine());
- break;
- }
- catch (FormatException e)
- {
- Console.WriteLine("請(qǐng)輸入-9的數(shù)字!");
- }
- }
- 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}號(hào)",
- M_1.ad.city,M_1.ad.street,M_1.ad.no);
- }
- catch(FormatException e/*,IOException i*/)
- {
- string msg = "\n--------------
- --------------------------------------
- ---------------------\n錯(cuò)誤:\n 源 :"+e.Source +
- "\n 消息:"+ e.Message+"\n 追蹤:\n"+e.StackTrace;
- Console.WriteLine(msg);
- }
- }
- }
C#結(jié)構(gòu)體變量示例之版本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="見到這條消息說(shuō)明你是笨蛋";
- static string write_info = "CUG來(lái)客";
- static string fileName = "信息記錄.txt";
- static void outMsg(int flag)//輸出信息到控制臺(tái)
- {
- Console.ForegroundColor = ConsoleColor.Green;
- switch (flag)
- {
- case 0:
- {
- Console.ForegroundColor = ConsoleColor.Red;
- //Console.BackgroundColor = ConsoleColor.White;
- Console.WriteLine("\n\n 2008世紀(jì)通訊錄 ");
- Console.WriteLine("_________________________
- _____________________________________________________\n");
- Console.WriteLine("錄入信息(I):");
- Console.WriteLine("查看記錄(V):");
- Console.WriteLine("退出系統(tǒng)(E):");
- break;
- }
- case 1:
- {
- Console.WriteLine("請(qǐng)輸入!");
- break;
- }
- case 2:
- {
- Console.WriteLine("請(qǐng)輸入-9的數(shù)字!");
- break;
- }
- case 3:
- {
- Console.ForegroundColor = ConsoleColor.Magenta;
- Console.WriteLine("-------
- --------------------------------------------");
- Console.WriteLine(info);
- break;
- }
- case 4:
- {
- Console.WriteLine("讀取文件錯(cuò)誤!");
- break;
- }
- case 5:
- {
- Console.WriteLine("寫文件錯(cuò)誤!");
- 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錯(cuò)誤:\n 源 :" + e.Source + "\n 消息:" +
- e.Message + "\n 追蹤:\n" + e.StackTrace;
- Console.WriteLine(msg);
- }
- }
- static void inPut(ref phoneBook pb)//從控制臺(tái)輸入信息
- {
- try
- {
- while (true)
- {
- Console.WriteLine("請(qǐng)輸入你的姓名:");
- pb.name = Console.ReadLine();
- if (pb.name != "")
- break;
- else
- {
- outMsg(1);
- }
- }
- while (true)
- {
- try
- {
- Console.WriteLine("請(qǐng)輸入你的年齡:");
- pb.age = Convert.ToUInt32(Console.ReadLine());
- break;
- }
- catch
- {
- outMsg(2);
- }
- }
- while (true)
- {
- Console.WriteLine("請(qǐng)輸入你的電話:");
- pb.phone = Console.ReadLine();
- if (pb.phone != "")
- break;
- else
- {
- outMsg(1);
- }
- }
- Console.WriteLine("請(qǐng)輸入你的住址:");
- while (true)
- {
- Console.WriteLine("請(qǐng)輸入你的城市:");
- pb.ad.city = Console.ReadLine();
- if (pb.ad.city != "")
- break;
- else
- {
- outMsg(1);
- }
- }
- while (true)
- {
- Console.WriteLine("請(qǐng)輸入你的街道:");
- pb.ad.street = Console.ReadLine();
- if (pb.ad.street != "")
- break;
- else
- {
- outMsg(1);
- }
- }
- //C#結(jié)構(gòu)體變量
- while (true)
- {
- try
- {
- Console.WriteLine("請(qǐng)輸入你的門牌號(hào):");
- pb.ad.no = Convert.ToUInt32(Console.ReadLine());
- break;
- }
- catch
- {
- outMsg(2);
- }
- }
- //C#結(jié)構(gòu)體變量
- info = "你的信息如下:\r\n" +
- " 姓名: " + pb.name + "\r\n 年齡: " + pb.age + "\r\n
- 電話: " + pb.phone + "\r\n 住址:
- " + pb.ad.city + "市" + pb.ad.street +
- "街" + pb.ad.no + "號(hào)";
- write_info = "姓名: " + pb.name +
- "\r\n年齡: " + pb.age + "\r\n電話: " + pb.phone +
- "\r\n住址: " + pb.ad.city + "市" +
- pb.ad.street + "街" + pb.ad.no + "號(hào)";
- }
- catch (FormatException e/*,IOException i*/)
- {
- string msg = "\n---------------
- ----------------------------------------------------------\n
- 錯(cuò)誤:\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;//記錄信息的條數(shù)
- while(srd.Peek()>=0)
- {
- str = srd.ReadLine();
- if (str.Contains("姓名"))//以出現(xiàn)姓名為標(biāo)志,處理?xiàng)l數(shù)記錄
- {
- count++;
- }
- Console.WriteLine(str);
- //在此處僅僅是將讀取的字符串輸出,
- //其實(shí)還可以做進(jìn)一步的處理,得到一個(gè)phoneBook
- //結(jié)構(gòu)做其他用處,這也是最初的目的,但由于時(shí)間原因,也就罷了。
- }
- 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': //退出系統(tǒng)
- {
- Console.WriteLine("機(jī)器名 :{0} \r\n
- OS版本 :{1} \r\n運(yùn)行時(shí)間:{2}毫秒",
- Environment.MachineName, Environment.OSVersion,
- Environment.TickCount);
- Console.WriteLine("退出系統(tǒng)");
- Environment.Exit(0);
- //Application.Exit();
- break;
- }
- default:
- {
- outMsg(6);
- break;
- }
- }
- } while (true);
- }
- }
C#結(jié)構(gòu)體變量的基恩內(nèi)容就向你介紹到這里,希望對(duì)你學(xué)習(xí)和了解C#結(jié)構(gòu)體變量有所幫助。
【編輯推薦】