3
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;
steveax
  • 17,527
  • 6
  • 44
  • 59
Cains
  • 883
  • 2
  • 13
  • 23
  • No, that is not possible. Why? Is it too much typing? – Marius Bancila Mar 13 '14 at 09:22
  • Note that public variables should be avoided. You should use properties instead. – EFrank Mar 13 '14 at 09:23
  • I have many variables in some classes and typing public behind every single one of them just seems really unnecessary. I thought I saw other languages have something like this. – Cains Mar 13 '14 at 09:23
  • Please, do not include information about a language used in a question title unless it wouldn't make sense without it. Tags serve this purpose. – Ondrej Janacek Mar 13 '14 at 09:24
  • also http://stackoverflow.com/questions/546869/labeling-a-group-of-members-as-private-public-in-c-sharp – Dexters Mar 13 '14 at 09:25
  • @Cains: Braces around single-statement `if` bodies are also technically unnecessary, but there is a very strong case to be made for using them. While C# doesn't go that far, requiring an explicit visibility specification before each member is a good thing, *especially* since definitions are always inline. Would you enjoy having to scroll two pages up to see what the visibility of a method is because it was unnecessary to type it on the spot? The designers thought you wouldn't, so they made this choice. – Jon Mar 13 '14 at 09:25
  • @Jon I see. I like to read my variables in sections of accessibility, as in my opinion scrolling to see what accessibility a section is is a lot easier than typing the accessibility for every variable. – Cains Mar 13 '14 at 09:28
  • @Cains: Well, I can't argue that your preference is wrong but do keep in mind that code is a write-once-read-many medium. You would save a little effort on typing (once) at the cost of a little effort on reading (many times). – Jon Mar 13 '14 at 09:30
  • @Jon I see your point, if others were reading my code it would be easier for them to understand it this way compared to the ease involved in me typing it that one time. – Cains Mar 13 '14 at 09:33
  • See this [Declaring and initializing multiple variables in C#](https://stackoverflow.com/a/67517250/4287015) – Sk Shahnawaz-ul Haque May 13 '21 at 10:11

4 Answers4

11

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;
  • 2
    Of course then they all have to share the same type. – Jon Mar 13 '14 at 09:22
  • @Jon I guess that is what the OP wanted, according to the example. –  Mar 13 '14 at 09:23
  • I apologize but the similar types were just coincidence. I'm looking for a solution that covers multiple types and preferably multiple lines. – Cains Mar 13 '14 at 09:24
  • @Tijesunimi It would still be a good idea to include it in your answer so it's more visible – Luke Marlin Mar 13 '14 at 09:25
  • @Cains you need to specify that in your question.And btw, what you are asking is **not possible** – Selman Genç Mar 13 '14 at 09:25
  • @Tijesunimi: Sometimes we should use common sense on example code. – Jon Mar 13 '14 at 09:28
  • @Jon Yes. If he had given the type of example in [this question](http://stackoverflow.com/questions/546869/labeling-a-group-of-members-as-private-public-in-c-sharp). Then the question wouldn't have been misunderstood. –  Mar 13 '14 at 09:29
3
public int var1, var2, var3, var4;
user3209815
  • 357
  • 1
  • 11
  • 25
2

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;
Sajad Karuthedath
  • 14,987
  • 4
  • 32
  • 49
1

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;

};