Code
posted @ 2008-08-11 03:50 one 阅读(4) | 评论 (0)编辑

原文:http://www.cnblogs.com/anytao/archive/2007/04/07/must_net_01.html

本文将介绍以下内容:

• 类型转换

• is/as操作符小议 

1. 引言 

类型安全是.NET设计之初重点考虑的内容之一,对于程序设计者来说,完全把握系统数据的类型安全,经常是力不从心的问题。现在,这一切已经在微软大牛们的设计框架中为你解决了。在.NET中,一切类型都必须集成自System.Object类型,因此我们可以很容易的获得对象的准确类型,方法是:GetType()方法。那么.NET中的类型转换,应该考虑的地方有那些呢?

2. 概念引入

类型转换包括显示转换和隐式转换,在.NET中类型转换的基本规则如下:

  • 任何类型都可以安全的转换为其基类类型,可以由隐式转换来完成;
  • 任何类型转换为其派生类型时,必须进行显示转换,转换的规则是:(类型名)对象名;
  • 使用GetType可以取得任何对象的精确类型;
  • 基本类型可以使用Covert类实现类型转换;
  • 除了string以外的其他类型都有Parse方法,用于将字符串类型转换为对应的基本类型;
  • 值类型和引用类型的转换机制称为装箱(boxing)和拆箱(unboxing)。

3. 原理与示例说明

浅谈了类型转换的几个普遍关注的方面,该将主要精力放在is、as操作符的恩怨情仇上了。类型转换将是个较大的话题,留于适当的时机讨论。

is/as操作符,是C#中用于类型转换的,提供了对类型兼容性的判断,从而使得类型转换控制在安全的范畴,提供了灵活的类型转换控制。

is的规则如下:

  • 检查对象类型的兼容性,并返回结果,true或者false;
  • 不会抛出异常;
  • 如果对象为null,则返回值永远为false。

其典型用法为:

 1object o = new object();
 2
 3class A
 4
 5{
 6
 7}

 8
 9if (o is A)  //执行第一次类型兼容检查
10
11{
12
13  A a = (A) o;  //执行第二次类型兼容检查
14
15}

16
17

 as的规则如下:

  • 检查对象类型的兼容性,并返回结果,如果不兼容就返回null;
  • 不会抛出异常;
  • 如果结果判断为空,则强制执行类型转换将抛出NullReferenceException异常。

其典型用法为: 

 1object o = new object();
 2
 3class B
 4
 5{
 6
 7}

 8
 9B b = o as B;  //执行一次类型兼容检查
10
11if (b != null)
12
13{  
14
15  MessageBox.Show("b is B's instance.");
16
17}

18
19

4. 结论

纵上比较,is/as操作符,提供了更加灵活的类型转型方式,但是as操作符在执行效率上更胜一筹,我们在实际的编程中应该体会其异同,酌情量才。 

参考文献

(USA)Jeffrey Richter, Applied Microsoft .NET Framework Programming

 (USA) Stanley B. Lippman, C# Primer

posted @ 2008-07-31 07:49 one 阅读(14) | 评论 (0)编辑
     摘要:   阅读全文
posted @ 2008-07-22 07:29 one 阅读(30) | 评论 (0)编辑

RE:I got this when I renamed my C# project then tried to publish it. To solve, I went to the "Signing" tab of the project properties and unchecked "Sign the ClickOnce manifests". Good luck!
posted @ 2008-07-10 04:11 one 阅读(12) | 评论 (0)编辑

What does it mean that a method is static?

A static method is simply one that is disassociated from any instance of its containing class. The more common alternative is an instance method, which is a method whose result is dependent on the state of a particular instance of the class it belongs to.

For example, both of these statements would return precisely the same string, but accomplish it through different types of methods:

// ToString() is an instance method of the DateTime class.
//  Its result depends on the value of each DateTime instance.
return DateTime.Now.ToString();
 
// String.Format is a static method of the String class.
//  Its result is not related to any instance of the String.
return String.Format("{0}", DateTime.Now);

The key difference to understand is that a static method can be called without setting up a proper instance of the class it belongs to.

In a sense, it is a stateless method.

posted @ 2008-07-08 04:20 one 阅读(11) | 评论 (0)编辑

The final advantage of using const over readonly is performance: Known
constant values can generate slightly more efficient code than the variable
accesses necessary for readonly values. However, any gains are slight and
should be weighed against the decreased flexibility.
Be sure to profile
performance differences before giving up the flexibility.


const must be used when the value must be available at compile times:
attribute parameters and enum definitions, and those rare times when
you mean to define a value that does not change from release to release.
For everything else, prefer the increased flexibility of readonly constants.

posted @ 2008-07-08 04:08 one 阅读(8) | 评论 (0)编辑
1)More and more people use the mobile phone or computer to communicate, and no longer write letters to each other. Some people think the skills of letter writing will soon disappear completely. Do you agree or disagree? How important do you think letter-writing is?

In the new millennium, rapid developed telecommunication technology makes our lives much easier and faster than ever before, inducing a large scale sweeping trend that the proliferation of mobile phones and computers is increasingly popular. In this case, whether the traditional letter should be replaced by such modern techniques has aroused severe debate,and some even claim that the skills of letter will disappear completely. Speaking for me, I totally disagree with this view.


In this day and age, the traditional means of writing a letter still plays a pivotal role and its advantages cannot be neglected. To begin with, not only writing a letter is to transfer information, but it represents a more sincere feeling as well- that is to say, a traditional letter transfers some feelings of sender, be it happy or sorry, angry or love, rather than merely simple words to the receiver. To illustrate, it is usual that companies would like to send formal invitation letters to guests for conferences and lovers would like to write love letters to each other. In addition, considering about the increasingly severe disparity between cities and countryside, some basic infrastructures such as signal base stations and broadband are unavailable in rural areas and numerous people still cannot afford the cost of a mobile phone and computer, thus letters and post offices are still an indispensable part of the communication network.

Admittedly, modern technology like SMS or MSN or email exert certain effects to increase efficiency, as it cut down the time wasted in transferring the message. However, such fast and convenient operations render people less careful about their words and expressions, resulting in a great many wrongly spelled characters or unsuitable sentences, which will, in turn, make misunderstanding or even degradation of the language. In contrast, when writing a letter, people will pay more attention to their words and carefully think about the contents.

In conclusion, mobile phones or computers can increase efficiency in modern society, but they will never render the traditional writing skills outdated. Letter writing can help us concentrate on the contents, reduce the cost of deploying facilities and above all, directly and effectively show our feelings.


2)It is now possible to perform everyday tasks as banking, shopping and business transactions without meeting people face-to-face. What are the effects it may bring on the individual and the society as a whole? 
http://bbs.topsage.com/dispbbs_63_175771_9_3.html
)Nowadays, a lot of advertisements are aimed at children. Some people think there are lots of negative effects for children and should ban the advertisements. To what extent do you agree or disagree?

4)Some think the government should be responsible for ensuring the people of the country that have healthy lifestyles. Others think we should make our own living decisions. Discuss both opinions and give your opinion.
posted @ 2008-06-30 02:52 one 阅读(28) | 评论 (0)编辑

积极方面  

1. give students/ citizens motivation to…给学生/公民动力去……
2. narrow the gap between the wealthy and the impoverished 减少贫富差距
3. curb crimes 控制犯罪
4. allocate money to投资于…
5. promote the development of… 促进…的发展
6. It is obvious that…显而易见,…
7. play a key role in 起关键作用  
8. enhance the efficiency of…提高效率
9. enrich one’s experience …丰富某人的经验
10. keep society safe and stable 保持社会稳定
11. have an obligation to do …有责任去…
12. afford people entertainment and pleasure 给人们提供娱乐
13. create employment opportunities   创造就业机会
14. put something in the first place = give priority to… 把…当成首要任务
15. is less time-consuming and more economical 省钱又省时间
16. broaden one’s horizons 开阔人的眼界
17. contribute to … 为…作贡献
18. fulfill one's potential 发挥......的潜力

消极方面

19. live a stressful life生活压力大
20. the competition is stiff 竞争激烈
21. impair people’s health 破坏人的健康
22. the population is booming 人口爆炸
23. restrict the development of 限制……的发展
24. lower the efficiency of… 降低……的效率
25. stem from 某事由…导致
26. suffer from 遭受
27. is a threat to… 对…构成威胁
28. have detrimental influence upon  对…产生有害的影响
29. cope with  解决,处理
30. take measures to … 采取措施…
31. There is a definite link between A and B     A和B之间有密切的联系
32. is increasingly severe 越来越严重
33. is monotonous 单调的
34. create psychological problems 产生心理问题

posted @ 2008-06-05 08:42 one 阅读(20) | 评论 (0)编辑

最近同事小朱受到减少代码的影响,在非正常状态下写下的代码:

1.构造函数给类的Property创造实例。
正常版:
 public class Parameters
    
{
        
public Parameters()
        
{
            Server 
= new KeyValue();
        
        }

       
        
public KeyValue Server getset; }
     }
无奈版:
1 public Parameters()
2        {
3            foreach (PropertyInfo pi in this.GetType().GetProperties())
4            {
5                pi.SetValue(this, Activator.CreateInstance(pi.PropertyType), null);
6            }

7        }
posted @ 2008-04-18 09:42 one 阅读(20) | 评论 (0)编辑

将List<T>转换为BindingList<T>,然后设置DataGridView的DataSource为BindingList<T>!!
代码:

DataGridView.DataSource = new BindingList<T>(List<T>);

将绑定BindingList<T>的DataSource转化为List<T>,同理
代码:
List<T> modelList=new List<T>((BindingList<T>)this.DataGridView.DataSource);


说明:BindingList<T>和List<T>都有个构造函数,参数是IEnumerable<T>,既然他们俩个都是继承IEnumerable,当然能相互转换。

下面是这个构造函数的执行过程:

public List(IEnumerable<T> collection)
{
    
if (collection == null)
    
{
        ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection);
    }

    ICollection
<T> is2 = collection as ICollection<T>;
    
if (is2 != null)
    
{
        
int count = is2.Count;
        
this._items = new T[count];
        is2.CopyTo(
this._items, 0);
        
this._size = count;
    }

    
else
    
{
        
this._size = 0;
        
this._items = new T[4];
        
using (IEnumerator<T> enumerator = collection.GetEnumerator())
        
{
            
while (enumerator.MoveNext())
            
{
                
this.Add(enumerator.Current);
            }

        }

    }

}


posted @ 2008-03-26 06:50 one 阅读(357) | 评论 (4)编辑

Advantage: benefit, positive aspect, strength, 好处

Disadvantage: weakness, drawback, negative aspect, fault不利

Casual: arbitrary, unplanned, unexpected, 随意的

Effective: efficient, effectual, fruitful, productive, valid多效的

Criticize: reproach, blame, 批评

Flaw: weakness, defect 缺点

Ability; capability, power, caliber能力

Dishonest: deceptive 不诚实的

Fair: equitable, equal, impartial, 公平的

Success: achievement, feat 成功

Reason: factor, contributor, origin, 原因

Result: outcome, consequence, implication结果

Result from: arise from, originate from, be due to, thanks to, 由于,因为

Give rise to: contribute to, lead to, result in, cause, breed, create, incur导致

Disaster: catastrophe 灾难

Pollute: contaminate 污染

Poisonous: toxic 有毒的

Decrease; fall, drop, plunge, decline, step back, downward, minimize, abate下降

Increase: rise, go up, surge, grow, 上升

Growing: increasing, rising, 上升的

posted @ 2008-03-26 06:28 one 阅读(30) | 评论 (0)编辑
posted @ 2008-03-25 12:47 one 阅读(10) | 评论 (0)编辑
.dude(老兄,老哥)

很多人认为该词单指“花花公子,纨绔子弟”的意思,实际上此词是叫男性年轻人的常用词,与guy的意思相同,只是guy用的范围更广。 例子:Hey dude look at that girl.(喂,老兄,看那个女孩)

2.chick(女孩)

容易被误解为“鸡,妓女”,实际上此词是叫女孩的常用词,语气中确实有轻佻、不尊重的倾向。例子:Look at that chick at the door.(看门口的那个女孩)

3.pissed off(生气,不高兴)

千万别认为是“尿尿”的意思,piss off在字典中则是“滚开,滚蛋”的意思,实际上此词是表示“生气,不高兴”的意思,与angry同意。例子:Man,is that guy pissed of ?(哎呀,那家伙真的生气了)

4.Hey,Give me five(嗨,好啊!)

此短语非常流行,经常在大片中出现,常在击掌庆贺时用。例子:Hey,dude! Give me five!(嗨,老兄,好啊!)

5.freak out(大发脾气)

总是在片子中看到这个词,freak本义是“奇异的,反常的”的意思,但freak out是“大发脾气”的意思,out也可以省略,这个词在美语中很常见,老式说法是be very upset。例子:He’s gonna freak(他快要发脾气了)

6.Get out of here(别开玩笑了,别骗人了)

大家很容易联想到“滚开”的意思,其实,现在很多时候都用在“别开玩笑了,别骗人了”的意思里,在美国片子中常可以听到。例子:(Man:)You look very beautiful(你很漂亮)(Girl)Get out of here.(别骗了)

7.gross(真恶心)

此词不是“混乱”的意思,字典中gross是“总的,毛重的”的意思,实际上此词是表示“恶心”的意思与gag相近,是美国年轻人一天到晚挂在嘴边的词。例子:Yuck, what is this stuff?It looks gross.(哎呀,这是什么东西?真恶心)

8.Hello(有没有搞错)

并不总是打招呼的意思,有时是“有没有搞错”的意思,要根据上下文来判断。 例子:Hello,anybody home,we’ll be late!(有没有搞错, 我们要迟到了)

9.green(新手,没有经验)

不是“绿色”的意思,也不是“生气”的意思,有时表示“新手,没有经验”。例子:She’s really green,she looks nervous.(她是新手,看起来很紧张)

10.Have a crush on someone(爱上某人)

由于crush是“压碎,碾碎”的意思,因此整个短语容易被误解为“对某人施加压力”的意思, 实际上此词表示“爱上某人”,与fall in love with 同意。例子:She thinks she has a crush on John.(她认为她爱上约翰了)

posted @ 2008-03-25 10:08 one 阅读(11) | 评论 (0)编辑

borrow 借进   
lend 借出   
organize the loan 借书   
out of loan 已借出   
available 可借到   
loan period 借期   
renew 续借   
circulate 借还   
circulation desk 借还书处   
information desk 问询处   
restrict 限制   
demagnetise 消磁   
reserve 保管   
works 著作   
newspaper 报纸   
magazine 杂志   
journal/periodical/current issues 期刊   
catalogue 目录   
category 分类   
librarian 图书馆管理员   
due 到期   
overdue 过期   
pay a fine 罚款   
stacks 书库   
open/closed shelves 开/闭架   
extension 图书馆扩建工程   
call slip 索书单 

posted @ 2008-03-25 10:02 one 阅读(15) | 评论 (0)编辑
travel agency 旅行社   
flight number 航班号   
take off 起飞   
land 降落   
check in 办理登机手续   
motel 汽车旅馆   
book the ticket 订票   
platform 站台   
museum 博物馆   
souvenir 纪念品   
art gallery 画廊   
hiking 徒步旅行   
hitch-hike 搭便车旅行   
surfing 冲浪   
skiing 滑雪   
bag-packer 肩背大包进行自助旅行的人   
traveler’s check 旅行支票   
sneakers 旅游鞋   
cream 护肤品   
scenery spot 景点   
mineral bath 温泉   
resorts 度假胜地   
tram 有轨电车   
express train 特快列车   
shuttle 往返汽车,航天飞机   
coach 交通班车,长途汽车   
ferry 渡船,轮渡   
tube/underground 地铁   
light rail 轻轨   
expressway/freeway 高速公路   
highway 大路   
cabby 计程车司机,出租马车车夫   
terminal 终点,航空集结站   
domestic 国内的   
international 国际的   
allow 留出时间   
flight connection centre 转机中心   
charge 付费   
fare 旅行费   
toll 通行费   
round/return trip 往返   
make a reservation 预定   
cancel one’s reservation 取消预定   
first come, first serve 先到先服务原则   
layover/stopover 中途停留   
saver ticket 优惠票   
airport tax 机场税   
duty-free shop 免税店   
vacant seat 空座   
first class 头等仓   
economy class 经济仓   
business class 商务仓   
pullman 卧铺车   
E.T.A (estimated time of arrival) 预计到达时间   
E.T.D (estimated time of departure) 预计起飞时间   
hobby 兴趣,爱好   
stamp collecting 集邮   
handicraft 手工艺   
taste 品位,嗜好   
refined taste 高雅的品位   
music appreciation 音乐欣赏   
baseball 棒球   
poll 普尔弹子戏   
tennis 网球   
scuba diving 戴水肺潜水   
soccer 足球   
man of versatile tastes 兴趣广泛的人   
parking sticker 停车许可证   
plate number 车牌   
parking lot 停车场   
fine 罚款   
tow your car away 拖车   
suspend 吊销   
brake 刹车   
hood 发动机罩   
engine 引擎   
steering wheel 方向盘   
make 品牌 
posted @ 2008-03-25 10:00 one 阅读(14) | 评论 (0)编辑