|
#region Normal Array
[TestMethod]
public void TestMethod1()
{
//Normal array changes individual member
string[] astr = new string[]{"aaa"};
ModifyArray1(astr);
Assert.AreEqual("bbb", astr[0]);
}
[TestMethod]
public void TestMethod2()
{
//Non-ref array doesn't change array itself
string[] astr = new string[] { "aaa" };
ModifyArray2(astr);
Assert.AreEqual(1, astr.Length);
Assert.AreEqual("aaa", astr[0]);
}
public static void ModifyArray1(string[] astr)
{
astr[0] = "bbb";
}
public static void ModifyArray2(string[] astr)
{
astr = new string[] { "ccc", "ccc" };
}
#endregion
#region Ref Array
[TestMethod]
public void TestMethodRef1()
{
string[] astr = new string[] { "aaa" };
ModifyArrayRef1(ref astr);
Assert.AreEqual("bbb", astr[0]);
}
[TestMethod]
public void TestMethodRef2()
{
//Ref array can change the array itself, like giving it a new length
string[] astr = new string[] { "aaa" };
ModifyArrayRef2(ref astr);
Assert.AreEqual(2, astr.Length);
Assert.AreEqual("ccc", astr[0]);
}
public static void ModifyArrayRef1(ref string[] astr)
{
astr[0] = "bbb";
}
public static void ModifyArrayRef2(ref string[] astr)
{
astr = new string[] { "ccc", "ccc" };
}
#endregion
Living in Chicago and interested in working for a great company? Check out the careers at Paylocity.