Dec 13, 2017, 06:09 PM

News:

Kalyan @ IT Acumens enjoy running his Cinema Portal : CineBuzz - Latest Movie Updates


Unsafe Code in C#

Started by VelMurugan, Jan 27, 2009, 10:13 PM

previous topic - next topic
Go Down

VelMurugan

Unsafe Code in C#

Visual C# .NET allows you to step outside of the safe environment of managed code and write code that is considered "unsafe" by the CLR. Running unsafe code presents a certain set of restrictions in exchange for opening up possibilities, like accessing memory-mapped data or implementing time-critical algorithms that use pointers directly.

These restrictions are mainly based in the CAS system of the CLR and are in place to draw a distinct line between code the CLR knows to be playing by the rules (or "safe") and code that needs to do a bit outside of the traditional sandbox of the CLR (and is thus "unsafe" code).

In order to run code that is marked as unsafe by the CLR, you need to have the CAS SkipVerification privilege granted to the assembly where the unsafe code is implemented. This tells the CLR to not bother verifying the code and to allow it to run, whereas normally unverified code would not run.

This is a highly privileged operation and is not to be done lightly, as you increase the permissions your application will require in order to operate correctly on a user's system.

If you use unsafe types in a method signature, you also make the code non-CLS-compliant. This means that interoperability with other .NET-based languages, like VB.NET or Managed C++, for this assembly is compromised.

Even though unsafe code allows you to easily write potentially unstable code, it does have several safeguards. You can create only pointers to value types or value types inside of reference types; you cannot create pointers to reference types.

This forces pointer types to be created solely on the stack, so you do not have to use the new and delete operations to allocate and release the memory to which the variable points.

You only have to wait for the method that declared the pointer type to return, forcing the pointer to go out of scope and clearing any stack space devoted to this method.

You can get into a bit of trouble if you are doing exotic things with unsafe code, such as pointing to a value type inside of a reference type. This behavior allows access to heap-based memory, thereby opening up the possibility for pointer pitfalls, such as those seen in C++.

Source : csharpthegreat

dhoni

this code is very useful to able in information
this gives more details .net with unsafe code

newwaysys

C# allows using pointer variables in a function of code block when it is marked by the unsafe modifier. The unsafe code or the unmanaged code is a code block that uses a pointer variable.
using System;

class Program
{
   unsafe static void Main()
   {
fixed (char* value = "sam")
{
   char* ptr = value;
   while (*ptr != '\0')
   {
Console.WriteLine(*ptr);
++ptr;
   }
}
   }
}

Output

s
a
m
more: http://sarkarinaukriexam.org

Go Up
 

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.
Name:
Email:
Verification:
Please leave this box empty:

Type the letters shown in the picture
Listen to the letters / Request another image

Type the letters shown in the picture:

shortcuts: alt+s submit/post or alt+p preview
IT Acumens | GinGly :: SMS Backup | Acumen :: Discussion Board | AshokPillar :: Hosting | CineBuzz :: Latest Cinema News | My Kids Diary :: Capture your kids magical moment
Copyright 2005 - 2017 :: IT Acumens :: All Rights Reserved.
ITAcumens Forum with 2 lakhs post running for 10 years - Powered by HostGator Dedicated Server