Definition of a private access modifier [duplicate] - c#

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.

Related

Why to use Overriding? [duplicate]

This question already has answers here:
C# virtual keyword
(9 answers)
C# - Keyword usage virtual+override vs. new
(11 answers)
Can you write virtual functions / methods in Java?
(6 answers)
Closed 4 years ago.
In an interview it was asked why do we need to override method of base class.
I tried to answer like when we want to have different implementation in derived class.
But then he said, "Why don't we just create a new method with different name and different implementation instead of overriding base class method?", anyway we are not reusing base class method as implementation will be different then just create a new method instead of overriding.
I got confused what to answer. Could somebody explain.
public class BaseClass
{
virtual void Foo(){}
}
public class DerivedClass: BaseClass
{
override void Foo(){}
}
Generally we implement overriding like above.
What he said is like why do we need concept of overriding we can do like below
public class BaseClass
{
void Foo(){}
}
public class DerivedClass: BaseClass
{
void Foo1(){}
}
His question was looking weird I tried to explain but like its a method of base class we are just redefining it in derived class. In this way our code will be clean as well. But looks like he was not satisfied.
I would check this answer:
Why does this polymorphic C# code print what it does?
then try to grasp the concept of methods in an object basically having pointers to code. When you override implementation in an subclass then that becomes the new code pointed to, whether it's used or cast as a superclass or not.
So the main purpose of overriding is to create classes that inherit from one class but each have their own implementation and then be able to treat or operate on them equally the same as the original superclass. This is the essence of the Liskov Principle or the 'L' in SOLID. What is an example of the Liskov Substitution Principle?

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

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.

Private & Public declarations in C# [duplicate]

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)?

Why can I call a private method of another instance of the same type outside of that instance? [duplicate]

This question already has answers here:
Why and how does C# allow accessing private variables outside the class itself when it's within the same containing class?
(3 answers)
Closed 4 years ago.
If I have ObjectA, and it has a private method GetPrice() and also has a "parent" field of the same type, why am I able to call GetPrice() on the parent instance from within the child instance?
Example:
private decimal GetPrice()
{
ObjectA parent = Parent;
if(parent != null)
{
return parent.GetPrice(); // Why is this OK?
}
return 0;
}
Because private means "not accessible to other types", not "not accessible to other instances".
Because private scope is limited to the class, not the instance as defined in the C# spec:
1.6.2 Accessibility
Each member of a class has an associated accessibility, which controls
the regions of program text that are able to access the member. There
are five possible forms of accessibility. These are summarized in the
following table.
Accessibility Meaning
public Access not limited
protected Access limited to this class or classes derived from this class
internal Access limited to this program
protected internal Access limited to this program or classes derived from this class
private Access limited to this class
An access modifier is related to it's implementing class/type not to instances of that class

Categories

Resources