C# 13 novidades

leandroats

Leandro Torres

Posted on November 9, 2024

C# 13 novidades

O Oleg Kyrylchuk fez um post no site dele sobre algumas novidades do C# 13. Vou reproduzir por aqui algumas para registrar.

🎯 Params Collections

/ Before C# 13
void Method1(params object[] args) { }

// C# 13
void Method2(params IList<object> args)
{ }

// C# 13
void Method3(params ICollection<object> args)
{ }

// C# 13
void Method4(params ReadOnlySpan<object> args)
{ }
Enter fullscreen mode Exit fullscreen mode

🎯 New Lock Object

public class OldLock
{
    private readonly object _lockObj = new();
    private static int sharedResource = 0;

    public void IncrementResource()
    {
        lock (_lockObj)
        {
            sharedResource++;
        }
    }
}
public class NewLock
{
    private readonly Lock _lockObj = new();
    private static int sharedResource = 0;

    public void IncrementResource()
    {
        using (_lockObj.EnterScope())
        {
            sharedResource++;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

🎯 New Escape Sequence

//OLD
string text = "\u001b[32mThis is green text\u001b[0m";
Console.WriteLine(text);

//NEW
string text = "\e[32mThis is green text\e[0m";
Console.WriteLine(text);
Enter fullscreen mode Exit fullscreen mode

🎯 Partial Properties And Indexers

public partial class Foo
{
    [GeneratedRegex("abc|def")]
    private static partial Regex AbcRegex { get; }

    public bool IsMatchAbc(string text)
        => AbcRegex.IsMatch(text);
}
Enter fullscreen mode Exit fullscreen mode

Links e Referências:

👉 Oleg Kyrylchuk
👉 What’s New in C# 13


Até a próxima! 👊
☕😊 Agora você pode apoiar comprando um café para mim

💖 💪 🙅 🚩
leandroats
Leandro Torres

Posted on November 9, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

C# 13 novidades
ptbr C# 13 novidades

November 9, 2024

if-else & early return
ptbr if-else & early return

October 26, 2024

Scripts em C#
ptbr Scripts em C#

September 14, 2021