Named Arguments

Added in .NET 4.0
Also referred to as Named Parameters


The named parameters feature does not affect the declaration of the method
You can use the named parameter feature when you call a method but including the names of the paramaters
The order of parameters can be different from the declaration
In combination with optional parameters you can leave out one or more of the optional parameters
This feature was introduced to support legacy APIs (especially Office) and should be used sparingly in new code


public void Folder_Create(string sFolderName = "C:\temp\", 
                          string sSubFolderName = "newfolder")
{
}

public void main()
{
   Folder_Create("C\temp\one\", "two");
   Folder_Create(sFolderName: "C\temp\one\", "two");
   Folder_Create(sFolderName: "C\temp\one\", sSubFolderName: "two");
   Folder_Create(sSubFolderName: "two", sFolderName: "C\temp\one\");
   Folder_Create(sSubFolderName: "two");
}

This allows you to pass arguments to methods using named arguments which means that the ordering of the arguments is irrelvant
This complements optional parameters
Provide the name of the argument followed by a colon and then the vvalue you want to pass in
The rest of the arguments will contain their default values


public bool Folder_Rename(string sFolderName, 
                          string sNewFolderName)
{
}
public void main()
   bool value = Folder_Rename(sFolderName: "C\temp\one\",
                              sNewFolderName: "C:\temp\two\")
}


© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext