2

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);
2
  • 1
    Very unclear what you are looking for... Commented Nov 5, 2015 at 23:52
  • Are you asking if member names can be the same as their enclosing types? If so, then no. Your 1st block of sample code would compile a non-static constructor. Based on your desired usage, I'd take a look at using an extension method for Data instead -> int anInt = data.ToInt(); Commented Nov 6, 2015 at 1:08

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm trying to explore different possibilities right now; I'm well aware it's odd. Thanks for clearing this up for me. :)
1

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...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.