extern, in and async modifiers in C#

Goud.Kv
Posted by in C# category on for Beginner level | Points: 250 | Views : 5879 red flag
Rating: 4 out of 5  
 1 vote(s)

C# is an Object-oriented programming language. C# comes with simplicity, expressiveness and great performance to meet the programmer productivity.
Recommendation
Read virtual, const and event modifiers in C# before this article.

Introduction

So far, we have seen virtual, const and event modifiers in C#. Now let's see extern, in and async in this chapter.

Objective

The main objective of this article is to learn extern, async and in modifiers in C# programming.

extern

extern simply means external. This is a C# keyword (modifier) which is used to declare an external method (i.e. external implemented method).
It is mostly used in interop services with DLLImport attribute to call into the unmanaged code.

Take a look at the below example which explains it easily,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices; // Interop services namespace
using System.Text;
using System.Threading.Tasks;

public delegate void Handler(); 

namespace Man
{
    class Program
    {
        class External
        {
            [DllImport("User32.dll", CharSet = CharSet.Unicode)]
            public static extern int MessageBox(IntPtr i, string s1, string s2, int type);

            static int Main()
            {
                string Name;
                Console.Write("Please type your message: ");
                Name = Console.ReadLine();
                return MessageBox((IntPtr)0, Name, "Message Box", 0);
            }

        }
    }
}
In the above code, we are using an external method called MessageBox from User32.dll library to get a string from the user and display in a message box.
We have to import System.Runtime.InteropServices to perform interop service.

If you run this code, you will get output something like,


After typing a message, press 'Enter' keyword to display it in the message box.

MessageBox method has seven types of boxes which are changeable like below.
Replace below line in the above code,
return MessageBox((IntPtr)0, Name, "Message Box", 1);
This will gives you a message box like below,


Now try changing it like below,
return MessageBox((IntPtr)0, Name, "Message Box", 2);
This will displays a message box like,


And if you give type as 3 like below,
return MessageBox((IntPtr)0, Name, "Message Box", 3);
It will shows message box like,


For type = 4 such as,
return MessageBox((IntPtr)0, Name, "Message Box", 4);
It will looks like,


And for type = 5 i.e,
return MessageBox((IntPtr)0, Name, "Message Box", 5);
The box will looks like,


Last one is type 6 i.e,
return MessageBox((IntPtr)0, Name, "Message Box", 6);
This will displays a message box something like,


These are the different types of message boxes that present in the MessageBox method of User32.dll library.

in

in is a generic modifier and one of the reserved keywords of C# language which means that no variable identifier can ever be in.
It is mostly used with foreach loop and with some query expressions such as LINQ.

Let's have an example code of using 'in' keyword,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public delegate void Handler(); 

namespace Man
{
    class Program
    {
        static void Main()
        {
            string[] Friends = new string[3];

            Friends[0] = "Ram";
            Friends[1] = "Raj";
            Friends[2] = "Ravi";

            foreach (string name in Friends)
            {
                Console.WriteLine(name);      // prints all the names
            }

            foreach (char c in "Hai")
            {
                Console.WriteLine(c);         // prints H, a, i
            }
        }
    }
}
In the above code, we have two foreach loops with two 'in' modifiers. Here, 'in' plays key role to get enter into that type and perform an action.

The above code will prints the following lines in your console,


async

async means asynchronous. It is used to specify any asynchronous methods.

syntax,
public async void Test() // Asynchronous method
{
    ..........;
}
Both async and await keywords are mostly used for asynchronous and non-blocking methods.
  • async can only return a Void or Task. Example,
    static async Task<int> FunctionAsync()
    {
        ...........;
        ..........;
    }
  • We cannot async and await with the Main method.
  • We can also run the async method as a synchronous method only if it is used without await.

Conclusion

In this article, we have seen extern, in and async modifiers in C# programming. Hope you understand.

Thanks for reading.

Regards,
Krishna.
Page copy protected against web site content infringement by Copyscape

About the Author

Goud.Kv
Full Name: Krishna Vamshi Goud
Member Level: Gold
Member Status: Member,MVP
Member Since: 2/12/2014 2:34:09 AM
Country: India
Thanks & Regards, Krishna


Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)