Introduction:
In this article, i am going to explain about how to use/perform the c# unsafe code option in asp.net application.
Main:
The unsafe is nothing but the code is straighly accessing the memory.In c# we are using unsafe keyword for defining
unsafe code.
Memory is accessed in C# using a special data type called a pointer. A pointer is a variable whose value points to
a specific memory address. A pointer is declared in C# with an asterisk placed between the pointer’s type and its identifier,
For ex,if you wants to retrieve the memory address of data by using pointers,we can go with unsafe,
we can access windows API’s and third party dll’s using unsafe code,but don’t forget unsafe code is very complex
and very harder to debug.
for ex,
int MInteger = 123;
int * MIntegerPointer = &MyInteger;
It declares an integer variable called MInteger and assigns a value of 123 to it.
It declares an integer pointer called MIntegerPointer and points it to the address of the MyInteger variable.
using System;
public class UnsafeClass
{
public unsafe static void Main()
{
int MInteger = 123;
int * MIntegerPointer = &MInteger;
Console.WriteLine(*MIntegerPointer);
}
}
using System; public class UnsafeClass { public unsafe static void Main() { int MInteger = 123; int * MIntegerPointer = &MInteger; Console.WriteLine(*MIntegerPointer); } } |
If you wants to combile the unsafe code in command prompt,
csc /unsafe sample.cs
Conclusion:
Hope this helps,
Happy Coding.
My cousin recommended this blog and she was totally right keep up the fantastic work!