惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
T
Threatpost
阮一峰的网络日志
阮一峰的网络日志
美团技术团队
F
Fortinet All Blogs
The GitHub Blog
The GitHub Blog
月光博客
月光博客
V
Visual Studio Blog
T
Tailwind CSS Blog
Stack Overflow Blog
Stack Overflow Blog
博客园 - 聂微东
Jina AI
Jina AI
J
Java Code Geeks
Martin Fowler
Martin Fowler
大猫的无限游戏
大猫的无限游戏
Recorded Future
Recorded Future
C
Check Point Blog
腾讯CDC
N
Netflix TechBlog - Medium
aimingoo的专栏
aimingoo的专栏
罗磊的独立博客
Hacker News: Ask HN
Hacker News: Ask HN
SecWiki News
SecWiki News
博客园 - Franky
Hacker News - Newest:
Hacker News - Newest: "LLM"
N
News | PayPal Newsroom
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
S
Security @ Cisco Blogs
W
WeLiveSecurity
The Last Watchdog
The Last Watchdog
Cloudbric
Cloudbric
F
Full Disclosure
The Cloudflare Blog
Y
Y Combinator Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
S
Schneier on Security
Schneier on Security
Schneier on Security
Spread Privacy
Spread Privacy
L
LINUX DO - 热门话题
AI
AI
N
News and Events Feed by Topic
T
Tor Project blog
P
Palo Alto Networks Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
H
Hackread – Cybersecurity News, Data Breaches, AI and More
G
Google Developers Blog

博客园 - 张少峰

在linuxdeepin10.12下源码安装bochs-2.4.6 设置pppoe时遇到“Oh, dear, I don't see the file '/etc/ppp/pppoe.conf' anywhere.”时的解决方法 安装ubuntu10.10时遇到ubi partman crashed,ubi-partman failed with exit code 141的解决方案 糗事的教训:做事一定要细心细心再细心 Reflector插件FileDisassembler汉化修改版 解压用Sixxpack2.2压缩过的程序,拿原始程序集 - 张少峰 - 博客园 利用网页挂马拿服务器的一种设想 第一次上首页,发一个玩具级的持久化工具~ python一些细微的东西 IndentationError: unindent does not match any outer indentation level Request的几种路径属性 值类型与引用类型 用反射把程序集中一些泛型类实例化,怎么确定实例的类型? 父类实现IComparable<T>接口,子类无法使用~ [转载]谓词和操作(c#版) 静态变量的继承 XPath初学笔记(四) XPath初学笔记(三) XPath初学笔记(二)
一个方法返回多个值的解决方法
张少峰 · 2008-11-21 · via 博客园 - 张少峰

刚才看到园子里一个兄弟讨论一个方法返回多个值的问题。 在开发过程中,总会遇到这个问题。这里说一下其他的解决方法。当然,这些方法是前人总结出来的,我只是拿来主义而已。

一、如果返回值类型相同的话,可以返回一个数组

    public int[] ReturnMultiValue()
    
{
        
int[] retvalue = new int[2];
        
//进行一些操作,把多个值存入int数组
        return retvalue;
    }

    
//可以这样使用
    int[] values=ReturnMultiValue();
    
int a=values[0];
    
int b=values[1];

 二、如果返回的值类型不同,可以使用Hashtable:

 public Hashtable ReturnMultiValue()
    
{
        Hashtable retvalue 
= new Hashtable();
        
//进行一些操作,把多个值存入Hashtable
        
//retvalue["xxx"] = value1;
        
//retvalue["yyy"] = value2;
        return retvalue;
    }

    
//可以这样使用
    Hashtable values = ReturnMultiValue();
    Type1 type1 
= (Type1)(values["xxx"]);
    Type2 type2 
= (Type2)(values["yyy"]);

 三、我最喜欢的方法:把要返回的结果封装到一个结果类里面,然后在方法内部new一个结果类,赋值并返回。
    当然封装成结构也可以,看你怎么用了。

//结果类
public class Result
{
    
private int value1;

    
public int Value1
    
{
        
get return value1; }
        
set { value1 = value; }
    }

    
private string vlaue2;

    
public string Vlaue2
    
{
        
get return vlaue2; }
        
set { vlaue2 = value; }
    }

    
private User value3;//也可以是自定义类型

    
public User Value3
    
{
        
get return value3; }
        
set { value3 = value; }
    }

}


public class Test
{
    
public Result ReturnMultiValue()
    
{
        Result retvalue 
= new Result();
        
//进行一些操作,把多个值存入结果类
        
//retvalue.Value1 = value1;
        
//retvalue.Value2 = value2;
        
//retvalue.Value3 = value3;
        return retvalue;
    }

    Result values 
= ReturnMultiValue();
    
int value1 = values.Value1;
    
string value2 = values.Value2;
    User value3 
= values.Value3;
    
}

(注:文章里的代码只是为了说明思路,随手写的,并不能真正运行。)