Last active
April 3, 2017 12:12
-
-
Save westonal/8ef7cf7bdcda136fbb63e1ec5f94aa8b to your computer and use it in GitHub Desktop.
C# is Pass-By-Value by default proof
This file contains hidden or 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
| 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