Porady: implementowanie klasy lekkiej przy użyciu automatycznie implementowanych właściwości (Przewodnik programowania w języku C#)

W tym przykładzie przedstawiono tworzenie niezmienne klasy lekkie, który służy jedynie do hermetyzacji zestawu właściwości auto wdrożone.Przy tego rodzaju konstrukcja zamiast struct należy użyć semantykę typu odwołania.

Należy zauważyć, że właściwości auto realizowane, zarówno uzyskać i set akcesor są wymagane.Można utworzyć klasy niezmienne oświadczając, set akcesorów jako prywatnego.Jednakże, jeżeli oświadczyć prywatnej set akcesor, inicjatora obiektu nie można używać do zainicjowania właściwości.Należy użyć konstruktora lub metoda fabrykująca.

Przykład

W poniższym przykładzie przedstawiono dwa sposoby zaimplementowania niezmienne klasy, która ma właściwości auto wdrożone.Pierwsza klasa używa konstruktora zainicjować właściwości i drugiej klasy używa metody statycznej factory.

// 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; private set; }
        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 set; }

        // 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();                
        }
    }

/* Output:
    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.
*/

Kompilator tworzy oporowe pola dla każdej właściwości auto wdrożone.Pola nie są dostępne bezpośrednio z kodu źródłowego.

Zobacz też

Informacje

Właściwości (Przewodnik programowania w języku C#)

struct (odwołanie w C#)

Inicjatory obiektów i kolekcji (Przewodnik programowania w języku C#)