C # tips written by Andrew troelsen

Source: Internet
Author: User
Tags mscorlib

Author: Andrew troelsen is Microsoft's C # MVP and vice president and partner of Intertech Training Technology Training Department. This is what I saw on the blog of Duncan macenzie. It is very interesting, so I translated a few articles. Original can view: http://blogs.msdn.com/csharpfaq/category/7580.aspx

1. Process Win32 messages through windows form

In the unmanaged world, it is common to obtain Win32 messages stored in message queues. If you want to host a Windows form applicationProgramTo process Win32 messages. First, you should create a help class to implement the imessagefilter interface. This interface provides a separate method prefiltermessage () to obtain the internal Message ID, that is, the original wparam and lparam data. A simple example:

 
Public class mymessagefilter: imessagefilter {public bool prefiltermessage (ref message m) {// intercept the left mouse button down message. if (M. MSG = 513) {MessageBox. show ("wm_lbuttondown is:" + M. MSG); Return true;} return false ;}}

In this case, you must use the application type to register your help class:

Public class mainform: system. windows. forms. FORM {private mymessagefilter msgfliter = new mymessagefilter (); Public mainform () {// register message filter. application. addmessagefilter (msgfliter );}...}

At this time, your custom filter will automatically take effect before the message gets the event handler. To remove the filter, you can use application's static method application. removemessagefilter ().

2. Pay attention to the relationship between references, 'using', and manifest.

If the. NET platform supports binary type reuse, it is common to add references to external assembly using the "add reference" dialog box of Visual Studio. NET. Many programmers (especially those in the C/C ++ family) are very "scared" to add unnecessary external references, because this will cause"CodeExpansion ". In fact, this is not the case. When you invoke a toolkit or use the javasusing‑key word, csc.exe ignores all the assemblies that are not actually used in your code. Therefore, when you add system. Data. dll and system. Windows. Forms. dll references in the following code:

 
Using system; using system. data; // ignored. using system. windows. forms; // ignored. public class myclass {public static void main () {console. writeline ("Hi there. ");}}

The compiler only references mscorlib. dll.

As you can see, when you open the. net sdk through ildasm.exe, double-click the manifest icon to display the obtained il information in the pop-up window. At the top, you can see the list of external assembly information actually used in the current compiled assembly:

 
. Assembly extern mscorlib {... }

[Note: The referenced assembly obtained by the Assembly. getreferencedassemblies () method does not include information about unused Assembly.]

At least, you don't need to waste time removing using statements and Assembly references that are useless in those applications. C # the compiler has already done this for you automatically.

3. EditSource codeEnable "full screen"

Yes, I have to admit that this is a "lame" suggestion and it seems worthless. However, this is a feature of my favorite Visual Studio. NET (such as the previous Visual Studio version), but many people ignore it. You can find the "full screen" menu item under the "View" menu. After activating this option, the current Menu displays only the currently activated documents. This is helpful for those who like refreshing interfaces, because there are too many window la s in IDE, making your code window "inactive ". To exit full screen mode, click "full screen" in the code window.

4. Add the custom assembly to the "add reference" dialog box.

You already know that Visual Studio. net's "add reference" dialog box does not display all the assembly in the machine, because it is not directly mapped to the Global Assembly Cache (GAC), naturally it does not display custom assembly. You can use the "Browse" button to view the. dll assembly.

However, if you want Visual Studio.. Net "add reference" dialog box shows your custom assembly, you must copy these assembly files to the publicassemblies folder, which is located in c: \ Program Files \ Microsoft Visual Studio. NET 2003 \ common7 \ IDE. Once you do this, let's take a look. The custom. dll assembly will be automatically displayed.

5. Use "using" to avoid type name conflicts

You already know that in a *. CS file, you can use the using keyword to direct the compiler to a complete type name. However, you may not know that you can use the using keyword to create aliases (which helps avoid name conflicts ). Assume that you have two namespaces:

 
Namespace my2dshapes {public class hexagon {}} namespace my3dshapes {public class hexagon {}}

Assume that you want to create a 3D hexagon instance in the following application:

 
Using my2dshapes; using my3dshapes; public class MyApp {public static void main () {// error! Which hexagon? Hexagon H = new hexagon ();}}

By creating an alias, it is easy to avoid the above naming conflicts:

 
Using my2dshapes; using the3dhex = my3dshapes. hexagon; public class MyApp {public static void main () {// This really creates a new my3dshapes. hexagon. the3dhex H = new the3dhex ();}}

other tips are simple. For example, you can use the tab key to directly obtain the stub code to implement the interface and the signature of the delegate. Although these tips cannot help you design programs, they can save at least a lot of trouble. Let's take a look at this.

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.