params-keyword

Overloading operator << - C++

徘徊边缘 提交于 2021-02-06 12:47:26
问题 Background I have a container class which uses vector<std::string> 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 container some time. For that I have to use container.AddChar("First"); container.AddChar("Second"); This makes the code larger. So to make it more easier, I plan to overload operator <<. So that I can write container << "First" << "Second" and two

Benefits of having both specific arguments and params method overloads in C#

心不动则不痛 提交于 2020-01-04 04:21:10
问题 There are a number of examples in the .NET framework where there are multiple overloads for a method, some of which use a specific number of parameters followed by a final "catch all" where the params keyword is used. Common examples of this are on the String class e.g.: String.Format() String.Concat() I was wondering if there is a particular reason for why there are so many of these method overloads? At first I thought it might be something to do with performance; the question and answers to

Changing the params modifier in a method override

╄→гoц情女王★ 提交于 2019-12-22 03:57:04
问题 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[] leaves) { Console.WriteLine("G"); } } class Okapi : Giraffid { public override void Eat(params int[] leaves) { Console.WriteLine("O"); } } This compiles with no warnings. Then saying: var okapi = new Okapi(); okapi.Eat(2, 4, 6); // will not compile! gives an

Overloading, generic type inference and the 'params' keyword

假装没事ソ 提交于 2019-12-07 06:20:18
问题 I just noticed a strange behavior with overload resolution. Assume that I have the following method : public static void DoSomething<T>(IEnumerable<T> items) { // Whatever // For debugging Console.WriteLine("DoSomething<T>(IEnumerable<T> items)"); } Now, I know that this method will often be called with a small number of explicit arguments, so for convenience I add this overload : public static void DoSomething<T>(params T[] items) { // Whatever // For debugging Console.WriteLine("DoSomething

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

送分小仙女□ 提交于 2019-12-06 21:32:01
问题 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 looked like this: void Log( string message, params object[] messageArgs = null); And we call it like this: log.Log("{0}: I canna do it cap'n, the engines can't handle warp {1}!", "Scotty", warpFactor); Now, we want to capture caller information and log

Overloading, generic type inference and the 'params' keyword

我们两清 提交于 2019-12-05 11:44:17
I just noticed a strange behavior with overload resolution. Assume that I have the following method : public static void DoSomething<T>(IEnumerable<T> items) { // Whatever // For debugging Console.WriteLine("DoSomething<T>(IEnumerable<T> items)"); } Now, I know that this method will often be called with a small number of explicit arguments, so for convenience I add this overload : public static void DoSomething<T>(params T[] items) { // Whatever // For debugging Console.WriteLine("DoSomething<T>(params T[] items)"); } Now I try to call these methods : var items = new List<string> { "foo", "bar

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

只愿长相守 提交于 2019-12-05 02:22:32
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 looked like this: void Log( string message, params object[] messageArgs = null); And we call it like this: log.Log("{0}: I canna do it cap'n, the engines can't handle warp {1}!", "Scotty", warpFactor); Now, we want to capture caller information and log that as well. So the signature becomes: void Log( string message, params object[] messageArgs,

Changing the params modifier in a method override

拜拜、爱过 提交于 2019-12-05 00:49:44
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[] leaves) { Console.WriteLine("G"); } } class Okapi : Giraffid { public override void Eat(params int[] leaves) { Console.WriteLine("O"); } } This compiles with no warnings. Then saying: var okapi = new Okapi(); okapi.Eat(2, 4, 6); // will not compile! gives an error( No overload for method 'Eat' takes 3 arguments ). Now, I know that the compiler translates the

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

萝らか妹 提交于 2019-11-30 11:48:42
问题 In C#, I am trying to use <see cref="blah"/> 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 finding nothing in my searches and no one I know has any idea, either. The compiler is choking on the square brackets. I've tried all kinds of different combinations, using curly braces, using the Array class, but nothing is working. Does anyone know

Changing the params modifier in a method override

耗尽温柔 提交于 2019-11-30 06:50:55
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[] leaves) { Console.WriteLine("G"); } } class Okapi : Giraffid { public override void Eat(params int[] leaves) { Console.WriteLine("O"); } } This compiles with no warnings. Then saying: var okapi = new Okapi(); okapi.Eat(2, 4, 6); // will not compile! gives an error( No overload for method 'Eat' takes 3 arguments ). Now, I know that the compiler translates the