Simple Tips On .Net Programming Using Out Parameter

Author: Otaku Geek /

some time your function that require to return more than 1 values, and different type of variable.
Previously, you would set the function as return a List or Collection to bring out more variables, but now .Net provide a more flexi way to solve it which is using the Out parameter. It is simple, easy to use and don't even need to import others namespace.
The Example is like below :




public void say_hello()
{
string name;
DateTime date1;
string message;

message = createMessage(name, out date1);

this.textbox1.text=message;
this.textbox2.text=date1.toString();
}

public string createMessage(string name, out DateTime date1)
{
string message;
date1=DateTime.Now;
message="Hello, " + name + "!!" ;

return message;
}




thus, from the example above, the function createMessage has return more than 1 values, more than 1 variable type.
Hope this help.

1 comments:

Graham Evans said...

Out - Parameter are a bad idea in my opinion. They increase the risk of side effects and are hard to debug.

Post a Comment