Skip to content

Instantly share code, notes, and snippets.

@manoj-choudhari-git
Created August 4, 2021 20:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manoj-choudhari-git/bafebb811261646970dab874880c5af2 to your computer and use it in GitHub Desktop.
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
// 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