扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:愚翁 来源:天极Yesky软件频道 2007年11月11日
关键字: 《Effective C#》 装箱 拆箱
int i = 123; object obj = i;//Boxing if( obj is int ) int j = (int) obj;//Unboxing |
Console.WriteLine( "Number list:{0}, {1}, {2}",1,2,3 ); |
Console.WriteLine( "Number list:{0}, {1}, {2}", 1.ToString(),2.ToString(),3.ToString() ); |
public struct Person { private string _Name; public string Name { get{ return _Name; } set{ _Name = value; } } public Person( string PersonName ) { _Name = PersonName; } public override string ToString() { return _Name; } } // Using the person in a collection ArrayList arrPersons = new ArrayList(); Person p = new Person( "OldName" ); arrPersons.Add( p ); // Try to change the name p = ( Person ) arrPersons[0] ; p.Name = "NewName"; Debug.WriteLine( ( (Person ) arrPersons[0] ).Name );//It's "OldName" |
( (Person ) arrPersons[0] ).Name = "NewName";//Can't be compiled |
// Using the person in a collection ArrayList arrPersons = new ArrayList(); Person p = new Person( "OldName" ); arrPersons.Add( p ); // Try to change the name p = ( Person ) arrPersons[0] ; p.Name = "NewName"; arrPersons.RemoveAt( 0 );//Remove old data first arrPersons.Insert( 0, p );//Add new data Debug.WriteLine( ( (Person ) arrPersons[0] ).Name );//It's "NewName" |
public interface IPersonName { string Name{ get;set;} } public struct Person:IPersonName { private string _Name; public string Name { get{ return _Name; } set{ _Name = value; } } public Person( string PersonName ) { _Name = PersonName; } public override string ToString() { return _Name; } } // Using the person in a collection ArrayList arrPersons = new ArrayList(); Person p = new Person( "OldName" ); arrPersons.Add( p ); // Change the name ( (IPersonName)arrPersons[0] ).Name = "NewName"; Debug.WriteLine( ( (Person ) arrPersons[0] ).Name );//It's "NewName" |
( (Person ) arrPersons[0] ).Name = "NewName";//Can't be compiled |
( (IPersonName)arrPersons[0] ).Name = "NewName"; |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者