Accessibility inside a class [duplicate] - c#

This question already has answers here:
Default visibility for C# classes and members (fields, methods, etc.)?
(4 answers)
Closed 9 years ago.
Inside a class by default everything is private.
By default non nested class ,interface, struct, delegate & enum have internal accessibility.
But that means if all these comes inside class, everything will become private by default.
Are there any types that will become non private inside a class by default?
PS. just a kind of exceptional case like an instance variable that we can't assign any values inside a struct, but by using null coalescing operator we can assign.

Please have a read of http://msdn.microsoft.com/en-us/library/ms173121.aspx
Struct members, including nested classes and structs, can be declared as public, internal, or private. Class members, including nested classes and structs, can be public, protected internal, protected, internal, or private. The access level for class members and struct members, including nested classes and structs, is private by default. Private nested types are not accessible from outside the containing type.
(emphasis mine).

Related

About protected and private [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 3 years ago.
I am new to C# programming and I have some questions about access modifier protected and private .
Is it true that when data members and member functions of the class are kept "private" ,they can only be accessed by that class and can't even be accessed by the child (derived) class of that class?
In case of protected, if a data members and member functions of that class are kept protected ,then only the code of that class can access those data members and member functions and also if that class has a derived(child ) class ,then even child class can also access the protected data members and member functions of that base (parent ) class ?
Thank
Protected members are accessible by subclasses, private are accessible only in the class
Well this is going to be a short answer but : yes, this is correct.

In C# can we ever use protected access modifier for an interface?

The default access modifier for an interface is public. However,
in C# can we ever use protected access modifier for an interface?
In this example:
class C
{
protected interface I
{
}
}
the nested type I is protected.
This means that I is visible to all of C and also to all classes that derive from C. For example this could be used to make protected instance methods inside C that contain I in their signatures or return types.
Note: The default accessibility for an interface is not public, like you claim. The default accessibility for a member of an interface is public. That is something else.
The default accessibility for the interface itself depends on what the interface itself is a member of. If the interface is a member of an "outer" class or struct (like my C above), it has the usual default accessibility of class and struct members, which is private. If the interface is a direct member of a namespace (possibly the global namespace), the default accessibility is the usual default for all non-nested types, which is internal.
You can change this acceccibility.
See this page : https://msdn.microsoft.com/en-us/library/ms173121.aspx.
It's say that "Interfaces declared directly within a namespace can be declared as public or internal and, just like classes and structs, interfaces default to internal access."
Be carefull, as you see in this link https://msdn.microsoft.com/en-us/library/ba0a1yw2.aspx members are public by default and can't be modified.

Protected Internal Member [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is the difference between ‘protected’ and ‘protected internal’?
I have seen a lot of controversy over the true meaning of declaring a member protected internal.
Under this context is the member's access modifier either "protected or internal" or "protected and internal"?
The documentation is clear that it is "protected or internal".
That is - a member is accessible both within the assembly and any subtype.
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. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.

C# Member Access from Nested Class to Containing Class [duplicate]

This question already has answers here:
What's the best way of accessing field in the enclosing class from the nested class?
(9 answers)
Closed 10 years ago.
I have ClassB, which is nested inside of ClassA. In ClassA I have a variable called _MyId... how can I access _MyId from ClassB?
Thanks in advance!
Put simply, you'll need a reference to an instance ClassA within ClassB.
C#'s nested classes work differently from Java's, if that's what you're used to. The closest analog would be Java's static class when applied to a nested type (meaning that C#'s nested classes are not associated with a particular instance of the outer class).
In other words, C#'s nested classes are not "special" when compared to outer classes, other than the fact that they have visibility into the private members of the outer class. Nonetheless, you still need a reference to the outer class in order to access them.
In ClassB's constructor pass an instance of class A.
If the field is static, you can simply refer to it as ClassA._MyId. If it's not you should use classAInstance._MyId where classAInstance is an instance of ClassA.
If you're coming from Java background, you should note that nested classes in C# are similar to static nested classes in Java.
You have to refer to a particular instance of ClassA in order to retrieve a member. If your instance is called foo, just use foo._MyId.
If _MyId is static you can access it by it's name or as ClassA._MyId.
But otherwise you need an instance of ClassA first, and there is little difference with acces from another class (that is not nested). But members from ClassB do gain access to private members of ClassA.
Explanation: Nesting classes is a static relation between the 2 Types, there is no implicit relation between instances. You will have to pass references to objects between them just as if the classes were not nested.

Default Modifier of a Class [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What are the Default Access Modifiers in C#?
When I define a class in C#, and don't specify any access modifier which one is the default? Is it public or protected?
In C# if the type is not nested (within other class or struct) and doesn't have access modifier applied it is internal. If it is nested - private.
From C# specification:
3.5.1 Declared accessibility
...
• Types declared in compilation units
or namespaces can have public or
internal declared accessibility and
default to internal declared
accessibility.
• Class members can
have any of the five kinds of declared
accessibility and default to private
declared accessibility. (Note that a
type declared as a member of a class
can have any of the five kinds of
declared accessibility, whereas a type
declared as a member of a namespace
can have only public or internal
declared accessibility.)
It is [internal]

Categories