MooshBeef
MooshBeef

Reputation: 379

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

Upvotes: 0

Views: 3467

Answers (3)

Caelan
Caelan

Reputation: 1040

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.

Upvotes: 0

Mohayemin
Mohayemin

Reputation: 3870

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

Upvotes: 0

Habib
Habib

Reputation: 223187

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

Upvotes: 6

Related Questions