.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month

Home >> Code Snippets >> OOPS >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Unsafe in C#

Posted By:nishithraj       Posted Date: November 27, 2009    Points: 10    Category: OOPS    URL: http://www.dotnetspark.com  

Unsafe in C#.

We must have worked with pointers in C. The main drawback of pointers is it's security issue. Csharp provides a simillar way of coding with the keyword "unsafe". The name itself is a self explanatory. But some scenarios we need to work with the address of some operators mean we can go for unsafe coding. Make sure that you are not accessing any sensitive or confidential data here.

public class UnsafeCode{
//declaring variables with unsafe keywords
    public static unsafe void GetValues(int* x, int* y) {
        *x = 6;
        *y = 42;
    public static  unsafe void Main(){
        int a = 1;
        int b = 2;
        Console.WriteLine("Before GetValues() : a = {0}, b = {1}",  a, b);
//Pointing towards the address of the variables
        GetValues(&a, &b);
        Console.WriteLine("After GetValues() : a = {0}, b = {1}", a, b);


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend