简体   繁体   English

类中的C#名称空间

[英]C# namespace in a class

I have basic knowledge of classes and methods. 我对类和方法有基本的了解。 I can make classes, and define methods for them: 我可以创建类,并为其定义方法:

myClass.awesome("test"); (example)

But I saw a class have the following method: 但是我看到一个类具有以下方法:

anotherClass.something.methodName(arguments);

How do I create a method that has additional namespace(s). 如何创建具有其他名称空间的方法。 I tried: 我试过了:

public Class test
{
    namespace subname
    {
        public void Test()
        {
            return;
        }
    }

    //also i tried:
    public void lol.Test()
    {
        return;
    }
}

But they both say that its not to be done like that, how do it correctly, so I can order/group my methods better? 但是他们俩都说不要那样做,如何正确执行,这样我才能更好地对方法进行排序/分组?

Please don't ask why, or give a alternative, I just want to make a class that has this kind of methods( Class.sub.Method() or Class.sub.sub....sub.Method() ) 请不要问为什么,或者给我一个替代选择,我只想制作一个具有这种方法的类( Class.sub.Method()Class.sub.sub....sub.Method()

Thank you for reading my question, and possibly giving ans answer :) 感谢您阅读我的问题,并可能给出答案:)

i saw a class have the following method: anotherClass.something.methodName(arguments); 我看到一个类具有以下方法:anotherClass.something.methodName(arguments);

The method is not from the class anotherClass instead it is from the class of object something . 该方法不是来自anotherClass类,而是来自something对象类。

The class anotherClass has a field/property something which is of another class type, that class has the method methodName anotherClass具有字段/属性something这是另一个类类型,即类有该方法methodName

What you think you have seen is not correct actually. 您认为您看到的内容实际上是不正确的。 It can be any of the followings: 可以是以下任意一项:

  • anotherClass is namespace, something is class, methodName is static method anotherClass是名称空间, something是类, methodName是静态方法
  • anotherClass is an object, something is a property of anotherClass , methodName is a method anotherClass是一个对象, somethinganotherClass的属性, methodName是一个方法

If you want to group your methods you should consider using static classes like this: 如果要对方法进行分组,应考虑使用如下静态类:

public class Test
{
    public static class InnerGroup
    {
        public static void Method1() { }
    }
    public static class AnotherInnerGroup
    {
        public static void Method2() { }
    }
}

or class properties like this: 或此类的属性:

public class Test
{
    public class InnerGroup
    {
        public static void Method1() { }
    }

    public class AnotherInnerGroup
    {
        public static void Method2() { }
    }

    public InnerGroup InnerGroup { get; set; }

    public AnotherInnerGroup AnotherInnerGroup { get; set; }

    public Test()
    {
        InnerGroup = new InnerGroup();
        AnotherInnerGroup= new AnotherInnerGroup();
    }
}

Hope you understood. 希望你能理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM