简体   繁体   English

如何在同一函数中传递不同数量的参数?

[英]How to pass different numbers of parameters in a same function?

I got similar questions of this, but not get satisfied answer. 我对此也有类似的问题,但没有得到满意的答案。

I have a function 我有一个功能

func(int a,int b)
{
//code....
}

This function accepts two parameters (a and b). 该函数接受两个参数(a和b)。 I want to pass different number of parameters in this same function. 我想在同一函数中传递不同数量的参数。 I know there is a concept of overloading but I don't know how many numbers of parameters I'll pass. 我知道有一个重载的概念,但是我不知道要传递多少个参数。 I am working in C#(asp.net). 我在C#(asp.net)中工作。

You could use varargs ( params in c#) ... 您可以使用varargs (c#中的params )...

Example in C# : C#中的示例:

public void func(params int[] numbers)
        { 
            for(int i = 0; i < numbers.Length; i++) 
            { 
                Console.WriteLine(numbers[i]);
            } 
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将函数传递给具有不同参数的函数并执行它c# - how to pass a function to a function with different parameters and execute it c# 如何将不同的参数传递给实现相同接口的不同http请求类 - How to pass different parameters to different http request classes that implement the same interface 如何在C#中将不同的对象传递给同一个函数 - How To Pass Different Objects to same Function in C# 如何在每个不同的CustomValidator上传递相同的JavaScript函数? - How to pass the same JavaScript function on every different CustomValidator? 多个后台工作人员使用不同的参数调用同一函数 - Multiple backgroundworkers calling same function with different parameters 如何对具有相同成员的不同类型参数使用相同的函数? - How can I use the same function for different type parameters which have the same member? 如何将参数传递给不同控制器中的 actionfilter - How to pass parameters to actionfilter in different controllers C#从不同的网址打开同一应用程序并传递参数 - C# Open Same Application From Different Url and pass parameters 是否可以将具有相同返回类型但参数不同的函数发送给另一个函数? - Is it possible to send a function to another function with the same return type but different parameters? 如何将一堆列表作为函数中的参数传递 - How to pass a bunch of lists as parameters in a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM