简体   繁体   中英

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() )

Thank you for reading my question, and possibly giving ans answer :)

i saw a class have the following method: anotherClass.something.methodName(arguments);

The method is not from the class anotherClass instead it is from the class of object something .

The class anotherClass has a field/property something which is of another class type, that class has the method 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 is an object, something is a property of anotherClass , methodName is a method

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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