System.IntPtr Structure

Assembly: Mscorlib.dll
Namespace: System
Summary
A platform-specific type that is used to represent a pointer or a handle.
C# Syntax:
[Serializable]
public struct IntPtr : ISerializable
Thread Safety
This type is safe for multithreaded operations.
Remarks
The IntPtr type is designed to be an integer whose size is platform-specific. That is, an instance of this type is expected to be 32-bits on 32-bit hardware and operating systems, and 64-bits on 64-bit hardware and operating systems.

The IntPtr type can be used by languages that support pointers, and as a common means of referring to data between languages that do and do not support pointers.

IntPtr objects can also be used to hold handles. For example, instances of IntPtr are used extensively in the FileStream class to hold file handles.

The IntPtr type is CLS-compliant, while the UIntPtr type is not. Only the IntPtr type is used in the common language runtime. The UIntPtr type is provided mostly to maintain architectural symmetry with the IntPtr type.

This type implements the ISerializable interface.

See also:
System Namespace | UIntPtr

System.IntPtr Member List:

Public Constructors
ctor #1 Overloaded:
.ctor(int value)

Initializes a new instance of IntPtr using the specified 32-bit pointer or handle.
ctor #2 Overloaded:
.ctor(long value)

Initializes a new instance of IntPtr using the specified 64-bit pointer.
ctor #3 Overloaded:
.ctor(void* value)

Initializes a new instance of IntPtr using the specified pointer to an unspecified type.
Public Fields
Zero A read-only field that represents a pointer or handle that has been initialized to zero.
Public Properties
Size Read-only

Gets the size of this instance.
Public Methods
Equals Overridden:
Returns a value indicating whether this instance is equal to a specified object.
GetHashCode Overridden:
Returns the hash code for this instance.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
ToInt32 Converts the value of this instance to a 32-bit signed integer.
ToInt64 Converts the value of this instance to a 64-bit signed integer.
ToPointer Converts the value of this instance to a pointer to an unspecified type.
ToString Overloaded:
ToString()

Overridden:
Converts the numeric value of this instance to its equivalent string representation.
ToString Overloaded:
ToString(string format)

Public Operators and Type Conversions
op_Equality Determines whether two specified instances of IntPtr are equal.
op_Explicit
(convert System.Int32 to System.IntPtr)
Overloaded:
op_Explicit(int value)

Converts the value of a 32-bit signed integer to an IntPtr.
op_Explicit
(convert System.Int64 to System.IntPtr)
Overloaded:
op_Explicit(long value)

Converts the value of a 64-bit signed integer to an IntPtr.
op_Explicit
(convert System.IntPtr to System.Int32)
Overloaded:
op_Explicit(IntPtr value)

Converts the value of the specified IntPtr to a 32-bit signed integer.
op_Explicit
(convert System.IntPtr to System.Int64)
Overloaded:
op_Explicit(IntPtr value)

Converts the value of the specified IntPtr to a 64-bit signed integer.
op_Explicit
(convert System.IntPtr to System.Void*)
Overloaded:
op_Explicit(IntPtr value)

Converts the value of the specified IntPtr to a pointer to an unspecified type.
op_Explicit
(convert System.Void* to System.IntPtr)
Overloaded:
op_Explicit(void* value)

Converts the specified pointer to an unspecified type to an IntPtr.
op_Inequality Determines whether two specified instances of IntPtr are not equal.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.IntPtr Member Details

Overloaded ctor #1
Summary
Initializes a new instance of IntPtr using the specified 32-bit pointer or handle.
C# Syntax:
public IntPtr(
   int value
);
Parameters:

value

A pointer or handle contained in a 32-bit signed integer.

Return to top


Overloaded ctor #2
Summary
Initializes a new instance of IntPtr using the specified 64-bit pointer.
C# Syntax:
public IntPtr(
   long value
);
Parameters:

value

A pointer or handle contained in a 64-bit signed integer.

Exceptions
Exception Type Condition
OverflowException On a 32-bit platform, value is too large to represent as an IntPtr.
Remarks
An exception is only thrown if the value of value requires more bits than the current platform supports.

Return to top


Overloaded ctor #3
Summary
Initializes a new instance of IntPtr using the specified pointer to an unspecified type.
This member is not CLS Compliant

C# Syntax:
[CLSCompliant(false)]
unsafe public IntPtr(
   void* value
);
Parameters:

value

A pointer to an unspecified type.

See also:
Void

Return to top


Field: Zero
Summary
A read-only field that represents a pointer or handle that has been initialized to zero.
C# Syntax:
public static readonly IntPtr Zero;
Remarks
The value of this field is not equivalent to null. Use this field to efficiently determine whether an instance of IntPtr has been set to a value other than zero.

For example, assume the variable, ip, is an instance of IntPtr. You can determine if it has been set by comparing it to the value returned by a constructor, for example: " if ip != new IntPtr(0)... ". However, invoking a constructor to get an unintialized pointer is inefficient. It is better to code either " if ip != IntPtr.Zero... ", or " if !IntPtr.Zero.Equals(ip)... ".

Return to top


Property: Size (read-only)
Summary
Gets the size of this instance.
C# Syntax:
public static int Size {get;}

Return to top


Overridden Method: Equals(
   object obj
)
Summary
Returns a value indicating whether this instance is equal to a specified object.
C# Syntax:
public override bool Equals(
   object obj
);
Parameters:

obj

An object to compare with this instance or null.

Return Value:
true if obj is an instance of IntPtr and equals the value of this instance; otherwise, false.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~IntPtr();

For more information on members inherited from System.Object click on the link above.

Return to top


Overridden Method: GetHashCode()
Summary
Returns the hash code for this instance.
C# Syntax:
public override int GetHashCode();
Return Value:
A 32-bit signed integer hash code.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: op_Equality(
   IntPtr value1,
   IntPtr value2
)
Summary
Determines whether two specified instances of IntPtr are equal.
C# Syntax:
public static bool operator ==(
   IntPtr value1,
   IntPtr value2
);
Parameters:

value1

An IntPtr.

value2

An IntPtr.

Return Value:
true if value1 equals value2; otherwise, false.

Return to top


Overloaded Method: op_Explicit System.IntPtr(
   int value
)
Summary
Converts the value of a 32-bit signed integer to an IntPtr.
C# Syntax:
public static operator explicit IntPtr(
   int value
);
Parameters:

value

A 32-bit signed integer.

Return Value:
A new instance of IntPtr initialized to value.
See also:
IntPtr.#ctor

Return to top


Overloaded Method: op_Explicit System.IntPtr(
   long value
)
Summary
Converts the value of a 64-bit signed integer to an IntPtr.
C# Syntax:
public static operator explicit IntPtr(
   long value
);
Parameters:

value

A 64-bit signed integer.

Return Value:
A new instance of IntPtr initialized to value.
Exceptions
Exception Type Condition
OverflowException On a 32-bit platform, value is too large to represent as an IntPtr.
See also:
IntPtr.#ctor

Return to top


Overloaded Method: op_Explicit System.Int32(
   IntPtr value
)
Summary
Converts the value of the specified IntPtr to a 32-bit signed integer.
C# Syntax:
public static operator explicit int(
   IntPtr value
);
Parameters:

value

An IntPtr.

Return Value:
The contents of value.
Exceptions
Exception Type Condition
OverflowException On a 64-bit platform, the value of value is too large to represent as a 32-bit signed integer.
Remarks
An exception is only thrown if the value of value requires more bits than the current platform supports.
See also:
IntPtr.ToInt32

Return to top


Overloaded Method: op_Explicit System.Int64(
   IntPtr value
)
Summary
Converts the value of the specified IntPtr to a 64-bit signed integer.
C# Syntax:
public static operator explicit long(
   IntPtr value
);
Parameters:

value

An IntPtr.

Return Value:
The contents of value.
See also:
IntPtr.ToInt64

Return to top


Overloaded Method: op_Explicit System.Void*(
   IntPtr value
)
Summary
Converts the value of the specified IntPtr to a pointer to an unspecified type.
This member is not CLS Compliant

C# Syntax:
[CLSCompliant(false)]
unsafe public static operator explicit void*(
   IntPtr value
);
Parameters:

value

An IntPtr.

Return Value:
The contents of value.
See also:
IntPtr.ToPointer

Return to top


Overloaded Method: op_Explicit System.IntPtr(
   void* value
)
Summary
Converts the specified pointer to an unspecified type to an IntPtr.
This member is not CLS Compliant

C# Syntax:
[CLSCompliant(false)]
unsafe public static operator explicit IntPtr(
   void* value
);
Parameters:

value

A pointer to an unspecified type.

Return Value:
A new instance of IntPtr initialized to value.
See also:
IntPtr.#ctor

Return to top


Method: op_Inequality(
   IntPtr value1,
   IntPtr value2
)
Summary
Determines whether two specified instances of IntPtr are not equal.
C# Syntax:
public static bool operator !=(
   IntPtr value1,
   IntPtr value2
);
Parameters:

value1

An IntPtr.

value2

An IntPtr.

Return Value:
true if value1 does not equal value2; otherwise, false.

Return to top


Method: ToInt32()
Summary
Converts the value of this instance to a 32-bit signed integer.
C# Syntax:
public int ToInt32();
Return Value:
A 32-bit signed integer equal to the value of this instance.
Exceptions
Exception Type Condition
OverflowException On a 64-bit platform, the value of this instance is too large to represent as a 32-bit signed integer.
Remarks
An exception is only thrown if the value of value requires more bits than the current platform supports.

Return to top


Method: ToInt64()
Summary
Converts the value of this instance to a 64-bit signed integer.
C# Syntax:
public long ToInt64();
Return Value:
A 64-bit signed integer equal to the value of this instance.

Return to top


Method: ToPointer()
Summary
Converts the value of this instance to a pointer to an unspecified type.
This member is not CLS Compliant

C# Syntax:
[CLSCompliant(false)]
unsafe public void* ToPointer();
Return Value:
A pointer to Void; that is, a pointer to memory containing data of an unspecified type.

Return to top


Overloaded Method: ToString()
Summary
Converts the numeric value of this instance to its equivalent string representation.
C# Syntax:
public override string ToString();
Return Value:
The string representation of the value of this instance.
Remarks
If the value of the IntPtr.Size property for this instance is 4, then this method is equivalent to Int32.ToString; otherwise, this method is equivalent to Int64.ToString.

Return to top


Overloaded Method: ToString(
   string format
)
C# Syntax:
public string ToString(
   string format
);
Parameters:

format

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.