C # tips,

Source: Internet
Author: User

C # tips,

Implement all functions similar to Typedef in C #

The keyword Typedef is useful, because sometimes we need to use aliases to help us remember the sharing of some struct or classes. (I personally think this is the only thing C and C ++ can attract me) to realize this idea, we can use the following methods.

1. Use the using statement for alias. This method is suitable for struct.

Many programming tasks related to the Win32 platform are required. For example, if I want to read a Unicode character, but C # does not have the double-Word type, you need to define your own classes or struct, but in fact, in C ++, dual words are actually unsigned short, that is, we can use UInt 16 to support Word keywords, but there is a small tangle here, that is, I know that Word is Unsigned Int16, but can it be defined as Word? This makes it easy for us to remember.

However, at this time, we only need to use the using statement.

The procedure is as follows:

using Word=System.UInt16;

word w=16;

In this way, you can use the Word keyword to declare the variable.

2. Use generic methods to build generic classes

I have never tried this method, because Typedef is actually used only for compatibility with the basic types of C ++. In fact, using can alias classes, but if you use the generic method to construct a class, you can rewrite its operator, XD

 

I also posted an original address, but it is all in English. If you can understand it, you can read it by yourself. If you cannot understand it, I posted a translation.

Http://www.codeproject.com/Questions/141385/typedef-in-C

 

The translation diagram is as follows: (in order to be closer to our understanding, the translation statements have been manually modified, and some translations are not easy to understand and have been abandoned for personal understanding, if you do not understand it, you can translate the original text on your own)

 

(After editing a few times, I suddenly found that the layout of my blog garden is not suitable for looking at the picture. No matter how I edit it, I cannot find the proper scaling size, so I put it in the Baidu disk here, get it by yourself: http://pan.baidu.com/s/1dFyVbY5)

Long chart warning

 

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.