Passing an empty array as default value of an optional parameter [duplicate]

You can’t create compile-time constants of object references. The only valid compile-time constant you can use is null, so change your code to this: public void DoSomething(int index, ushort[] array = null, bool thirdParam = true) And inside your method do this: array = array ?? new ushort[0]; (from comments) From C# 8 onwards you … Read more

How would I skip optional arguments in a function call?

Your post is correct. Unfortunately, if you need to use an optional parameter at the very end of the parameter list, you have to specify everything up until that last parameter. Generally if you want to mix-and-match, you give them default values of ” or null, and don’t use them inside the function if they … Read more

How to create default value for function argument in Clojure

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 … Read more

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

You can work around this very easily by changing your signature. void Foo(TimeSpan? span = null) { if (span == null) { span = TimeSpan.FromSeconds(2); } … } I should elaborate – the reason those expressions in your example are not compile-time constants is because at compile time, the compiler can’t simply execute TimeSpan.FromSeconds(2.0) and … Read more

AngularJS Directive with default options

Use the =? flag for the property in the scope block of the directive. angular.module(‘myApp’,[]) .directive(‘myDirective’, function(){ return { template: ‘hello {{name}}’, scope: { // use the =? to denote the property as optional name: ‘=?’ }, controller: function($scope){ // check if it was defined. If not – set a default $scope.name = angular.isDefined($scope.name) ? … Read more