欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C#教程

當(dāng)前位置:主頁 > 軟件編程 > C#教程 >

舉例講解C#中自動(dòng)實(shí)現(xiàn)的屬性

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C#教程|點(diǎn)擊: 次

在 C# 3.0 及更高版本,當(dāng)屬性訪問器中不需要任何其他邏輯時(shí),自動(dòng)實(shí)現(xiàn)的屬性會(huì)使屬性聲明更加簡(jiǎn)潔。它們還允許客戶端代碼創(chuàng)建對(duì)象。當(dāng)你聲明以下示例中所示的屬性時(shí),編譯器將創(chuàng)建僅可以通過該屬性的 get 和 set 訪問器訪問的專用、匿名支持字段。
下列示例演示一個(gè)簡(jiǎn)單的類,它具有某些自動(dòng)實(shí)現(xiàn)的屬性:

// This class is mutable. Its data can be modified from
// outside the class.
class Customer
{
  // Auto-Impl Properties for trivial get and set
  public double TotalPurchases { get; set; }
  public string Name { get; set; }
  public int CustomerID { get; set; }

  // Constructor
  public Customer(double purchases, string name, int ID)
  {
    TotalPurchases = purchases;
    Name = name;
    CustomerID = ID;
  }
  // Methods
  public string GetContactInfo() {return "ContactInfo";}
  public string GetTransactionHistory() {return "History";}

  // .. Additional methods, events, etc.
}

class Program
{
  static void Main()
  {
    // Intialize a new object.
    Customer cust1 = new Customer ( 4987.63, "Northwind",90108 );

    //Modify a property
    cust1.TotalPurchases += 499.99;
  }
}

在 C# 6 和更高版本中,你可以像字段一樣初始化自動(dòng)實(shí)現(xiàn)屬性:

public string FirstName { get; set; } = "Jane";

上一示例中所示的類是可變的。創(chuàng)建客戶端代碼后可以用于更改對(duì)象中的值。在包含重要行為(方法)以及數(shù)據(jù)的復(fù)雜類中,通常有必要具有公共屬性。但是,對(duì)于較小類或僅封裝一組值(數(shù)據(jù))且只有很少行為或沒有行為的結(jié)構(gòu),則應(yīng)該通過聲明 set 訪問器為 專用(對(duì)使用者的不可變)或通過聲明僅一個(gè) get 訪問器 (除構(gòu)造函數(shù)外都不可變),使對(duì)象不可變。
動(dòng)實(shí)現(xiàn)的屬性上允許使用特性,但很明顯支持字段上不允許,因?yàn)椴荒軓哪愕脑创a訪問它們。如果必須使用屬性的支持字段上的特性,只需創(chuàng)建一個(gè)常規(guī)屬性。

使用自動(dòng)實(shí)現(xiàn)的屬性實(shí)現(xiàn)輕量類
本示例演示如何創(chuàng)建一個(gè)僅用于封裝一組自動(dòng)實(shí)現(xiàn)的屬性的不可變輕型類。 當(dāng)你必須使用引用類型語義時(shí),請(qǐng)使用此種構(gòu)造而不是結(jié)構(gòu)。
可通過兩種方法來實(shí)現(xiàn)不可變的屬性。 可以將 set 取值函數(shù)聲明為 private。 屬性只能在該類型中設(shè)置,但它對(duì)于使用者是不可變的。 也可以僅聲明 get 取值函數(shù),使屬性除了能在該類型的構(gòu)造函數(shù)中設(shè)置,在其他任何位置都不可變。
當(dāng)你聲明一個(gè) private set 取值函數(shù)時(shí),你無法使用對(duì)象初始值設(shè)定項(xiàng)來初始化屬性。 你必須使用構(gòu)造函數(shù)或工廠方法。
示例
下面的示例演示了實(shí)現(xiàn)具有自動(dòng)實(shí)現(xiàn)屬性的不可變類的兩種方法。 這兩種方法均使用 private set 聲明其中一個(gè)屬性,使用單獨(dú)的 get 聲明另一個(gè)屬性。 第一個(gè)類僅使用構(gòu)造函數(shù)來初始化屬性,第二個(gè)類則使用可調(diào)用構(gòu)造函數(shù)的靜態(tài)工廠方法。

// This class is immutable. After an object is created, 
  // it cannot be modified from outside the class. It uses a 
  // constructor to initialize its properties. 
  class Contact
  {
    // Read-only properties. 
    public string Name { get; }
    public string Address { get; private set; }

    // Public constructor. 
    public Contact(string contactName, string contactAddress)
    {
      Name = contactName;
      Address = contactAddress;        
    }
  }

  // This class is immutable. After an object is created, 
  // it cannot be modified from outside the class. It uses a 
  // static method and private constructor to initialize its properties.  
  public class Contact2
  {
    // Read-only properties. 
    public string Name { get; private set; }
    public string Address { get; }

    // Private constructor. 
    private Contact2(string contactName, string contactAddress)
    {
      Name = contactName;
      Address = contactAddress;        
    }

    // Public factory method. 
    public static Contact2 CreateContact(string name, string address)
    {
      return new Contact2(name, address);
    }
  }

  public class Program
  { 
    static void Main()
    {
      // Some simple data sources. 
      string[] names = {"Terry Adams","Fadi Fakhouri", "Hanying Feng", 
               "Cesar Garcia", "Debra Garcia"};
      string[] addresses = {"123 Main St.", "345 Cypress Ave.", "678 1st Ave",
                 "12 108th St.", "89 E. 42nd St."};

      // Simple query to demonstrate object creation in select clause. 
      // Create Contact objects by using a constructor. 
      var query1 = from i in Enumerable.Range(0, 5)
            select new Contact(names[i], addresses[i]);

      // List elements cannot be modified by client code. 
      var list = query1.ToList();
      foreach (var contact in list)
      {
        Console.WriteLine("{0}, {1}", contact.Name, contact.Address);
      }

      // Create Contact2 objects by using a static factory method. 
      var query2 = from i in Enumerable.Range(0, 5)
             select Contact2.CreateContact(names[i], addresses[i]);

      // Console output is identical to query1. 
      var list2 = query2.ToList();

      // List elements cannot be modified by client code. 
      // CS0272: 
      // list2[0].Name = "Eugene Zabokritski"; 

      // Keep the console open in debug mode.
      Console.WriteLine("Press any key to exit.");
      Console.ReadKey();        
    }
  }

輸出:

  Terry Adams, 123 Main St.
  Fadi Fakhouri, 345 Cypress Ave.
  Hanying Feng, 678 1st Ave
  Cesar Garcia, 12 108th St.
  Debra Garcia, 89 E. 42nd St.

編譯器為每個(gè)自動(dòng)實(shí)現(xiàn)的屬性創(chuàng)建了支持字段。 這些字段無法直接從源代碼進(jìn)行訪問。

上一篇:詳解C#中的屬性和屬性的使用

欄    目:C#教程

下一篇:詳解C#面相對(duì)象編程中的繼承特性

本文標(biāo)題:舉例講解C#中自動(dòng)實(shí)現(xiàn)的屬性

本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6743.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請(qǐng)與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有