unmanaged Resources if(Nativeresource! =IntPtr.Zero) {Marshal.freehglobal (Nativeresource); Nativeresource=IntPtr.Zero; } //let the type know that they have been releaseddisposed =true; } Public voidSamplepublicmethod () {if(disposed) {Throw NewObjectDisposedException ("SampleClass","SampleClass is disposed"); } //omitted } } classanotherresource:idisposable { Public voidDispose () {}}Inheriting the IDisposable interface also facilitates the
enumeration type week, you can explicitly remove the value of an element, and the compiler automatically counts from the 0 value, then the value of the element +1.Note that in addition to the 8th value of week above, if the element type of the enumeration type is integer, you can also assign the values of the other integral types to week: Week = (week)9; This code does not go wrong, output: 9.Turn from: 157 recommendations for writing high-quality
Recommendation 45: Specifying contravariance for generic type parametersContravariance refers to a method's parameters can be a base class for a delegate or a generic interface's parameter type. Common delegates that support contravariance in FCL4.0 are:FuncPredicateCommon delegates are:IcomparerThe following example shows the benefits of specifying the inverse of a generic type parameter: classProgram {Static voidMain () {Programmer P=NewProgrammer {Name ="Mike" }; Manager m=NewManager {Name
stringName {Get;Set; } } classProgrammer:employee {}classManager:employee {}In Printsalary this method, the type that the method receives is isalaryCannot convert from "mytest.isalaryThe compiler checks the interface and delegate type parameters very strictly, unless the keyword out is specifically declared, otherwise this code will only compile failed. To get printsalary to complete the requirements, we can use generic type parameters: Static void Printsalary s) { s
Secondtype=NewSecondtype () {Name ="Second Type" }; Firsttype FirstType1=(Firsttype) Secondtype; Firsttype firstType2= Secondtype asFirsttype; } } classFirsttype { Public stringName {Get;Set; } } classSecondtype:firsttype {}However, even if you can use a forced transformation, from an efficiency standpoint, we recommend that you use the as operator.Knowing the difference between forced transformation and as, let's look at the is operator. Another version of Dowithsomety
thread exception"); } Catch(Exception ex) { This. Dispatcher.invoke (Action)Delegate { Throwex; }); } }); T.start ();However, in addition to the two methods above, we recommend using event callbacks to wrap the worker thread's exception to the main thread. The benefit of handling exceptions in the form of event callbacks is to provide a uniform entry for exception handling. This approach will be elabor
threadCTS. Cancel (); Console.WriteLine ("T1num:"+t1num.tostring ()); Console.WriteLine ("T2num:"+t2num.tostring ()); } As a result we will find that if this program runs on a single-core computer, the priority is highest thread T1, and its output value will almost always be greater than the priority normal (default) thread T2.In C #, with thread and threadpool new threads, the default priority is normal. Although it is possible to modify the priority of a thread as in the example above, i
(() = { while(true) {Console.WriteLine ("T201 is executing"); } }); t201. Start (); Console.readkey (); } In addition to the startup problem, there is the same problem with switching between threads, and the next execution of the T201 will wait for a considerable amount of time.Therefore, do not abuse threads, especially excessive threads. When you start a new thread, you need to think carefully whether the job really needs a new thread to complete. Even
the two methods as extension methods so that all UI types in the project can be used.WPF supports both methods, with all the code shown below (note the While loop section):Private voidButtonStart_Click (Objectsender, RoutedEventArgs e) {Task T=NewTask (() = { while(true) { This. Dispatcher.begininvoke (NewAction (() ={Textblock1.text=DateTime.Now.ToString (); })); Thread.Sleep ( +); } }); //to catch an exception, a new task is startedT.
Recommendation 29: Distinguish between IenumerableLINQ queries provide a total of two types of extension methods, under the System.Linq namespace, there are two static classes: The Enumerable class, which extends for collections that inherit the IenumerableLINQ queries can actually be functionally divided into 3 classes: LINQ to OBJECTS, LINQ to SQL, LINQ to XML. The purpose of designing two sets of interfaces is to differentiate between LINQ to OBJECTS and LINQ to SQL, both of which are used in
C # Programming style (English-Chinese comparison)The Elements of C # Style*net Design specification:. NET conventions, idiomatic methods and patternsFramework Design Guildline*. NET Design specification (CD-ROM attached)Framework Design Guildline (with CD)SQL Programming StyleJoe Celko ' s SQL programming Style [by Joe Celko. Turing]More effective C # Chinese version-50 specific ways to improve C # programsSharper C # code-writing high-
checklist can be checked against the list at the time of the code, avoiding repetitive errors, and the quality of the team's code will improve steadily as checklist accumulates.5, the development of code specifications and strict implementation, the violation of the problem recorded in checklist;In short, can be summed up as analysis + accumulation + sharing, no checklist accumulation, just rely on the exp
To improve the quality of your code, in addition to improving logical control and understanding of business processes, there is room for improvement in the code itself, such as some potential problems that can be avoided early. Similar to the content of the coding code, if all rely on the coder to do their own checks, it will undoubtedly require a lot of work, if you can use the static inspection tools to c
Improve and optimize cs' anti-terrorism image effects/QualityNonsense: Anti-Terrorism 2!But counter-terrorism 2 has changed a lot, without the pleasure of CS. Our CS fans only use cs1.6.Cs1.6 releases zero-point action condition zeroHis Screen Effect is much better than that of cs1.6, and CS is very pleasant.Recently I found out how to improve the zero-point action picture-d3d mode. Why does CS2 not use the
:Class A { function First () {$this->a = Get_large_array (); $this->pass_to_function (); } function Pass_to_function () {//process $this->a }}In short, to unset them as soon as possible, let the memory release, reduce the burden of the script.4. Error logfunction Insert_member () { if (! $model->insert ($data)) {Log::record ($model->geterror, Log::sql, ' Insert_ Member '); return false; }}Above is an Insert member information method, when inserting data failure, will record th
The article introduced how to use the unconscious attention and intentional attention in the cognitive system of shallow processing to improve the quality and effectiveness of advertising. Next, we enter the third stage, phase three, including memory and association, the main role of the deep processing of information is persuasion, that is, through the analysis of Memory and association, the use of skills
Many computer users will reflect that their network connection speed is too slow, especially slow download, some users of the network is worse, even the Web page can not open, but the monthly network costs still need to hand over to the landlord. So, is there any way to improve the network connection? Below, the small series to everyone to share two ways to improve the
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.