C#多值返回功能。 [英] C# multiple value return function.

查看:65
本文介绍了C#多值返回功能。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我想创建如下的新功能:



Hallo all,
I want to create new function like below:

public static Bitmap TapisKotak(Bitmap Img,int Width,int Hight,int StartX,int StartY, int RowsY,int ColumnX)
{
    bitmap Img_process=new bitmap(Img);
    int New_Coordinat_X, New_Coordinat_Y;
    //process here and I get that I want... so i return the image modified (image object) and any value in int variable

    return(bitmap Img_process);   //==> this object image
    return(New_Coordinat_X);      //==> this int value 1
    return(New_Coordinat_Y);     //==> this int value 2
}





。如何在C#????

感谢您的帮助。



on this code I want to return multiple value including object and numeric value. how to do it on C#????
thanks for help.

推荐答案

脚本?你是认真的吗? C#不是脚本语言。



有多种方法可以从一个函数返回两个对象。您可以像往常一样返回一个,另一个返回 out 参数。您可以使用两个成员(对象和数字)创建某种类型(类或结构)并返回此类型的对象。或者,您可以使用已定义的具有两个成员的泛型类型。这种类型,特别是它元组

http://msdn.microsoft.com/en-us/library/system.tuple.aspx [ ^ ]。



- SA
Script? Are you serious? C# is not a scripting language.

There are a number of ways to return two object from one function. You can return one as usual return and another one as out parameter. You can create some type (class or structure) with two members (object and numeric) and return an object of this type. Alternatively, you can use an already defined generic type with two members. Such type, in particular, it Tuple:
http://msdn.microsoft.com/en-us/library/system.tuple.aspx[^].

—SA


在同一个程序集中你可以使用anon类型:





Inside the same assembly you can use anon types:


public static dynamic TapisKotak(Bitmap Img,int Width,int Hight,int StartX,int StartY, int RowsY,int ColumnX)
{
    bitmap Img_process=new bitmap(Img);
    int New_Coordinat_X, New_Coordinat_Y;
    //process here and I get that I want... so i return the image modified (image object) and any value in int variable

    return new {Image = Img_process, X = New_Coordinat_X, Y = New_Coordinat_Y};
}


试试这个男人

Try This man
public void CallFuntion()
{
  //your code
   object[] Return = TapisKotak(Img, width, Heigth, StartX, StartY, RowsY, ColumnX)
   bitmap img_processed = (bitmap)Return[0];
   int New_Coordinat_X = (int)Return[1];
   int New_Coordinat_Y = (int)Return[2];
   //Now you have all values returned!
}

public static object[] TapisKotak(Bitmap Img,int Width,int Hight,int StartX,int StartY, int RowsY,int ColumnX)
{
    bitmap Img_process=new bitmap(Img);
    int New_Coordinat_X, New_Coordinat_Y;
    //process here and I get that I want... so i return the image modified (image object) and any value in int variable

    return new object[] {Img_process, New_Coordinat_X, New_Coordinat_Y};
}


这篇关于C#多值返回功能。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆