How to return multiple data types from a C # method?

I have a method that should return different types of data based on a search. I am considering two approaches since I am new to C #. I do not know which is better, so please help me think.

The first approach is to overload the method as follows:

public int Get(string name){
   //...
   return intValue;
}

public double Get(string name){
   //...
   return doubleValue;
}

public string Get(string name){
   //...
   return stringValue;
}

the second approach is to have different methods for each data type, for example:

public int GetInt(string name){
   //...
   return intValue;
}

public double GetDouble(string name){
   //...
   return doubleValue;
}

public string GetString(string name){
   //...
   return stringValue;
}

which one is the most secure for C #, given that this code will be published from a DLL?

+4
source share
5 answers

The first way is not even possible. You cannot overload methods only by changing their return type.

, , :

public T Get<T>(string name){
   //...
   return somegenericvalue;
}

, , , , . , , (string, int, double)

. , , Get . . , , , , .. - .

, , "" .

, Framework , :

, , :

, , pass_through, , .

+9

Tuples ( ItemX, ). , person, . :

public static Tuple<Person, string, int> GetMyData()
{
    var person = GetPerson();
    var text = "Hello";
    var number = 2016;

    var result = Tuple.Create(person, text, number);
    return result;
}

:

var data = GetMyData();
data.Item1; // person
data.Item2; // text
data.Item3; // number

, , .

public Tuple<int, double, string> Search(searchPattern)
{
    if (serachPattern == "double")
    {
        double myDouble = SearchDouble();
        return Tuple.Create(0, myDouble, null);
    }

    // Handle other patterns here
}

:

var myDouble = Search("double").Item2;
var myString = Search("string").Item3;
var myInt = Search("int").Item1;
+2

, :

public T Get<T>(string name)
{
    ...
}
0

public T GetT<T>(string name)
{
    // Code goes here
    return T;
}

, , . int, int . . http://msdn.microsoft.com/en-us/library/ms379564%28v=vs.80%29.aspx

0

WPF: Windows Presentation Foundation | C Sharp: C & # 35;

📘 , ()


namespace CodeSample {    
    internal class ClassSample {

        //[?]| StartUp Object | Application start point;
        static void Main() {

           //[?]| return "One Hundred" |
           new (System.String)CodeSample.ClassSample.SampleMethod(2);

           //[?]| return "100.0" |
           new (System.Double)CodeSample.ClassSample.SampleMethod(4);

        }

        //[?]| Method | Used to perform operations;
        internal System.Object SampleMethod(Int MyParameter = 0) {
            switch(MyParameter) {
                case 1:
                    return 100;    
                case 2:
                    return "One Hundred";    
                case 3:
                    return true;    
                case 4:
                    return 100.0;

                default:
                    return null;
            }
        }

    }    
}

System.Object ;

: C Sharp | & # 35; ; 7. +

0

Source: https://habr.com/ru/post/1526964/


All Articles