摘要:A constant is a symbol that has a never-changing value. When defining a constant symbol,its value must be determinable at compile time. The compiler then saves the constant’s valuein the assembly’s metadata. This means that you can define a constant only for types thatyour compiler considers primit.
阅读全文
摘要:The CLR offers two IL instructions for calling a method: The call IL instruction can be used to call static, instance, and virtual methods.When the call instruction is used to call a static method, you must specify the typethat defines the method that the CLR should call. When the call instruction .
阅读全文
摘要:You need to bear in mind some performance considerations when you’re working with reference types . First, consider these facts: The memory must be allocated from the managed heap . Each object allocated on the heap has some additional overhead members associatedwith it that must be initialized . T.
阅读全文
摘要:Allobjects on the heap contain two overhead members: the type object pointer and the syncblock index.
阅读全文
摘要:If an assembly is to be accessed by multiple applications, the assembly must be placed into awell-known directory, and the CLR must know to look in this directory automatically when areference to the assembly is detected. This well-known location is called the global assemblycache (GAC), which can .
阅读全文
摘要:A response file is a text file that contains a set of compiler commandlineswitches. When you execute CSC.exe, the compiler opens response files and uses anyswitches that are specified in them as though the switches were passed to CSC.exe on thecommand line. You instruct the compiler to use a respon.
阅读全文
摘要:The NGen.exe tool that ships with the .NET Framework can be used to compile IL code tonative code when an application is installed on a user’s machine. Since the code is compiledat install time, the CLR’s JIT compiler does not have to compile the IL code at runtime, andthis can improve the applicat.
阅读全文
摘要:You can leverage dynamic to create methods that are intended to be usedwith anonymous types. It’s a technique to be used sparingly, like strongspices. If you find yourself creating many methods using dynamic invocationthat are intended for use with anonymous types, that’s a strong indicationthat yo.
阅读全文
摘要:Cast<T>, like all generic methods, compiles with only limited knowledgeof its type parameters. That can lead to generic methods not working theway you’d expect. The root cause is almost always that the generic methodcould not be made aware of particular functionality in the type representingth
阅读全文
摘要:There are also performance costs with using dynamic and with buildingexpressions at runtime. Just like any dynamic type system, your programhas more work to do at runtime because the compiler did not performany of its usual type checking. The compiler must generate instructions toperform all those .
阅读全文
摘要:Exceptions are complicated in any algorithm. Parallel tasks create morecomplications. The Parallel Task Library uses the AggregateException classto hold any and all exceptions thrown somewhere in the depths of yourparallel algorithms. Once any of the background threads throws an exception,any other.
阅读全文
摘要:The Parallel Task Library provides a series of methods that enable workingwith I/O bound operations as well as CPU bound partitions of work.Using the Task class, you can support a variety of asynchronous patternsthat work with I/O bound operations, or those that are a mixture of I/Oand CPU bound op.
阅读全文
摘要:Every parallel query begins with a partitioning step. PLINQ needs to partitionthe input elements and distribute those over the number of taskscreated to perform the query. Partitioning is one of the most importantaspects of PLINQ, so it is important to understand the differentapproaches, how PLINQ .
阅读全文
摘要:1usingSystem;2usingSystem.Collections.Generic;3usingSystem.Linq;4usingSystem.Text;56namespaceEffectiveCSharpItem347{8publicclassB2{}9publicclassD2:B2{}1011publicclassB12{13publicvoidFoo(D2parm)14{15Console.WriteLine("InB.Foo");16}17publicvoidBar(B2parm)18{19Console.WriteLine("InB.Bar&
阅读全文
摘要:codeCode highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->1usingSystem;2usingSystem.Collections.Generic;3usingSystem.Linq;4usingSystem.Text;56namespaceEffectiveCSharpItem337{8classBase9{10publicvoidMagicMethod()11{12Console.WriteLine("magicmethodfr
阅读全文
摘要:ICloneable does have its use, but it is the exception rather than rule. It’s significantthat the .NET Framework did not add an ICloneable<T> when itwas updated with generic support. You should never add support forICloneable to value types; use the assignment operation instead. Youshould
阅读全文
摘要:IComparable and IComparer are the standard mechanisms for providingordering relations for your types. IComparable should be used for themost natural ordering. When you implement IComparable, you shouldoverload the comparison operators (<, >, <=, >=) consistently with ourIComparable order
阅读全文
摘要:When you have one function that handles one event in a derived class, theoverride is the better approach. It is easier to maintain, more likely to becorrect over time, and more efficient. Reserve the event handlers for otheruses. Prefer overriding the base class implementation to attaching an eventh
阅读全文
摘要:It certainly can get complicated describing exactly how covariance andcontravariance work. Thankfully, now the language supports decoratinggeneric interfaces and delegates with in (contravariant) and out (covariant)modifiers. You should decorate any interfaces and delegates you definewith the in or
阅读全文