I viewed in few blogs they are passing parameter to methods by comma separated null value and also in particular where parameter defined. By 3.5 or 4.0 (not clear) .net introduced Optional parameter and Named Argument.
Optional parameter
In some cases we need to pass one parameter sometimes no need for that parameter on a method such situation optional parameter can be used.
Named Argument
Parameter prefixed by name of the parameter ex : method(Name: paraName).Its for more readability.in such case u don't need to follow the exact order how the parameter defined.
Below is an example
protected void
Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
try
{
NameValueCollection nvp =
Request.QueryString;
if (nvp.HasKeys())
{
string
_query = Request.QueryString.ToString();
nvp = null;
nvp = HttpUtility.ParseQueryString(_query);
message(message: nvp.Keys[0].ToString()); //
named argument Here its call by Message: like that ALSO Optional paramter with
one Parameter
}
else
{
message(); // Optional parameter message()
with no paramater
}
}
catch (Exception)
{
throw;
}
}
}
protected void
btnQuerystring_Click(object sender, EventArgs e)
{
try
{
Response.Redirect("default.aspx?name=Arun");
}
catch (Exception)
{
throw;
}
}
void message(string
message = null)
//define Optional paramter
{
if (string.IsNullOrWhiteSpace(message))
lblMsg.Text = "No query string";
else
lblMsg.Text = message;
}
in c# and Html side
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="Scripts/jquery-1.8.3.min.js"
type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnQuerystring"
runat="server"
Text="With Query
String"
onclick="btnQuerystring_Click"
/>
<br />
<asp:Label ID="lblMsg" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
In the above example I'm calling a message method on page load ,with and without query string.
Hope u got it well
download code here
If u had any trouble just ask, Happy to help u :)
Stay Tune...
Have a nice day... 'N happy Coding :)
No comments:
Post a Comment