In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail what the C# development skills are, and the editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
C # Development skills (1): on the naming and attributes of variables
Static readonly and const variables have the same function, regardless of whether the access modifier is public or other (private, protected, internal), the variable name is generally uppercase with an underscore in the middle.
Public static readonly int MAX_HEIGHT; public const int MIN_HEIGHT = 10
Some programmers are insensitive to uppercase. In the above example, it's okay to replace MAX_HEIGHT with Max_Height, or even MaxHeight. In the .net class library, int.MaxValue and int.MinValue are defined in this way.
The const constant is more specifically a compile-time constant because it does not exist at run time, and in compilation all variable references are replaced by actual values. This is not the case with static readonly, which also exists at runtime. In principle, const is superior to static readonly in efficiency. But in a comparative project, if the value of a const variable is changed during a local upgrade of dll, and if the unupgraded dll also has this const variable, it is obvious that the problem will arise at this time. If you upgrade all dll as a result, it will not be worth it. Therefore, in large-scale and changeable applications, it is recommended to use static readonly instead of const. Its minimal efficiency impairment is acceptable compared to the problems that may arise in the upgrade layout.
Except for the above two static read-only and constant variables, the naming of other variables starts with an underscore, and the access modifier is private (internal, protected, let alone public is not recommended):
Private static int _ maxHeight; private int _ minHeight
If its name is not underlined, it is easy to be confused with parameter variables.
For the following definition:
Private int _ minHeight = default (int); public int MinHeight {set {_ minHeight = value;} get {return _ minHeight;}}
Beginners may think it's a bit superfluous, so they might as well just name it:
Isn't it easy to use public int MinHeight;? why wrap it in getter and setter? extra function calls also degrade efficiency.
Sometimes in the development of a project, we may just draw a snake at the beginning, but the later requirements of the project have changed and the one-stop painting has been changed. Therefore, it is very far-sighted to add a pair of feet when drawing snakes at the beginning of the project.
Getter and setter (attribute accessors) can encapsulate logic like methods and use them like variables. It is recommended that all non-static read-only and constants be defined as private, and then add corresponding attribute accessors for assignment and reading. In other methods (both out-of-class and in-class), direct reading and writing of variables is not recommended. Even if it can currently be read and written directly, we will also call it by calling the property accessor. This is a bit troublesome, but it is very important. Veterans sometimes make mistakes. As follows:
Private int _ minHeight = int.MinValue; public int MinHeight// or protected, internal, or even private {set {_ minHeight = value; / / even though there is currently no other processing logic} get {return _ minHeight;}} public void Method1 (int minHeight) {this.MinHeight = minHeight / / do not use this._minHeight to read and write directly here / /}
Property accessors are used even if the access to the variable is protected or private.
The principle is that the reading of a variable should be encapsulated with an attribute accessor, regardless of its access modifier, even if there is currently no logic in the attribute accessor other than access.
C # Development skills (2): on namespaces and directory partitioning
From the naming of namespaces, the division and naming of directories, we can see whether a programmer is experienced or not. A veteran programmer will never allow architectural chaos.
In .net development, general directory names correspond to namespace names. The question of how to divide namespaces and catalogs seems simple, but it is actually more complex, although it does not have a complete taxonomy like zoology and botany.
In the .net Bhand S architecture, it is generally divided into the following three main namespaces:
[company name / author name]. [project name] .Business
[company name / author name]. [project name] .Data
[company name / author name]. [project name] .Web
These three parts can be in a project, or they can be separated into three places.
The difficulty of catalogue classification and spatial naming is that the classification factor is two-dimensional, while the classification is only one-dimensional. Explain: classification is one-dimensional, which means that a word can only represent the meaning of one classification name, regardless of two meanings at the same time; classification factors are two-dimensional, which means that classification can be classified either horizontally or vertically.
Suppose I am developing an e-commerce book website [Zhan Blue Bookstore www.ZLBook.cn]. According to convention, this business has a user center, a help center, a payment center, a merchandise center, and so on. My project is divided into three project, as follows:
Sban.ZLBook.Business
Sban.ZLBook.Data
Sban.ZLBook.Web
In the Sban.ZLBook.Web project, I set up catalogs such as UserCenter, HelpCenter, PayCenter, ProductCenter, etc., which are classified horizontally by category.
In these categories, pictures must be used, and there are some css style files. Where do I put these files? I put them in the Images directory of the Web project (if there is no other image server). If there are too many files to manage, its subdirectories can be divided into UserCenter, HelpCenter, PayCenter, ProductCenter and so on. In this way, the directory division of Images is classified by vertical attributes.
There is no general way to name it. It depends on the specific project. If you do more projects, the architecture will be able to see the level. Uppercase is recommended for namespaces and directories.
When you don't know how to structure it, you might as well look through the official class library.
In the btw:flex project, the package name (pakeage) and directory are lowercase, while the class name is uppercase.
C # Development skills (3): for generic collections, use them if you can
Using Array,ArrayList,Dictionary to store a collection of objects is not only faced with the problem of performance loss of unpacking. From a system architecture point of view, all object objects should be strongly typed. To solve this problem, generics have been available since .Net2. Look at the following code:
Public class Mobile {private ArrayList friends= new ArrayList (); / / it is not enough to fetch public void Add (IFriend f) {friends.Add (f)} private void SayBless () {for (int I = 0; I < friends.Count; I +) {IFriend friend = (IFriend) friends [I]; / / when disassembling and disassembling here, you must know that the element type is IFriend friend.Say () }
The tip for this tip is to use generic collections to avoid explicit type conversions. If you have explicit conversions in your code, or if you have as operations, you may need to rethink the architecture. The as operator looks elegant to use, but it doesn't matter if you don't use it for type conversion.
C # Development skills (4): use interfaces instead of classes for parameters
The interface is honest, it is clear what can and cannot be done at a glance, and there has never been any deception. Unlike classes, you may have methods or properties whose interfaces are not defined, and you can effectively avoid using these methods and properties when programming. When defining a method, for the object parameters we need, all we need is a description of the function or function of the object, and the interface negotiation can provide these. Use the interface instead of the class for parameters, and any class that implements this interface can be used as a parameter instance. It is obvious that the interface has more flexibility.
For the return value of a method, if the returned object is required to have a function that is declared in the interface, simply return to the interface.
The principle is: the input and transmission of parameters should be as abstract as possible and expand its coverage.
This is the end of this article on "what are the C# development skills?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.