C#語法學習(類型轉換)
/*
* Created by SharpDevelop.
* User: Administrator
* Date: 2008/8/27
* Time: 下午 04:52
* 類型轉換
* C#中類型轉換的機制分為兩種:
* 隱式轉換(Implicit Conversions)
* 顯式轉換(Explicit Conversions)
* 隱式轉換不需指明欲轉變的目的類型;
* 而顯式轉換明確地使用轉換運算符(cast)指定要轉換成哪一种類型.
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
class Test
{
static void Main()
{
int a=5;//System.Int32
long b;//System.Int64
b=a;//隱式轉換
long c=10;
c=(int)a;//顯式轉換
Console.WriteLine(b);
Console.WriteLine("int的最大值" + int.MaxValue);
Console.WriteLine("long的最大值" + long.MaxValue);
Console.WriteLine(b.GetType());
}
}
* Created by SharpDevelop.
* User: Administrator
* Date: 2008/8/27
* Time: 下午 04:52
* 類型轉換
* C#中類型轉換的機制分為兩種:
* 隱式轉換(Implicit Conversions)
* 顯式轉換(Explicit Conversions)
* 隱式轉換不需指明欲轉變的目的類型;
* 而顯式轉換明確地使用轉換運算符(cast)指定要轉換成哪一种類型.
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
class Test
{
static void Main()
{
int a=5;//System.Int32
long b;//System.Int64
b=a;//隱式轉換
long c=10;
c=(int)a;//顯式轉換
Console.WriteLine(b);
Console.WriteLine("int的最大值" + int.MaxValue);
Console.WriteLine("long的最大值" + long.MaxValue);
Console.WriteLine(b.GetType());
}
}
/*
* Created by SharpDevelop.
* User: Administrator
* Date: 2008/8/27
* Time: 下午 05:02
* C#中對轉換進行檢查
* checked
* unchecked
* checked語句
* unchecked語句
* 兩者的使用準則
* 1,寫代碼時將可能發生的非預期溢出的代碼放到一個checked塊中.
* 2,寫代碼是將允許發生溢出的代碼顯式地放到一個unchecked塊中
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Windows.Forms;
class Test
{
static void Main()
{
int a=5;
long b=700000000000;
try
{
//a=checked((int)b);//checked方法
checked//checked語句
{
a=(int)b;
}
}
catch(System.OverflowException e)
{
MessageBox.Show(e.Message);
return;
}
Console.WriteLine(a);
}
}
* Created by SharpDevelop.
* User: Administrator
* Date: 2008/8/27
* Time: 下午 05:02
* C#中對轉換進行檢查
* checked
* unchecked
* checked語句
* unchecked語句
* 兩者的使用準則
* 1,寫代碼時將可能發生的非預期溢出的代碼放到一個checked塊中.
* 2,寫代碼是將允許發生溢出的代碼顯式地放到一個unchecked塊中
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Windows.Forms;
class Test
{
static void Main()
{
int a=5;
long b=700000000000;
try
{
//a=checked((int)b);//checked方法
checked//checked語句
{
a=(int)b;
}
}
catch(System.OverflowException e)
{
MessageBox.Show(e.Message);
return;
}
Console.WriteLine(a);
}
}
/*
* Created by SharpDevelop.
* User: Administrator
* Date: 2008/8/27
* Time: 下午 05:12
* 在數值型在行運算時,程序會隱式的先把兩個數轉換成較大的類型,最後再進行運算.
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Windows.Forms;
class Con
{
static void Main()
{
Byte b=100;
//b=(Byte)(b+100);//進行顯式轉換後才能運行
b+=100;//此與不會發生錯誤,有點怪![](https://www.cnblogs.com/Images/dot.gif)
Console.WriteLine(b);
}
}
* Created by SharpDevelop.
* User: Administrator
* Date: 2008/8/27
* Time: 下午 05:12
* 在數值型在行運算時,程序會隱式的先把兩個數轉換成較大的類型,最後再進行運算.
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Windows.Forms;
class Con
{
static void Main()
{
Byte b=100;
//b=(Byte)(b+100);//進行顯式轉換後才能運行
b+=100;//此與不會發生錯誤,有點怪
![](https://www.cnblogs.com/Images/dot.gif)
Console.WriteLine(b);
}
}
/*
* Created by SharpDevelop.
* User: Administrator
* Date: 2008/8/27
* Time: 下午 05:17
* 引用類型的轉換
* 開發人員經常需要將一個對像從一种類型轉換成其他類型.
* CLR允許將一個對像強制轉換成其它類型或者它的任何基類型.
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Windows.Forms;
class Fruit
{
}
class Apple:Fruit
{
public int i=1;
}
class Con
{
static void Main()
{
Fruit f=new Apple();//CLR允許將一個對像強制轉換成其它類型或者它的任何基類型
//Apple a=new Fruit();//發生錯誤無法將fruit隱式轉換成Apple
Type t=f.GetType();
Apple a=(Apple)f;
Console.WriteLine(a.i);
Console.WriteLine(t.FullName);//取類的名稱
}
}
* Created by SharpDevelop.
* User: Administrator
* Date: 2008/8/27
* Time: 下午 05:17
* 引用類型的轉換
* 開發人員經常需要將一個對像從一种類型轉換成其他類型.
* CLR允許將一個對像強制轉換成其它類型或者它的任何基類型.
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Windows.Forms;
class Fruit
{
}
class Apple:Fruit
{
public int i=1;
}
class Con
{
static void Main()
{
Fruit f=new Apple();//CLR允許將一個對像強制轉換成其它類型或者它的任何基類型
//Apple a=new Fruit();//發生錯誤無法將fruit隱式轉換成Apple
Type t=f.GetType();
Apple a=(Apple)f;
Console.WriteLine(a.i);
Console.WriteLine(t.FullName);//取類的名稱
}
}
/*
* Created by SharpDevelop.
* User: Administrator
* Date: 2008/8/27
* Time: 下午 05:52
* C#中進行強制類型轉換的另一种方法是用
* IS 操作符
* is操作符只會返回真或假,不會發生異常.
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Windows.Forms;
class Fruit
{
}
class Apple:Fruit
{
public int i=1;
}
class Con
{
static void Main()
{
Fruit f=new Fruit();
Apple a=new Apple();
Console.WriteLine(f is Fruit);
Console.WriteLine(f is Apple);
Console.WriteLine(a is Fruit);
Console.WriteLine(a is Apple);
}
}
* Created by SharpDevelop.
* User: Administrator
* Date: 2008/8/27
* Time: 下午 05:52
* C#中進行強制類型轉換的另一种方法是用
* IS 操作符
* is操作符只會返回真或假,不會發生異常.
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Windows.Forms;
class Fruit
{
}
class Apple:Fruit
{
public int i=1;
}
class Con
{
static void Main()
{
Fruit f=new Fruit();
Apple a=new Apple();
Console.WriteLine(f is Fruit);
Console.WriteLine(f is Apple);
Console.WriteLine(a is Fruit);
Console.WriteLine(a is Apple);
}
}
/*
* Created by SharpDevelop.
* User: Administrator
* Date: 2008/8/27
* Time: 下午 06:00
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Windows.Forms;
class Fruit
{
}
class Apple:Fruit
{
public int i=1;
}
class Con
{
static void Main()
{
//這里CLR對類型進行了兩次操作符的檢查,這樣無形之中就影響了性能.
Fruit f=new Apple();
if(f is Apple)//用IS驗證,假如對像為NULL,那麼其結果總是為false
{
Apple a=(Apple)f;
Console.WriteLine(a.i);
}
}
}
* Created by SharpDevelop.
* User: Administrator
* Date: 2008/8/27
* Time: 下午 06:00
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Windows.Forms;
class Fruit
{
}
class Apple:Fruit
{
public int i=1;
}
class Con
{
static void Main()
{
//這里CLR對類型進行了兩次操作符的檢查,這樣無形之中就影響了性能.
Fruit f=new Apple();
if(f is Apple)//用IS驗證,假如對像為NULL,那麼其結果總是為false
{
Apple a=(Apple)f;
Console.WriteLine(a.i);
}
}
}
/*
* Created by SharpDevelop.
* User: Administrator
* Date: 2008/8/27
* Time: 下午 06:00
* as操作符
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Windows.Forms;
class Fruit
{
}
class Apple:Fruit
{
public int i=1;
}
class Con
{
static void Main()
{
Fruit f=new Apple();
Apple a=f as Apple;
//clr會驗證是否符合apple如果沒有問題則返回一個對像的引用否則會返回一個空值null
if(a!=null)
{
Console.WriteLine(a.i);
}
}
}
* Created by SharpDevelop.
* User: Administrator
* Date: 2008/8/27
* Time: 下午 06:00
* as操作符
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Windows.Forms;
class Fruit
{
}
class Apple:Fruit
{
public int i=1;
}
class Con
{
static void Main()
{
Fruit f=new Apple();
Apple a=f as Apple;
//clr會驗證是否符合apple如果沒有問題則返回一個對像的引用否則會返回一個空值null
if(a!=null)
{
Console.WriteLine(a.i);
}
}
}
///////////
下面是WINFORM中的一個事件程序
void Button5Click(object sender, EventArgs e)
{
foreach(Control c in this.Controls)
{
this.listBox1.Items.Add(c.Name);
Control c1=c as Button;
if(c1!=null)
{
c1.Text="kkkkkkkkkk";
}
}
}
{
foreach(Control c in this.Controls)
{
this.listBox1.Items.Add(c.Name);
Control c1=c as Button;
if(c1!=null)
{
c1.Text="kkkkkkkkkk";
}
}
}
申明
非源创博文中的内容均收集自网上,若有侵权之处,请及时联络,我会在第一时间内删除.再次说声抱歉!!!
博文欢迎转载,但请给出原文连接。