Difference between private protected and internal protected [duplicate] - c#

This question already has answers here:
What is the use case for the (C# 7.2) "private protected" modifier?
(4 answers)
Closed 5 years ago.
C# 7.2 introduced the private protected modifier, whats the difference to internal protected?
From the doc:
A private protected member is accessible by types derived from the containing class, but only within its containing assembly.
Isn't that exactly what internal protected does?

From Access Modifiers (C# Programming Guide)
Protected Internal : The type or member can be accessed by any code in
the assembly in which it is declared, or from within a derived class
in another assembly.
And
Private Protected : The type or member can be accessed only within its
declaring assembly, by code in the same class or in a type that is
derived from that class.
Another useful link C# 7 Series, Part 5: Private Protected

Related

Can someone please explain what is protected override void? [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 6 years ago.
I am learning about xamarin in basic stage. All the function calls are done by 'protected override void'. So that anyone please help what is the use of it. How it differs from public void.
protected - access modifier (in C# you have public, protected, private, internal)
override - you override virtual method which was implemented scope above
void - return type of your method
public - everyone can access this method.
protected - only inherited classes can access this 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.

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