Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge
  • All
  • C#
  • MVC
  • Web API
  • Azure
  • IIS
  • JavaScript
  • Angular
  • Node.js
  • Java
  • Python
  • SQL Server
  • SEO
  • Entrepreneur
  • Productivity

Out keyword in C#

The out keyword can be used with variables and method parameters. The out paramters are always passed by reference for both, the value type and the reference type data types.

Declare Method with Out Parameter

The out keyword can be used with the variable declaration or method parameters.

Syntax:
out <data type> <variable name>; <method name>(out <data type> <parameter name>)

The following example demonstrates the method declaration with out parameters.

Example: Method with out Parameter
public static void OutParamExample(out int x){
    x = 100;
}

The above example defines the OutParamExample() method with one out parameter x. The out keyword is applied before the type and name of a parameter.

Calling Method with Out Parameter

The variable must be declared without initializing before calling a method that includes the out parameters. Also, while calling the method, a variable must be passed with out keyword.

Example: out Parameter
int a; // declare variable without initialization

OutParamExample(out a);// calling method with out keyword

Console.Write(a);// accessing out parameter value

public static void OutParamExample(out int x){
    x = 100;
}
Try it

C# 7 introduced a new way of declaring the out parameters. In C# 7 onwards, you don't need to declare out variable before passing to parameters. It can now be declared while calling the method.

Example: out Parameter in C# 7
OutParamExample(out int a);// declare out variable while calling method
Console.Write(a);// accessing out parameter value

public static void OutParamExample(out int x){
    x = 100;
}

When to use out parameters?

The out parameters can be used when you want to return more than one values from the method.

The following example demonstrates getting two random numbers from the single method call.

Example: out Keyword Usage
public static void GetMultipleRandomValue(out int x, out int y)
{
    var random = new Random();
    x = random.Next();
    y = random.Next();
}

public static void Main()
{
    int random1, random2;
    GetMultipleRandomValue(out random1, out random2);

    Console.WriteLine($"{random1}, {random2}");
}
Try it

The out parameters can be used to remove the possibility of the return null value. C# uses it effectively in built-in TryParse methods. C# has Parse() and TryParse() methods for int, float, char, and bool data types. The difference between Parse() and TryParse() methods are that the Parse() method can throw an exception whereas TryParse() method will never throw an exception because it uses out parameter to which the valid value will be assigned if successful. Check how the TryParse() method uses out parameter while converting string to int.

TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.