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