Advertisement
Guest User

Untitled

a guest
Jul 6th, 2010
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.06 KB | None | 0 0
  1. List<string> MainStringList = new List<string>();
  2.  
  3. public void RunTest()
  4. {
  5.     MainStringList.Add("Original string");
  6.  
  7.     Console.WriteLine("Before SetList: {0}", MainStringList[0]);
  8.     SetList(MainStringList);
  9.  
  10.     Console.WriteLine("After SetList: {0}", MainStringList[0]);
  11.  
  12. }
  13.  
  14. public void SetList(List<string> listReference)
  15. {
  16.     listReference = new List<string>();
  17.     listReference.Add("Changed string");
  18. }
  19.  
  20. // Output:
  21. // Before SetList: Original string
  22. // After SetList: Original string
  23.  
  24. // -------------------------------
  25.  
  26. List<string> MainStringList = new List<string>();
  27.  
  28. public void RunTest()
  29. {
  30.     MainStringList.Add("Original string");
  31.  
  32.     Console.WriteLine("Before SetList: {0}", MainStringList[0]);
  33.     SetList(ref MainStringList);
  34.  
  35.     Console.WriteLine("After SetList: {0}", MainStringList[0]);
  36.  
  37. }
  38.  
  39. public void SetList(ref List<string> listReference)
  40. {
  41.     listReference = new List<string>();
  42.     listReference.Add("Changed string");
  43. }
  44.  
  45. // Output:
  46. // Before SetList: Original string
  47. // After SetList: Changed string
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement