Created
August 4, 2021 20:52
-
-
Save manoj-choudhari-git/bafebb811261646970dab874880c5af2 to your computer and use it in GitHub Desktop.
.NET - C# Features - Pass by value and Pass by reference for value types
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
// ValueTypesDemo.cs | |
// PassByValue vs PassByReference | |
public class ValueTypeDemo | |
{ | |
public void Execute() | |
{ | |
int value = 9; | |
Console.WriteLine("----------------------------------------------------------------"); | |
Console.WriteLine($"Inside Caller: Before ProcessByValue : Value: {value}"); | |
ProcessByValue(value); | |
Console.WriteLine($"Inside Caller: After ProcessByValue : Value: {value}"); | |
Console.WriteLine("----------------------------------------------------------------"); | |
Console.WriteLine($"Inside Caller: Before ProcessByReference : Value: {value}"); | |
ProcessByReference(ref value); | |
Console.WriteLine($"Inside Caller: After ProcessByReference : Value: {value}"); | |
Console.WriteLine("----------------------------------------------------------------"); | |
} | |
private void ProcessByValue(int value) | |
{ | |
value = value * 2 + 1; | |
Console.WriteLine($"Inside ProcessByValue: Current Value: {value}"); | |
} | |
private void ProcessByReference(ref int value) | |
{ | |
value = value * 2 + 1; | |
Console.WriteLine($"Inside ProcessByReference: Current Value: {value}"); | |
} | |
} | |
// Driver Class | |
// Instantiates demo class and executes the demo. | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
ValueTypeDemo valueTypeDemo = new ValueTypeDemo(); | |
valueTypeDemo.Execute(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment