struct Books {
public string title;
public string author;
public string subject;
public int book_id;
};
using System; namespace task.lmcjl.com { struct Books { public string title; public string author; public string subject; public int book_id; } class Demo { static void Main(string[] args) { Books book1; // 声明 Books 类型的 book1 Books book2; // 声明 Books 类型的 book2 // 定义 book1 的属性 book1.title = "C#教程"; book1.author = "C语言中文网"; book1.subject = "C#编程教程"; book1.book_id = 123456; // 定义 book2 的属性 book2.title = "HTTP教程"; book2.author = "C语言中文网"; book2.subject = "HTTP协议教程"; book2.book_id = 123455; // 输出 book1 的属性信息 Console.WriteLine("book1 title : {0}", book1.title); Console.WriteLine("book1 author : {0}", book1.author); Console.WriteLine("book1 subject : {0}", book1.subject); Console.WriteLine("book1 book_id :{0}", book1.book_id); // 输出 book2 的属性信息 Console.WriteLine("book2 title : {0}", book2.title); Console.WriteLine("book2 author : {0}", book2.author); Console.WriteLine("book2 subject : {0}", book2.subject); Console.WriteLine("book2 book_id :{0}", book2.book_id); Console.ReadKey(); } } }运行结果如下:
book1 title : C#教程
book1 author : C语言中文网
book1 subject : C#编程教程
book1 book_id :123456
book2 title : HTTP教程
book2 author : C语言中文网
book2 subject : HTTP协议教程
book2 book_id :123455
using System; namespace task.lmcjl.com { struct Books { public string title; public string author; public string subject; public int book_id; public void getValue(string t, string a, string s, int id){ title = t; author = a; subject = s; book_id = id; } public void disPlay(){ Console.WriteLine("Title:{0}", title); Console.WriteLine("Author:{0}", author); Console.WriteLine("Subject:{0}", subject); Console.WriteLine("Book_id:{0}", book_id); } } class Demo { static void Main(string[] args) { Books book1 = new Books(); // 实例化 Books 结构体 Books book2 = new Books(); // 实例化 Books 结构体 // 定义 book1 的属性 book1.getValue("C#教程","C语言中文网","C#编程教程",123456); // 定义 book2 的属性 book2.getValue("HTTP教程","C语言中文网","HTTP协议教程",123455); // 输出 book1 的属性信息 book1.disPlay(); // 输出 book2 的属性信息 book2.disPlay(); Console.ReadKey(); } } }运行结果如下:
Title:C#教程
Author:C语言中文网
Subject:C#编程教程
Book_id:123456
Title:HTTP教程
Author:C语言中文网
Subject:HTTP协议教程
Book_id:123455
本文链接:http://task.lmcjl.com/news/18296.html