C# 筆記 變數與類型轉換(基礎+總結)

來源:互聯網
上載者:User

文章雖然淺顯,適合初學者,主要在總結的同時方便自己查閱。

簡單的 隱式轉換和顯式轉換就不解釋了

下列是整數型別的記憶體位元組數:
int 4位元組記憶體使用量   -2147483648~2147483647
uint 不帶負號的整數 0-4294967295

short  2位元組 -32768~32767
ushort 2位元組 0-65535

long  8位元組 -9223372036854775808~9223372036854775807
ulong 8位元組
 
sbyte   1位元組   -128-127
Byte    1位元組   0-255

char    2個位元組  表示1個字元

float   4位元組  7位精度
double  8位元組 15-16精度
decimal 16位元組 28位精度

注意:對於ushort 與char是可以相互轉換的,因為char也是單字元的整型,儲存的是一個數值,所以是數實值型別。
案例1:
    

static void Main(string[] args)
{
ushort shortnum;
char charnum ='a';
shortnum = charnum;
Console.WriteLine(charnum);//顯示a
Console.WriteLine(shortnum);//顯示97
}

即使儲存的資訊一樣,但是表示的方式不一樣的。

註:bool 和string 沒有隱式轉換!彼此之間沒有關係的類型不能進行強制轉換。

溢出檢查環境:(用於強制轉換)
案例2:
    

static void Main(string[] args)
{
ushort shortnum=281;
byte bytenum;
bytenum = (byte)shortnum;
Console.WriteLine(bytenum);//顯示25,因為資料溢出了
Console.WriteLine(shortnum);//顯示281

//使用checked檢測是否溢出
bytenum = checked((byte)shortnum);//未處理的異常: System.OverflowException: 算術運算導致溢出。//unchecked就是不檢測是否溢出

}

Convert顯式轉換:
註:對於Convert轉換是肯定要檢測資料溢出的
       

        Convert.ToByte();//轉換Byte
Convert.ToChar();
Convert.ToInt16();//轉換short
Convert.ToInt32();//轉換int
Convert.ToInt64();//轉換long
Convert.ToSByte();//轉換sbyte
Convert.ToUInt16();//轉換ushort
Convert.ToUInt32();//轉換uint
Convert.ToUInt64();//轉換ulong
Convert.ToBoolean();//轉換bool
Convert.ToSingle();//轉換float
Convert.ToDouble();
Convert.ToDecimal();

案例3:
   

static void Main(string[] args)
{
ushort shortnum=281;
byte bytenum;
bytenum = Convert.ToByte(shortnum);//對於Convert轉換是肯定要檢測資料溢出的
//未處理的異常: System.OverflowException: 值對於無符號的位元組太大或太小。
Console.WriteLine(bytenum);//顯示25,因為資料溢出了
Console.WriteLine(shortnum);//顯示281
}

註:short*float是需要進行轉換的,預設是把short轉換成float,因為short是2位元組,float是4位元組-隱式轉換

複雜的變數類型:
1.枚舉

test1:
public class EnumTest
{
enum Days { Sun, Mon, Tue, Wed, Thu, Fri, Sat };

static void Main()
{
int x = (int)Days.Sun;
int y = (int)Days.Fri;
Console.WriteLine("Sun = {0}", x);
Console.WriteLine("Fri = {0}", y);
}
}
/* Output:
Sun = 0
Fri = 5
*/

注意:enum Days {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri}; 強制序號從1開始而不是0.enum的預設類型是Int.但是不可以是char類型

test2:
public class EnumTest2
{
enum Range : long { Max = 2147483648L, Min = 255L };
static void Main()
{
long x = (long)Range.Max;
long y = (long)Range.Min;
Console.WriteLine("Max = {0}", x);
Console.WriteLine("Min = {0}", y);
}
}
/* Output:
Max = 2147483648
Min = 255
*/

test3:
public enum CarOptions
{
// The flag for SunRoof is 0001.
SunRoof = 0x01,
// The flag for Spoiler is 0010.
Spoiler = 0x02,
// The flag for FogLights is 0100.
FogLights = 0x04,
// The flag for TintedWindows is 1000.
TintedWindows = 0x08,
}

class FlagTest
{
static void Main()
{
// The bitwise OR of 0001 and 0100 is 0101.
CarOptions options = CarOptions.SunRoof | CarOptions.FogLights;

// Because the Flags attribute is specified, Console.WriteLine displays
// the name of each enum element that corresponds to a flag that has
// the value 1 in variable options.
Console.WriteLine(options);
// The integer value of 0101 is 5.
Console.WriteLine((int)options);
}
}
/* Output:
SunRoof, FogLights
5
*/

2.結構
結構類型可能是多個變數,但是資料類型不一樣。
 

public struct Book
{
public decimal price;
public string title;
public string author;
}

案例1:
   

enum ori:byte  //enum預設是int類型
{
north = 1,
south = 2,
east = 3,
west = 4
}
struct routemap //結構類型,用方位和精度表示路由
{
public double distance;
public ori direction;

}
static void Main()
{
routemap map;//聲明結構類型的變數
ori ori1=ori.east;//聲明枚舉的變數並賦值

map.direction = ori.east;
map.distance = 17.5;
Console.WriteLine(ori.east);//直接調用枚舉
Console.WriteLine((byte)ori.east);//轉換枚舉類型的值
Console.WriteLine(ori1);
Console.WriteLine((byte)ori1);

Console.WriteLine(map.distance+" "+map.direction);
}

案例2:

public class EnumTest
{
enum ori:byte //enum預設是int類型
{
north = 1,
south = 2,
east = 3,
west = 4
}
struct routemap //結構類型,用方位和精度表示路由
{
public double distance;
public ori direction;

}
static void Main()
{
routemap map;//聲明結構類型的變數
ori ori1=ori.east;//聲明枚舉的變數並賦值
int i = 4;

map.direction = (ori)i;//讓值變為枚舉
map.distance = 17.5;

Console.WriteLine(ori.east);//直接調用枚舉
Console.WriteLine((byte)ori.east);//轉換枚舉類型的值
Console.WriteLine(ori1);
Console.WriteLine((byte)ori1);

Console.WriteLine(map.distance+" "+map.direction);
}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.