C# 从同一方法返回字符串和int

C# 从同一方法返回字符串和int,c#,C#,在上面的代码中,如何获取一个人的姓名(字符串)和薪水(整数)?规范是,我需要为信息检索调用该方法两次。如果创建一个包含信息和工资的类,则可以单独传递该方法,或者更好地传递该方法 public static int getInfo(string info) { string inputValue; int infor; Console.WriteLine("Information of the employee: {0}", info);

在上面的代码中,如何获取一个人的姓名(字符串)和薪水(整数)?规范是,我需要为信息检索调用该方法两次。

如果创建一个包含信息和工资的类,则可以单独传递该方法,或者更好地传递该方法

public static int getInfo(string info)
    {
        string inputValue;
        int infor;
        Console.WriteLine("Information of the employee: {0}", info);
        inputValue = Console.ReadLine();
        infor = int.Parse(inputValue);
        return infor;

    }
或者创建一个类

 public static int getInfo(string info,int salary)
方法签名如下:

 public class MyInfo
    {
        public int info { get; set; }
        public int salary { get; set; }

    }
如果希望同时返回字符串和int,最好将方法签名更改为class
MyInfo

 public static int getInfo(MyInfo info)

如果你创建了一个包含信息和薪水的类,你可以单独或更好地通过它

public static int getInfo(string info)
    {
        string inputValue;
        int infor;
        Console.WriteLine("Information of the employee: {0}", info);
        inputValue = Console.ReadLine();
        infor = int.Parse(inputValue);
        return infor;

    }
或者创建一个类

 public static int getInfo(string info,int salary)
方法签名如下:

 public class MyInfo
    {
        public int info { get; set; }
        public int salary { get; set; }

    }
如果希望同时返回字符串和int,最好将方法签名更改为class
MyInfo

 public static int getInfo(MyInfo info)

您可以让它返回一个包含两个值的元组,如下所示:

 public static MyInfo getInfo(MyInfo info)
内部元组GetBoth(字符串信息)
{
字符串输入值;
国际信息;
WriteLine(“员工信息:{0}”,info);
inputValue=Console.ReadLine();
infor=int.Parse(inputValue);
返回新元组(infor,inputValue);
}
内部无效方法imcallingitfrom()
{
var result=GetBoth(“某物”);
int theInt=result.Item1;
字符串字符串=result.Item2;
}

您可以让它返回一个包含两个值的元组,如下所示:

 public static MyInfo getInfo(MyInfo info)
内部元组GetBoth(字符串信息)
{
字符串输入值;
国际信息;
WriteLine(“员工信息:{0}”,info);
inputValue=Console.ReadLine();
infor=int.Parse(inputValue);
返回新元组(infor,inputValue);
}
内部无效方法imcallingitfrom()
{
var result=GetBoth(“某物”);
int theInt=result.Item1;
字符串字符串=result.Item2;
}

为什么不返回一个大小为2的字符串数组呢?第一个(数组[0])是字符串,第二个(数组[0])是整数

internal Tuple<int, string> GetBoth(string info)
{
    string inputValue;
    int infor;
    Console.WriteLine("Information of the employee: {0}", info);
    inputValue = Console.ReadLine();
    infor = int.Parse(inputValue);
    return new Tuple<int, string>( infor, inputValue );
}

internal void MethodImCallingItFrom()
{
    var result = GetBoth( "something" );
    int theInt = result.Item1;
    string theString = result.Item2;
}

我希望我正确理解了你的问题^^

为什么不返回一个大小为2的字符串数组呢?第一个(数组[0])是字符串,第二个(数组[0])是整数

internal Tuple<int, string> GetBoth(string info)
{
    string inputValue;
    int infor;
    Console.WriteLine("Information of the employee: {0}", info);
    inputValue = Console.ReadLine();
    infor = int.Parse(inputValue);
    return new Tuple<int, string>( infor, inputValue );
}

internal void MethodImCallingItFrom()
{
    var result = GetBoth( "something" );
    int theInt = result.Item1;
    string theString = result.Item2;
}

我希望我正确理解了你的问题^ ^

你需要创建一个person类并阅读姓名和薪水

public static string[] GetInfo (string info)
你的职能是:

public class Person
{
    public string Name {get; set;}
    public decimal Salary {get; set;}
}

您需要创建一个person类并读取name和salary

public static string[] GetInfo (string info)
你的职能是:

public class Person
{
    public string Name {get; set;}
    public decimal Salary {get; set;}
}

如果您希望一个方法返回不同类型的信息,那么我将使用泛型:

public static Person getInfo(string info)
{
    string inputName;
    string inputSalary;

    Console.WriteLine("Information of the employee: {0}", info);

    Console.WriteLine("Name:");
    inputName = Console.ReadLine();

    Console.WriteLine("Salary:");
    inputSalary = Console.ReadLine();

    Person person = new Person();
    person.Name = inputName;
    person.Salary = int.Parse(inputSalary);
    return person;

}
现在,您如何实现该方法

public static T GetInfo<T>(string name, Func<string, T> convert);

// This can be called as following:
string name = GetInfo<string>("name", s => s);
int salary = GetInfo<int>("salary", int.Parse);
public static T GetInfo(字符串名,Func convert)
{
Console.WriteLine(“请输入”+名称);
字符串输入=Console.ReadLine();
返回转换(输入);
}
请注意:

  • 类型参数通常只是命名为
    T
    ,但我发现给它们更多的描述性名称很有用,例如
    TInfo
  • 第二个示例中可以省略
    部分,因为编译器有足够的信息来推断它们的类型
  • 为了使示例简短,我将错误处理留给大家。不过,在生产代码中,您需要想出一种策略来处理无效输入

如果您想要一种方法返回不同类型的信息,那么我将使用泛型:

public static Person getInfo(string info)
{
    string inputName;
    string inputSalary;

    Console.WriteLine("Information of the employee: {0}", info);

    Console.WriteLine("Name:");
    inputName = Console.ReadLine();

    Console.WriteLine("Salary:");
    inputSalary = Console.ReadLine();

    Person person = new Person();
    person.Name = inputName;
    person.Salary = int.Parse(inputSalary);
    return person;

}
现在,您如何实现该方法

public static T GetInfo<T>(string name, Func<string, T> convert);

// This can be called as following:
string name = GetInfo<string>("name", s => s);
int salary = GetInfo<int>("salary", int.Parse);
public static T GetInfo(字符串名,Func convert)
{
Console.WriteLine(“请输入”+名称);
字符串输入=Console.ReadLine();
返回转换(输入);
}
请注意:

  • 类型参数通常只是命名为
    T
    ,但我发现给它们更多的描述性名称很有用,例如
    TInfo
  • 第二个示例中可以省略
    部分,因为编译器有足够的信息来推断它们的类型
  • 为了使示例简短,我将错误处理留给大家。不过,在生产代码中,您需要想出一种策略来处理无效输入
}

这是需要的。你们提到的其他把戏本可以搞定的。无论如何,谢谢你

}



这是需要的。你们提到的其他把戏本可以搞定的。无论如何,谢谢你。

那么你想让你的方法返回字符串或int?使用C#7+元组:。这就是XY问题(如果你不知道这意味着什么,用谷歌搜索它;我在手机上没有现成的链接)。你们想用这个做什么?谢谢你们。我用另一种方法将字符串显式转换为main方法。因此,您希望您的方法返回字符串或int?请使用C#7+元组:。这会导致XY问题(如果您不知道这意味着什么,请用谷歌搜索它;我在移动设备上手头没有链接)。你们想用这个做什么?谢谢你们。我用另一种方法将字符串显式转换为主方法。我想OP希望他的方法返回信息。@PieterWitvoet ok按原样编辑。在c#!:)中再获得2枚金牌@Sajeetharan 2更多金币!嫉妒的祝你好运,你会得到它的。在这种特殊情况下,Fast不能使用类,因为规范说我需要两次调用方法,并从一个方法中获取名称和薪水。@Prashandthandari什么规范?谁建议的?我想OP希望他的方法返回信息。@PieterWitvoet ok按原样编辑。在c#!:)中再获得2枚金牌@Sajeetharan 2更多金币!嫉妒的祝你好运,你会得到它的。在这种特殊情况下,Fast不能使用类,因为规范说我需要两次调用方法,并从一个方法中获取名称和薪水。@Prashandthandari什么规范?谁建议的?我本可以使用TryParse和if-else来完成这项工作,但情况是我需要调用该方法两次:(你也可以使用'stringstringname+Convert.ToString(int-intname);'t