Unsafe Code in C# Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report Unsafe code in C# is the part of the program that runs outside the control of the Common Language Runtime (CLR) of the .NET frameworks. The CLR is responsible for all of the background tasks that the programmer doesn't have to worry about like memory allocation and release, managing stack etc. Using the keyword "unsafe" means telling the compiler that the management of this code will be done by the programmer. Making a code content unsafe introduces stability and security risks as there are no bound checks in cases of arrays, memory related errors can occur which might remain unchecked etc. A programmer can make the following sub-programs as unsafe: Code blocksMethodsTypesClassStructNeed to use the unsafe code? When the program needs to implement pointers.If native methods are used.Syntax: unsafe Context_declarationExample: Here, we are declaring a block of code inside main as unsafe so that we can use pointers. csharp // C# program to demonstrate the unsafe code using System; namespace GFG { class Program { // Main Method static void Main(string[] args) { // Declaring a code block as // unsafe to make use of pointers unsafe { int x = 10; int* ptr; ptr = &x; // displaying value of x using pointer Console.WriteLine("Inside the unsafe code block"); Console.WriteLine("The value of x is " + *ptr); } // end unsafe block Console.WriteLine("\nOutside the unsafe code block"); } // end main } } Note: This code will not compile directly, it gives the following error. Therefore, if you are using Visual Studio, then you need to follow the given steps: 1) Go to the project properties 2) Select the build option and check the "Allow unsafe code" option. Output: Comment More infoAdvertise with us Next Article Collections in C# M ManasiKirloskar Follow Improve Article Tags : Programming Language C# Similar Reads Nested Classes in C# A class is a user-defined blueprint or prototype from which objects are created. Basically, a class combines the fields and methods (member function which defines actions) into a single unit. In C#, a user is allowed to define a class within another class. Such types of classes are known as nested c 7 min read Collections in C# .math-table { border-collapse: collapse; width: 100%; } .math-table td { border: 1px solid #5fb962; text-align: left !important; padding: 8px; } .math-table th { border: 1px solid #5fb962; padding: 8px; } .math-table tr>th{ background-color: #c6ebd9; vertical-align: middle; } .math-table tr:nth-c 5 min read Managed and Unmanaged Code in .NET In .NET, the terms "Managed Code" and "Unmanaged Code" refer to how the code is executed and managed by the system. Managed code runs under the control of the CLR(Common Language Runtime), while unmanaged code runs directly on the operating system. Each type of code has its own advantages and is use 3 min read Multicast Delegates in C# A type-safe function pointer is a delegate. It means that the delegate contains a reference to a method or function, and that when we invoke the delegate, the method to which it refers will be executed. The delegate signature and the method to which it points must both be signed. The method must be 3 min read Console.Read() Method in C# Console.Read() Method is used to read the next character from the standard input stream. This method basically blocks its return when the user types some input characters. As soon as the user press ENTER key it terminates. Syntax: public static int Read (); Return Value: It returns the next characte 1 min read C# Exceptions An exception in C# is an unwanted or unexpected event that occurs at runtime. It affects the normal flow of the program. Common exceptions include invalid input, divide-by-zero operations, or accessing invalid array indices. C# provides a powerful exception-handling mechanism to successfully recover 4 min read Like