Scenario: Difference between Ref vs Out parameter
Solution:
While creating a new object two things get created:
- The block of memory that holds data for the object.
- A reference or pointer to that block of data.
When the object is sent through a method without ref it copies the reference pointer but not
the data. So If we modify a property on an object, it will affect the same data pointed to by the earlier reference. If we set the reference to null or point it to new data it will not affect the earlier reference and also not the data referenced by it
When it is passed with ref keyword to a method then the actual reference to the object gets sent to the method and so now there only one reference to the data:
using System;
namespace ConsoleAppCore
{
public class Program
{
static void Main(string[] args)
{
var profile = new Profile
{
Id = 1,
Name = "Test User",
Address = "Los Angeles"
};
Console.WriteLine(
$"Outside the Method Profile (Before Method call): {profile.Id} - {profile.Name}, {profile.Addrress}");
ChangeAddress(profile);
Console.WriteLine(
$"Outside the Method Profile (After Method call w/o ref): {profile.Id} - {profile.Name}, {profile.Addrress}");
Console.ReadLine();
ChangeAddressRef(ref profile);
Console.WriteLine(
$"Outside the Method Profile (After Method call w/ ref): {profile.Id} - {profile.Name}, {profile.Addrress}");
Console.ReadLine();
}
private static bool ChangeAddress(Profile data)
{
//data.Addrress = "New York";
//Console.WriteLine(
// $"Outside the Method Profile (Before Method call): {data.Id} - {data.Name}, {data.Addrress}");
data = null;
return true;
}
private static bool ChangeAddressRef(ref Profile data)
{
//data.Addrress = "New York";
//Console.WriteLine(
// $"Outside the Method Profile (Before Method call): {data.Id} - {data.Name}, {data.Addrress}");
data = new Profile();
return true;
}
}
public class Profile
{
public int Id{ get; set; }
public string Name { get; set; }
public string Address { get; set; }
}
}
using System; namespace ConsoleAppCore { public class Program { static void Main(string[] args) { var profile = new Profile { Id = 1, Name = "Test User", Address = "Los Angeles" }; Console.WriteLine( $"Outside the Method Profile (Before Method call): {profile.Id} - {profile.Name}, {profile.Addrress}"); ChangeAddress(profile); Console.WriteLine( $"Outside the Method Profile (After Method call w/o ref): {profile.Id} - {profile.Name}, {profile.Addrress}"); Console.ReadLine(); ChangeAddressRef(ref profile); Console.WriteLine( $"Outside the Method Profile (After Method call w/ ref): {profile.Id} - {profile.Name}, {profile.Addrress}"); Console.ReadLine(); } private static bool ChangeAddress(Profile data) { //data.Addrress = "New York"; //Console.WriteLine( // $"Outside the Method Profile (Before Method call): {data.Id} - {data.Name}, {data.Addrress}"); data = null; return true; } private static bool ChangeAddressRef(ref Profile data) { //data.Addrress = "New York"; //Console.WriteLine( // $"Outside the Method Profile (Before Method call): {data.Id} - {data.Name}, {data.Addrress}"); data = new Profile(); return true; } } public class Profile { public int Id{ get; set; } public string Name { get; set; } public string Address { get; set; } } }
No comments:
Post a Comment