15-结构体

示例:

using System;
using System.Text;
     
struct Books
{
   private string title; // 支持 public
   private string author;
   private string subject;
   private int book_id;
   public void getValues(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);
   }

};  

public class testStructure
{
   public static void Main(string[] args)
   {

      Books Book1 = new Books(); /* 声明 Book1,类型为 Book */
      Books Book2 = new Books(); /* 声明 Book2,类型为 Book */

      /* book 1 详述 */
      Book1.getValues("C Programming",
      "Nuha Ali", "C Programming Tutorial",6495407);

      /* book 2 详述 */
      Book2.getValues("Telecom Billing",
      "Zara Ali", "Telecom Billing Tutorial", 6495700);

      /* 打印 Book1 信息 */
      Book1.display();

      /* 打印 Book2 信息 */
      Book2.display(); 

      Console.ReadKey();

   }
}

  

注意事项:

1. 不能定义析构函数;

2. 不能继承;

3. 成员不能指定为 abstract、virtual 或 protected;

4. 使用 New 操作符创建一个结构对象时,会调用适当的构造函数来创建结构;

5. 不使用 New 操作符,只有在所有的字段都被初始化之后,字段才被赋值,对象才被使用。

参考:

http://www.runoob.com/csharp/csharp-struct.html

猜你喜欢

转载自www.cnblogs.com/alexYuin/p/9068415.html