As we know that now a days C# plays a major role in most of the Web technologies to write the server side code, Ex: ASP.NET, ASP.NET MVC etc., Lets see Language Basics of C# in this chapter.
Introduction
Hello friends, we have seen creating a simple
Hello program in
C# in the
previous chapter. Lets see some more language basics of C# in this chapter.
Language Basics of C#
We have already seen statement blocks
, console
and comments
that are being used in C# in the previous chapter.
What is a source code?
-Source code is a set of files with .cs as extension. Ex: Program.cs
What is Assembly?
-Assembly is a Packaging and Deployment unit of .NET such as applications
or libraries
.
What is a Library?
-Library is a file without an entry point which is called(referenced) by an application
or other library
. These files are extended with .dll.
.NET Framework is a set of libraries.
Compilation:
C# Compiler compiles the source code into an Assembly. '
csc.exe
' is the name of
C# compiler. We can use
IDE(Integrated Development Environment) such as
Visual Studio or calling it from the command line manually.
We have already seen compiling with Visual Studio in the before chapter and if we want to compile it using command line,
- Save a program with
.cs
extension such as Program.cs
.
- Now go to the Command and invoke csc which is located at,
RootFolder(Windows Directory)\Microsoft .NET\Framework\
i.e, csc Program.cs
- This will create an application named Program.exe.
- Now, do the following to create a library.
csc /target:library Program.cs
Lets have a simple program like below to explain Identifiers and Keywords,
using System;
class Addition
{
static void Main()
{
int a = 15 + 25;
Console.WriteLine (x);
}
}
In the above code, we are performing addition operation.
Identifiers:
Identifiers(names) are the being chosen by programmer for his requirements such as variables
, methods
, classes
etc.
System,
Addition,
Main,
a,
Console,
WriteLine, are the Identifiers in the above program.
- Identifier must be a complete word, which is made up of Unicode characters that are started with a
letter
or underscore
. - In C#, Identifiers are case-sensitive.
- Generally, local variables, parameters and private fields should be in Camel case(Ex: myVariable) and remaining should be in Pascal case(Ex: MyMethod).
Keywords:
Keywords are the reserved names of the compiler which we can't use as Identifiers.
Using, class, static, void, int are the Keywords in the above program.
Below are the list of Keywords used in C#
abstract | as | base | bool | break |
byte | case | catch | char | checked |
class | const | continue | decimal | default |
delegate | do | double | else | enum |
event | explicit | extern | false | finally |
fixed | float | for | foreach | goto |
if | implicit | in | int | interface |
internal | is | lock | long | namespace |
new | null | object | operator | out |
override | params | private | protected | public |
readonly | ref | return | sbyte | sealed |
short | sizeof | stachalloc | static | string |
struct | switch | this | throw | true |
try | typeof | uint | ulong | unchecked |
unsafe | ushort | using | virtual | void |
volatile | while |
|
|
|
Contextual Keywords:
There are some keywords which might be used as Identifiers. Those are called as Contextual Keywords.
Below are the Contextual keywords which are also used as Identifiers
add | ascending | async | await |
by | descending | dynamic | equals |
from | get | global | group |
in | into | join | let |
on | orderby | partial | remove |
select | set | value | var |
where | yield |
|
|
Conclusion
In this article, we have seen basics of C# language in which Keywords, Identifiers and Compilation. Hope you understand them clearly. If you have any doubts on this topic, please feel free to ask in the below comments.
Thanks for reading,
Regards,
Krishna.