Introducing C# 9: Attributes on Local Functions

Introducing C# 9: Attributes on Local Functions

As you might have guessed C# 9 now allows attributes on local functions (as well as parameter attributes of local functions). I will show you how to use the Conditional attribute to perform conditional local function executions, for example, depending on the environment in which we are running our program.

Introduction

Well. It has been a while since I wanted to write this post but I couldn’t find any relevant code example to illustrate it. As you might have guessed C## 9 now allows attributes on local functions (as well as parameter attributes of local functions). To illustrate this I will show you how to use the Conditional attribute to perform conditional local function executions, for example, depending on the environment in which we are running our program. Applying it to local functions can be interesting in this case, however I admit that for the moment it is the only relevant example but it does not matter it is not necessarily the functionality of C## 9 either. more popular now. Finally we will compare with the C## 9 forward to finish our example.

Before C## 9

In this example we’ll see how to execute a local function regarding the execution environment: Let’s consider we are running an action only in DEBUG, we have to run it with the #if preprocessor

using System;
using System.Diagnostics;

namespace CSharp9Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            static void DoAction()
            {
                // Perform action

                Console.WriteLine("Performing action");
            }

#if DEBUG
            DoAction();
#endif
        }
    }
}

We cannot use here the attribute [Conditional(“DEBUG”)] because C## 8 and lower don’ support it on local functions. Too bad! 🙁

With C## 9

Here we are! C## 9 do support attributes on local function, let’s rewrite the previous code using C## 9:

using System;
using System.Diagnostics;

namespace CSharp9Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            [Conditional("DEBUG")]
            static void DoAction()
            {
                // Perform action

                Console.WriteLine("Performing action");
            }

            DoAction();
        }
    }
}

csharp programming developer

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How long does it take to develop/build an app?

This article covers A-Z about the mobile and web app development process and answers your question on how long does it take to develop/build an app.

Developer Career Path: To Become a Team Lead or Stay a Developer?

For a developer, becoming a team leader can be a trap or open up opportunities for creating software. Two years ago, when I was a developer, ... by Oleg Sklyarov, Fullstack Developer at Skyeng company

Tracking a Developer’s Journey From Documentation Visit

Measuring website activity provides only half the story. See how to best track the developer's journey and what funnel stages makes sense for API-first products

Offshore Software Development - Best Practices

To make the most out of the benefits of offshore software development, you should understand the crucial factors that affect offshore development.

Hire Ruby on Rails Developer | Hire RoR Developer

#1 Ruby on Rails development company. Hire Ruby on rails developer or a team to build secure, scalable and complex web solutions with a quick turnaround time.