Telerik blogs
DotNetT Light_870x220

The next major version release of the C# language (C# 8) comes with exciting new features. Let's look at two new features in C# 8, static local functions and the using declaration.

In this post, we'll explore the new static local functions and the using declaration. If you want to try out these features, you’ll need to install .NET Core 3 and set your project to use C# 8. I talked about how to do those in my previous post.

Static Local Functions

Local functions enable you to declare methods inside the context of another method. This makes it easier for readers of the class to know that the local method is only called from the context in which it is declared. This gives you a better place to write methods that are called from only one location and to write code that looks like this:

public string MethodWithLocalFunction()
  string firstname = "Hiccuo";
  string lastname = "Mauritius";
  return LocalFunction();
  string LocalFunction() => firstname + " - " + lastname;

Local functions automatically capture the context of the enclosing scope to make any variables from the containing method available inside them. This feature was added in C# 7.0, and in C# 8, you can declare a local function as static by adding the static modifier. By adding this, you ensure the local function doesn’t reference any variables from the enclosing scope. By adding this to the code snippet from before, you’ll hereby pass firstname and lastname as parameters.

public string MethodWithLocalFunction()
  string first = "Hiccuo";
  string last = "Mauritius";
  return LocalFunction(first, last);
  static string LocalFunction(string firstname, string lastname) => firstname + " - " + lastname;

Using Declaration

The using statement in C# provides a convenient syntax that ensures the correct use of IDisposable objects. C# 8 gives you an alternative which is using declaration. A using declaration is a variable declaration preceded by the using keyword. When the variable goes out of scope (i.e. the containing block of code is exited), the Dispose method will automatically be called for that object. The following example reads a file and uses the using declaration.

void ReadFile(string path)
  using FileStream fs = File.OpenRead(path); // using declaration
  byte[] b = new byte[1024];
  UTF8Encoding temp = new UTF8Encoding(true);
  while (fs.Read(b,0,b.Length) > 0)
  // fs is disposed here

In this example, the variable fs is disposed when the closing brace of the method is reached.


C# 8 comes with interesting features, and, in this post, I wrote about using declaration and static local function. The using declaration allows you to declare variables preceded by the using keyword and the compiler will generate code to call Dispose() when the object gets out of scope. The static keyword can now be added to a local function declaration, making it such that any variable from the enclosing scope cannot be referenced inside the local function. These are interesting features coming in C# 8.

Peter Mbanugo
About the Author

Peter Mbanugo

Peter is a software consultant, technical trainer and OSS contributor/maintainer with excellent interpersonal and motivational abilities to develop collaborative relationships among high-functioning teams. He focuses on cloud-native architectures, serverless, continuous deployment/delivery, and developer experience. You can follow him on Twitter.


Comments are disabled in preview mode.