tagged [optional-parameters]

Can a Delegate have an optional parameter?

Can a Delegate have an optional parameter? I have the below code that was working fine until I tried adding the `bool NetworkAvailable = true` portion. Now I get a `Method name expected` compile time ...

21 Sep at 17:53

Conflicting overloaded methods with optional parameters

Conflicting overloaded methods with optional parameters I have two overloaded methods, one with an optional parameter. now I call: interestingly the first overload is called. why not the second overl...

How do I default a parameter to DateTime.MaxValue in C#?

How do I default a parameter to DateTime.MaxValue in C#? I wish to say: But the compiler complains that DateTime.MaxValue is not a compile time constant. DateTime.MinValue is easy, just use default(Da...

23 May at 12:18

Check inside method whether some optional argument was passed

Check inside method whether some optional argument was passed How do I check if an optional argument was passed to a method? Another approach is to use `Nullable.HasValue` ([MSDN definitions](http://m...

Optional Parameters, Good or Bad?

Optional Parameters, Good or Bad? I am writing and browsing through a lot of methods in the project im working with and as much as I think `overloads` are useful I think that having a simple `optional...

15 May at 13:29

Using an optional parameter of type System.Drawing.Color

Using an optional parameter of type System.Drawing.Color I am starting to take advantage of optional parameters in .Net 4.0 The problem I am having is when I try to declare an optional parameter of Sy...

6 Aug at 14:27

Optional argument followed by Params

Optional argument followed by Params So I see that it's possible to have a method signature where the first parameter provides a default value and the second parameter is a params collection. What I c...

13 May at 05:52

How to execute a private static method with optional parameters via reflection?

How to execute a private static method with optional parameters via reflection? I have a class with a private static method with an optional parameter. How do I invoke it from another class via Reflec...

Method parameter array default value

Method parameter array default value In c# it is possible to use default parameter values in a method, in example: But now I want to use an array as the parameter in the method, and set a default valu...

Combining CallerMemberName with params

Combining CallerMemberName with params Right now (C# 4.0), our logging method looks like where the logger does the string formatting, so that the caller does not have to put String.Format's to create ...

Can't use optional parameters when implementing an interface for a WCF

Can't use optional parameters when implementing an interface for a WCF In my interface I have declared this. I implemented it as follows. It compiles and uploads as my WCF service. However, when I use...

11 Jun at 11:46

How to provide default value for a parameter of delegate type in C#?

How to provide default value for a parameter of delegate type in C#? In C# we can provide default value of the parameters as such: But, when the method signature is: How can we pass the default parame...

C# "Constant Objects" to use as default parameters

C# "Constant Objects" to use as default parameters Is there any way to create a constant object(ie it cannot be edited and is created at compile time)? I am just playing with the C# language and notic...

21 Mar at 00:18

Optional parameters for interfaces

Optional parameters for interfaces Using c# 4.0 -- building an interface and a class that implements the interface. I want to declare an optional parameter in the interface and have it be reflected in...

C# 4.0: Can I use a Color as an optional parameter with a default value?

C# 4.0: Can I use a Color as an optional parameter with a default value? This results in an error that c must be a compile-time constant. I've read up on this a little and most examples are dealing wi...

10 May at 16:27

C# optional parameters on overridden methods

C# optional parameters on overridden methods Seems like in .NET Framework there is an issue with optional parameters when you override the method. The output of the code below is: "bbb" "aaa" . But th...

C# 4.0 - How to Handle Optional String Parameters

C# 4.0 - How to Handle Optional String Parameters This code is not valid: But this code is: Why? Because string.Empty is a readonly field, not a constant, and defaults for optional parameters must be ...

Is the new feature of C# 4.0 - "Optional Parameters" CLS-Compliant?

Is the new feature of C# 4.0 - "Optional Parameters" CLS-Compliant? This new feature is really convenient. Lately I read the document of the "Microsoft All-In-One Code Framework", and it mentions that...

Method Overriding and Optional Parameters

Method Overriding and Optional Parameters Would someone care to explain how this code produces the folowing output? ``` using System; namespace ConsoleApplication1 { class Test { public overri...

SQL Server stored procedure parameters

SQL Server stored procedure parameters I am developing a framework, where in I am a calling stored procedure with dynamically created parameters. I am building parameter collection at the runtime. The...

C# Optional Array Parameter for Class

C# Optional Array Parameter for Class I know this can be done using `null` so I have a workaround for that, but I was wondering if there was a better way that I can have an optional `int[]` parameter ...

11 Oct at 06:18

How to pass a nullable type to a P/invoked function

How to pass a nullable type to a P/invoked function I have a few p/invoked functions (but I'm rewriting my code at the moment so I'm tidying up) and I want to know how to use/pass a nullable type as o...

Using an enum as an optional parameter

Using an enum as an optional parameter I have several methods in an application I'm working on loaded with optional parameters, some of which are enums. Currently, in order to do that I'm writing meth...

7 Feb at 00:51

Warning From Explicitly Implementing an Interface with Optional Parameters

Warning From Explicitly Implementing an Interface with Optional Parameters I was playing with optional parameters to see how they would work with interfaces and I came across a strange warning. The se...

Why can't I give a default value as optional parameter except null?

Why can't I give a default value as optional parameter except null? I want to have a and set it to default value that I determine, when I do this: I'm getting the following error (`Foo` is a class): >...

21 Oct at 11:7