Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int result =-1;
- if (!Int32.TryParse(SomeString, out result){
- // log bad input
- }
- return result;
- using System;
- public class MyClass
- {
- public static int TestOut(out char i)
- {
- i = 'b';
- return -1;
- }
- public static void Main()
- {
- char i; // variable need not be initialized
- Console.WriteLine(TestOut(out i));
- Console.WriteLine(i);
- }
- }
- using System;
- class ParameterTest
- {
- static void Mymethod(out int Param1)
- {
- Param1=100;
- }
- static void Main()
- {
- int Myvalue=5;
- MyMethod(Myvalue);
- Console.WriteLine(out Myvalue);
- }
- }
- public struct BigStruct
- {
- public int A, B, C, D, E, F, G, H, J, J, K, L, M, N, O, P;
- }
- SomeMethod(instanceOfBigStruct); // A copy is made of this 64-byte struct.
- SomeOtherMethod(out instanceOfBigStruct); // No copy is made
- public void ReferenceExample(SomeReferenceType s)
- {
- s.SomeProperty = "a string"; // The change is persisted to outside of the method
- }
- public void ValueTypeExample(BigStruct b)
- {
- b.A = 5; // Has no effect on the original BigStruct that you passed into the method, because b is a copy!
- }
- public void ValueTypeExampleOut(out BigStruct b)
- {
- b = new BigStruct();
- b.A = 5; // Works, because you refer to the same thing here
- }
- int a;
- if(TrySomething(out a)) {}
- int a;
- if(TrySomething(ref a)) {}
- int a = 0;
- if(TrySomething(ref a)) {}
- public void Example(SomeReferenceType s)
- {
- s = null;
- }
- public void Example1(ref SomeReferenceType s)
- {
- s = null; // Sets whatever you passed into the method to null
- }
- public bool TryGet(
- string key,
- out string value
- )
- string value;
- if (!lookupDictionary.TryGet("some key", out value))
- value = "default";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement