Mega Code Archive

 
Categories / C# Book / 01 Language Basics
 

0104 ref and out modifiers and method overloading

ref and out modifiers are part of the method signature. class Math { int add(int i, int j) { return i + j; } int add(ref int i, ref int j){ return i + j; } } ref and out cannot coexist class Math { int add(out int i, out int j) { return i + j; } int add(ref int i, ref int j){ return i + j; } } Compile time error: C:\g>csc Program.cs Microsoft (R) Visual C# 2010 Compiler version 4.0.30319.1 Copyright (C) Microsoft Corporation. All rights reserved. Program.cs(9,9): error CS0663: Cannot define overloaded method 'add' because it differs from another method only on ref and out Program.cs(5,9): (Location of symbol related to previous error)