我有一个方法需要根据搜索返回不同的数据类型。我正在考虑两种方法,因为我是C#的新手,我不知道哪种方法最好,所以请帮我解释一下。
第一种方法是重载这样的方法:
public int Get(string name){
//...
return intValue;
}
public double Get(string name){
//...
return doubleValue;
}
public string Get(string name){
//...
return stringValue;
}
第二种方法是为每种数据类型设置不同的方法:
public int GetInt(string name){
//...
return intValue;
}
public double GetDouble(string name){
//...
return doubleValue;
}
public string GetString(string name){
//...
return stringValue;
}
考虑到这个代码将从DLL发布,哪一个是C#最安全的?
答案 0 :(得分:9)
第一种方法甚至不是可能。只能通过更改返回类型来重载方法。
根据代码的作用,您可以将其设为通用:
public T Get<T>(string name){
//...
return somegenericvalue;
}
但我怀疑你无论如何都要使用反射决定做什么,所以泛型的优势将被否定。另外,您不能将其仅限于您提到的类型(string
,int
,double
)
按类型使用不同方法的主要好处是强类型。对于消费者而言,受支持类型的列表是有限的,因此他们不必担心Get
方法是否支持特定类型。对于编写者,您可以为该特定类型编写代码。所以你不需要反射,类型检查,转换,解析等是通用的 - 你可以只为那种类型编写代码。
它可能感觉像冗余代码,但您仍然可以重构将常用部分放入内部“帮助程序”方法。
仅供参考框架在某些地方这样做,所以这并非闻所未闻:
另一方面,有一些扩展方法可以选择使用泛型方法:
但这些通常只是pass_through方法,他们不会做任何需要特定类型代码的转换。
答案 1 :(得分:2)
你可以使用Tuples(我不喜欢ItemX的东西,但仍然是一个选项)。 假设您需要一种方法来返回人物对象,字符串和数字。然后,您可以简单地编写以下代码:
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 :(得分:1)
要将多个数据类型返回给C#方法,您还可以尝试一种将方法返回类型复合到方法中并分别进行检索的方法。
观察
public ( int, double, string) Get(string name) {
//...
return (intValue, doubleValue, stringValue);
}
// Once returned to the main method the code is disassembled into smaller code //variables
public static void Main(string args[]) {
(int Ivals, double Dvals, string Svals) = Get(name);
Console.WriteLine(Ivals);
Console.WriteLine(Dvals);
Console.WriteLine(Svals);
}
答案 3 :(得分:0)
您可以将该方法设为通用,这是唯一的方法:
public T Get<T>(string name)
{
...
}
答案 4 :(得分: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
答案 5 :(得分:0)
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 | C#版本;版本7。+
编写的代码