Effective C # Principle 49: Prepare for c#2.0

Source: Internet
Author: User
Tags prepare visual studio


C#2.0, which was available in 2005, has some of the major new features. This allows some of the best practical experiences that are currently in use to change, and will be modified as the next generation of tools is released. Although you may not use these features at the moment, you should be prepared to do so.



When Visual Studio. net2005 is released, it gets a new development environment and upgrades the C # language. The content attached to this language does make you a more productive developer: you'll be able to write more reusable code and use a few lines to write more advanced structures. In a word, you can finish your work faster.



C#2.0 has four big new features: Paradigm, Iteration, anonymous method, and partial type. The main purpose of these new features is to enhance your development efficiency as a C # development. This principle will discuss three new features and why you should be prepared for them. Compared with other new features, the paradigm has a greater impact on how you develop the software. Paradigm is not a special product of C #. To achieve the paradigm of C #, MS has extended the CLR and the Intermediate language (MSIL) of Ms. C #, managed C + +, and VB. NET will be able to use the paradigm. J # will also be able to use these features.



The paradigm provides a "lot of arguments," which is an amazing way to use the same code to create a series of similar classes. When you give a special type of generic parameter, the compiler can generate different versions of the class. You can use a paradigm to create algorithms that are related to parameterized structures that implement algorithms on these structures. You can find many candidate paradigms in the. NET collection namespaces: Hashtables, Arraylist,queu, and stack can store different objects without having to worry about how they are implemented. These sets are good generic candidate types for 2.0来, which are generic in System.Collections.Generic and are a copy of the current class. C#1.0 is to store a reference to the SYSTEM.OBEJCT type, although the current design is reusable for this type, but it has a lot of deficiencies and it is not a type-safe. Consider the code:


ArrayList myIntList = new ArrayList( );
myIntList.Add(32 );
myIntList.Add(98.6 );
myIntList.Add ("Bill Wagner" );



This compilation is fine, but it doesn't mean anything at all. Do you really want to design such a container to store elements that are always completely different? Or do you want to work in a restricted language? This practice means that when you remove an element from a collection, you must add additional code to determine what object already exists in such a collection. In any case, you need to force the conversion of these elements from the System.Object to the actual type you want.



It's not just that when you put them in the 1.0 version of the collection, the cost of the value type is more special. Any time you put a value-type data into a collection, you have to box it. And when you remove it from the collection, you spend it again. These losses are small, but these costs accumulate quickly for a large set of thousands of elements. Generics have eliminated these losses by generating special code for each of the different value types.



If you are familiar with C + + templates, there is no problem with the C # paradigm, as these are syntactically very similar. The inner work of the paradigm, no matter how it is produced, is completely different. Let's look at some simple examples to see how things work and how it is done. Consider some of the code for one of the following classes:


public class List
{
 internal class Node
 {
  internal object val;
   internal Node next;
 }
 private Node first;
public void AddHead( object t )
 {
  // ...
 }
  public object Head()
 {
  return first.val;
 }
}


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.