user4673238
user4673238

Reputation:

C# Namespaces: Calling Methods and Classes

I've been messing around with Namespaces to improve the structure of my programs. I ran across a case where I wanted to use a public static method which returns a value without also calling its enclosing class. One method of doing this is by using an out parameter. So, let's say I have this code:

namespace Namespace1
{
     namespace Namespace2
     {
          public class ClassName
          {
               public ClassName(Data data, out int AnInt)
               {
                     AnInt = (int)data;
               }
          }
     }
}

I can use this as follows:

Int AnInt;
Namespace1.Namespace2.ClassName(data, out AnInt);

But, let's say I want to get rid of the out parameter. Is there any way to is there any way to do this without exposing another level of the hierarchy, adding a using directive, or adding a Func<> delegate? Say:

Int AnInt = Namespace1.Namespace2.ClassName(data);

Upvotes: 2

Views: 5259

Answers (2)

D Stanley
D Stanley

Reputation: 152654

One problem in your code is you have a "method" with the same name as the class. This is reserved for the constructor of the class, which does not have a "return" value, and thus must use out parameters to provide output (other than the instance that's created). While it's possible for a constructor to have an out parameter, it's very bizarre and can almost certainly be done in a more standard manner.

You certainly can create a static method that returns a value:

  public class ClassName
  {
       public static int CalcSomeInt(Data data)
       {
             return (int)data;
       }
  }

and then call it ike so:

int i = ClassName.CalcSomeInt(data);

or if you really want to use the fully qualified name:

int i = Namespace1.Namespace2.ClassName.CalcSomeInt(data);

If you're asking if a static method can be called without referencing the class, then the answer is No. However in C# 6 you can have a "static alias" that lets you reference static members without specifying the class:

using static Namespace1.Namespace2.ClassName.

...

int i;
i = CalcSomeInt(data);

But note that the compiler implicitly adds the class name to the method call, and if there are any ambiguities with other reachable methods then you must specify the class name to resolve the ambiguity.

Upvotes: 5

Gottfried Lesigang
Gottfried Lesigang

Reputation: 67341

You might do something like this:

namespace Namespace1 {
    namespace Namespace2 {
        public class ClassName {
            public string SomeMethod(int data, out int AnInt) {
                AnInt = data;
                return "hello world";
            }
            public string SomeMethod(int data) {
                int dummy;
                return SomeMethod(data, out dummy);
            }
        }
    }
}

Your calls could look like:

string s1 = new Namespace1.Namespace2.ClassName().SomeMethod(3);
int x;
string s2 = new Namespace1.Namespace2.ClassName().SomeMethod(3, out x);

In both cases the strings hold the value "hello world". After the second call your "x" is set to "3".

But - to be honest - the whole concept smells a bit...

Upvotes: 1

Related Questions