C # some practical and easy-to-Forget features, experiences, and skills

Source: Internet
Author: User
I suddenly thought that I wanted to get the enumerated value by name. I read a lot of information and found that the above only explained the enumeration syntax. I did not mention it at all for its practical application, can I only use a switch to determine? No, it's too earthy. Later I found that enumeration has such usage. It is really useful. Check the Code:

 

Code
Namespace enumtest
{
Enum Date {sun, Mon, Tue, Wes, Thu, Fri, sat}

Class Program
{
Static void main (string [] ARGs)
{
Console. writeline ("enter the name of the week :");
String name = console. Readline ();

// The type parameter indicates the type of the enumeration to be converted to. True indicates case-insensitive.
Object OBJ = enum. parse (typeof (date), name, true );
Console. writeline ("Number of the output week :");
Console. writeline (OBJ + ":" + (INT) OBJ );
}
}
}

 

Let's take a look at the running result diagram:

How about it? Do you think it is very practical.

 

Next, we will introduce the delegation. If you have a poor understanding of the Delegate, let's take a look at what I wrote earlier as a delegation ). We usually use a single method to specify the delegate, but what if we need to dynamically specify the delegate through parameters? Is switch also used? This is too troublesome, and there are better ways to look at the Code:

 

Code
Namespace delegatetest
{
Class person
{
Public void firstmethod ()
{
Console. writeline ("This is the first method! ");
}
Public void secondmethod ()
{
Console. writeline ("this is the second method! ");
}
}

Delegate void DELE ();

Class Program
{
Static void main (string [] ARGs)
{
Console. writeline ("Name of the Input Method :");
String name = console. Readline ();
Person P = new person ();

// The type parameter is the type of the delegate to be converted, and the P parameter is the instance of the delegate to be called. True indicates case-insensitive.
Dele d = delegate. createdelegate (typeof (DELE), P, name + "method", true) as DELE;
D. Invoke ();
Console. writeline ("Name of the Input Method :");
Name = console. Readline ();
D = delegate. createdelegate (typeof (DELE), P, name + "method", true) as DELE;
D. Invoke ();
}
}
}

 

The following figure shows the running result:

 

Next is a feature that C # is often ignored, namely the anonymous method. Although the anonymous method is rarely used, it is better to know than to know. Taking delegate as an example, we usually assign a specific method to delegate, but sometimes there is a method to delegate, but the function to be executed is really simple, and it may only be used once, but it is really troublesome to write a specific method for it. C #2.0 provides the anonymous method feature. The Code is as follows:

 

Code
Namespace Anonymity
{
Delegate void DELE ();
Class Program
{
Static void main (string [] ARGs)
{
Dele d = delegate () {console. writeline ("this is an anonymous method! ");};
D. Invoke ();
}
}
}

 

The running result is as follows:

 

The next step is the indexer. Writing technical articles is not as tired as writing novels. You need to design, describe, write code, debug, and, more importantly, press the keyboard. Okay, so far. Indexer allows us to access objects just like an array. What is the role of Indexer? You only need to know when it is used. Check the Code:

 

Code
Namespace indexertest
{
Class personnames
{
List <string> names = new list <string> ();
/// <Summary>
/// Create an index for the object. Note that it is different from the get/set attribute.
/// </Summary>
/// <Param name = "Index"> index value </param>
/// <Returns> </returns>
Public String This [int Index]
{
Get {return Names [Index];}
Set {names. Add (value );}
}
}

Class Program
{
Static void main (string [] ARGs)
{
Personnames names = new personnames ();
Names [0] = "Microsoft ";
Names [1] = "google ";
Names [2] = "Baidu ";
For (INT I = 0; I <3; I ++)
{
Console. writeline (Names [I]);
}
}
}
}

 

Unfortunately, it does not support foreach. To implement the sending function, you can implement the ienumerable interface.

 

Next is ...? What's next? I'm tired of writing it. Next we will use a hash table (hashtable). We all know that hashtable is very useful. You may find that it does not seem to support foreach. In fact, it can be delivered on behalf of, but it is a little different, look at the Code:

 

Code
Namespace hashtabletest
{
Class Program
{
Static void main (string [] ARGs)
{
Hashtable table = new hashtable ();
Table ["name1"] = "Microsoft ";
Table. Add ("name2", "Google ");
Table ["name3"] = "Baidu ";
// Note the type name of name
Foreach (dictionaryentry name in table)
{
Console. writeline (name. Key + ":" + name. value );
}
}
}
}

 

The running result is as follows:

Let's take a closer look. The returned type is dictionaryentry rather than the expected string. Why? Unlike other collections, hashtable saves not only values but also keys, which are saved as dictionaryentry in hashtable. Let's review the above indexer so that we can break it out. hashtable may be a class that implements the function of the indexer (not to scold me, I guess it too ).

Now, I have finished writing it. I hope it will help you.

From: http://www.cnblogs.com/reallypride/archive/2008/09/08/1287095.html

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.