TAM
TAM

Reputation: 347

Get/Set in C# easier way?

Hey so I am currently writing a program for university and when creating my classes I am using get set my question is, is there an easier way than this method shown bellow to set things.

Code snippet showing my current way

    private string customer_first_name;
    private string customer_surname;
    private string customer_address;
    public DateTime arrival_time;

//Using accessors to get the private variables
//Using accessors to set the private variables

    public string Customer_First_Name
    {
        get { return customer_first_name; }
        set { customer_first_name = value; }
    }

Upvotes: 4

Views: 237

Answers (3)

public string Customer_First_Name {get; set;}

Doing that will also implicitly create an underlying field for you.

Upvotes: 0

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236318

Use auto-implemented properties instead:

public string Customer_First_Name { get; set; }

In this case compiler will generate field for storing property value.

BTW you can save even more time if you will use prop code snippet in Visual Studio. Just start typing prop and hit Tab - it will paste snippet for auto-generated property. All what you need - provide property name and type.

Upvotes: 9

trydis
trydis

Reputation: 3925

public string Customer_First_Name { get; set; }

is sufficient if you don't need any logic in your getter/setter.

Upvotes: 1

Related Questions