out parameters

In this chapter you will learn:

  1. Use out for int type
  2. Use out for reference type

Use out for int type

out is like ref modifier, but when calling the method you don't have to provide a value for the out parameter.

An out parameter is used to pass a value out of a method. It is not necessary to give the variable used as an out parameter an initial value. An out parameter is always considered unassigned. The method must assign the parameter a value prior to the method's termination.

using System;// ja  v a  2s. c  om
class Point
{
    public Point(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
    
    public void GetPoint(out int x, out int y)
    {
        x = this.x;
        y = this.y;
    }
    
    int x;
    int y;
}

class MainClass
{
    public static void Main()
    {
        Point myPoint = new Point(10, 15);
        int x;
        int y;
        
        myPoint.GetPoint(out x, out y);
        Console.WriteLine("myPoint({0}, {1})", x, y);
    }
}

The code above generates the following result.

Use out for reference type

using System;/*  ja v a  2 s  .c  om*/

class MyClass
{
   public int Val = 20;                     
}

class MainClass
{
   static void MyMethod(out MyClass f1, out int f2)
   {
      f1 = new MyClass();                   
      f1.Val = 25;                          
      f2 = 15;                              
   }

   static void Main()
   {
      MyClass myObject = null;
      int intValue;

      MyMethod(out myObject, out intValue);             

      Console.WriteLine("After  -- myObject.Val: {0}, intValue: {1}", myObject.Val, intValue);
   }
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Variable arguments to a method
  2. How to pass in array to a method with variable length parameter
  3. How to mix normal parameters and variable length parameter
Home » C# Tutorial » Class
Classes and Structs
Encapsulation
Class and Struct Accessibility
Members
Static Members
Class
Class instance variables
Class instance Methods
Class Inheritance
Class constructor
Default constructors
Parameters of constructors
Constructor overloading
Static Constructor
Destructor
Override destructor
new operator
Class field
Static Fields
Read only field
Field default value
const value
Abstract class
Static Class
Partial type
Methods
Method local variable
Method Overloading
Method overloading with ref and out
Type conversion and method overloading
Error in Method overloading
virtual methods
Override methods
Method Parameter
Pass parameters
ref parameters
out parameters
Variable parameter length
Optional parameters
Named parameters
static method
function Return
Recursive methods
Virtual methods and polymorphism
Cast
Shadow inherited members
Seal a member
this keyword
base keyword
base and name hiding
Access Specifiers
private
public
protected
internal
Indexer
String type indexer
Multidimensional indexer
Indexer overloading
Indexer to append
Indexer logic
Properties
Properties getter and setter
Accessibilities of properties
Automatic properties
Read only and write only property
Virtual properties
Abstract Properties
Static Properties
interface
interface implementation
Explicit interface implementation
Interface inheritance
Indexer in an interface
Interface Properties
struct and interface
virtual interface implementation
as operator
is operator
null reference
Nested classes
object class
ToString method
GetHashCode
Object initialization
Extension method
Struct Instances vs. Class Instances