Dumbo
Dumbo

Reputation: 14142

Why this syntax is wrong? Can not use GET and SET for more than a property in a line

I have to define a lot of fields, and I can't do it like:

public double d1, d2, d3, d4, d5, d6 {get;set;} //does not work

Instead I have to do

public double d1 { get; set; } public double d2 { get; set; } //and so on

but on private fields we have:

private double d1, d2, d3, d4 = 0; //Works

I cant get it why it is not working. Maybe somone can explain please? is there a way around?

Update: It seems that a lot of copy pasting made me to screw my knowledge and forget the difference between Fields & Properties. Anyway in the first line I meant everything should be Properties

Upvotes: 2

Views: 187

Answers (8)

saadan
saadan

Reputation: 487

type propf and then tap tap or prop and tap tap. think thats the easiest way. I use this alot my self.

with propf you will get

    private int myVar;

    public int MyProperty
    {
        get { return myVar; }
        set { myVar = value; }
    }

and with prop you will get

    public int MyProperty { get; set; };

Upvotes: 1

Larry
Larry

Reputation: 18051

Properties are function calls, you cannot give them a default value this way. Use the class constructor or another function instead.

Here is a quick tip to type faster in Visual Studio. Type fields :

public int a;
public int b;
public int c;
public int d;

Then type { get; set; } and cut it to the clipboard CTRL+X

Select all the semicolons up to down maintaining ALT key and left mouse pressed together

Then paste CTRL+V : TA-DA !

public int a { get; set; }
public int b { get; set; }
public int c { get; set; }
public int d { get; set; }

Upvotes: 7

Andrii Startsev
Andrii Startsev

Reputation: 767

From C# Language Specification

1.6.7.2 Properties

Properties are a natural extension of fields. Both are named members with associated types, and the syntax for accessing fields and properties is the same. However, unlike fields, properties do not denote storage locations. Instead, properties have accessors that specify the statements to be executed when their values are read or written.

Upvotes: 4

George Duckett
George Duckett

Reputation: 32448

On the first line there would be an ambiguity; do you want d1 to d5 to be properties or fields (which are 2 different things). To avoid this problem it simply isn't allowed.


Note the below:

double field;
double property {get; set;}

Upvotes: 7

Valamas
Valamas

Reputation: 24759

Sorry friend, you gotta do a lot of copying and pasting.

Upvotes: 1

Fischermaen
Fischermaen

Reputation: 12468

I don't feel happy that those statements work:

private double d1, d2, d3, d4 = 0;

I think the code is harder to read. Personally I prefer "one variable - one line", so I have no trouble that

public double d1, d2, d3, d4, d5, d6 {get;set;} 

don't work.

It is the syntax of the language.

Upvotes: 1

bobbymcr
bobbymcr

Reputation: 24177

Fields (int x) and properties (int X { get; set; }) are different things. You can't mix the two types of declarations in the same statement.

Upvotes: 3

gdoron
gdoron

Reputation: 150313

This is the way Microsoft designed the syntax...
It's like asking why I leave an apple out of the window it falls. answer: gravity power works that way...
=)

Upvotes: 2

Related Questions