Before C# 7, for calling a method with out parameters, we were required to declare variables to pass to it. Now with C# 7 out parameters enhancements (out variables):

we can declare a variable right at the point where it is passed as an out argument we can use var instead of a type to declare out variables we can ignore out parameters that are not required

Below are some examples to explains the use of C# 7’s out variables (out parameters enhancements):

Before C# 7: Sample code to explain the use of out parameters in older versions of C#

public static class CSharp6Sample { /// <summary> /// Input String is valid or not. /// </summary> /// <param name="value">Input string.</param> /// <returns>true or false.</returns> public static bool IsValidInputStringWithErrorLogging(string value) { string errorMessage; bool valid = CSharp6Sample.HasValue(value, out errorMessage); if (valid == false) { Console.Write(errorMessage); } return valid; } /// <summary> /// Input String is valid or not. /// </summary> /// <param name="value">Input string.</param> /// <returns>true or false.</returns> public static bool IsValidInputStringWithoutErrorLogging(string value) { string errorMessage; bool valid = CSharp6Sample.HasValue(value, out errorMessage); // out parameter cannot be ignored. return valid; } /// <summary> /// Input string has value or not. /// </summary> /// <param name="value">Input string.</param> /// <param name="errorMessage">Error Message (out parameter).</param> /// <returns>true or false.</returns> public static bool HasValue(string value, out string errorMessage) { errorMessage = string.Empty; if (value == null) { errorMessage = "Value is null."; } else if (value.Equals(string.Empty)) { errorMessage = "Value is empty."; } else if (value.Trim().Equals(string.Empty)) { errorMessage = "Value contains only whitespaces."; } return string.IsNullOrEmpty(errorMessage); } }

Modifications to the above sample code to use C# 7’s out variables (out parameters enhancements):

Inside IsValidInputStringWithErrorLogging method, we have declared errorMessage variable right at the point where it is passed as an out argument to HasValue method.

method, we have declared variable right at the point where it is passed as an out argument to method. Inside IsValidInputStringWithErrorLogging method , var is used instead of a type to declare out errorMessage variable.

method is used instead of a type to declare out variable. Inside IsValidInputStringWithoutErrorLogging method, we don’t require out parameter (errorMessage) value from HasValue method, so we have ignored the out parameter.

Note the highlighted changes in the below code.

public class CSharp7Sample { /// <summary> /// Input String is valid or not. /// </summary> /// <param name="value">Input string.</param> /// <returns>true or false.</returns> public static bool IsValidInputStringWithErrorLogging(string value) { bool valid = CSharp7Sample.HasValue(value, out var errorMessage); // use of var instead of a type to declare out variables if (valid == false) { Console.Write(errorMessage); } return valid; } /// <summary> /// Input String is valid or not. /// </summary> /// <param name="value">Input string.</param> /// <returns>true or false.</returns> public static bool IsValidInputStringWithoutErrorLogging(string value) { bool valid = CSharp7Sample.HasValue(value, out _); // Ignore out parameter that are not required. return valid; } /// <summary> /// Input string has value or not. /// </summary> /// <param name="value">Input string.</param> /// <param name="errorMessage">Error Message (out parameter).</param> /// <returns>true or false.</returns> public static bool HasValue(string value, out string errorMessage) { errorMessage = string.Empty; if (value == null) { errorMessage = "Value is null."; } else if (value.Equals(string.Empty)) { errorMessage = "Value is empty."; } else if (value.Trim().Equals(string.Empty)) { errorMessage = "Value contains only whitespaces."; } return string.IsNullOrEmpty(errorMessage); } }

Few more examples:

Before C# 7: Some custom String Extensions sample code (note the use of out parameters) in older versions of C#

public static class StringExtensions { /// <summary> /// Converts string to DateTime. /// </summary> /// <param name="dateString">Date string.</param> /// <param name="defaultValue">Default value.</param> /// <returns>Converted DateTime.</returns> public static DateTime ToDate(this string dateString, DateTime defaultValue) { DateTime date; bool result = DateTime.TryParse(dateString, out date); return result ? date : defaultValue; } /// <summary> /// Converts string to Enum. /// </summary> /// <typeparam name="TEnum">Enum Type</typeparam> /// <param name="value">String value.</param> /// <param name="defaultValue">Default value</param> /// <returns>Converted Enum.</returns> public static TEnum ToEnum<TEnum>(this string value, TEnum defaultValue) where TEnum : struct { TEnum outputValue; bool result = Enum.TryParse(value, out outputValue); return result ? outputValue : defaultValue; } }

Modifications to String Extensions sample code to use C# 7’s out variables (out parameters enhancements):

Note the highlighted changes in the below code.

public static class StringExtensions { /// <summary> /// Converts string to DateTime. /// </summary> /// <param name="dateString">Date string.</param> /// <param name="defaultValue">Default value.</param> /// <returns>Converted DateTime.</returns> public static DateTime ToDate(this string dateString, DateTime defaultValue) { bool result = DateTime.TryParse(dateString, out DateTime date); return result ? date : defaultValue; } /// <summary> /// Converts string to Enum. /// </summary> /// <typeparam name="TEnum">Enum Type</typeparam> /// <param name="value">String value.</param> /// <param name="defaultValue">Default value</param> /// <returns>Converted Enum.</returns> public static TEnum ToEnum<TEnum>(this string value, TEnum defaultValue) where TEnum : struct { bool result = Enum.TryParse(value, out TEnum outputValue); return result ? outputValue : defaultValue; } }

Happy Coding !!!