Home DEVELOPER New class for lock statement in .NET 9.0 (10): C# 13.0

New class for lock statement in .NET 9.0 (10): C# 13.0

0


There are new classes to lock the code block before access by other threads from .NET 9.0/c# 13.0 System.Threading.LockWho are now in standard in relation to you lockIn C# -Stetment should use “for best performance” Like Microsoft in Documentation Write

Advertisement





Dr. Holder Schwichtenberg Technical Manager of the Network of experts www.it-visions.de, which supports many medium sizes and large companies through advice and training with 53 renowned experts as well as software development. Through his appearance in many national and international expert conferences as well as his appearance in more than 90 expert books and more than 1,500 expert articles, Holgar Schwichtanberg is one of the most famous experts for .NET and web techniques in Germany.

CLC 2025: Developer Experience and Lecture on Platform Engineering Wanted

Low code C#from documentation on learning microsoft Shows an example with keywords lock And square System.Threading.Lock,

using System;
using System.Threading.Tasks;

namespace NET9_Console.CS13;

public class Account
{
 // Vor .NET 9.0/C# 13.0 wurde hier System.Object verwendet statt 
 // System.Threading.Lock 
 private readonly System.Threading.Lock _balanceLock = new();
 private decimal _balance;
 
 public Account(decimal initialBalance) => _balance = initialBalance;
 
 public decimal Debit(decimal amount)
 {
  if (amount < 0)
  {
   throw new ArgumentOutOfRangeException(nameof(amount), "The debit amount cannot be negative.");
  }
 
  decimal appliedAmount = 0;
  lock (_balanceLock)
  {
   if (_balance >= amount)
   {
    _balance -= amount;
    appliedAmount = amount;
   }
  }
  return appliedAmount;
 }
 
 public void Credit(decimal amount)
 {
  if (amount < 0)
  {
   throw new ArgumentOutOfRangeException(nameof(amount), "The credit amount cannot be negative.");
  }
 
  lock (_balanceLock)
  {
   _balance += amount;
  }
 }
 
 public decimal GetBalance()
 {
  lock (_balanceLock)
  {
   return _balance;
  }
 }
}
 
class AccountTest
{
 static async Task Main()
 {
  var account = new Account(1000);
  var tasks = new Task(100);
  for (int i = 0; i < tasks.Length; i++)
  {
   tasks(i) = Task.Run(() => Update(account));
  }
  await Task.WhenAll(tasks);
  Console.WriteLine($"Account's balance is {account.GetBalance()}");
  // Output:
  // Account's balance is 2000
 }
 
 static void Update(Account account)
 {
  decimal() amounts = (0, 2, -3, 6, -2, -1, 8, -5, 11, -6);
  foreach (var amount in amounts)
  {
   if (amount >= 0)
   {
    account.Credit(amount);
   }
   else
   {
    account.Debit(Math.Abs(amount));
   }
  }
 }
}

C#-13.0 compiler produces then

lock (_balanceLock)
{
  _balance += amount;
}

A call for EnterScope()Mithod in class System.Threading.Lock,

using (balanceLock.EnterScope())
{
  _balance += amount;
}


(RME)

Salesforce Boss: We will not appoint new developers this year, AI is enough

NO COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exit mobile version