Avoid unsafe blocks

Metadata

ID: csharp-security/avoid-unsafe

Language: C#

Severity: Notice

Category: Security

Description

Avoid unsafe code blocks as much as possible. While unsafe blocks provide access to some important features of the C# language, you need to avoid using them as much as possible. For example, unsafe code allows developers to use pointers, but pointers and pointers arithmetic can lead to critical security issues. Unsafe code should be avoided or at least clearly identified in a small scope.

Learn More

Non-Compliant Code Examples

using System.IO;
using System.Security.Cryptography;

class MyClass {
    public void myMethod
    {
        unsafe{
            // statements
        }
       
    }
}
using System.IO;
using System.Security.Cryptography;

class MyClass {
    public unsafe void myMethod
    {
       // statements
    }
}

Compliant Code Examples

using System.IO;
using System.Security.Cryptography;

class MyClass {
    public void myMethod
    {
       // statements
    }
}