Click here to Skip to main content
Click here to Skip to main content

UnCommon C# keywords - A Look

, 7 Sep 2009
Rate this:
Please Sign up or sign in to vote.
Some of both Documented and Undocumented Keywords that we dont use often like Check, StackAlloc, Fixed,default,@variables,Nullables, __arglist,__reftype etc are explained with simple examples


CONTENTS 


This is really a weird topic to start with. But still I would like to give you an insight on some of the uncommon things that you may not have noticed while doing programming. I have framed them in 2 sections.

1st one for Undocumented Keywords, which you will not find anywhere, not even in MSDN Documentation, which are not listed to intellesense menu in visual studio.
2nd one for Documented Keywords which are uncommon or just being introduced to C#. Documented keywords which are uncommon can be found over MSDN.

I have also made a sample application where I have given some demonstration of each of the topics mentioned here in this article. If you want to test these, please download the sample application from here :
Download UncommonTestSample_V3.zip - 121.34 KB  

Undocumented Keywords   

Let us discuss some of the undocumented keywords that I found. If you know any keyword other than this please let me know :

1. __arglist 

Lets start with __arglist. __arglist is used to send parameters to a method. Generally we used to send parameters to a function by having a list of arguments specified in the method head. If we want to pass a new set of arguments we need to have Method Overloading. Even when we want to send any number of arguments we might use param array.

Now why should we use __arglist. In case of each of these methods, the problems are :

1. If we use Method Overloading, we might have to add new methods whenever a new set of argument list is thought to be  sent.
2. If we use param array we need to have same type of arguments or need to have param array of objects.


__arglist reveals each of those. It can  send any no of argument to a function. It might be of any type and we can parse each argument easily using simple steps.

Lets have a look at the Code :

public int paramLength(__arglist)
{
          ArgIterator iterator = new ArgIterator(__arglist);
          return iterator.GetRemainingCount();
}


Now if I call the function using this  statement
int x = this.paramLength(__arglist(49,34,54,6,"Manimoy")); // returns 5 


5 will be returned to the variable x. It is because we send 5 arguments to the method. We can access each methods using :

TypedReference tf = iterator.GetNextArg();
TypedReference.ToObject(tf)


On each call to GetNextArg, the GetRemainingCount will decrease by one until it wipes out each objects set to the iterator.

2. __refvalue  


Another interesting keyword you can use instead is __refvalue. It is used to fetch the value from a reference object. You can use this to get the actual object from TypedReference object.

It takes 2 arguments, first one is the object of  TypedReference and the Type in which to cast. Take a look on the line below :

int tfValue = __refvalue(tf, int); 


On the execution of the line tfValue will be assigned to the value of the integer where tf is pointing.

3. __makeref 


Another undocumented keyword is __makeref which will give the TypedReference object from the object itself. This is just the reverse to __refvalue. Take a look at the code below :

string name = "Ayan";
TypedReference tf = __makeref(name);

4. __reftype 

__reftype is used to get Type object from a TypedReference. Have a look at the code below to understand the use :

Type t = __reftype(tf);
if(t.ToString().equals("System.String"))
string str = __refvalue(t,string); 

Note  : Though I found these keywords in all the versions of C#, yet I dont use it in production environment.  There is no sureity of these keywords to be there in future version of C#, so use it in your own risk.


Documented Yet Uncommon 


In this section, I am going to discuss some of the uncommon documented keywords which is not needed very often while we do programs. Let us discuss the most common one here in this section.

1. Yield

Yield is a keyword introduced in .NET 2.0, is used to yield a list of return statements in the form of IEnumerable. The block which yields IEnumerable is commonly called as iterator block.  In the code below, I have created a list of few names and returned the list of all of them which has length less than 5 until it reaches yield break statement for a length >12

List<string> lst = new List<string>();
lst.Add("Abhishek");
lst.Add("Abhijit");
lst.Add("Manimoy");
lst.Add("Raj");
lst.Add("Ayan");
lst.Add("MacMillanRojer");
lst.Add("Rizzuto");
foreach (string x in lst)
{
   if (x.Length > 12) // Breaks on MacMillanRojer
     yield break;
   else if (x.Length > 5) // Only returns those which are having length >5
     yield return x;
   else continue;
}<string><string><string>
</string> </string></string>

Actually the yield return x will evaluate each elements and creates the enumerable of all the elements that satisfies the condition(length>5).
The break statement will terminate the loop and return the existing Enumerable created.

2. Fixed

Another uncommon keyword is Fixed which can only be used in Unsafe C# code blocks. Fixed statement sets the pointer to be in a fixed memory address so that, it will not be moved to anywhere even if Garbage Collection Thread is invoked.  Let us have a look at the code below:

 int[] a = new int[] { 1, 2, 3 };
fixed (int* pt = a)
{
   int* c = pt;
   MessageBox.Show("Value : " + *c);
   // This will fix the variable totally so that it will
   // not be moved when Garbage collector is invoked. 
}

Here, the Pointer c is be assigned the same location as pt.

Fixed often comes at a cost. It is actually hampers the normal process of Garbage collection. Thus if is good to avoid fixed statement if not actually needed.

3 Checked / Unchecked  


Another keyword called checked which is used to control arithmetic overflow context. Checked keyword throws OverflowException when an arithmetic operation overflows the necessary size.

Take a look at the code :

int x = int.MaxValue;
int y = int.MaxValue;
int z = checked(x + y);  

The above statement throws OverflowException when x+y is invoked. The checked is used to check the overflow in arithmetic calculations and throw exception accordingly. z is assinged to 0 when OverflowException occurs.

We might use unchecked keyword when we dont need to throw exception.

int x = int.MaxValue;
int y = int.MaxValue;
int z = unchecked(x + y);

Through execution of above code the value of z will be assigned to -2.

4. Volatile 


Volatile keyword is used to define a variable which is to be modified across multiple threads without invoking lock statements(Although we do lock them most of the times). Volatile variables are not subject to compiler optimization and thus we will get the most updated value of the variable all the time. See the example below :
public volatile int i;
Thread th = new Thread(new ThreadStart(VolatileInvoke));
th.Start();
Thread.Sleep(5000); //Holds current Thread for 5 seconds.
MessageBox.Show("Value of i : " + i);
th.Abort();

private void VolatileInvoke()
{
   while (true)
   {
         i++;
   }
} 

The thread is started and will increment the volatile integer value by 1 until it is aborted by the main thread.

Note : Volatile types dont have Thread optimization.

5. StackAlloc 


It is also used with unsafe C# code which allocates memory dynamically from stack. stackalloc is used to acquire memory quickly when it is very essential memory quickly. We can use the advantage of Fast accessible memory when we use it from Stack. We can declare an array like this :

int* array = stackalloc new int[1000]  

The memory is available as soon as the statement is invoked.

Note: You should always keep in mind, stackalloc memory is very limited. It is by default 1MB for each thread. So if we need huge amount of memory(more than 1MB) we have to rely back to our original Heap memory structure.

6. Global ::  


It is very handy when local namespace hides global namespace. Suppose we have created a class named System in our project. C# allowes to do that. But the problem is that whenever I want to call System that is defined in the global space, we unable to do that without using global::. Let us see the example below :

internal class System
{
        internal class Array :IEnumerable 
        {
            public int Length
            {
                get{return 1000}
            }
            #region IEnumerable Members
            public IEnumerator GetEnumerator()
            {
                Debug.Write("This is Dummy Array");
                return base.GetEnumerator();
            }

            #endregion
        }
}

Now if you want to call System.Array it will call the one defined locally. To call global System we need to use global::System

It is always better to use global:: when you are sure of calling the global namespace. This ensures your code to work even in this sort of weird situations.

7. Namespace/Class Alias  

We use using to define Alias. There are 2 types of Alias:

1. Namespace Alias:
Namespace alias are used when you want to shorten a long namespace. To do that :

using Abhishek = System.Drawing;
public class X 
{
     public X()
     {
        Abhishek.Graphics g = this.CreateGraphics();
     }
}  

Here in the header we made an alias Abhishek of System.Drawing. Thus within the code if we refer to Abhishek, it will be same as referring to System.Drawing

Class Alias:

You can also make use of using statement to define reference to a single class. Say if I write
using Abhishek=System.Drawing.Bitmap;
Abhisek will hold the class Bitmap. We can create Object of Abhishek, access static functions directly.

using Abhishek=System.Drawing.Graphics;
public class X
{
     public X()
     {
            Abhishek g = this.CreateGraphics();
     }
}

Thus Abhishek will be pointing to native Graphics object rather than the entire namespace.

8. extern alias 

Most of us while working with C# have used external control sets. There may come a situation when we want to add 2 versions of dlls in the same application with same Fully Qualified Namespaces. In such cases we need extern alias feature to refer to two different assemblies.
For example:

Suppose we add an assembly x10.dll (V1) which have a class called Y.
We add x20.dll(V2) where we may want to use Class Y.

First of all, to reference the fully qualified assembly we need to declare an alias in command line.
/r:XV1=X10.dll

/r:XV2=X20.dll 

Now to reference that we use

extern alias XV1;
extern alias XV2;

9. ?? (Null coalescing operator)  

Null coalescing operator is used to work with null values. It is introduced in 2.0. See the following :

MyClass x = null;
x = x ?? new MyClass();


?? means if x has null value it will call new MyClass() otherwise it will assign the existing x.

10. @variables 


Generally C# doesn't allow to create variables in the same name as keywords. But there is a way out to this. We can define variable with same name as keywords using @.
Suppose we define

int @int = 10;

That means a variable with name int is declared and assigned a value 10 in it.

11.  Readonly 


readonly is a keyword present in C# which is used to create variables that will not change throughout the program. The variable declared as readonly will be assigned its value only once and it will remain the same value throughout the execution of the object.

To declare a variable as readonly :

public readonly int readonlyvariable = 20; 


This will instruct the program to make the variable 20 and any further re-assign to the variable is not permitted.

12 Difference between Const & readonly ?

readonly is almost similar to const keyword. The only difference is that const variables are defined in compile time, while readonly variables are defined at runtime during object initialization. You can assign readonly variables from within constructors, so based on the constructor call you may assign readonly values differently.

public readonly int readsecond;
public Documented()
{
      readsecond = DateTime.Now.Second;
} 

Here the readsecond will assign values differently based on the object initialization, which is impossible in case of const.

13 Default

Sometimes default keywords comes very handy when working with generic objects. It returns the default value when the object is not initialized. For example, we all know integers are initialized to 0 if not given any value. Characters are Empty when not given any value, objects are null when not assigned any value.

These values are assigned based on default keyword.
Thus if we write :

int x = default(int);//will be assigned to 0

will be same as
int x;

In case of Generic object when the type is undefined, we can use default to assign specific value to the object. Let us look at the sample :

public T GetDefault<T>()
{
     return default(T);
} 

The function returns default value for each individual types sent. Thus

int defx = this.GetDefault<int>(); //will assign 0
char defy = this.GetDefault<char>(); // will assign null('\0')
object defz = this.GetDefault<object>(); // will assign null

Thus we can use default keyword to get the default assignments to the object very easily.

14. Nullable Types  

Nullable types of C# can handle nulls even being primitive data types. Each nullable types are derived from System.Nullable. We can define nullables like this :

int? nullableint = null; 


Thus nullableint will be assigned to null.

If you access nullable variables, you will get 2 properties.

HasValue which returns false if null is assigned to the variable, and Value which returns the actual value of the variable.  You can also use GetValueOrDefault function of each nullable type object to get the default value when null is assigned to the variable.

Conclusion  

This concludes most of the keywords. My intention is to give you a brief knowledge about them and how to use them. Please feel free to comment.

You can try out the sample application  from here
Hope you like this article. For further reference you can read MSDN

History 


First Version : 03rd August 2009
Second Version : 06th August 2009
Fixes : A sample application added and article modified to eliminate language flaws.
Third Version : 16th August 2009
Fixed: Added some more Uncommon keywords.

Fourth Version : 30th August 2009 

Fixes : Added Index, some more keywords.
Fifth Version : 08th September 2009
Fixes : Fixed the mistakes I made.


License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Abhishek Sur
Architect
India India
Did you like his post?
 
Oh, lets go a bit further to know him better.
Visit his Website : www.abhisheksur.com to know more about Abhishek.
 
Abhishek also authored a book on .NET 4.5 Features and recommends you to read it, you will learn a lot from it.
http://bit.ly/EXPERTCookBook
 
Basically he is from India, who loves to explore the .NET world. He loves to code and in his leisure you always find him talking about technical stuffs.
 
Presently he is working in WPF, a new foundation to UI development, but mostly he likes to work on architecture and business classes. ASP.NET is one of his strength as well.
Have any problem? Write to him in his Forum.
 
You can also mail him directly to abhi2434@yahoo.com
 
Want a Coder like him for your project?
Drop him a mail to contact@abhisheksur.com
 
Visit His Blog

Dotnet Tricks and Tips



Dont forget to vote or share your comments about his Writing
Follow on   Twitter   Google+

Comments and Discussions

 
Questionsmall errors PinmemberMember 1036823814-Jul-14 11:11 
SuggestionWhen yield was introduced... [modified] PinmemberCoperNick11-Feb-13 22:37 
GeneralRe: When yield was introduced... PinmemberAbhishek Sur12-Feb-13 12:49 
GeneralRe: When yield was introduced... PinmemberCoperNick19-Feb-13 3:30 
GeneralMy vote of 5 PinmemberIsuru Nanayakkara2-Dec-12 6:59 
GeneralMy vote of 5 PinmemberMark Lemke15-Aug-12 1:41 
GeneralMy vote of 5 Pinmemberfeeza.ibrahim3-Jul-12 21:37 
GeneralMy vote of 5 Pinmemberjim lahey2-Mar-12 5:08 
Very interesting stuff
BugDifference between const and readonly PinmemberSebastian Krysmanski25-Jan-12 21:21 
QuestionNice Article PinmemberMatt U.26-Oct-11 16:51 
AnswerRe: Nice Article PinmvpAbhishek Sur27-Oct-11 9:03 
GeneralMy vote of 5 Pinmemberfmsalmeida7-Oct-11 10:24 
GeneralMy vote of 5 Pinmembergreendragons11-Mar-11 10:12 
GeneralMissing implicit and explicit operators PinmemberThiago Burgo9-Mar-11 16:09 
GeneralMy vote of 5 PinmemberMohammad Elsheimy8-Mar-11 9:48 
GeneralRe: My vote of 5 PinmvpAbhishek Sur8-Mar-11 10:19 
GeneralMy vote of 5 PinmemberStephen Boissiere6-Dec-10 22:22 
GeneralMy vote of 5 PinmemberThiagoMasters1-Nov-10 9:40 
GeneralNice peace of information PinmemberHiren Solanki10-Sep-10 22:06 
GeneralRe: Nice peace of information PinmvpAbhishek Sur12-Sep-10 1:55 
Generalthanksn for the info PinmemberNitin Sawant5-Nov-09 23:39 
GeneralRe: thanksn for the info PinmemberAbhishek Sur14-Dec-09 4:57 
Generalmy vote is 4.5 Pinmembersanjay_dreams19-Sep-09 20:31 
GeneralRe: my vote is 4.5 PinmemberAbhishek Sur20-Sep-09 7:50 
GeneralNot so uncommon... PinmemberJon Rista18-Sep-09 9:54 
GeneralRe: Not so uncommon... PinmemberAbhishek Sur19-Sep-09 9:50 
GeneralExcellent collection on undocumented keyword PinmemberAnjum.Rizwi14-Sep-09 19:14 
GeneralRe: Excellent collection on undocumented keyword PinmemberAbhishek Sur15-Sep-09 12:39 
Questionempty char??? PinmemberIOIOOIIIOOI14-Sep-09 2:51 
AnswerRe: empty char??? [modified] PinmemberAbhishek Sur14-Sep-09 3:18 
GeneralNice one!!! Pinmemberjawed998641500612-Sep-09 11:21 
GeneralRe: Nice one!!! PinmemberAbhishek Sur12-Sep-09 12:58 
GeneralThis is a dangerous article PinmemberSteve Solomon10-Sep-09 22:33 
GeneralRe: This is a dangerous article PinmemberAbhishek Sur11-Sep-09 10:51 
GeneralMy Vote 4.5 Pinmembermanas9-Sep-09 19:16 
GeneralRe: My Vote 4.5 PinmemberAbhishek Sur9-Sep-09 22:33 
GeneralMy vote of 1 PinmemberJohn_Crocker9-Sep-09 3:15 
GeneralRe: My vote of 1 PinmemberK.Collins9-Sep-09 5:18 
GeneralRe: My vote of 1 PinmemberAbhishek Sur9-Sep-09 8:02 
Newsoverflow in checked regions don't throw StackoverflowException PinmemberAndrei Rinea7-Sep-09 7:33 
GeneralRe: overflow in checked regions don't throw StackoverflowException PinmemberAbhishek Sur7-Sep-09 8:37 
GeneralStackAlloc and other mistakes PinmemberS. Senthil Kumar6-Sep-09 21:24 
GeneralRe: StackAlloc and other mistakes PinmemberAbhishek Sur6-Sep-09 21:35 
GeneralRe: StackAlloc and other mistakes PinmemberAbhishek Sur7-Sep-09 8:46 
GeneralChaining the null coalescing operator Pinmemberoliwan18-Aug-09 5:57 
GeneralRe: Chaining the null coalescing operator PinmemberAbhishek Sur19-Aug-09 3:45 
Generalthanks ! Pinmemberalejandro29A18-Aug-09 3:57 
GeneralRe: thanks ! PinmemberAbhishek Sur18-Aug-09 4:11 
GeneralLink to a better discussion on the undocumented stuff PinmemberSDX200017-Aug-09 17:25 
GeneralRe: Link to a better discussion on the undocumented stuff [modified] PinmemberAbhishek Sur18-Aug-09 3:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 7 Sep 2009
Article Copyright 2009 by Abhishek Sur
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid