Blog Home  Home RSS 2.0 Atom 1.0 CDF  
Andreas Mersch - C# 3.0: Automatisch implementierte Eigenschaften
software development and more...
 
 Tuesday, December 11, 2007

Ein neues Sprachfeature von C# 3.0 sind automatisch implementierte Eigenschaften. Wo bisher ausimplementierte Getter und Setter notwendig waren, reicht nun die simple Anweisung "get;" bzw. "set;" bei der Property Deklaration.

Beispiel:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace
_001_AutoImplProperties
{
   
   ///
<summary>
   /// Sample class for Automatically Implemented Properties
   /// </summary>
   class Person
   {
      public Guid Id { get; private set; } // only public getter
      public string Name { get; set; } // public getter and setter
      public string Address { get; set; }
      public string City { get; set; }
      public string Zip { get; set; }

      public Person(Guid id)
      {
         Id = id;
      }

      public override string ToString()
      {
         return string.Format("{0} ({1})", Name, Id);
      }
   }

   class Program
   {
      static void Main(string[] args)
      {
         Person a = new Person(Guid.NewGuid()) { Name = "Andreas", Address = "Adresse 1", Zip = "12345", City = "Ort 1" };
         Person b = new Person(Guid.NewGuid()) { Name = "Melanie", Address = "Adresse 2", Zip = "98765", City = "Ort 2" };

         Console
.WriteLine(a);
         Console.WriteLine(b);
      }
   }
}

12/11/2007 8:54:09 AM (Mitteleuropäische Zeit , UTC+01:00)  #      C#  | 
Copyright © 2012 andreas mersch. All rights reserved.
DasBlog 'Portal' theme by Johnny Hughes.
Pick a theme: