Private & Public declarations in C# [duplicate] - c#

This question already has answers here:
labeling a group of members as private/public in c#
(3 answers)
Closed 9 years ago.
Is there a way in C# to declare a group of variables and methods as private or public like in C++ (example below). I am just trying to avoid typing a million "public"s and "private"s.
class Foo
{
private:
int Alpha;
string Dog;
public:
bool Bites;
bool Bad;
}
I keep getting an error in C# and have exhausted my internet search abilities. Thanks

No. You need to specify visibility for each member.
private is default for members, so it is safe to omit it (unless your coding guidelines tell you must specify). More details/links - Default visibility for C# classes and members (fields, methods, etc)?

Related

Is it possible to create indexer in helper class c# [duplicate]

This question already has answers here:
C# extend indexer?
(6 answers)
Closed 1 year ago.
I am intrested in creating custom indexer for char[,]. It seems to me that it is prohibited, but I am wondering if there is an oportunity. The code that could have solved the problem is:
public static class GeneratorHelpers
{
public static char int[Vector2D position] (this char[,] field)
{
return field[position.X, position.Y];
}
}
The above code does not compile.
Currently, there is no such things as extension properties, operators or indexers in the C# language. The most common pattern for this would be just to take additional parameters to an extension method.

Why members in static class are not static by default [duplicate]

This question already has answers here:
Why do members of a static class need to be declared as static? Why isn't it just implicit?
(8 answers)
Closed 7 years ago.
My question is why members are not by default static in static class.
As we see, interface members are by default public and abstract.
Thanks,
Anil
We should ask c# language designer.
But I understand the ratio behind: it forces the programmer to say "this function is static", even if it would be implied by the fact the the class is static.
Maybe It's a matter of readability: when you read a method without body (and no abstract keyword), you know that this method can only be part of an interface.
When you read a method without "static" modifier, you would need to read also class declaration to understand that is part of a static class and therefore static itself

Definition of a private access modifier [duplicate]

This question already has answers here:
What does the "private" modifier do?
(13 answers)
Closed 7 years ago.
Is this definition for a private access modifier accurate?:
private: Any thing declared as private can’t be seen outside of its class.
What about a private nested class?
This class can be seen "outside of its own class" by the class that is nesting it.
Maybe you could say that this nested class is actually a private member of the class nesting it and therefore can't be seen outside of the nesting class which would make the definition provided above true.
If a private nested class is NOT considered a member of the nesting class then I think the definition provided above is not accurate.
Maybe you could say that this nested class is actually a private member of the class nesting it and therefore can't be seen outside of the nesting class.
That's exactly what it means.

What is the difference between a public and private variable [duplicate]

This question already has answers here:
In C#, what is the difference between public, private, protected, and having no access modifier?
(19 answers)
Closed 7 years ago.
Newbie Incoming, coming with a question about Unity, and C# in particular. This could be really simple, but I could not figure it out.
What's the difference between public and private mean? I don't get it. public, private, it boggles my mind. I just can't get it through my head, which doesn't happen very often. Can someone explain it to me like I'm five? It would really help me out on my journey of making a ball move across the ground. Thank you in advance.
Good night .
The difference is :
Private : Those variables or " functions" which may only be used in the source class .
Public : Those variables or "functions" that can be used in various class .
Classes: A class is a construct that allows create your own custom types by grouping variables of other types , methods and events. A class is like a blueprint . Defines the data and behavior of a type.

properties without body [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What’s the difference between encapsulating a private member as a property and defining a property without a private member?
In C#, usually when I define a property I declare and implement a single line or more for get and set. e.g.
public bool IsThere
{
get { return _isThere; }
set { _isThere = value;}
}
now what does this mean?
public bool IsThere
{
get;
set;
}
Those are auto-properties. They work the same way as your first example, but allow you to omit the unnecessary source code.
They're best used when there is no longer to your getter/setter methods.
They also allow you to add logic to your getter/setter methods later without breaking any calling code (even though you'll also have to implement the private backing property yourself).
It's an Auto-Implemented Property (automatic property).
The C# compiler will automatically create a private field member for the get/set methods to read/write from.
Note that there are limitations to automatic properties (for now). For example, you cannot use modifiers such as readonly, though you can still mark it as private set it isn't quite the same.

Categories

Resources