Last active
August 4, 2021 21:26
-
-
Save manoj-choudhari-git/cd75a45f94da5c9d7953f080663726fa to your computer and use it in GitHub Desktop.
.NET - C# Features - Pass reference type as parameter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Reference Type for Demo | |
public class Person | |
{ | |
public int Age { get; set; } | |
public string Name { get; set; } | |
public override string ToString() | |
{ | |
return $"{{ Age: {Age}, Name: {Name} }}"; | |
} | |
} | |
// ReferenceTypeDemo.cs | |
public class ReferenceTypeDemo | |
{ | |
public void Execute() | |
{ | |
Person p = new Person(); | |
p.Age = 25; | |
p.Name = "Sachin Tendulkar"; | |
Console.WriteLine("----------------------------------------------------------------"); | |
Console.WriteLine($"Inside Caller: Before ChangeDetails : Value: {p}"); | |
ChangeDetails(p); | |
Console.WriteLine($"Inside Caller: After ChangeDetails : Value: {p}"); | |
} | |
// Passed object is modified here | |
private void ChangeDetails(Person p) | |
{ | |
p.Age = p.Age + 10; | |
p.Name = "Rahul Dravid"; | |
Console.ForegroundColor = ConsoleColor.Green; | |
Console.WriteLine($"Inside ChangeDetails: Value: {p}"); | |
Console.ResetColor(); | |
} | |
} | |
// Driver Class | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
ReferenceTypeDemo referenceTypeDemo = new ReferenceTypeDemo(); | |
referenceTypeDemo.Execute(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment