Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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; }
    }
like image 675
TAM Avatar asked Dec 27 '22 14:12

TAM


1 Answers

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.

like image 149
Sergey Berezovskiy Avatar answered Jan 11 '23 23:01

Sergey Berezovskiy