Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

Unsafe Keyword

Can we use pointer in C# this question frequently asked by beginner level C# programmer, yes there are some situations we require that.

The unsafe keyword denotes an unsafe context, which is required for any operation involving pointers.

Pointers in C# we use with below situations:

1. Dealing with existing structures on disk
2.  Advanced COM or Platform Invoke scenarios that involve structures with pointers in them
3. Performance-critical code

Frequently this is used with unmanaged code in C# , for example you want to write code for image data.

Syntax:

unsafe
{
 
// unsafe context: can use here unmanaged code  or pointer code.
 
}

Method:

unsafe static void sammpleMethod(unmanaged paramter 1,unmanaged parameter2,unmanaged paramter n..)
{
   // Unsafe context: can use pointers here.
}

Example:

namespace UnsafeExample
{
   class Program
   {
      public static void Main()
      {
         unsafe
         {
            int var = 50;
            int* p = &var;
            Console.WriteLine("value is: {0} " , var);
            Console.WriteLine("value is: {0} " , p->ToString());
            Console.WriteLine("Address is: {0} " , (int)p);
         }
        
         Console.ReadKey();
      }
   }
}

Example with block:

 unsafe
{
    // whole block code is considered as unsafe
    BitmapData bmData = testbm.LockBits(...)
    byte *bits = (byte*)pixels.ToPointer();
    // Do stuff with bits
}
        }

Interface in Detail

An interface looks like a class which contains only the declaration of methods, properties, events or indexers and has no implementation for any member.

Class or structure that implements interface must implement all interface members.

An interface can be member of class and namespace and interface has below members:

     a.      Methods
b.      Properties
c.       Indexers
d.      Events

Points To Remember:

1. Interface members cannot have any access modifiers even public, default access specifiers is public.

2. An interface can extend another interface.

3. Interface has no implementation for any member.

4. Interface contains only Methods, Properties, Indexers, delegates and events signature.

5. A class or structure can implement more than one interface.

6. Interface cannot be instantiated but it can be referenced by the class object which implements it.

i.e  ICollage collageInfo=new student();

7. In C# multiple inheritance is possible with interface, an interface can inherit from one or more base interfaces.


Interface Example:

public interface ICollage

    {

        // Property declaration:

        string Name

        {

            get;

            set;

        }

 

        void GetCollageInfo();

    }

Implementation:

public class student : ICollage

   {

      public static int numberOfStudents;

 

      private string name;

      public string Name  // read-write instance property

      {

         get

         {

            return name;

         }

         set

         {

            name = value;

         }

      }

 

      private int counter;

      public int Counter  // read-only instance property

      {

         get

         {

            return counter;

         }

      }

 

      public student()  // constructor

      {

         counter = ++counter + numberOfStudents;

      }

 

      public void GetCollageInfo()

      {

         Console.Write("Engineering collage");

      }

   }

 

Multiple inheritances Example with interface:

    public interface ICollage

    {

        void GetCollageInfo();

    }

 

 

public partial class Teacher

    {

        public static int numberOfTeachers;

 

        private int counter;

        public int Counter  // read-only instance property

        {

           get

           {

              return counter;

           }

        }

 

        public Teacher()  // constructor

        {

            counter = ++counter + numberOfTeachers;

        }

    }

 

 

public class student :Teacher,ICollage

   {

    

      public void DiaplyStudentData()

      {

        Console.WriteLine("i am Nagnath Kendre");

         GetCollageInfo();

      }

 

      public void GetCollageInfo()

      {

         Console.Write("Engineering collage");

      }

 

      static void main()

      {

         student collageInfo = new student();

         Teacher techerDetails =new Teacher();

         collageInfo.DiaplyStudentData();

      }

 

   }

Unsafe Keyword

Can we use pointer in C# this question frequently asked by beginner level C# programmer, yes there are some situations we require that.

The unsafe keyword denotes an unsafe context, which is required for any operation involving pointers.

Pointers in C# we use with below situations:

1. Dealing with existing structures on disk
2.  Advanced COM or Platform Invoke scenarios that involve structures with pointers in them
3. Performance-critical code

Frequently this is used with unmanaged code in C# , for example you want to write code for image data.

Syntax:

unsafe
{
 
// unsafe context: can use here unmanaged code  or pointer code.
 
}

Method:

unsafe static void sammpleMethod(unmanaged paramter 1,unmanaged parameter2,unmanaged paramter n..)
{
   // Unsafe context: can use pointers here.
}

Example:

namespace UnsafeExample
{
   class Program
   {
      public static void Main()
      {
         unsafe
         {
            int var = 50;
            int* p = &var;
            Console.WriteLine("value is: {0} " , var);
            Console.WriteLine("value is: {0} " , p->ToString());
            Console.WriteLine("Address is: {0} " , (int)p);
         }
        
         Console.ReadKey();
      }
   }
}

Example with block:

 unsafe
{
    // whole block code is considered as unsafe
    BitmapData bmData = testbm.LockBits(...)
    byte *bits = (byte*)pixels.ToPointer();
    // Do stuff with bits
}
        }