Shahid Riaz Bhatti

if(my.work == “Interesting” || my.availableTime > my.workHours) { this.blog.Post();}

Why to use unsafe in C# ?

September 25
by Shahid Riaz Bhatti 25. September 2008 15:19

Regarding my article "How to use Pointers in C#" http://www.shahidriaz.com/post/2008/09/22/Unsafe-code-in-Net.aspx alot of people started to ask that why I posted this article and why shoud we use unsafe in C# even some of said to me that I should close my web Frown

First of all I would like to say that in that article I never said that start using pointers and unsafe code in C#. The idea behind was just for the beginners because most of the people think that we can not use pointers in C#.

Now here I am mentioning the scenarios where using pointers is more suitable as compared to other techniques.

What should I do if I am using some external DLL which is not being developed in .Net. Well some will say that creating a RCW (Run time callable wraper) is the solution of using non. Net DLL in .Net. I agree with them, but what if that RCW has a function in it which is taking some pointer as parameter?? I am sure that all of you will find the answer of their question that why and when to use pointers in C#.

I think that if some one is using the non .Net dll in their application then the above scenario will be halpfull for him.

Now lets talk about some other cases where we have to use pointers and unsafe keyword in C#.

Some old third parties control still use pointers. So again we have to use pointers in C# to address those third parties.(e.g. Most of the SDK of  Biomatric devices are in VC which used pointers extensivly, Windows API etc)

Well these all were my own thoughts because I though alot today becasue I was also speechless when people started to ask these question, but I was also sure that there will be some reason of unsafe in C# and that is why microsoft introduced it.

Now I have also done some googling and I am copying some of the lines that why to use pointers in C#.

  • Performance and flexibility, by using pointer you can access data and manipulate it in the most efficient way possible.
  • Memory Addresses, there is no way to know the memory address of some data without using pointers.

Disadvantages of Unsafe Code in C#:

  • Complex syntax, to use pointers you need to go throw more complex syntax than we used to experience in C#.
  • Harder to use, you need be more careful and logical while using pointers, miss using pointers might lead to the following:
    • Overwrite other variables.
    • Stack overflow.
    • Access areas of memory that doesn?t contain any data as they do.
    • Overwrite some information of the code for the .net runtime, which will surely lead your application to crash.
  • Your code will be harder to debug. A simple mistake in using pointers might lead your application to crash randomly and unpredictably.
  • Type-safety, using pointers will cause the code to fail in the .net type-safety checks, and of course if your security police don?t allow non type-safety code, then the .net framework will refuse to execute your application.

Sorry for my poor english..

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

C# | Tips and Tricks | Visual Studio

How to use unsafe code in C#

September 22
by Shahid Riaz Bhatti 22. September 2008 15:44

It is a very small and basic article showing that how can we use pointers in C#. I am posting this article because most of the people think that C# does not allow the use of pointers. In this example I will create a small function which will changes the value of a variable using the Pointers. So lets start:

First of all make a simple console application. Copy the following function:  

unsafe private static void UseUnsafeCode()

{

int count = 10;

int* p;

p = &count;

System.Console.WriteLine("Value of count before changing=" + count.ToString());

*p = 25;

System.Console.WriteLine("Value of count after changing=" + count.ToString());

System.Console.ReadLine();

}

Now call the above function in the main program.

Please make sure to allow the compilation of safe code in the property of the project.

The sample code will look something like this:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ConsoleApplication1

{

class Program

{

public static void Main(string[] args)

{

UseUnsafeCode();

}

unsafe private static void UseUnsafeCode()

{

int count = 10;int* p;

p = &count;

System.
Console.WriteLine("Value of count before changing=" + count.ToString());

*p = 25;

System.Console.WriteLine("Value of count after changing=" + count.ToString());System.

Console.ReadLine();

}

}

}

In the above example I have made a function in which I have declared two variables. The first one is count and its data type is int and the second is an int pointer. Also in the code I have changed the value of count variable using the pointer p variable. The output is given below:

Value of count before changing = 10

Value of count after chaning = 25

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,

C# | General | Random Thoughts | Tips and Tricks | Visual Studio

A tutorial on WCF is Comming Soon

September 22
by Shahid Riaz Bhatti 22. September 2008 13:45

 

Hi all,

Book mark this page. I am currently writing a Jump start tutorial on Windows Communication foundation (WCF). At the end of this week i.e. on 28 Sep, 2008 It would be uploaded.

Some brief introduction of the WCF is given below.

WCF stands for Windows communication foundation. WCF was introduced by Microsoft in .Net 3.0 framework. It is a brand new API which allows to build the distrubuted application regradless of their underlying plumbing, in a symmetrical manner.

WCF enables a developer to expose services to caller using wide variety of techniques and It is the huge advantage of WCF because prior to WCF if you developed an application using Remoting and later you came to know that XML web services is more suitable choice then you have to re-engineer your code.

Note:

I am writing just a lesson code. In that tutorial em not gona write the production code..

Keep in touch

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

ASP.Net | ASP.Net | C# | C# | Visual Studio | Visual Studio | WCF | WCF

RecentComments

Comment RSS

Most comments

supplynflshop supplynflshop
51 comments
tiffany-bracelets tiffany-bracelets
39 comments
AVI to iPad AVI to iPad
36 comments