0

I have the following class:

public class NoteLink
{

    public IList<NoteLinkDetail> NoteLinkDetails
    {
        get { return _noteLinkDetails; }
    }
    private List<NoteLinkDetail> _noteLinkDetails = new List<NoteLinkDetail>();

    public NoteLink()
    {
        _noteLinkDetails = new List<NoteLinkDetail>();
    }

}

and then another class that's used just within the first class.

public class NoteLinkDetail
{
    public string L { get; set; }
    public string R { get; set; }
}

Is there anything I could do to optimize this. I DO need the second class as I use JSON to store the contents of NoteLinkDetail in a string. However is there such a thing as an inner class in C#? Any recommendation on how I could optimize this code? Thanks.

Bianca
  • 21
  • 3
  • Yes, there are inner classes in C#. [See this answer on SO for more details.](http://stackoverflow.com/questions/804453/using-inner-classes-in-c) – Peter K. Jul 27 '11 at 17:02

4 Answers4

8

Yes, you can. Just nest it.

public class NoteLink
{
    // ...

    public NoteLink()
    {
        _noteLinkDetails = new List<NoteLinkDetail>();
    }

    public class NoteLinkDetail
    {
        public string L { get; set; }
        public string R { get; set; }
    }
}

Moreover, if it is not exposed outside the outer class, your inner class can even be private and stay invisible to outer class users.

NOtherDev
  • 9,542
  • 2
  • 36
  • 47
3

Yes, C# allows nested classes.

C# nested classes are much more like nested classes in C++ than "inner classes" in Java. This link explains:

http://blogs.msdn.com/b/oldnewthing/archive/2006/08/01/685248.aspx

paulsm4
  • 114,292
  • 17
  • 138
  • 190
1

Sure; just declare the inner class from within the outer class

class NoteLink
{
    class NoteLinkDetail
    {

    }
}
Ed S.
  • 122,712
  • 22
  • 185
  • 265
0

Yes, you can have nested classes in C#

james_bond
  • 6,778
  • 3
  • 28
  • 34