0

I'm not even sure how should I frame this question so that you all get what actually I'm asking for.

I'm wondering how does the keywords work in programming languages, being specific, C#. In the below code:

using System;
namespace TestApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "Hello";
            Console.WriteLine(a.ToString());
            Console.ReadLine();
        }
    }
}

Here, Console is a predefined class of System namespace which lies in mscorlib.dll. So when the compiler/CLR meets the Console.WriteLine(), it will invoke the static method WriteLine() with appropriate overload.

So the definition of WriteLine method and Console class all are already written and kept in the System namespace of the mscorlib assembly.

But my question is when the complier/CLR meets with the keywords like using,namespace, class,static, what does it do? Where is it written that it must treat the word next to the class keyword as a new type? Is it built in to the compiler/CLR? How does it work then?

Peter Ritchie
  • 35,463
  • 9
  • 80
  • 98
snippetkid
  • 282
  • 4
  • 16
  • 2
    The keyword is `keyword`. The compiler knows when it comes across a word like `class` that it has to do something with what follows (compile it into a new class). This is programmed into the compiler and spelt out in the C# language specs. – Matt Burland Sep 29 '14 at 17:18
  • 1
    Check out "dragon book" or any other book/article on compilers... – Alexei Levenkov Sep 29 '14 at 17:18
  • Feel free to [download the spec](http://www.microsoft.com/en-us/download/details.aspx?id=7029) if you really want to. – Matt Burland Sep 29 '14 at 17:19
  • 1
    possible duplicate of [Learning to write a compiler](http://stackoverflow.com/questions/1669/learning-to-write-a-compiler) – GSerg Sep 29 '14 at 17:19
  • Thanks for all the answers and comments. Got to know a lot of things I am unaware about! – snippetkid Sep 30 '14 at 06:34

2 Answers2

2

C# keywords, which you can see the full list of here, are built into the CSC compller. When the compiler comes across any keyword, it is programmed to know what to expect and what to do.

Icemanind
  • 47,519
  • 50
  • 171
  • 296
1

This is part of the compiler, not the BCL. Check out the language specification which explains exactly what the compiler must do when it encounters any of the keywords.