C
C#2y ago
Binto86

set default value of parameter to other parameter [Answered]

ok, i am aware that this is probably not possible, but can i set default value of parameter x to value of parameter y? consider this example:
public void MyMethod(string x, string y=x)
{
//some code
}
public void MyMethod(string x, string y=x)
{
//some code
}
would this be possible?
11 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
senox13
senox132y ago
There could absolutely be cases in which that makes sense. You can do that at the beginning of the method and use a sentinel value for the default argument value Or null, since it's a string
Binto86
Binto862y ago
i started doing some library that generates some sql from json and i want the user to be able to say that column names are not the same as json fields, i know i can do it with overload, but i wanted to know if it is possible like this, mostly out of curiosity
senox13
senox132y ago
public void MyMethod(string x, string y=null)
{
if(y==null)
x=y;
//some code
}
public void MyMethod(string x, string y=null)
{
if(y==null)
x=y;
//some code
}
Binto86
Binto862y ago
ok thats interesting approach, but at this point i think that it is easier to do it like this:
void MyMethod(string x, string y){
//my code here
}
void MyMethod(string x)=>MyMethod(x,x);
void MyMethod(string x, string y){
//my code here
}
void MyMethod(string x)=>MyMethod(x,x);
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
senox13
senox132y ago
Yep, both can be used exactly the same ways, though the first would be more DRY to document imo Irrelevant if you aren't adding XML docs
Binto86
Binto862y ago
oh i am probably
senox13
senox132y ago
I'd definitely opt for my initial chunk of code there. It'll be half the typing to document
Binto86
Binto862y ago
ok anyways, thanks for help, i am going to close this thread !close
Accord
Accord2y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server
More Posts
Checking if an arbitrary (remote) URL is a file attachment, and extracting it if it is? [Answered]If I'm given an arbitrary URL (I don't have control over it), how can I check if it appears to be a How does assigning default values when creating an instance work? [Answered]I'm not sure how to ask this question. I also can't for the life of me remember what this style of iProcess.EnableRaisingEvents does not work on WindowsI have an app that have 20 workers that are other processes. On production, on ubuntu, when I close Top-level statements in MSVSSay you've created a new project with top-level statements in MSVS. Is it possible to change it to *how to format multiple conditions assignments in return statement [CLOSED] [Answered]Greetings, I was wondering, how could I properly format this piece of code. ```cs return res > PagEntity Framework One-To-One [Answered]Code at: https://paste.mod.gg/rccwzntafwoj/0 When attempting to change the database I'm getting: TDatabase - Relationship returns empty set even though records are present [Answered]I think I'm probably missing something stupid obvious... In my batch model, I have ```cs private reaSave storageFile to a folder on driveI have a storagefile ready to give to a control to crop an image the problem is that image crop wilDynamically load .dll into .NET 6 ProgramHi, I've a Blazor WebAssembly Hosted (.NET 6) web application. I've put some part of the applicationMove File To A Matching Type RefactoringWhen everything is top-level do you not need namespaces because everything is "top-level"? Thanks fo