zoukankan      html  css  js  c++  java
  • c#中的整形类型

    一、整型类型

            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: ");
    Console.Write(SByte.MinValue);
    Console.Write("~");
    Console.WriteLine(SByte.MaxValue);

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

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

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

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

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

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

    // ulong
    Console.Write("UInt64: ");
    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

    感谢分享,  这个类型所占的数字范围   在其他编程语言里面似乎也是相似或者通用的

  • 相关阅读:
    Mysql数据优化--DBA梳理珍藏篇
    Spring缓存注解@Cacheable
    Spring常用知识点
    谈一谈对MySQL InnoDB的认识及数据库事物处理的隔离级别
    MySQL主从复制的实现过程
    List 与 数组 互转
    位运算实现小正整数乘法
    最大的K个数
    Java 编码与字符(2)
    JSP工作原理
  • 原文地址:https://www.cnblogs.com/isItOk/p/5619476.html
Copyright © 2011-2022 走看看