A class declared inside another class in C #

I am working on some outdated code and come across something that I'm not sure about. We have class y that is declared inside another class x . class y used only inside class x , but my question is, why don't you create a separate class file and put class y instead of declaring it inside class x ? Does this not violate OOP, or is it just a matter of style, since it is used only inside this class. I am refactoring some of this code, and my first reaction would be to separate class y from it in my own file.

 namespace Library { public class x { // methods, properties, local members of class x class y { // methods, properties, local members of class y } } } 
+45
c # oop class
Mar 26 '09 at 12:48
source share
7 answers

You create an inner class because it is used only within the class x and it is logically suitable for factoring / architecture of the class x.

The class y can also be devoted to details of the implementation of class x, which should not be publicly known.

+58
Mar 26 '09 at 12:51
source share

This has implications for permissions. The top level “class y” will be “internal”, however here “y” is private to “x”. This approach is useful for implementation details (e.g. cache lines, etc.). Similarly, y has access to all particular states of x .

There are also consequences with generics; x<T>.y is a common "from T" inherited from the outer class. You can see this here, where Bar has full use of T - and note that any static Bar fields are limited to T

 class Foo<T> { void Test(T value) { Bar bar = new Bar(); bar.Value = value; } class Bar { public T Value { get; set; } } } 

Often people incorrectly think that they need to define Bar as Bar<T> - this is now (effectively) doubly common - i.e. Foo<TOld, T> - where TOld is (now unavailable) T from Foo<T> , so don't do this! Or, if you want it to be twice generic, select different names. Fortunately, the compiler warns you about this ...

+19
Mar 26 '09 at 12:52
source share

This code is great for the reason that you indicated - "class y is used only inside class x". These are nested types , and one of the recommendations for their use is that nested types should be closely related to their declaration type and should not be used as a general purpose type. Thus, a nested class is not acceptable for other classes, but still allows you to follow object-oriented principles.

+7
Mar 26 '09 at 12:55
source share

I think this is normal if the contained class is only used as a utility. I use this construct, for example, to define complex return types for private methods.

+2
Mar 26 '09 at 12:53
source share

I just looked at the code that I am updating (and I originally wrote) and deleted all the nested classes. Unfortunately, I originally used a nested class outside the class in which it was defined. Moving nested classes made a huge difference to me because I originally had a bad design.

If Y is used only in X and will never be used outside of X, I would say keep it there

+2
Mar 26 '09 at 12:54
source share

Let me give you an example of using nested classes that can clarify when such an architecture is suitable. Recently, I needed to create an HTML table by pulling selected columns from the data table and "expanding" them so that the rows become columns and vice versa. In my case, there were two main operations: rotating the data and creating some rather complex output (I didn’t just show the data: each row of the column / data table obeyed the operations for extracting the header, generating image tags, setting links, etc., therefore, using SQL Pivot was not quite right).

After the initial attempt to create one class to do all this, I realized that most of the data / methods fell into three different sections: heading processing, string processing, and rotation. Thus, I decided that the best approach would be to encapsulate the logic for the "header" and "string" in separate nested classes. This allowed me to separate the data stored in each row and program the rotation operations very cleanly (calling a separate row object for each column in your data table). Upon completion of the rotation operations, I generated the output by calling the header object, and then each row object, in turn, to generate my output back to the main class.

Separate classes were not suitable because A) the nested classes needed some data from the master class, and B) the processing was very specific and was not useful elsewhere. Simply programming one large class was simply messy due to confusion around terms such as “column” and “row”, which differed depending on whether you were talking about data or HTML output. Also, it was an unusual job in that I created HTML in my business class, so I wanted to share pure business logic with a generation of user interface. In the end, nested classes provided the perfect balance, and then encapsulated and exchanged data.

+2
Mar 26 '09 at 13:24
source share

You can still reorganize your y class into another file, but use the parial class. The advantage of this is that you still have one class for each file and it has no problem refactoring the movement of the declaration outside of class x.

eg. you may have a code file: xycs, which will look something like

 partial class X { class Y { //implementation goes here } } 
+1
Mar 26 '09 at 13:51
source share



All Articles