I have a class for which some functions need to be encapsulated in some way.
I was thinking of a nested class and adding this functionality to it plus some states. The relationship between these two classes is one-to-one.
The problem is that to access external class variables or methods, they must be declared static, and I do not want this. Another solution passes the reference of the outer class to the inner class.
static
What is the best practice for my problem?
using System; class OuterType { private static OuterType _instance; public OuterType() { _instance = this; } private String Message { get { return "Hello from OuterType"; } } public void testInnerType() { InnerType innerType = new InnerType(); Console.WriteLine(innerType.FormattedOutertMessage); } private class InnerType { private readonly OuterType _outerType = _instance; public String FormattedOutertMessage { get { return _outerType.Message.ToUpper(); } } // InnerType doesn't need to dispose any object of OuterType. } }
Then it works as follows:
class Program { static void Main(string[] args) { OuterType outerType = new OuterType(); outerType.testInnerType(); Console.ReadKey(); } }
But I'm not sure if this is a good idea or not ?!
, , , , "", , , - .
, , , :
? - , : OuterType + InnerType
, ( ).
Visual Studio ( ), , FxCop, .
, .
6 , . 20 "" , , , .
, , 1 1 .
( ) , , . , GC , , . , , , .
, :
Parent | +-Child1 | +-Child2
Child, , .
ADO.Net , , .
Source: https://habr.com/ru/post/1783789/More articles:Need to remove new lines from object / embed tags only with Nokogiri - ruby ββ| fooobar.comError opening serial port in C # - c #Is it possible to store a sqlite database containing 333,000 rows locally on iPhone / iPod Touch? - sqliteJQuery UI datipicker parameters as variables - javascriptWhat JS objects can be added via appendChild ()? - javascriptHow can I write a python function to accept * arguments and a keyword argument? - pythonFind symbolic links in a directory - findedit the routing form - when updating submit starts the #show method instead of #update - ruby-on-railsThe wpf dragenter / dragleave problem - wpfThe field value is 1 or true in the search results solr - luceneAll Articles