Questions tagged [params-keyword]

params is a C# keyword that allows a variable number of parameters in function calls.

From MSDN:

The params keyword lets you specify a method parameter that takes an argument where the number of arguments is variable.

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

39 questions
53
votes
3 answers

Clojure keyword arguments

In Common Lisp you can do this: (defun foo (bar &key baz quux) (list bar baz quux)) (foo 1 :quux 3 :baz 2) ; => (1 2 3) Clojure doesn't have keyword arguments. One alternative is this: (defn foo [bar {:keys [baz quux]}] (list bar baz…
Brian Carper
  • 71,150
  • 28
  • 166
  • 168
53
votes
5 answers

Can I use params in Action or Func delegates?

When I'm trying to use params in an Action delegate... private Action WriteToLogCallBack; I received this design time error: Invalid token 'params' in class, struct, or interface member declaration Any help!
Homam
  • 23,263
  • 32
  • 111
  • 187
17
votes
3 answers

Using C#'s XML comment cref attribute with params syntax

In C#, I am trying to use to reference a method signature that contains the params keyword. I know this converts the parameter list to an array, but I can't even figure out how to refer to an array in a CREF attribute. I am…
Matt H
  • 7,311
  • 5
  • 45
  • 54
16
votes
2 answers

Changing the params modifier in a method override

I'm aware that a params modifier (which turns in one parameter of array type into a so-called "parameter array") is specifically not a part of the method signature. Now consider this example: class Giraffid { public virtual void Eat(int[]…
Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
16
votes
3 answers

Can params[] be parameters for a lambda expression?

I've recently started exploring lambda expressions, and a question came to mind. Say I have a function that requires an indeterminate number of parameters. I would use the params keyword to model that variable number of parameters. My question:…
Andrew Gray
  • 3,756
  • 3
  • 39
  • 75
11
votes
4 answers

How to use params keyword along with caller Information in C#?

I am trying to combine the C# 5.0 Caller Information along with the C# params keyword. The intention is to create a wrapper for a logging framework, and we want the logger to format the text like String.Format. In previous versions, the method…
Moby Disk
  • 3,761
  • 1
  • 19
  • 38
11
votes
5 answers

Unpacking an array into method arguments

As you know C# supports variadic methods through the params keyword: int Add(params int[] xs) { return xs.Sum(); } Which can then be called with any number of arguments you like: Add(1); Add(1, 2); Add(1, 2, 3); But say I want to call Add…
user142019
9
votes
2 answers

How to ensure compilation error on signature change where 'params' keyword is used

I have a method like this: public void Foo(params string[] args) { bar(args[0]); bar(args[1]); } The new requirements lead to a change like this: public void Foo(string baz, params string[] args) { if("do bar".Equals(baz)) { …
Manolo
  • 1,597
  • 4
  • 21
  • 35
6
votes
6 answers

String.Format() - Repassing params but adding more parameters

I would like to do something like that: public string GetMessage(params object otherValues[]) { return String.Format(this.Message, this.FirstValue, otherValues); } So, I would like to repass an array of params to String.Format() but adding a…
Luciano
  • 2,695
  • 6
  • 38
  • 53
5
votes
2 answers

Why can't I have two method signatures with the only difference being the "params" keyword for the array parameter?

public class SomeClass { public SomeClass(SomeType[] elements) { } public SomeClass(params SomeType[] elements) { } } I printed this code and got CS0111 error. I'm surprised, are SomeType[] elements and…
5
votes
7 answers

Overloading operator << - C++

Background I have a container class which uses vector internally. I have provided a method AddChar(std::string) to this wrapper class which does a push_back() to the internal vector. In my code, I have to add multiple items to the…
Navaneeth K N
  • 15,295
  • 38
  • 126
  • 184
5
votes
5 answers

What is a real example of when to use params as a method argument?

As I understand it, params is just syntactic sugar that "under the hood" simply gives you an array of the type you specify. First, when would you use this? Second, why would you use it instead of just declaring an array argument?
richard
  • 12,263
  • 23
  • 95
  • 151
5
votes
1 answer

Practical usage of params indexer

Recently, I have found out that indexer can accept an array of arguments as params: public class SuperDictionary { public Dictionary Dict { get; } = new Dictionary(); public IEnumerable
Yeldar Kurmangaliyev
  • 33,467
  • 12
  • 59
  • 101
5
votes
5 answers

Is it possible to explode an array so that its elements can be passed to a method with the params keyword?

Take this non-compiling code for instance: public string GetPath(string basefolder, string[] extraFolders) { string version = Versioner.GetBuildAndDotNetVersions(); string callingModule = StackCrawler.GetCallingModuleName(); return…
Rick Minerich
  • 3,078
  • 19
  • 29
5
votes
1 answer

Overloading, generic type inference and the 'params' keyword

I just noticed a strange behavior with overload resolution. Assume that I have the following method : public static void DoSomething(IEnumerable items) { // Whatever // For debugging …
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
1
2 3