Multiple Return Method

I looked through many questions similar to this one, but none of them dealt with what I definitely want to do. What I'm trying to do is read from an external source a list of variables that also include their data type in a string array:

Example:

ID/Key Type Value/Data; varName1 bool true; varName2 string str; varName3 int 5; 

Then I save these objects in the dictionary as objects containing several lines, and the key also serves as an identifier.

Now I want to create a method that uses a switch statement that converts a string to the correct data type and returns it without specifying anything in the method call. The function should look something like this:

 public ??? Method(string key) { if(dictionary.ContainsKey(ID)) { Var temp = dictionary[ID]; switch (temp.Type) { case "bool": return Convert.ToBoolean(temp.Value); case "int" return Convert.ToInt(temp.Value); case "string" return temp.Value; } } return "NULL"; } 

The method call should look something like this:

 int x = Method(string key); string word = Method(string key); bool isTrue = Method(string key); 

Maybe I missed something, but I still have to find something that really does something like this. Any thoughts on this are also welcome.

+9
source share
3 answers

The compiler does not have the ability to distinguish between the three method calls that you provided, because they all look like Method(key);

One option is to return object , and then expect the consumption code to lead to what it wants:

 public object Method(string key) { if(dictionary.ContainsKey(key)) { var temp = dictionary[key]; switch (temp.Type) { case "bool": return Convert.ToBoolean(temp.Value); case "int" return Convert.ToInt(temp.Value); case "string" return temp.Value; } } return "NULL"; } ... int x = (int) Method(key); string word = (string) Method(key); bool isTrue = (bool) Method(key); 

You can also use the dynamic keyword to do an implicit action:

 public dynamic Method(string key) { if(dictionary.ContainsKey(key)) { var temp = dictionary[key]; switch (temp.Type) { case "bool": return Convert.ToBoolean(temp.Value); case "int" return Convert.ToInt(temp.Value); case "string" return temp.Value; } } return "NULL"; } ... int x = Method(key); string word = Method(key); bool isTrue = Method(key); 

However, dynamic is a very powerful concept, and it is easy for him to get out of hand, so you need to be very careful with this.

It seems to me that you expect your call code to know what type of object it expects to receive for each key. It seems like the best approach is to simply provide the user with this information:

 public T Method<T>(string key) { if(dictionary.ContainsKey(key)) return (T) Convert.ChangeType(dictionary[key].Value, typeof(T)); return default(T); } ... int x = Method<int>(key); string word = Method<string>(key); bool isTrue = Method<bool>(key); 

Thus, there is no need to track the type value in your dictionary objects in the first place.

+10
source

In C # 7, you have the option of returning multiple values โ€‹โ€‹from a method like this:

 public (string SomeString, int SomeInt) DoSomething() {... } 

You can get these values:

 var result = DoSomething(); Console.WriteLine(result.SomeString); Console.WriteLine(result.SomeInt.ToString()); 

Or

 (var someString, var someInt) = DoSomething(); Console.WriteLine(someString); Console.WriteLine(someInt.ToString()); 

This works under the surface with a tuple, and you are not limited to just two values. I do not know how much you can return, but I suggest, when you need to return so many values, create a class. More information: https://blogs.msdn.microsoft.com/dotnet/2016/08/24/whats-new-in-csharp-7-0/

+5
source

You must enter the type of the returned function. As with any other variable or operation, any type that inherits from the specified type is a valid return value (therefore, the object resolves anything as a value).

Personally, I donโ€™t think itโ€™s useful to make one method with several return types, but if you really want to have one method with several return types, you can use dynamic in .NET 4.0:

  private static void Main(string[] args) { int x = Method("varName3"); string word = Method("varName2"); bool isTrue = Method("varName1"); } private static dynamic Method(string key) { var dictionary = new Dictionary<string, KeyValuePair<Type, object>>() { { "varName1", new KeyValuePair<Type, object>(typeof(bool), false) }, { "varName2", new KeyValuePair<Type, object>(typeof(string), "str") }, { "varName3", new KeyValuePair<Type, object>(typeof(int), 5) }, }; if (dictionary.ContainsKey(key)) { return dictionary[key].Value; } return null; } 

Hope this helps

0
source

All Articles