what is the reason for using a class inside another class c#?

why would we need to use a class inside another? is this called  a wrapper class? why would this be required or is there a design concern for this?
nicedoneAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ankit PareekOnline MarketingCommented:
nested class are sometime useful and its help lots. you can see  sample example here
http://www.dotnetperls.com/nested-class
0
ste5anSenior DeveloperCommented:
Please post a concise and complete example.

There are two scenarios I can imagine, which match your question.

1) Nested classes
    class A {
        class B { }

        B instanceOfB = new B();
    }

Open in new window

This defines a scope. Thus no one outside of class A can use class B.

2) Class usage
   class C {  }
    class D {
        C instanceOfC = new C();
    }

Open in new window

This is the normal use case for classes.
0
nicedoneAuthor Commented:
I am talking about the nested ones
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

nicedoneAuthor Commented:
Actually what I would like to understand here relates to;

If you I use
 
  class A {}
   class B{}

instead of ;

  class A {
        class B { }
    }
what would differ?
0
ste5anSenior DeveloperCommented:
You can do

class B { }
class A { B instanceOfB = new B(); }
class C { B instanceOfB = new B(); }

Open in new window


but not

class A { 
  class B { }
  B instanceOfB = new B(); 
}

class C { B instanceOfB = new B(); } // compiler error.

Open in new window

0
nicedoneAuthor Commented:
Below is a POST equest that is triggered in a site, but i need to define the design for this request , should i include "CategoryPairs" inside "Sports" class, i mean should i make them nested here or how should i think about the c# class design here?

{
  "SourceType": "Matrix",
  "UserId": "userdefault",
  "DefinitionId": "id-356",
  "Variables": {
    "#EndOfTime#": "04\/11\/2016 10:00",
    "#StartOfTime#": "04\/11\/2015 10:53",
    "#StartOfTime#": "04\/11\/2015 10:00",
    "$Home$": "aaa",
    "$HomeL$": "aaa",
  },
  "SportsMile": [
    {
      "SportId": "RA",
      "IsPrimary": true,
      "CategoryPairs": [
        {
          "CategoryId": "DS",
          "CategoryName": "Premier Channel",
          "FixtureId": "",
          "FixtureName": ""
        }
      ]
    }
  ],
  "SuperSportId": "BC"
}

Open in new window

0
it_saigeDeveloperCommented:
In the instance you have posted this is more of a relationship and not the definition of a class inside of another one.  In other words, CategoryPairs represents a list of Categories, not a defined class.

Usually the purpose behind nesting classes is the ability to restrict scope.  This way only the parent class *can* implement and use the nested class.

Understand, though, that the *can* stipulation is more recommended than a hard rule. You code will compile and work as expected if you mark a nested class as public, but if you are doing this then it would make more sense to have the nested class as a standalone class.

A perfect example of the usage of a nested class/structure is in a linked-list implementation where your node may be defined internally to the class as the caller would not be responsible for creating, deleting and/or maintaining the nodes.  Therefore the caller does not need to know about the node structure.  The caller would only need to say to the linked-list instance, "here is an item add it." or "do you have this item?", etc. etc.

-saige-
0
it_saigeDeveloperCommented:
In case my previous response did not really help to resolve the issue you are having.  In cases where you have questions about the implementation details for json objects, you can always use an online generator, e.g. - http://json2csharp.com, in order to build a data model for you.

In your case, the posted json produces the following data model:
public class Variables
{
    public string __invalid_name__#EndOfTime# { get; set; }
    public string __invalid_name__#StartOfTime# { get; set; }
    public string __invalid_name__$Home$ { get; set; }
    public string __invalid_name__$HomeL$ { get; set; }
}

public class CategoryPair
{
    public string CategoryId { get; set; }
    public string CategoryName { get; set; }
    public string FixtureId { get; set; }
    public string FixtureName { get; set; }
}

public class SportsMile
{
    public string SportId { get; set; }
    public bool IsPrimary { get; set; }
    public List<CategoryPair> CategoryPairs { get; set; }
}

public class RootObject
{
    public string SourceType { get; set; }
    public string UserId { get; set; }
    public string DefinitionId { get; set; }
    public Variables Variables { get; set; }
    public List<SportsMile> SportsMile { get; set; }
    public string SuperSportId { get; set; }
}

Open in new window

-saige-
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ste5anSenior DeveloperCommented:
Please clarify what "but i need to define the design for this request" means.
0
John Gates, CISSPSecurity ProfessionalCommented:
Nested classes allow you to easily reuse code.  It's all a matter of your style on if you utilize them or not.  Like with anything there are 30 ways to accomplish things :-)

-D-
0
nicedoneAuthor Commented:
Thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.