Is any concept of nested class in C#? Please help... [Resolved]

Posted by Kumarkrishna184 under C# on 11/27/2015 | Points: 10 | Views : 451 | Status : [Member] | Replies : 3
Is any concept of nested class in C#? if yes then please clear....

Thanks and Regards,
Krishna

Thanks and Regards,
Krishna Kumar



Responses

Posted by: Rajnilari2015 on: 11/27/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
Yes , the concept of nested class is present in C#.

Nested classes are those classes that are contained in another class declarations.

Consider the below example

using System;


namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
OuterClass objOuterClass = new OuterClass();
objOuterClass.SayHelloFromOuterClass();

var innerClass = new OuterClass.InnerClass();
innerClass.SayHelloFromInnerClass();
}
}

class OuterClass
{
public void SayHelloFromOuterClass()
{
Console.WriteLine("Hello from Outer Class");
}

public class InnerClass
{
public void SayHelloFromInnerClass()
{
Console.WriteLine("Hello from Inner Class");
}
}
}
}


In this case, the inner class can never be directly called from the invocation class. So if we write as

static void Main(string[] args)

{
var innerClass = new InnerClass();

}


the compiler will report

Error CS0103 The name 'InnerClass' does not exist in the current context

Which indicates highlights the concepts of encapsulation.

Let us look into another example.

A pattern quite frequently use is to have private nested classes that extend their outer class e.g.

abstract public class BankAccount

{
private BankAccount() { }
// Now no one else can extend BankAccount because a derived class
// must be able to call a constructor, but all the constructors are
// private!
private sealed class ChequingAccount : BankAccount { ... }
public static BankAccount MakeChequingAccount() { return new ChequingAccount(); }
private sealed class SavingsAccount : BankAccount { ... }
}


Nested classes work very well with the factory pattern. Here BankAccount is a factory for various types of bank account, all of which can use the private implementation details of BankAccount. But no third party can make their own type that extends BankAccount.

From th above examples and explanations we can infer that we can use a nested class when the class we are nesting is only useful to the enclosing class.

Benefits

- It brings the Class Clusters pattern (https://developer.apple.com/library/ios/documentation/General/Conceptual/CocoaEncyclopedia/ClassClusters/ClassClusters.html ).

- It is a way of logically grouping classes that are only used in one place.This concept in in C# for a preety long
time by the name partial class ( https://msdn.microsoft.com/en-IN/library/wa80x488.aspx )

- It brings encapsulation.

- It is in full aggrement with Single Responsibility Principal (SRP) ( https://en.wikipedia.org/wiki/Single_responsibility_principle )

- More readable and maintainable code.

Hope this helps

--
Thanks & Regards,
RNA Team

Kumarkrishna184, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kumarkrishna184 on: 11/28/2015 [Member] Starter | Points: 25

Up
0
Down
Thanks Rajnilari

Thanks and Regards,
Krishna Kumar

Kumarkrishna184, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 11/28/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
Glad that it has helped you (:

--
Thanks & Regards,
RNA Team

Kumarkrishna184, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response