我们可以为嵌套类创建构造函数吗?

时间:2010-08-24 13:30:19

标签: c#

In the below example i want to create folder in nested class constructor:

    namespace Nm
    {
        class Outer
        {
            private const string s1 = "@.\bin";

            void O1()
            {
                Nested nestedFile = new Nested();
                nestedFile.FileName = "Queue1";
            }
        }

        internal class Nested
        {
            public string FileName
            {
                set
                {
                    Directory.CreateDirectory(s1); // !
                    fileName = s1+ value + ".dat";
                    Console.writeLine(fileName);
                }
            }
        }
    }

2 个答案:

答案 0 :(得分:3)

绝对 - 与平时一样:

class Outer
{
    private void Foo()
    {
        Nested nested = new Nested("bar");
    }

    class Nested
    {
        internal Nested(string x)
        {
            ...
        }
    }
}

答案 1 :(得分:0)

至于你的班级是internal(不是外 - private),你可以做下一步:

class Outer
{
    internal const string s1 = "@.\bin";
}

internal class Nested
{
    public string FileName
    {
        set
        {
            Outer.s1;field
        }
    }
}

但是程序集中的任何类都可以访问此字段