如何从一个方法返回多个变量?

时间:2015-07-09 11:47:40

标签: c# .net winforms

此方法仅返回进程文件名:

public static string GetProcessInfo(IntPtr hwnd)
{
    uint pid = 0;
    GetWindowThreadProcessId(hwnd, out pid);
    Process proc = Process.GetProcessById((int)pid);
    return proc.MainModule.FileName.ToString();
}

但我想还要返回进程名称:

proc.ProcessName;

5 个答案:

答案 0 :(得分:8)

我相信你有四个选择(按优先顺序)

  • 直接返回proc.MainModule并从来电者处提取必要的信息。
public static ProcessModule GetProcessInfo(IntPtr hwnd)
{
    uint pid = 0;
    GetWindowThreadProcessId(hwnd, out pid);
    Process proc = Process.GetProcessById((int)pid);
    return proc.MainModule;
}
  • 创建一个包含两个值的类并返回
public class ProcessInformation
{
    public string FileName;
    public string ProcessName;
}

public static ProcessInformation GetProcessInfo(IntPtr hwnd)
{
    uint pid = 0;
    GetWindowThreadProcessId(hwnd, out pid);
    Process proc = Process.GetProcessById((int)pid);
    var pi = new ProcessInformation 
    {  
        proc.MainModule.FileName,
        proc.MainModule.ProcessName
    }
    return pi;
}
  • 从方法Tuple<string, string>
  • 返回一个元组
public static Tuple<string, string> GetProcessInfo(IntPtr hwnd)
{
    uint pid = 0;
    GetWindowThreadProcessId(hwnd, out pid);
    Process proc = Process.GetProcessById((int)pid);
    return return Tuple.Create(proc.MainModule.FileName,proc.MainModule.ProcessName);
}
  • 在您的方法上创建2个out参数(我从未见过实施过两个参数,我不鼓励这个,因为它确实有气味,但它是C#提供的选项)
string GetProcessInfo(IntPtr hwnd, out fileName, out processName)

答案 1 :(得分:5)

您可以创建并返回描述结果的对象:

public class ProcessInfo
{
    public string ProcessName { get; set; }
    public string FileName { get; set; }
}

public ProcessInfo GetProcessInfo(IntPtr hwnd)
{
    uint pid = 0;
    GetWindowThreadProcessId(hwnd, out pid);
    Process proc = Process.GetProcessById((int)pid);

    return new ProcessInfo 
    {
        FileName = proc.MainModule.FileName.ToString(),
        ProcessName = proc.ProcessName
    }
 }

或者(我个人更喜欢这个),Tuple<string, string>

public Tuple<string, string> GetProcessInfo(IntPtr hwnd)
{
    uint pid = 0;
    GetWindowThreadProcessId(hwnd, out pid);
    Process proc = Process.GetProcessById((int)pid);

    return Tuple.Create(proc.MainModule.FileName.ToString(),
                        proc.ProcessName);
}

答案 2 :(得分:3)

由于两者都是string,如何返回Tuple<string, string>呢?

public static Tuple<string, string> GetProcessInfo(IntPtr hwnd)
{
    uint pid = 0;
    GetWindowThreadProcessId(hwnd, out pid);
    Process proc = Process.GetProcessById((int)pid);
    Tuple<string, string> t = new Tuple<string, string>
    (
         proc.MainModule.FileName,
         proc.ProcessName
    );
    return t;
}

答案 3 :(得分:2)

最后一个选项是使用out-params:

public voidstring GetProcessInfo(IntPtr hwnd, out string fileName, out string processName{
    uint pid = 0;
    GetWindowThreadProcessId(hwnd, out pid);
    Process proc = Process.GetProcessById((int)pid);
    fileName = proc.MainModule.FileName.ToString();
    processName = proc.ProcessName;
}

答案 4 :(得分:2)

您可以将返回类型更改为Process

    public static Process GetProcessInfo(IntPtr hwnd)
    {
        uint pid = 0;
        GetWindowThreadProcessId(hwnd, out pid);
        return Process.GetProcessById((int)pid);
     }

然后从返回的对象中获取所需的数据:

var proc = GetProcessInfo(hwnd);
var processName = proc.ProcessName;
var moduleFileName = proc.MainModule.FileName.ToString();