optional-parameters

How to create default value for function argument in Clojure

為{幸葍}努か 提交于 2019-11-28 15:26:34
I come with this: (defn string->integer [str & [base]] (Integer/parseInt str (if (nil? base) 10 base))) (string->integer "10") (string->integer "FF" 16) But it must be a better way to do this. A function can have multiple signatures if the signatures differ in arity. You can use that to supply default values. (defn string->integer ([s] (string->integer s 10)) ([s base] (Integer/parseInt s base))) Note that assuming false and nil are both considered non-values, (if (nil? base) 10 base) could be shortened to (if base base 10) , or further to (or base 10) . You can also destructure rest arguments

Optional parameters in managed C++/CLI methods

江枫思渺然 提交于 2019-11-28 13:15:50
How can I declare a managed method in C++/CLI that has an optional parameter when used from C#? I've decorated the parameter with both an Optional and a DefaultParameterValue attribute (see: How default parameter values are encoded ), but only the Optional attribute seems to be honored. C++/CLI: public ref class MyClass1 { public: MyClass1([System::Runtime::InteropServices::Optional] [System::Runtime::InteropServices::DefaultParameterValue(2)] int myParam1) ↑ { System::Console::WriteLine(myParam1); } }; C#: var myInstance1 = new MyClass1(); // compiles and runs Output : 0 Expected Output: 2

Is there any reason to declare optional parameters in an interface?

烂漫一生 提交于 2019-11-28 09:01:27
You can declare optional parameters in an interface method but implementing classes are not required to declare the parameters as optional, as Eric Lippert explained . Conversely, you can declare a parameter as optional in an implementing class but not in the interface. So is there any reason to declare optional parameters in an interface? If not, why is it allowed? Examples: public interface IService1 { void MyMethod(string text, bool flag = false); } public class MyService1a : IService1 { public void MyMethod(string text, bool flag) {} } public class MyService1b : IService1 { public void

Optional parameters on delegates doesn't work properly [duplicate]

笑着哭i 提交于 2019-11-28 08:45:43
问题 This question already has answers here : Can a Delegate have an optional parameter? (2 answers) Closed 6 years ago . Why this piece of code does not compile? delegate int xxx(bool x = true); xxx test = f; int f() { return 4; } 回答1: Optional parameters are for use on the calling side - not on what is effectively like a single-method-interface implementation . So for example, this should compile: delegate void SimpleDelegate(bool x = true); static void Main() { SimpleDelegate x = Foo; x(); //

Method parameter array default value [duplicate]

北城余情 提交于 2019-11-28 07:20:48
问题 This question already has an answer here: Passing an empty array as default value of an optional parameter [duplicate] 3 answers In c# it is possible to use default parameter values in a method, in example: public void SomeMethod(String someString = "string value") { Debug.WriteLine(someString); } But now I want to use an array as the parameter in the method, and set a default value for it. I was thinking it should look something like this: public void SomeMethod(String[] arrayString = {

ASP.net MVC routing with optional first parameter

笑着哭i 提交于 2019-11-28 07:02:12
I need to provide following functionality for one of the web sites. http://www.example.com/ [sponsor] /{controller}/{action} Depending on the [sponsor], the web page has to be customized. I tried combination of registering the routes with Application_Start and Session_Start but not able to get it working. public static void RegisterRoutes(RouteCollection routes, string sponsor) { if (routes[sponsor] == null) { routes.MapRoute( sponsor, // Route name sponsor + "/{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional } //

SQL Server stored procedure parameters

你。 提交于 2019-11-28 05:49:48
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 problem occurs when I am passing a parameter to stored procedure, but stored proc doesn't accept such parameter. For example, my stored procedure is: CREATE PROCEDURE GetTaskEvents @TaskName varchar(50) AS BEGIN -- SP Logic END Calling stored procedure as: EXEC GetTaskEvents @TaskName = 'TESTTASK', @ID = 2 This throws below error: Msg 8144, Level 16, State 2, Procedure GetTaskEvents, Line 0 Procedure or function GetTaskEvents has too

How can I default a parameter to Guid.Empty in C#?

北城以北 提交于 2019-11-28 04:13:41
I wish to say: public void Problem(Guid optional = Guid.Empty) { } But the compiler complains that Guid.Empty is not a compile time constant. As I don’t wish to change the API I can’t use: Nullable<Guid> Meligy Solution You can use new Guid() instead public void Problem(Guid optional = new Guid()) { // when called without parameters this will be true var guidIsEmpty = optional == Guid.Empty; } You can also use default(Guid) default(Guid) also will work exactly as new Guid() . Because Guid is a value type not reference type, so, default(Guid) is not equal to null for example, instead, it's

Named and optional parameters, and WCF

試著忘記壹切 提交于 2019-11-28 02:48:45
问题 so .Net 4 added named and optional parameters which are pretty sweet. I don't need to make as many 1 line overload methods. Will that work over WCF? 回答1: Since these are compiler semantics I'd say no. However you'd expect them to work in the only following way. On the Service Code side all code would accept the defaulted parameters. On the client side I note that the 'Add Service Reference' tooling on VS2010 doesn't take the defaults and add them to the generated proxy. So You'd have to

Is it possible to use optional/default parameters in a lambda expression in c#?

一笑奈何 提交于 2019-11-28 02:27:32
问题 Is there a way to use optional arguments (default parameters) with lambda expressions in c#? I have read through the documentation but can find nothing to say one way or the other. To illustrate, I can define simple method that uses an optional argument to supply a default value like so: void MyMethod(string arg = "default-value") { Console.WriteLine(arg); } What I want to know is if I am able to do the same thing using a lambda expression. // gives a syntax error Action<string> MyMethod =