---
title: "UNSAFE CODE in C#"  
description: "The C# language has omitted pointers as a data type. C#instead provides references and the ability to create objects that are managedby a garbage coll"  
author: "Amit Singh"  
published: 2011-02-12  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/115/unsafe-code-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# UNSAFE CODE in C#

The [C# language](https://www.mindstick.com/forum/157570/how-to-use-face-recognition-in-c-sharp-language-for-attendance-register-software) has omitted [pointers](https://www.mindstick.com/articles/1811/objective-c-pointers) as a data type. C#instead provides references and the ability to create objects that are managedby a [garbage collector](https://www.mindstick.com/forum/158204/what-are-the-benefits-and-drawbacks-of-using-a-garbage-collector-for-memory-management). This design contributes in making C# a much saferlanguage than C or C++.These [categories](https://answers.mindstick.com/qa/47601/what-are-the-categories-of-stations-for-provision-of-passenger-amenities-explain-them) are main cause of bugs in C and C++programs, so are eliminated in c#.

In [unsafe code](https://www.mindstick.com/forum/33851/what-is-unsafe-code-code-using-pointers-in-c-sharp) it is [possible to declare](https://www.mindstick.com/forum/23359/is-it-possible-to-declare-a-class-as-static-in-java) and operate onpointers, to perform conversions between pointers and integral types, to takethe address of variables.

##### Unsafe contexts

The unsafe [features](https://www.mindstick.com/articles/12993/5-advanced-features-for-your-odoo-ecommerce-theme) are available only in unsafe contexts.An unsafe context is introduced by including an unsafe modifier in thedeclaration.

· Adeclaration of a class, struct, [interface](https://www.mindstick.com/articles/12101/interfaces-in-java-extending-interfaces), or [delegate](https://www.mindstick.com/articles/777/delegate-and-event-in-c-sharp) may include an unsafemodifier, in which case the entire declaration (including the body of theclass, struct, or interface) is considered an unsafe context.

· A declaration of a field, method, property,event, indexer, operator, constructor, destructor, or static constructor mayinclude an unsafe modifier, in which case the entire member declaration isconsidered an unsafe context.

· An unsafe-statement enables the use of an unsafecontext within a block. The entire associated block is considered an unsafecontext.

##### Example:

```
public classSample{publicunsafe virtual void show() {char* p;//other codehere//}}
```

\
The unsafemodifier on the show method in sample class simply causes the show to become anunsafe context in which the unsafe features of the language can be used.

---

Original Source: https://www.mindstick.com/blog/115/unsafe-code-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
