Skip to content

Different behaviour between using-statement and using-declaration in C# 8.0 #4230

Discussion options

You must be logged in to vote

In my opinion, this may be re-written in C# 8.0 as follows,

Tihs is incorrect. Your rewritten form is equivalent to the broken code:

static byte[] EncryptStringToBytes_Aes(string plainText, byte[] Key, byte[] IV)
{
    // Check arguments.
    if (plainText == null || plainText.Length <= 0)
        throw new ArgumentNullException("plainText");
    if (Key == null || Key.Length <= 0)
        throw new ArgumentNullException("Key");
    if (IV == null || IV.Length <= 0)
        throw new ArgumentNullException("IV");
    byte[] encrypted;

    // Create an Aes object
    // with the specified key and IV.
    using (Aes aesAlg = Aes.Create())
    {
      aesAlg.Key = Key;
      aesAlg.IV = IV;

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by 333fred
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #4230 on December 14, 2020 23:50.