Friday, January 20, 2012

Sizeof and Typeof in C# and Marshling


Marshal class: Provides a collection of methods for allocation unmannaged memory, copying unmanaged blocks, and converting managed to unmanaged types, as well as 
other miscellanous methods used when interacting with unmanaged code.



sizeof() and typeof() operators in c#
-------------------------------------

The sizeof() operator
---------------------
C# provides a facility to perform low-level funtions through a construct
known as unsafe code.

The sizeof() operator works only in unsafe code. The operator takes a type
and returns the type's size in bytes.

Here is example
---------------

unsafe
{
int intsize=sizeof(int); // intsize =4
}

The typeof() operator
---------------------

The typeof() operator returns a TypeObject.The Type class holds information
about a value or reference type.

The typeof() operator is used in various places in c# to discover information
about reference and value types.

for example
-----------
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        
        static void Main(string[] args)
        {

            Type mytype = typeof(int);
            Console.WriteLine(mytype);//displays Int32

              
           
        }

    }
}

No comments:

Post a Comment

Qualcomm Short Term

  113 is the SL. 1st Target by mid July.

Total Pageviews