CA2202, how to solve this case

后端 未结 12 957
闹比i
闹比i 2020-11-22 08:18

Can anybody tell me how to remove all CA2202 warnings from the following code?

public static byte[] Encrypt(string data, byte[] key, byte[] iv)
{
    us         


        
12条回答
  •  隐瞒了意图╮
    2020-11-22 08:56

    Off-topic but I would suggest you to use a different formatting technique for grouping usings:

    using (var memoryStream = new MemoryStream())
    {
        using (var cryptograph = new DESCryptoServiceProvider())
        using (var encryptor = cryptograph.CreateEncryptor(key, iv))
        using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
        using (var streamWriter = new StreamWriter(cryptoStream))
        {
            streamWriter.Write(data);
        }
    
        return memoryStream.ToArray();
    }
    

    I also advocate using vars here to avoid repetitions of really long class names.

    P.S. Thanks to @ShellShock for pointing out I can't omit braces for first using as it would make memoryStream in return statement out of the scope.

提交回复
热议问题