18 Rare C # keywords, how many do you use?

Source: Internet
Author: User
Tags garbage collection new set

1, __arglist

Let's start with the __arglist first. __arglist is used to transmit parameters to a method. Usually we pass parameters to the method by the parameter list specified in the function header. If we want to pass a new set of parameters to the method, we need to overload the method. If the number of arguments we want to pass is uncertain, we need to use the Param array. Why do we use __arglist, because of the two methods above, the following problems exist:

A, if we use method overloading, we will add a new function overload once we pass a new set of parameters.

b, if we use a param array, the parameter types must be the same or use an array of Param objects.

__arglist can solve the above problem. You can pass any argument to the function, which can be any type, and we can use the simple steps to parse each parameter.

Let's first look at the following code: 1 public int paramlength (__arglist)
2 {
3 ArgIterator iterator = new ArgIterator (__arglist);
4 return iterator. Getremainingcount ();
5}

If I use the following code to call the function 1 int x = this. Paramlength (__arglist, 6, "Manimoy")); Returns 5

will be returned 5 to the variable x. This is because we have passed 5 parameters to this method. We can access each parameter, such as: 1 typedreference tf = iterator. Getnextarg ();
2 Typedreference.toobject (TF)

Each call to Getnextarg,getremainingcount will be reduced by 1 until each object of the iterator is traversed.

2, __refvalue

Another interesting key word is __refvalue. It is used to get the value of a reference object. You can use it to get the actual object from the TypedReference object. This requires two parameters, the first is the TypedReference object and the other is the type of conversion. Look at the following code: 1 int tfvalue = __refvalue (tf, int);

3, __makeref

__makeref will cause the TypedReference object to be identified from the object. It's just the opposite of _refvalue. See the following code: 1 String name = "Ayan";
2 TypedReference tf = __makeref (name);

4, __reftype

__reftype is used to derive types from objects of type TypedReference. Look at the following code: 1 Type t = __reftype (TF);
2 if (t.tostring (). Equals ("System.String"))
3 String str = __refvalue (t, string);

Note: Although I found these keywords in all C # versions, I didn't use it in my work. In future versions of C # may not exist in these keywords, so use it to look at your own risk.

5, Yield

Yield appears in. NET2.0, and yield is returned as an enumerator object value, and the yield statement can only appear in the iterator block, which can be used as a method, operator, or accessor body.  In the following code, I built a list of names that would return a list of names that were less than 5 in length and encountered Yieldbreak statements. 1 List <</span>string> lst = new list<</span>string> ();
2 lst. ADD ("Abhishek");
3 lst. ADD ("Abhijit");
4 lst. ADD ("Manimoy");
5 lst. ADD ("Raj");
6 lst. ADD ("Ayan");
7 LST. ADD ("Macmillanrojer");
8 lst. ADD ("Rizzuto");
9 foreach (string x in LST)
10 {
One if (X.length >)//Breaks on Macmillanrojer
Yield break;
5 else if (X.length >)//only returns those which are have Length >5
yield return x;
or else continue;
16}
17

In fact, yield return x will judge each element and create a enumerable (length>5) that satisfies all the elements of the condition.
The break statement terminates the loop and returns an existing, created enumeration.

6. Fixed

Another uncommon keyword is fixed, which can only be used in unsafe C # code blocks. The fixed statement sets a pointer at a constant memory address, and it is not moved anywhere, even if the garbage collection thread is executed. Let's take a look at the following code: 1 int [] a = new int [] {1, 2, 3};
2 Fixed (int * pt = a)
3 {
4 int

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.