1

For example:

void building(int doors, bool multi_story) {...}
or
void building(int doors = 1, bool multi_story = false) {...}

Therefore:

new building(4, true);
new building(1, false);
or
new building(doors: 4, multi_story: true);
new building();

Thease things work well with nullable 'type?' as well. However it would be nice to do able to do this.

new building(doors: 4, multi_story:); // thanks SLaks for the correction

To mean "everything is default except -> has 4 doors and is multi-story". Removing the one word "true" is only a little thing but is it possible to simulate this?

alan2here
  • 3,223
  • 6
  • 37
  • 62
  • 2
    What are you asking about nullable types? – SLaks Dec 22 '11 at 16:35
  • 2
    Sounds to me you found out that a bool argument type kinda sucks. It does, use an enum instead. – Hans Passant Dec 22 '11 at 16:45
  • I've figured out the answer to the Nullable types part of the question so I removed it, you can have an unspecified Nullable as a parameter but as one can't be created with 'new' that parameter can never be used. – alan2here Dec 22 '11 at 17:17
  • See also [my blog posts](http://blog.slaks.net/2011/09/clarifying-boolean-parameters-part-1.html). – SLaks Dec 22 '11 at 17:32

3 Answers3

2

No; C# does not support such a syntax.

What would happen if you have a local variable named multi_story?

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • You'r right, ty, perhaps "(doors: 4, multi_story:)" I've corrected this in the question with a comment to show this, sorry that it outdates some of you'r answer. – alan2here Dec 22 '11 at 17:10
  • What are you trying to do with `new building(doors: 4, multi_story:);`? It doesn't make sense as-is. Maybe `new building(doors: 4, multi_story: true);`? – Tim S. Dec 22 '11 at 17:13
  • Thats effectivly what it would mean, I suppose it would be syntactic sugar. To me it conceptually makes a lot of sence, after all it's much less often that ", multi_story: false)" would be used because it's optional, instead of saying "isn't" the user of the function would just not use that parameter, but to say "yes, has multi_story" one must specify "yes, has multi_story and multi_story also has the value true", thats usefull for doors but less so either for something that just is or isn't, or in a case where all that is being specified about it is that it exists. – alan2here Dec 22 '11 at 17:25
0

You can just write:

new building(doors: 4); 

or even:

new building(4); 

If you do not specify a parameter, it will default to its specified default value. So multi_story will be false here.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • That would mean that it's not multi_story, how about to specify that it is? Would require "new building(doors: 4, multi_story: true);" or "new building(multi_story: true);" if I wanted the default number of doors. Instead of something like "new building(doors: 4, multi_story:);" – alan2here Dec 22 '11 at 17:07
  • How sould c# know that you mean `true`? There is no exceptional rule for boolean optional parameters. What would `building(doors:)` mean then? -1, 0, 1 or may be 100. Either you do specify a value for an optional parameter or you do not. There is no "between". – Olivier Jacot-Descombes Dec 22 '11 at 17:17
  • I would imagine building(doors:) would fail, especially if it were done with "specified but with nothing = true" as numbers can't be true/false, perhaps optionally including a specified default as well as unspecified default, or even the unspecified default being null might work well, except that I like to use null as default sometimes. But yeah, I see what you're saying. Also it's all somewhat hypothetical because the concept seemingly dosn't exist in C#. – alan2here Dec 22 '11 at 17:48
0

You can create several functions/methods with the same name as long as the signature of the function is different in each.

e.g.

public void building() {...}
public void building(int doors) {...}
public void building(bool multiStory) {...}
public void building(int doors, bool multiStory) {...}

for the first one above, what I would look at doing is:

public void building()
{
    building(1); // call building passing default doors
}
public void building(int doors)
{
    building(doors, true); // call the building function passing the default story flag
}
public void building(bool multiStory)
{
    building (1, multiStory); // Call building function passing default doors and ms flag
}
public void building(int doors, bool multiStory)
{
    //final building function - deal with doors & story arguments.
}

Note you can do this sort of thing with Constructors of class objects, using the "this" keyword to call the next constructor something like:

void building() : this(1) {}
void building(int doors) : this(doors, true) {}
void building(bool multiStory) :this(1, multiStory) {}
void building(int doors, bool multiStory){
    //do stuff
}
Harag
  • 1,532
  • 4
  • 19
  • 31
  • This dosn't use optional arguments at all, just simple polymorphism, and dosn't improve on my optional arguments example. My question is about subties of expressing things with optional arguments. – alan2here Dec 23 '11 at 23:51