3.1.1、整數類型

來源:互聯網
上載者:User

一、整數型別

       
C#中定義了8中整數類型:位元組型(byte)、無符號位元組型(ubyte)、短整型(short)、無符號短整型(ushort)、整型(int)、無
符號整型(uint)、長整型(long)、無符號長整型(ulong)。劃分依據是該類型的變數在記憶體中所佔的位元。

        C#中每個整數類型都對應於.NET類庫中定義的一個結構,這些結構在程式集System中定義。上述結構均提供兩個基本屬性:MinValue和MaxValue,分別表示類型的最小值和最大值。

資料類型 說明 取值範圍 對應於System程式集中的結構
sbyte 有符號8位整數 -128~127  SByte
 byte  無符號8位整數 0~255  Byte
 short  有符號16位整數  -32768~32767  Int16
 ushort  無符號16位整數  0~65535  UInt16
 int  有符號32位整數  -2147483648-2147483647  Int32
 uint  無符號32位整數  0~4294967295  UInt32
 long  有符號64位整數  -9223372036854775808~9223372036854775807  Int64
 ulong  無符號64位整數  0~18446744073709551615  UInt64

整數取值範圍代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IntegerRange
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("整數類型的取值範圍:");
// sbyte
Console.Write("SByte:\t");
Console.Write(SByte.MinValue);
Console.Write("~");
Console.WriteLine(SByte.MaxValue);

// byte
Console.Write("Byte:\t");
Console.Write(Byte.MinValue);
Console.Write("~");
Console.WriteLine(Byte.MaxValue);

// short
Console.Write("Int16:\t");
Console.Write(Int16.MinValue);
Console.Write("~");
Console.WriteLine(Int16.MaxValue);

// ushort
Console.Write("UInt16:\t");
Console.Write(UInt16.MinValue);
Console.Write("~");
Console.WriteLine(UInt16.MaxValue);

// int
Console.Write("Int32:\t");
Console.Write(Int32.MinValue);
Console.Write("~");
Console.WriteLine(Int32.MaxValue);

// uint
Console.Write("UInt32:\t");
Console.Write(UInt32.MinValue);
Console.Write("~");
Console.WriteLine(UInt32.MaxValue);

// long
Console.Write("Int64:\t");
Console.Write(Int64.MinValue);
Console.Write("~");
Console.WriteLine(Int64.MaxValue);

// ulong
Console.Write("UInt64:\t");
Console.Write(UInt64.MinValue);
Console.Write("~");
Console.WriteLine(UInt64.MaxValue);

Console.WriteLine();
}
}
}

執行結果:

整數類型的取值範圍:
SByte: -128~127
Byte: 0~255
Int16: -32768~32767
UInt16: 0~65535
Int32: -2147483648~2147483647
UInt32: 0~4294967295
Int64: -9223372036854775808~9223372036854775807
UInt64: 0~18446744073709551615

請按任意鍵繼續. . .

如果類型取值超出了取值範圍,程式在運行時就會發生溢出。

Byte溢出代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IntegrateOverflow
{
class Program
{
static void Main(string[] args)
{
byte b = 100;
b = (byte)(b + 200); // 溢出

Console.WriteLine(b);
}
}
}

執行結果:

44
請按任意鍵繼續. . .

二、原始碼

IntegerRange.rar

IntegrateOverflow.rar

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.