Item 3: Prefer the is or as Operators to Casts(Effective C#)
2011-01-09 11:16
585 查看
Remember that user-defined conversion operators operate only on the compile-time type of an object, not on the runtime type. Now that you know to use as when possible, let’s discuss when you can’t use it. The as operator does not work on value types. The is operator should be used only when you cannot convert the type using as. Otherwise, it’s simply redundant. foreach uses a cast operation to perform conversions from an object to the type used in the loop. foreach needs to use casts to support both value types and reference types. By choosing the cast operator, the foreach statement exhibits the same behavior, no matter what the destination type is. However, because a cast is used, foreach loops can cause an InvalidCastException to be thrown. 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Collections;
6
7 namespace EffectiveCSharpItem3
8 {
9 public class MyType
{
}
public class SecondType
{
private MyType type = new MyType();
public static implicit operator MyType(SecondType t)
{
return t.type;
}
}
class Program
{
static void Main(string[] args)
{
SecondType st = new SecondType();
MyType t = (MyType)st;
IEnumerable collection = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var small = from int item in collection
where item < 5
select item;
var small2 = collection.Cast<int>().Where(item => item < 5);
foreach (int i in small)
{
Console.WriteLine(i);
}
foreach (int i in small2)
{
Console.WriteLine(i);
}
Console.WriteLine(t == null);
Console.Read();
}
}
}
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Collections;
6
7 namespace EffectiveCSharpItem3
8 {
9 public class MyType
{
}
public class SecondType
{
private MyType type = new MyType();
public static implicit operator MyType(SecondType t)
{
return t.type;
}
}
class Program
{
static void Main(string[] args)
{
SecondType st = new SecondType();
MyType t = (MyType)st;
IEnumerable collection = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var small = from int item in collection
where item < 5
select item;
var small2 = collection.Cast<int>().Where(item => item < 5);
foreach (int i in small)
{
Console.WriteLine(i);
}
foreach (int i in small2)
{
Console.WriteLine(i);
}
Console.WriteLine(t == null);
Console.Read();
}
}
}
相关文章推荐
- Item 3: Prefer the is or as Operators to Casts
- Item 3: 使用 is 或 as 去做类型转换(Prefer the is or as Operators to Casts)
- Effective C# Item 3: Prefer the is or as Operators to Casts
- Prefer the is or as Operators to Casts - Effective C#学习笔记(3)
- Effective C# 3:Prefer the is or as Operators to cast
- Item 3:Prefer the is or as Operation to class
- mySql主从配置出错:The server is not configured as slave; fix in config file or with CHANGE MASTER TO
- Mysql 出现错误The server is not configured as slave; fix in config file or with CHANGE MASTER TO
- 对象的当前状态使该操作无效 or SPListItem Update Operation is not valid due to the current state of the object
- 对象的当前状态使该操作无效 or SPListItem Update Operation is not valid due to the current state of the object
- Using the isBranch() method to determine if a Tree item is a branch or leaf
- The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
- Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify pr
- The JVM found at JAVA_HOME is damaged.Please reinstall or define EXE4J_JAVA_HOME to point to an installed 32-bit JDK or JRE
- 关于http客户端常见错误"警告:Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is rec"
- Spring3.x错误---- Cannot proxy target class because CGLIB2 is not available. Add CGLIB to the class path or specify proxy interfaces.
- How to Enable Trace or Debug for APIs executed as SQL Script Outside of the Applications ?
- ind the integer A such that pow(A,N) is as close as possible to B
- How to find if native dll is compiled as x64 or x86?
- Cannot connect to WMI provider.You do not have permission or the server is unreachable.Note that you can only manager SQL Server 2005 and later version with SQL Server Configuration Manager.Invalid namespace [0x8004100e]