Microsoft
Software
Hardware
Network
Question : Implementing optional parameters in a function - C#
Hi,
I have the following strings A, B, C, D, E that i want to pass as parameters in a function like,
public class X
{
public void func1(string A) {}
public void func1(string A, string B) {}
public void func1(string A, string B, string C) {}
public void func1(string A, string B, string C, string D) {}
public void func1(string A, string B, string C, string D, string E) {}
}
So all these functions become overloading functions.
Is there any way to write up all these functions into a single function? Like is there any concept of optional parameters?
I want my function should be like,
public class X
{
public void func1(string A, optional string B, optional string C, optional string D, optional string E)
{
//Implement the logic on String A
//Implement the logic on String B
//Implement the logic on String C
//Implement the logic on String D
//Implement the logic on String E
}
}
Now if I call the above function in any classs like
class y
{
public void func()
{
string str1, str2;
X obj = new X();
obj.func1(str1, str2);
}
}
Here in the above class y, I should be able to provide only 2 parameters and other 3 parameters not necessary. Based on these 2 parameters my logic should run only for the 2 strings and not the other 3 string parameters.
Kindly provide solution in C#.
regards,
Sanjay
Answer : Implementing optional parameters in a function - C#
Definitely nothing wrong!
Random Solutions
Any way to use CSS to suppress empty container elements?
Is there a patch that will allow me to have Windows Server 2008 server run KMS to activate Windows 7 clients.
Running two VMWare Windows Servers on Same Linux Box
How do i make Timeslips 2009 work in Citrix Xendesktop on Windows XP
Terminal Services on Windows SBS 2008 Standard
combo boxes feeding id number into table and not name
Help with centering this background image
CPU TDP value/ reading rises up and down
Cisco 3560 connecting to Dell switches
SQL Timeout when Changing Char Field Length