Introducing C# 9: Attributes on Local Functions

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

What is GEEK

Buddha Community

Introducing C# 9: Attributes on Local Functions
Tamale  Moses

Tamale Moses

1620179880

Use local function attributes with C# 9

If you look at what’s new in C## 9, you’ll see records, init-only properties, and top-level statements get all the glory. And that’s fine, because they’re great. At the end of the bulleted list, I noticed support for local attributes on local functions.

When it comes to local functions, with C## 9 you can apply attributes to function declarations, parameters, and type parameters.

The most common use case I’ve seen is using the ConditionalAttribute. This attribute is used for checking conditional compilation symbols—instead of using #define DEBUG regions, for example, you can do it here. You can declare multiple attributes, just like for a “normal” method. For example:

#c# 9 #c# #function #attributes

Loma  Baumbach

Loma Baumbach

1603381251

Introducing C# 9: Static anonymous functions

Introduction

C## 9 brings an important improvement to anonymous functions by allowing the modifier static on them and we now have static anonymous functions ! Why Microsoft brought this feature ? Because allocation matters ! Microsoft explains here that lambda are not cost less (https://devblogs.microsoft.com/premier-developer/dissecting-the-local-functions-in-c-7/) :

“Anonymous methods are not cheap:”

  • Overhead of a delegate invocation (very very small, but it does exist).
  • 2 heap allocations_ if a lambda captures local variable or argument of enclosing method (one for closure instance and another one for a delegate itself)._
  • 1 heap allocation_ if a lambda captures an enclosing instance state (just a delegate allocation)._
  • 0 heap allocations_ only if a lambda does not capture anything or captures a static state._

#c# #.net 5 #c# 9 #static anonymous function #c++

Let's Give Some Unit Testing Love to C# 8 and C# 9 Features

According to StackOverflow, C## is one of the most-loved programming languages. And I completely understand that—it is powerful, easy to learn and consistently improving and developing. It is a living language. :)

The last couple of years, there were new features added to the languages, and the new versions keep coming up—C## 7, C## 8, C## 9.

As you know, we at Progress Telerik are proud that our products are always in sync with the latest things in the .NET world, and C## 9 and JustMock are no exception.

#c #c# #c#8 #c#9

Ari  Bogisich

Ari Bogisich

1590565090

A Guide to using the strdup() function in C/C++

In this article, we’ll take a look at using the strdup() function in C/C++.

The strdup() function is very useful if you want to duplicate the contents of a string onto another string.

Let’s see how we can utilize this function, using some simple examples.

#c programming #c++ #c #c#

Tamale  Moses

Tamale Moses

1624240146

How to Run C/C++ in Sublime Text?

C and C++ are the most powerful programming language in the world. Most of the super fast and complex libraries and algorithms are written in C or C++. Most powerful Kernel programs are also written in C. So, there is no way to skip it.

In programming competitions, most programmers prefer to write code in C or C++. Tourist is considered the worlds top programming contestant of all ages who write code in C++.

During programming competitions, programmers prefer to use a lightweight editor to focus on coding and algorithm designing. VimSublime Text, and Notepad++ are the most common editors for us. Apart from the competition, many software developers and professionals love to use Sublime Text just because of its flexibility.

I have discussed the steps we need to complete in this blog post before running a C/C++ code in Sublime Text. We will take the inputs from an input file and print outputs to an output file without using freopen file related functions in C/C++.

#cpp #c #c-programming #sublimetext #c++ #c/c++