Skip to content

Instantly share code, notes, and snippets.

@westonal
Last active April 3, 2017 12:12
Show Gist options
  • Select an option

  • Save westonal/8ef7cf7bdcda136fbb63e1ec5f94aa8b to your computer and use it in GitHub Desktop.

Select an option

Save westonal/8ef7cf7bdcda136fbb63e1ec5f94aa8b to your computer and use it in GitHub Desktop.
C# is Pass-By-Value by default proof
using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Framework.Tests.General
{
[TestClass]
public class PassByValueProofTests
{
public class SomeClass
{
public SomeClass(String value)
{
SomeValue = value;
}
public String SomeValue {get;}
}
public void PassByValue(SomeClass s)
{
s = new SomeClass("Hello World");
}
public void PassByRef(ref SomeClass s) //ref is only difference
{
s = new SomeClass("Hello World");
}
[TestMethod]
public void ByRef()
{
SomeClass c = new SomeClass("Hello");
PassByRef(ref c);
Assert.AreEqual("Hello World", c.SomeValue);
}
[TestMethod]
public void ByValue()
{
SomeClass c = new SomeClass("Hello");
PassByValue(c);
Assert.AreEqual("Hello", c.SomeValue); //this is different result, because "ref" is not implicit
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment