C# Int 64 Struct
Last Updated :
01 Feb, 2025
Improve
In C#, the Int64 struct represents a 64-bit signed integer and is commonly used as the long
data type. It belongs to the System namespace and provides various methods to perform operations like mathematical computations, parsing, and type conversion. The Int64 struct inherits from ValueType, which in turn inherits from Object.
- Range: Int64 ranges from -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807.
- Bitwise Operations: It supports bitwise operations like AND (&), OR (|), XOR (^), NOT (~), and shifts (<<, >>).
- Convert and Math Class Compatibility: It works with methods from Convert and Math classes for operations like rounding, absolute value, and power calculations.
Fields
Fields | Description |
---|---|
MaxValue | This field is used to represent the largest possible value of an Int64. This field is constant. |
MinValue | This field is used to represent the smallest possible value of an Int64. This field is constant. |
Example: Using MaxValue and MinValue Fields
// C# program to demonstrate
// MaxValue and MinValue fields
using System;
class Geeks
{
static public void Main()
{
// Declaring and initializing Int64 values
long v1 = 89;
long v2 = 50;
long v3 = 10;
Console.WriteLine("Value 1: {0}", v1);
Console.WriteLine("Value 2: {0}", v2);
Console.WriteLine("Value 3: {0}", v3);
// Displaying MaxValue and MinValue
Console.WriteLine("Maximum Value: {0}"
, long.MaxValue);
Console.WriteLine("Minimum Value: {0}"
, long.MinValue);
}
}
Output
Value 1: 89 Value 2: 50 Value 3: 10 Maximum Value: 9223372036854775807 Minimum Value: -9223372036854775808
Methods
Method | Description |
---|---|
CompareTo() | This method is used to compare this instance to a specified object or Int64 and returns an indication of their relative values. |
Equals() | This method is used to return a value indicating whether this instance is equal to a specified object or Int64. |
GetHashCode() | This method is used to return the hash code for this instance. |
GetTypeCode() | This method is used to return the TypeCode for value type Int64. |
Parse() | This method is used to convert the string representation of a number to its 64-bit signed integer equivalent. |
ToString() | This method is used to convert the numeric value of this instance to its equivalent string representation. |
TryParse() | This method is used to convert the string representation of a number to its 64-bit signed integer equivalent. A return value indicates whether the conversion succeeded or failed. |
Example 1: Using Equals() Method
// C# program to demonstrate the Int64.Equals() method
using System;
class Geeks
{
public static void Main()
{
// Declaring and initializing values
long v1 = 45643212342;
long v2 = 543256344233;
// Using Equals() method
bool status = v1.Equals(v2);
if (status)
Console.WriteLine("{0} is equal to {1}", v1, v2);
else
Console.WriteLine("{0} is not equal to {1}", v1, v2);
}
}
Output
45643212342 is not equal to 543256344233
Example 2: Using GetTypeCode() Method
// C# program to illustrate GetTypeCode() method
using System;
class Geeks
{
static public void Main()
{
// Declaring an Int64 value
long value = 45789478123;
// Getting the type code
// using GetTypeCode() method
TypeCode res = value.GetTypeCode();
// Displaying the TypeCode
Console.WriteLine("TypeCode for Int64 is: {0}", res);
}
}
Output
TypeCode for Int64 is: Int64
Bitwise Operations with Int64
Example:
// C# program to demonstrate bitwise operations on Int64
using System;
class Geeks
{
static void Main()
{
long a = 5;
long b = 3;
// Performing bitwise operations
Console.WriteLine("Bitwise AND (a & b): "
+ (a & b));
Console.WriteLine("Bitwise OR (a | b): "
+ (a | b));
Console.WriteLine("Bitwise XOR (a ^ b): "
+ (a ^ b));
Console.WriteLine("Bitwise NOT (~a): "
+ (~a));
}
}
Output
Bitwise AND (a & b): 1 Bitwise OR (a | b): 7 Bitwise XOR (a ^ b): 6 Bitwise NOT (~a): -6