New C sharp 7 features in action: out variables to make using out parameters smooth
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.
- Inside IsValidInputStringWithErrorLogging method, var is used instead of a type to declare out errorMessage 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 !!!
.NET Professional | Microsoft Certified Professional | DZone’s Most Valuable Blogger | Web Developer | Author | Blogger
Doctorate in Computer Science and Engineering
Microsoft Certified Professional (MCP) with over 12+ years of software industry experience including development, implementation & deployment of applications in the .NET framework
Experienced and skilled Agile Developer with a strong record of excellent teamwork, successful coding & project management. Specialises in problem identification and proposal of alternative solutions. Provided knowledge and individual mentoring to team members as needed
Among top 3% overall in terms of contribution on Stack Overflow (~2.3 million people reached my posts). Part of the top 1% Stack Overflow answerers in ASP.NET technology.
DZone’s Most Valuable Blogger (MVB)
Created and actively maintain the TechCartNow.com tech blog while also editing, writing, and researching topics for publication.
Excellent skills in Application Development using C#/Vb.Net, .NET Framework, ASP.NET, MVC, ADO.NET, WCF, WPF, Web API, SQL Server, jQuery, Angular, React, BackboneJS