C# Return Different Types?

后端 未结 15 2043
无人及你
无人及你 2020-12-08 09:11

I got something like this:

public [What Here?] GetAnything()
{
     Hello hello = new Hello();
     Computer computer = new Computer();
     Radio radio = ne         


        
相关标签:
15条回答
  • 2020-12-08 09:44

    May be you need "dynamic" type?

    public dynamic GetAnything()
    {
         Hello hello = new Hello();
         Computer computer = new Computer();
         Radio radio = new Radio();
    
         return /*what boject you needed*/ ;`enter code here`   
    }
    
    0 讨论(0)
  • 2020-12-08 09:45

    You could just return an Object as all types are descended from Object.

    public Object GetAnything()
    {
         Hello hello = new Hello();
         Computer computer = new Computer();
         Radio radio = new Radio();
    
         return radio; or return computer; or return hello //should be possible?!      
    }
    

    You could then cast to its relevant type:

    Hello hello = (Hello)GetAnything();
    

    If you didn't know what the type was going to be then you could use the is keyword.

    Object obj = GetAnything();
    if (obj is Hello) {
        // Do something
    }
    

    This being said I would be reluctant to write code like that. It would be much better to have an interface which is implemented by each of your classes.

    public ISpeak GetAnything()
    {
         Hello hello = new Hello();
         Computer computer = new Computer();
         Radio radio = new Radio();
    
         return radio; or return computer; or return hello //should be possible?!      
    }
    
    interface ISpeak 
    {
       void Speak();
    }
    

    and have each of your classes implement the interface:

    public class Hello : ISpeak
    {
        void Speak() {
            Console.WriteLine("Hello");
        }
    }
    

    You could then do something like:

    GetAnything().Speak();
    
    0 讨论(0)
  • 2020-12-08 09:47

    Here is how you might do it with generics:

    public T GetAnything<T>()
    {
       T t = //Code to create instance
    
       return t;
    }
    

    But you would have to know what type you wanted returned at design time. And that would mean that you could just call a different method for each creation...

    0 讨论(0)
  • 2020-12-08 09:49

    You could use external class, set the properties types as you wish, then use it in your function.

    public class MultipleOpjects
    {
        private List<string> _ObjectOne;
        public List<string> ObjectOne {
            get { return _ObjectOne; }
            set { _ObjectOne = value; }
        }
        private List<object> _ObjectTwo;
        public List<object> ObjectTwo {
            get { return _ObjectTwo; }
            set { _ObjectTwo = value; }
        }
        private object _ObjectThree;
        public object ObjectThree {
            get { return _ObjectThree; }
            set { _ObjectThree = value; }
        }
    }
    public MultipleOpjects GetAnything()
    {
        MultipleOpjects Vrble = new MultipleOpjects();
        Vrble.ObjectOne  = SomeThing1;
        Vrble.ObjectTwo = SomeThing2;
        Vrble.ObjectThree = SomeThing3;
    
        return Vrble;      
    }
    
    0 讨论(0)
  • 2020-12-08 09:51

    Marc's answer should be the correct one, but in .NET 4 you couldn't also go with dynamic type.

    This should be used only if you have no control over the classes you return and there are no common ancestors ( usually with interop ) and only if not using dynamic is a lot more painful then using(casting every object in every step :) ).

    Few blog post trying to explain when to use dynamic: http://blogs.msdn.com/b/csharpfaq/archive/tags/dynamic/

    public dynamic GetSomething()
    {
         Hello hello = new Hello();
         Computer computer = new Computer();
         Radio radio = new Radio(); 
         return // anyobject
    
    }
    
    0 讨论(0)
  • 2020-12-08 09:52

    You have a few options depending on why you want to return different types.

    a) You can just return an object, and the caller can cast it (possibly after type checks) to what they want. This means of course, that you lose a lot of the advantages of static typing.

    b) If the types returned all have a 'requirement' in common, you might be able to use generics with constriants.

    c) Create a common interface between all of the possible return types and then return the interface.

    d) Switch to F# and use pattern matching and discriminated unions. (Sorry, slightly tongue in check there!)

    0 讨论(0)
提交回复
热议问题