user3308470
user3308470

Reputation: 107

C# setter/getter of two variables

Is there an option to do one setter/getter for two variables? Or the only option are two separate setter/getter's like this:

int var1;
int var2;

public int var1 
    {
    get { return var1; }
    set { var1 = value; }
    }

public int var2 
    {
    get { return var2; }
    set { var2 = value; }
    }

Upvotes: 0

Views: 4341

Answers (2)

Alexei Levenkov
Alexei Levenkov

Reputation: 100547

"one setter/getter for two variables" - there no syntax to simplify that (you can use automatic properties for single value only).

It can be implemented with wrapping these variables into class and using single property to get/set. I.e. using built in Tuple class:

var1;
int var2;

public Tuple<int,int> BothVars
{
  get { return Tuple.Create(var1,var2); }
  set { 
       var1 = value.Item1;
       var2 = value.Item2;
      }
}

Upvotes: 3

Anik Islam Abhi
Anik Islam Abhi

Reputation: 25352

You can try this

public int var1 { get;set;}

public int var2 { get;set;}

Upvotes: 5

Related Questions