-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Proposal: out void/null #10799
Comments
public class ResultObject
{
public int A1 { get; }
public int A2 { get; }
public string B1 { get; }
public string B2 { get; }
public bool Looped { get; }
public bool C2 { get; }
public bool Success { get; }
}
//elsewhere
public ResultObject MyFunction(int param)
{
//....
}
//usage
var result = MyFunction(someInt);
if (result.Success)
{
//....
} |
These parameters are used, but not here. What I want is that when I don't use them I don't need to create lots of variables that I won't use just for storing the result of the |
So again, it makes a lot more sense to just put them all into one object and use that instead. Heck, make it a |
Besides, if you put them in the function, they HAVE to have values, so you MUST use them each time. What you're doing actually gets you FURTHER from your goal. C#, by nature, MAKES you assign values to ANY parameter present. ANY of them. |
With |
#6183 is probably your best bet, that way you can at least declare the throwaway variables inline with the method call. |
This is the problem of converting code from one language to another: the idioms of the languages are different. So you need to redesign the code, not just copy/paste with syntax changes. The way to handle this with C# 7 will be to use a tuple: (bool success, int a1, int a2, int b1, int b2, int c1, int c2) myFunction(int param)
{
....
}
void otherFunction()
{
var looped = false;
[...]
var myFunctionResult = myFunction(5);
if(myFunctionResult.success)
{
looped = myFunctionResult.c1;
[...]
}
[...]
} In the meantime @Joe4evr's suggestion of a return type is the most idiomatic C# solution. |
Wildcard syntax #20 (comment) #8074 could be extended to take care of this case. E.g. testing if something is parsable without worrying about the result: if (!int.TryParse(input, out *))
Console.WriteLine("Error: not a valid integer value."); PS: I wouldn't fixate on refactoring the original C++ example too much. Sometimes we have to deal with the code that is outside of our control. |
The C# 7 language feature of discards addresses this feature request. You can put |
While porting a C++ application to C# recently, I needed to translate code like this:
In C#, it gives something like this:
As you can see, here I need to create lots of unneeded temporary variables.
These parameters aren't used here, but are used elsewhere in the code, so they are needed.
The thing here would be to make something like this:
Where the
out null
means that the values passed "won't go anywhere", like if you didecho "text" > /dev/null
Also by the way, where'd the declaration expressions (#254) go?
The text was updated successfully, but these errors were encountered: