Unsafe Code in C# Last Updated : 10 Apr, 2024 Summarize Comments Improve Suggest changes Share 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 M ManasiKirloskar Follow Improve Article Tags : Programming Language C# Similar Reads Introduction of Object Oriented Programming As the name suggests, Object-Oriented Programming or OOPs refers to languages that use objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc in programming. The main aim of OOP is to bind together the data and the functi 6 min read C# Tutorial C# (pronounced "C-sharp") is a modern, versatile, object-oriented programming language developed by Microsoft in 2000 that runs on the .NET Framework. Whether you're creating Windows applications, diving into Unity game development, or working on enterprise solutions, C# is one of the top choices fo 4 min read Introduction to .NET Framework The .NET Framework is a software development framework developed by Microsoft that provides a runtime environment and a set of libraries and tools for building and running applications on Windows operating systems. The .NET framework is primarily used on Windows, while .NET Core (which evolved into 6 min read C# Interview Questions and Answers C# is the most popular general-purpose programming language and was developed by Microsoft in 2000, renowned for its robustness, flexibility, and extensive application range. It is simple and has an object-oriented programming concept that can be used for creating different types of applications.Her 15+ min read C# Dictionary Dictionary in C# is a generic collection that stores key-value pairs. The working of Dictionary is quite similar to the non-generic hashtable. The advantage of a Dictionary is, that it is a generic type. A dictionary is defined under System.Collections.Generic namespace. It is dynamic in nature mean 5 min read C# List Class In C#, the List<T> class represents the list of objects that can be accessed by index. It comes under the System.Collections.Generic namespace. List class can be used to create a collection of different types like integers, strings, etc. List<T> class also provides the methods to search, 7 min read R Programming Language - Introduction R is a programming language and software environment that has become the first choice for statistical computing and data analysis. Developed in the early 1990s by Ross Ihaka and Robert Gentleman, R was built to simplify complex data manipulation and create clear, customizable visualizations. Over ti 4 min read C# Delegates A delegate is an object which refers to a method or you can say it is a reference type variable that can hold a reference to the methods. It provides a way which tells which method is to be called when an event is triggered. For example, if you click on a Button on a form (Windows Form application), 6 min read ASP.NET Interview Questions and Answer ASP.NET is a popular framework by Microsoft for building fast and scalable web applications. It allows developers to create dynamic websites, services, and apps, using server-side code and offering a user-friendly experience. Trusted by companies like Microsoft, Dell, and Accenture, ASP.NET is used 15+ min read Features of C Programming Language C is a procedural programming language. It was initially developed by Dennis Ritchie in the year 1972. It was mainly developed as a system programming language to write an operating system.The main features of C language include low-level access to memory, a simple set of keywords, and a clean style 3 min read Like