Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A simple test of setting via value and setting via reference.
using System;
using System.Diagnostics;
class ValueVsReference
{
public static void SetValue(int somenumber, int maxi)
{
for (int i = 0; i < maxi; i++)
{
somenumber = i;
}
}
public static void SetReference(ref int somenumber, int maxi)
{
for(int i = 0; i < maxi; i++)
{
somenumber = i;
}
}
public static void Main()
{
int iterations = 100000;
int a = 0;
Console.Write("Iterations: {0} ", iterations);
Console.Write("Value: ");
Stopwatch valuetime = new Stopwatch();
valuetime.Start();
SetValue(a, iterations);
valuetime.Stop();
Console.Write("{0} ", valuetime.Elapsed);
Console.Write("Reference: ");
Stopwatch referencetime = new Stopwatch();
referencetime.Start();
SetReference(ref a, iterations);
referencetime.Stop();
Console.WriteLine(referencetime.Elapsed);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.