Hi friends,in this article I will explain about C# Passing Parameters by Reference Or How to use “ref”
Keyword in C#.
In Previous articles I explained about ASP.NET MVC Tutorial or Intro to ASP.NET MVC 4 or what is MVC (Model – View – Controller),Detecting sequence of at least n sequential/Consecutive Dates from a given List in ASP.NET using C# or VB.NET and CSS - Gmail-style progress bar when page is loading
In Previous articles I explained about ASP.NET MVC Tutorial or Intro to ASP.NET MVC 4 or what is MVC (Model – View – Controller),Detecting sequence of at least n sequential/Consecutive Dates from a given List in ASP.NET using C# or VB.NET and CSS - Gmail-style progress bar when page is loading
A reference parameter is a reference to a memory location of a variable. When you pass parameters by reference, unlike value parameters, a new storage location is not created for these parameters. The reference parameters represent the same memory location as the actual parameters that are supplied to the method.
In C#.Net:
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using
System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected
void Page_Load(object
sender, EventArgs e)
{
int y = 1;
ex(y);
Response.Write(y + "<br/>");
int y2 = 2;
ex2(ref y2);
Response.Write(y2);
}
protected
void ex(int
x)
{
x = 10;
}
protected
void ex2(ref int x)
{
x = 10;
}
}
|
The output of the above code as shown in the below figure.
1
10
The output of y after calling a method will be 1 because changes in the called method does not affect there.But the output y2 after calling a method will be 10 because changes in the called method does effect there.
"If you like my blog or articles, you can appreciate by leaving your comments or Liking my Facebook pageAspdotnet-kishore, following on Google+ Aspdotnet-Kishore, Twitter on AspdotnetKishore, Linked in Aspdotnet-Kishore, stumbling my posts on stumble upon and subscribing on RSSfeed Aspdotnet-Kishore for free updates directly to your Email inbox . Watch my blog for more articles."
No comments:
Post a Comment