optional-parameters

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

回眸只為那壹抹淺笑 提交于 2019-12-03 04:47:25
I want to have a optional parameter and set it to default value that I determine, when I do this: private void Process(Foo f = new Foo()) { } I'm getting the following error ( Foo is a class): 'f' is type of Foo, A default parameter of a reference type other than string can only be initialized with null. If I change Foo to struct then it works but with only default parameterless constructor. I read the documentation and it's clearly states that I cannot do this but it doesn't mention why? , Why is this restriction exists and why string is excluded from this? Why the value of an optional

Why is the empty dictionary a dangerous default value in Python? [duplicate]

对着背影说爱祢 提交于 2019-12-03 04:39:18
问题 This question already has answers here : “Least Astonishment” and the Mutable Default Argument (31 answers) Python methods: default parameter values are evaluated ONCE (3 answers) Closed 5 years ago . I put empty braces as the default value for an optional argument to a Python function, and pylint (using Sublime package) told me it was dangerous. Can someone explain why this is the case? And is a better alternative to use None instead? 回答1: It's dangerous only if your function will modify the

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

限于喜欢 提交于 2019-12-03 01:08:16
This new feature is really convenient. Lately I read the document of the "Microsoft All-In-One Code Framework", and it mentions that "Optional Parameters" is not CLS-Compliant. So I tested it by using "Optional Parameters" in a public API, and turned on FxCop, then I compiled and FxCop did not complain about anything. At the mean while, FxCop did report a warning when I add an API that has uint as its return type. So now I am confused, is "Optional Parameters" CLS-Compliant or not? And what's the best way to find out whether a new language feature is CLS-Compliant or not? Optional arguments

Groovy method with optional parameters

China☆狼群 提交于 2019-12-03 00:56:26
I would like to write a wrapper method for a webservice, the service accepts 2 mandatory and 3 optional parameters. To have a shorter example, I would like to get the following code working def myMethod(pParm1='1', pParm2='2') { println "${pParm1}${pParm2}" } myMethod(); myMethod('a') myMethod(pParm2:'a') // doesn't work as expected myMethod('b','c') The output is: 12 a2 [pParm2:a]2 a2 bc What I would like to achieve is to give one parameter and get 1a as the result. Is this possible (in the laziest way)? Can't be done as it stands... The code def myMethod(pParm1='1', pParm2='2'){ println "$

Set default value for DateTime in optional parameter [duplicate]

爷,独闯天下 提交于 2019-12-02 19:53:30
This question already has answers here : C# 4.0: Can I use a TimeSpan as an optional parameter with a default value? (8 answers) How can I set default value for DateTime in optional parameter? public SomeClassInit(Guid docId, DateTime addedOn = DateTime.Now???) { //Init codes here } There is a workaround for this, taking advantage of nullable types and the fact that null is a compile-time constant. (It's a bit of a hack though, and I'd suggest avoiding it unless you really can't.) public void SomeClassInit(Guid docId, DateTime? addedOn = null) { if (!addedOn.HasValue) addedOn = DateTime.Now; /

Why is the empty dictionary a dangerous default value in Python? [duplicate]

走远了吗. 提交于 2019-12-02 18:40:29
This question already has an answer here: “Least Astonishment” and the Mutable Default Argument 31 answers Python methods: default parameter values are evaluated ONCE 3 answers I put empty braces as the default value for an optional argument to a Python function, and pylint (using Sublime package) told me it was dangerous. Can someone explain why this is the case? And is a better alternative to use None instead? It's dangerous only if your function will modify the argument. If you modify a default argument, it will persist until the next call, so your "empty" dict will start to contain values

Optional parameters for interfaces

不问归期 提交于 2019-12-02 17:48:58
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 the class. So, I have the following: public interface IFoo { void Bar(int i, int j=0); } public class Foo { void Bar(int i, int j=0) { // do stuff } } This compiles, but it doesn't look right. The interface needs to have the optional parameters, because otherwise it doesn't reflect correctly in the interface method signature. Should I skip the optional parameter and just use a nullable type? Or will this work as intended with no

Does optional parameter and optional attribute not supported together?

匆匆过客 提交于 2019-12-02 13:11:35
public void ObjTest(StringBuilder sb, List<string> list, int i = 0, [Optional] string bs) { ...... } The above code throwing compilation error " Optional parameters must appear after all required parameters ". Does optional parameter and optional attribute not supported together in same method parameter, but it allows params arry after optional paramer ? You can use them in conjunction but the optional parameter (the language construct) must be the last parameter in the parameter list. public void X(StringBuilder sb, List<string> list, [Optional] string bs, int i = 0) { } 来源: https:/

Optional parameter in UriTemplate in WCF

核能气质少年 提交于 2019-12-02 04:51:12
问题 I've used the hint in this thread and provided a default value, so that when a user doesnät specify the virutal sub-directory, I'm making the assumption that he meant all the stuff to be listed. It works. [OperationContract] [WebInvoke(UriTemplate = "GetStuff/{type=all}", ...] IEnumerable<Stuff> GetStuff(String type); However, it would be nicer to specify a default value, instead. However, default(String) is null and I'd like to sent in an actual value. Particularly, I've set my heart on

Set optional parameters in JPQL Query

一个人想着一个人 提交于 2019-12-02 03:02:57
问题 I got this list of coins that i want to filter with that 3 optional parameters(Currency, quantity and year) How can I set parameters in JPQL as optional? I dont want to do 9 "if else" for checking if its null I got the function filtradoMonedas (filterCoins) that filter the object Moneda(coin) using that 3 OPTIONAL parameters but its not working if there´s a null parameter. This just work well if dont set empty parameters, if cantidad or ano is "" returns exception of bad query. Just want it