Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Reference: Passing the original object that can be changed
- Value: Passing a value is like copying it, the initial value stays untouched
- Related keywords:
- ref
- - used to pass a reference
- - May be modified by the called method
- out
- - used to pass a reference
- - Must be modified by the called method
- in
- - used to pass a reference
- - Can't be modified by the called method
- */
- class Reference //classes are by default reference types
- {
- public int property { get; set; }
- }
- struct Value //structs are by default value types
- {
- public int property { get; set; }
- }
- class App
- {
- public static void Main()
- {
- int apples = 8;
- int bananas = 15;
- DoStuff(apples, ref bananas);
- Console.WriteLine($"apples: {apples}\nbananas: {bananas}\n");
- //Output: apples: 8, bananas: 6969
- Reference myClass = new Reference();
- Value myStruct = new Value();
- myClass.property = 99;
- myStruct.property = 77;
- DoOtherStuff(myClass, myStruct);
- Console.WriteLine($"Class: {myClass.property}\nStruct: {myStruct.property}");
- //Output: Class: 420, Struct: 77
- }
- public static void DoStuff(int value, ref int reference)
- {
- value = 69;
- reference = 6969;
- }
- public static void DoOtherStuff(Reference aClass, Value aStruct)
- {
- aClass.property = 420;
- aStruct.property = 1337;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement