Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow implicitly typed variable(var) in methods optional paramters #14066

Closed
imranbaloch opened this issue Sep 26, 2016 · 6 comments
Closed

Allow implicitly typed variable(var) in methods optional paramters #14066

imranbaloch opened this issue Sep 26, 2016 · 6 comments

Comments

@imranbaloch
Copy link

imranbaloch commented Sep 26, 2016

Let's say we have this method,

public void SomeMethod(string str = "", int id = -1)
{
}

It will be great if we have something like,

public void SomeMethod(var str = "", var id = -1)
{
}

Due to optional parameter, compiler can easily determines the type. Just a suggestion.

@DavidArno
Copy link

DavidArno commented Sep 26, 2016

Um, how do you propose that the compiler work out that var str = null is of type string, as opposed to any other reference type?

@dsaf
Copy link

dsaf commented Sep 26, 2016

It will be great if we have something like...

Why? It's not likely to be something like Dictionary<Dictionary<string, Tuple<string, decimal>>> due to type limitation. The type names are likely to be quite short. Even your example only saves 3 characters.

@alrz
Copy link
Member

alrz commented Sep 26, 2016

As @DavidArno said, for all default values (null, zero and other structs) you will require to specify the type anyways. I'd prefer #7737 as it helps with consistency, conciseness and DRY.

void SomeMethod(string str = default, int id = default, CancellationToken ct = default)

As for non-default values like -1 I don't think that it needs to be different as it doesn't even save a single keystroke. Moreover, inferring public contract from usage doesn't seem to be a good idea overall.

@imranbaloch
Copy link
Author

@DavidArno oh sorry I mean,

string str = "".

What I mean is that when we can do

var s = "";

why not this,

someMethod(var s = "")

@DavidArno
Copy link

DavidArno commented Sep 26, 2016

why not this,
someMethod(var s = "")

I'm no fan of default/optional parameters and would always go for the following instead:

someMethod() => ...
someMethodWithString(string s) => ...

@gafter
Copy link
Member

gafter commented May 6, 2019

Closing as this doesn't appear to have much support. Having said that, if you want to continue to pursue this discussion, please open a new thread in csharplang.

@gafter gafter closed this as completed May 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants