On the C # forced lattice Handbook

Source: Internet
Author: User

Hydrological. How to make your own code look, more force lattice?

To make your own code, look more elegant, more force lattice, more tall, you must write obscure, and concise code to.

For the class itself, the global variables must be added this, for the base class, must add base. Reflection not much, but must have, and occasionally come on so one or two.

interface, abstract class, abstract method must know how to use. If you can, match up: base (param), where t:interface and the like.

For event,delegate to be used. And do not directly delegate (), to use Delegate.invoke ();

If you are sure of your own skills, Delegate.begininvoke () is also a very obvious way to force lattice.

Async,await and the like can be exempted, as far as possible packaging delegate, to BeginInvoke.

The name must also be forced lattice, for the variable name, how short how to. For method name, event name, how long to come.

If you package the cache, to use the Key/value data format, do not use dictionary,hashtable, be sure to use CONCURRENTDICTIONARY/IENUMABLE<KEYVALUEPAIR> In addition to exposing methods such as Object GetValue (string Key) externally, you must also match the indexer public object this[string Key].

For Lock,monitor,autoresetevent, as with reflection, not much, but must have, and have to be eye-catching.

For any function, if you are sure, get an init, start more than two threads, and add a variety of concurrentstack,concurrentqueue and other code. If the function is too simple, you can also add Task.Factory.StartNew (() =>{});

Then learn to use a variety of iqueryable,lambda,func<>, linq,linq tree, eyes are a piece of startwith, continuewith,asparallel,expression<> , () =>{} etc., and must be seen everywhere from ... into ... select and so on.

All kinds of anonymous functions, anonymous delegates, anonymous classes, all kinds of constraints, but also as usual, handy.

Then is a variety of grammatical sugars, but also to be flexible to use, for the need to return more than two values, do not use ref, out to return parameters, to use a tuple, for the return [], to the common yield.

For any class, not required, first inherit the IDispose and then write down. Using is the absolute force must not be less.

For switch: Case, factory, strategy, all of them, to be flexible with the IOC, and must not use any third party, such as Autofac,unity,castle. Must own package, play a few attribute,createinstance, local can also use Dynmaic.

For code that is no longer in use, or code that has been refactored, do not comment, or delete, and add [Obsolete] to show that this is your own optimization result.

Well, the layout should also note that for more parameters, it is necessary for each parameter to occupy a row, for the transfer of entities, must not be defined outside the function, but on the parameters above the new class{XXX}, and each brace, must be exclusive row.

For the method inside the code is long, also want to use {} to fragment, and each curly brace, must be exclusive line.

For the low-naïve, be sure to avoid. like return num = = 1? True:false; such as the code, do not appear, directly return num = = 1;

For Convert.ToInt32 (request["key"]); double. TryParse (request["key"], out double) and so on, do not expose the code, to learn to use the extension class request.form[]. ToInt (); Configurationmanager.appsetting[""]. ToDouble (), etc., can also be used. Tovalue<int/double> the way.

On the C # forced lattice Handbook

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.