English 中文(简体)
如何从1 c#方法中恢复2项价值? [复制]
原标题:how to return 2 values from 1 c# methods? [duplicate]
  • 时间:2011-11-23 14:54:17
  •  标签:
  • c#
This question already has answers here:
Closed 11 years ago.

Possible Duplicate:
How can I return multiple values from a function in C#?

C#方法始终如一:

public <return type> funName()
{
//do sth
return someValueformatedasReturnType;
}

how to return 2 values from 1 c# methods? This is a interview question, as many as you know. Thanks!

最佳回答

从最好到最坏(IMHO):

问题回答

你有几种选择:

  1. output parameters
  2. Tuple
  3. Class that has 2 properties

您可使用您职能参数的重字或关键词。

int i;
int j;

dosomething(out i, out j);    

public void dosomething(out int a, out int b)
{
   a = 1;
   b = 2;
}

在称职后,即1,j = 2. 换关键词时,斜线表示:

或交还遗体。

@tudor

查阅你的名字,用标语编号:S.,用字标出。

界定新的数据类型,其产出如下:

public class Output
{
    public object Val1 { get; set; }
    public object Val2 { get; set; }
}

并返还新的类别产出:

public Output FunName()
{
     Output out = new Output;
     out.Val1 = val1;
     out.Val2 = val2;
     return out;
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签