Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a C# alternative to Java's vararg parameters?

Tags:

c#

I have worked on Java and new to .Net technology

Is it possible to declare a function in C# which accepts variable input parameters

Is there any C# syntax similar to the following Java syntax?

void f1(String... a) 
like image 738
Pradeep K M Avatar asked Aug 12 '13 19:08

Pradeep K M


People also ask

Why is there a slash in AC?

Senior Member. You read it as "ei-cee" (no "slash" pronounced). In terms of distinguishing between "air conditioning" and "air conditioner," I can think of an example like "Today, I bought a new air conditioner" ("conditioning" not allowed). I personally would not say "Today, I bought a new AC."

Why is it called AC?

Air conditioning, often abbreviated as A/C or AC, is the process of removing heat from an enclosed space to achieve a more comfortable interior environment (sometimes referred to as 'comfort cooling') and in some cases also strictly controlling the humidity of internal air.

What is the mean of AC?

a/ c is an abbreviation for air-conditioning. Keep your windows up and the a/c on high. 60 Motel Units. All Units A/C, Heat, Cable TV.

Is AC the same as AC?

In the air conditioning industry, the term HVAC is often used instead of AC. HVAC refers to heating, ventilation, and air conditioning, whereas AC simply refers to air conditioning. AC is generally used when referring to systems that are designed to cool the air in your home.


2 Answers

Yes, C# has an equivalent of varargs parameters. They're called parameter arrays, and introduced with the params modifier:

public void Foo(int x, params string[] values) 

Then call it with:

Foo(10, "hello", "there"); 

Just as with Java, it's only the last parameter which can vary like this. Note that (as with Java) a parameter of params object[] objects can easily cause confusion, as you need to remember whether a single argument of type object[] is meant to be wrapped again or not. Likewise for any nullable type, you need to remember whether a single argument of null will be treated as an array reference or a single array element. (I think the compiler only creates the array if it has to, but I tend to write code which avoids me having to remember that.)

like image 78
Jon Skeet Avatar answered Sep 22 '22 18:09

Jon Skeet


Have a look at params (C# Reference)

The params keyword lets you specify a method parameter that takes a variable number of arguments.

You can send a comma-separated list of arguments of the type specified in the parameter declaration, or an array of arguments of the specified type. You also can send no arguments.

No additional parameters are permitted after the params keyword in a method declaration, and only one params keyword is permitted in a method declaration.

As shown in the example the method is declared as

public static void UseParams(params int[] list) {     for (int i = 0; i < list.Length; i++)     {         Console.Write(list[i] + " ");     }     Console.WriteLine(); } 

and used as

UseParams(1, 2, 3, 4); 
like image 37
Adriaan Stander Avatar answered Sep 18 '22 18:09

Adriaan Stander