Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. int result =-1;
  2. if (!Int32.TryParse(SomeString, out result){
  3. // log bad input
  4. }
  5.  
  6. return result;
  7.  
  8. using System;
  9. public class MyClass
  10. {
  11. public static int TestOut(out char i)
  12. {
  13. i = 'b';
  14. return -1;
  15. }
  16.  
  17. public static void Main()
  18. {
  19. char i; // variable need not be initialized
  20. Console.WriteLine(TestOut(out i));
  21. Console.WriteLine(i);
  22. }
  23. }
  24.  
  25. using System;
  26. class ParameterTest
  27. {
  28. static void Mymethod(out int Param1)
  29. {
  30. Param1=100;
  31. }
  32. static void Main()
  33. {
  34. int Myvalue=5;
  35. MyMethod(Myvalue);
  36. Console.WriteLine(out Myvalue);
  37. }
  38. }
  39.  
  40. public struct BigStruct
  41. {
  42. public int A, B, C, D, E, F, G, H, J, J, K, L, M, N, O, P;
  43. }
  44.  
  45. SomeMethod(instanceOfBigStruct); // A copy is made of this 64-byte struct.
  46.  
  47. SomeOtherMethod(out instanceOfBigStruct); // No copy is made
  48.  
  49. public void ReferenceExample(SomeReferenceType s)
  50. {
  51. s.SomeProperty = "a string"; // The change is persisted to outside of the method
  52. }
  53.  
  54. public void ValueTypeExample(BigStruct b)
  55. {
  56. b.A = 5; // Has no effect on the original BigStruct that you passed into the method, because b is a copy!
  57. }
  58.  
  59. public void ValueTypeExampleOut(out BigStruct b)
  60. {
  61. b = new BigStruct();
  62. b.A = 5; // Works, because you refer to the same thing here
  63. }
  64.  
  65. int a;
  66. if(TrySomething(out a)) {}
  67.  
  68. int a;
  69. if(TrySomething(ref a)) {}
  70.  
  71. int a = 0;
  72. if(TrySomething(ref a)) {}
  73.  
  74. public void Example(SomeReferenceType s)
  75. {
  76. s = null;
  77. }
  78.  
  79. public void Example1(ref SomeReferenceType s)
  80. {
  81. s = null; // Sets whatever you passed into the method to null
  82. }
  83.  
  84. public bool TryGet(
  85. string key,
  86. out string value
  87. )
  88.  
  89. string value;
  90. if (!lookupDictionary.TryGet("some key", out value))
  91. value = "default";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement