Reputation: 901
public int var1;
public int var2;
public int var3;
Is there an easier way to declare multiple variables under one accessibility like this?
Something like
public:
int var1;
int var2;
int var3;
Upvotes: 4
Views: 8253
Reputation: 937
C# Doesn't support C++ syntax access specifier declaration. The below programs shows how to declare multiple variables i C#
class MultipleVariable
{
public int x,y,z;
private int x1,x2,x3;
public float a,b,c;
private float a1,b2,c3;
public char p,q,r;
private char p1,q2,r3;
};
Upvotes: 1
Reputation: 15767
Use all in a single line
A single statement can assign multiple variables. Languages derived from C-style syntax often allow you to use commas to declare multiple variables in one statement.
public int var1,var2,var3;
public double d1,d2,d3;
public string txt1,txt2,txt3,txt4;
Upvotes: 2
Reputation:
You can do it putting the accessibility first and all the variable names separated by a comma on the same line. Like this :
public int var1, var2, var3;
From your comment, it seems that you want the accessibility but different types. That is not possible, you would have to do it this way :
public int var1, var2;
public double var3;
Upvotes: 11