C # language features that programmers must know

Source: Internet
Author: User

Let's take a look at some of the frequently used C #4.0 language features. The order is random.
1. InterfaceInterface is not something unique to C #. object-oriented languages all have interfaces. However, in C #, the status of interfaces is actually more important than Abstract Class. This is especially manifested in the use of Mock in unit tests. In C #, only the explicit virtual method can be overwritten. Therefore, when dynamic Mock is required in testing, the Mock target must be an interface rather than a class, so that the method can be dynamically replaced.
2. The Automatic Property should be considered the most enviable thing for Java Programmers. Compared with Java, it saves the tedious getter and setter. In fact, with the help of the powerful IDE, generating getter and setter is not a big problem and is also very convenient. However, with the automation Property, the Code does not need to define these redundant methods, making the code simpler and easier to read.
3. LinqLinq is an essential tool for every C # programmer. Our code is more concise and intuitive by using Linq. At the same time, with the scalability of Linq, we can use Linq not only when accessing Collection, but also when processing XML, database, and text, this unifies many APIs in different environments, greatly improving code efficiency.
4. DataTableDataTable provides a method to represent database tables in memory. With DataTable, we can easily perform lightweight operations on the database. Of course, when the data volume is large, the DataTable format is still very problematic. After all, we need to read all the data into the memory. I personally think that the biggest charm of DataTable is in data display and update. Using DataTable to display data, we do not need to generate POCO, which can greatly improve work efficiency. At the same time, because DataTable itself supports Row State management, we can easily know which data has been modified by users, but only store this data. DataTable also supports XML export of data, so we can easily serialize and deserialize the data. At last, DataTable supports all the Linq functions through Linq Extension, which makes it easy for us to perform various operations on data in memory. To sum up, when we need to quickly complete the work and generate Prototype, DataTable is definitely a trustworthy multi-party player.
5. True hero behind Expression TreeLinq. Expression Tree is used all the time when we use Linq. Directly Using Expression Tree can easily solve some problems that are difficult to solve in static languages. I will introduce an example of my recent work in the future.
6. extension Method many people say that he is Magic. When I first came into contact with Extension Method, I had a bad idea about it. The reason is very simple: the Method cannot be found in the class, this greatly reduces the readability of the Code. However, it must be said that the Extension Method can save a lot of Utility Classes. At the same time, this Method of on-demand addition can greatly improve the reusability of code. The Extension method also provides an easy way to simplify the interface design and will be introduced later.
7. using Indexer, Indexer can encapsulate complex subscript operations of some collections. It is useful when designing some special data structures, such as sparse matrices, interface and encapsulation operations can be well integrated.
8. DynamicDynamic variables play an odd role in simplifying code when we need to handle complex interface inheritance and frequently Cast classes. However, like all Dynamic languages, once Dynamic variables are used, all static checks will fail. At the same time, Expression tree is behind Dynamic. When an exception occurs, the Exception is often very difficult to understand. Therefore, it is best to use dynamic code through strict unit tests. It is explained that many people will make dynamic and DynamicObject a thing. In fact, these two things are actually different, but they only have some relationships.

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.