Alex
Alex

Reputation: 307

Access levels in C#

Quick simple questions,

Is there any way to declare the access level of more then one variable or method in C# as in C++?

Aswell, is it the same in C# as in C++ where the members of a struct are, if not defined, public and the members of a class are private?

Regards, Alex

Upvotes: 1

Views: 362

Answers (4)

Oscar Gomez
Oscar Gomez

Reputation: 18488

If you don't declare an access type, it will be as private as it can be. The default for non-nested types is internal, and for nested types is private.

Upvotes: 1

The Evil Greebo
The Evil Greebo

Reputation: 7138

In C# you can declare multiple variables on a single line as such:

<access> <type> <name> [= <default>] [, <name> [= <default>]...]

so

private int a = 1, b, c=3;

Structs are not often used bit of the access modifier is not declared, it is private for structs and classes alike.

Upvotes: 1

Petar Ivanov
Petar Ivanov

Reputation: 93090

No you can't do that in general, unless the variables are of the same type as in

public int a,b,c;

Upvotes: 2

BrokenGlass
BrokenGlass

Reputation: 161012

1.) No: Each variable is declared separately and may be qualified with a member access modifier. Exceptions are variables of the same type which can share a type and member access modifier.

2.) No: The same rules as for classes apply - by default members are private if no access modifier is specified, big difference is that struct is a value type and not a reference type.

Upvotes: 3

Related Questions