Csharp/CSharp Tutorial/Data Type/Integer Family

Материал из .Net Framework эксперт
Версия от 15:18, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

A C# int is a shorthand for System.Int32, which inherits the following members from System.Object

<source lang="csharp">using System; using System.Collections.Generic; using System.Text;

 class Program
 {
   static void Main(string[] args)
   {
     Console.WriteLine("12.GetHashCode() = {0}", 12.GetHashCode());
     Console.WriteLine("12.Equals(23) = {0}", 12.Equals(23));
     Console.WriteLine("12.ToString() = {0}", 12.ToString());
     Console.WriteLine("12.GetType() = {0}", 12.GetType());
   }
 }</source>

Family of Integers

C# defines nine integer types:

  1. char,
  2. byte,
  3. sbyte,
  4. short,
  5. ushort,
  6. int,
  7. uint,
  8. long,
  9. ulong.

char type is used for representing characters

The integer structures are

  1. Byte
  2. SByte
  3. Int16
  4. UInt16
  5. Int32
  6. UInt32
  7. Int64
  8. UInt64

The integer structures also define the following const fields:

  1. MaxValue
  2. MinValue

Methods Supported by the Integer Structures

Method Meaning public int CompareTo(object v) Returns 0 if the values are equal. Returns a negative value if the invoking object has a lower value. Returns a positive value if the invoking object has a greater value. public override bool Equals(object v) Returns true if equal. public override int GetHashCode() Returns the hash code. public TypeCode GetTypeCode() Returns the TypeCode enumeration value. For example, for Int32, the type code is TypeCode.Int32. public static retType Parse(string str) Parses the string. public static retType Parse(string str,IFormatProvider fmtpvdr) Parse string using the culture-specific information provided by fmtpvdr. public static retType Parse(string str, NumberStyles styles) Parse string using the style information provided by styles. public static retType Parse(string str, NumberStyles styles,IFormatProvider fmtpvdr) Parse string using the style information provided by styles and the culture-specific format information provided by fmtpvdr. public override string ToString() Convert value to string. public string ToString(string format) Convert value to string as specified by the format string. public stringToString(IFormatProvider fmtpvdr) Returns the string representation of the value of the invoking object using the culture-specific information. public stringToString(string format,IFormatProvider fmtpvdr) Returns the string representation of the value of the invoking object using the culture-specific information and the format.

All of the integer structures implement the following interfaces:

  1. IComparable,
  2. IConvertible, and
  3. IFormattable.

Integer ranges

Type Width in Bits Range byte 8 0 to 255 sbyte 8 -128 to 127 short 16 -32,768 to 32,767 ushort 16 0 to 65,535 int 32 -2,147,483,648 to 2,147,483,647 uint 32 0 to 4,294,967,295 long 64 -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 ulong 64 0 to 18,446,744,073,709,551,615


<source lang="csharp">using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Text; using System.Security.Cryptography; public class MainClass {

   public static void Main()
   {
       Console.WriteLine("{0}: bytes: {1}, range: [{2},{3}]",
           typeof(byte).ToString(), sizeof(byte), byte.MinValue, byte.MaxValue);
       Console.WriteLine("{0}: bytes: {1}, range: [{2},{3}]",
           typeof(char).ToString(), sizeof(char), (int)char.MinValue, (int)char.MaxValue);
       Console.WriteLine("{0}: bytes: {1}, range: [{2},{3}]",
           typeof(short).ToString(), sizeof(short), short.MinValue, short.MaxValue);
       Console.WriteLine("{0}: bytes: {1}, range: [{2},{3}]",
           typeof(int).ToString(), sizeof(int), int.MinValue, int.MaxValue);
       Console.WriteLine("{0}: bytes: {1}, range: [{2},{3}]",
           typeof(long).ToString(), sizeof(long), long.MinValue, long.MaxValue);
       Console.WriteLine("{0}: bytes: {1}, range: [{2},{3}]",
           typeof(sbyte).ToString(), sizeof(sbyte), sbyte.MinValue, sbyte.MaxValue);
       Console.WriteLine("{0}: bytes: {1}, range: [{2},{3}]",
           typeof(ushort).ToString(), sizeof(ushort), ushort.MinValue, ushort.MaxValue);
       Console.WriteLine("{0}: bytes: {1}, range: [{2},{3}]",
           typeof(uint).ToString(), sizeof(uint), uint.MinValue, uint.MaxValue);
       Console.WriteLine("{0}: bytes: {1}, range: [{2},{3}]",
           typeof(ulong).ToString(), sizeof(ulong), ulong.MinValue, ulong.MaxValue);
   }

}</source>

Integer Types

<source lang="csharp">Type Size Range (Inclusive) BCL Name Signed sbyte 8 bits -128 to 127 System.SByte Yes byte 8 bits 0 to 255 System.Byte No short 16 bits -32,768 to 32,767 System.Int16 Yes ushort 16 bits 0 to 65,535 System.UInt16 No int 32 bits -2,147,483,648 to 2,147,483,647 System.Int32 Yes uint 32 bits 0 to 4,294,967,295 System.UInt32 No long 64 bits -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 System.Int64 Yes ulong 64 bits 0 to 18,446,744,073,709,551,615 System.UInt64 No</source>

Use "is" for int value type

<source lang="csharp">using System; public class MainClass {

   static void Main() {
       int j = 123;
       object boxed = j;
       object obj = new Object();
       Console.WriteLine( "boxed {0} int", boxed is int ? "is" : "isnot" );
       Console.WriteLine( "obj {0} int", obj is int ? "is" : "isnot" );
       Console.WriteLine( "boxed {0} System.ValueType", boxed is ValueType ? "is" : "isnot" );
   }

}</source>

boxed is int
obj isnot int
boxed is System.ValueType