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

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.

Related

Since the C# Array class is abstract, how is it instantiated?

I just started to learn C# and I came across the fact that Array is an abstract class, not a concrete one. I am wondering how it is possible that array instances can be created if it is declared as abstract in the documentation (as seen in https://learn.microsoft.com/en-us/dotnet/api/system.array?view=net-5.0).
'Array' is an abstract class, so it cannot be instantiated directly. However it has an static 'CreateInstance' method, which creates an instance of a concrete implementation of an array containing elements of the required type.
see https://learn.microsoft.com/en-us/dotnet/api/system.array.createinstance?view=net-5.0#System_Array_CreateInstance_System_Type_System_Int32_
and also: Difference between Array.CreateInstance and using the new operator to create new array instance

Base class instance in inheritance

Hope I'm not asking something that has been already answered in here.
If my class B inherits from class A, does new B() create two instances in heap where B instance contains pointer to A instance, or there will be created only one instance of B including A members?
From Microsoft Inheritance article :
This is how an object is represented in memory, given the Class Publication directly inherits the class Object.
So a inherited class is an object that contains all information about itself but also about its base class.
It will create one instance of B, and that B is also an A.
it will create B instance that (because of inheritance) already include A members, if i understood your question well
It creates one instance and you can access all the members of both A and B on that instance. As stated A is of type B as well. I imagine that in the low level code there probably exists a pointer to A.

Derived Classes and Protecting Member Data of Base Class

I'm wondering if there is some way to declare a variable that is member data of a base class so that it is not inherited by a class derived from it. I have some member data in my base class that should not be part of objects of the derived class, so I'd like to separate what should be inherited from what should not. Is there some way to do this?
If you're trying to 'hide' data from derived/inherited classes, use private access modifier.
Yes, even though they are inherited, you cannot access them unless they are marked protected or public.
Ric, yes. I think it's just not a feature available in C++. What I'd really like to do is create a base class with member data with some kind of prefix that prevents the data from being inherited by child classes. Something like: noinherit void func1(); or noinherit double x; Where noinherit is just some keyword I made up to define data that should not be inherited by child classes. In a way, I want to be able to determine the genes inherited by the children from the parent, instead of the children just getting the full set of the parent's genes, and simply having a certain phenotype based on which genes are private, and which are public or protected, to use a genetics analogy.

Accessibility inside a class [duplicate]

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

When do we use a nested class in C# [duplicate]

This question already has answers here:
Why/when should you use nested classes in .net? Or shouldn't you?
(14 answers)
Closed 9 years ago.
Would like to know when it is right to uses a nested classes in C#?
Do we have incidents in which the use of it is unjustified and therefore not correct?
If you can give examples for both situations
Thanks
I find it's convenient to use a nested class when you need to encapsulate a format of data that is primarily going to be used within the parent class. This is usually because the purpose or format of the data is so bespoke to the parent class that it's not really suitable for wider use within your solution.
Here's a simple basic introduction to nested classes.
Nested_Classes
C# doesn't have a way to write a using directive to target a class, so that the static members of the class can be accessed without writing the class name as a qualifier (compare with Java's import static, which does allow that).
So for users of your classes, it is a little more convenient if you make any public classes as direct members of a namespace, not nested within other public classes. That way they can pull them into the global namespace with a using directive.
For private classes, go nuts, preferably put them close to where they are used to enhance the readability of your code.
I am not sure if there is room in my world for nested classes. It simply blurs the design for me. If you need to hide the information inside a class, why not just store it in member variables?
Besides, testing becomes more cumbersome without the ability to inject a stub in the place of the class.
User of Nested class is depending upon the scenario like below.
1) Organizing code into real world situations where there is a special relationship between two objects.
2) Hiding a class within another class so that you do not want the inner class to be used from outside of the class it is created within.
Suppose you have 2 classes called A and B and class B is depending upon class A without class A you cannot use class B # that scenario you can use nested classes
As per my knowledge
DataRow class is nested class for DataTable
i.e you cannot create a DataRow Class untill u declare a object of DataTable class
I find two main resons:
Personalize a class' name without ruining it.
Example: Vercas.ExplorerView, where I personalize the name of my class without ruining the meaning.
Private classes.
Example: Vercas.ExplorerView.Item is used only inside Vercas.ExplorerView.

Categories