C# was created around 2000 by Microsoft as part of it's .Net platform. It's an object oriented , general purpose language. It borrows heavily from C and Java in terms of syntax and operation. The .Net platform is an implementation of the Common Language Infrastructure (CLI ) .
The CLI is a formal specification developed by Microsoft that describes a runtime environment and and allows high level languages to be run on a platform independent of the underlying hardware. C# is one of the languages that is supported on the .Net platform ( others being J# , VB.Net) .
Let us study how a computer program is executed on a computer. The diagram is somewhat simplistic and offers an overview of how a computer will work in general.
The instructions for the program are stored in the RAM and the CPU grabs an instruction from the RAM and executes it. It will then grab the next instruction and execute that and so on. What type of instructions are these ? Well these are the instructions that the CPU understands. In case of an Intel CPU the machine language is based on x86 . That is the only thing the CPU understands. It does not know about C++ , Java or any other language. It only knows about the x86 machine language. What do the instructions of this machine language look like ?
000000 00001 00010 00110 00000 100000
100011 00011 01000 00000 00001 000100
As you can imagine writing machine language instructions is a tedious and impractical task. Programmers
used Assembly language instead to do their programming. So how did the CPU run Assembly language .
Well it didn't. The Assembly language program was converted to machine language by a compiler.
What did the Assembly language instructions look like ?
MOV AL, 1h ; Load AL with immediate value 1
MOV CL, 2h ; Load CL with immediate value 2
MOV DL, 3h ; Load DL with immediate value 3
The Assembly language instruction had one to one correlation with machine language and was
slightly better in terms of using opcodes but still a tedious task. So now the high level languages
came such as Fortran, Pascal, C . For a long time programming was done in these high level
languages . These language had features like functions, procedures , structures. A programmer could
use variables and did not have to worry about mapping variables to specific addresses in memory.
However these languages involved the programmer converting the application problem in
terms of the language which still bore a close relationship to the way the CPU worked. The
concept of object oriented languages was developed and thus came languages such as Java,
C# and C++ . Now a programmer could translate the application problem in terms of objects and
classes. The way we think about problems and concepts can be correlated and organized better with
object oriented programming. As an example think of an application for the bank . We can think of
a customer with properties such as name, age, gender, address, phone. This customer could have an
account and the account could be checking or saving. An account can have properties like amount and
account number. It could also have methods like add a deposit and so on. This sort of technique can
be used in many problems where we can model the solution in terms of classes to more closely resemble
the problem domain.
.Net Framework is a software solution developed by Microsoft for a wide variety of applications from stand alone apps, Web development, Database connectivity, GUI apps, network communications and so on. It consists of something called CLR ( Common Language Runtime ) that is a software virtual machine and is software that is independent of the hardware. It takes a program that is in Microsoft Intermediate Language (MSIL) and executes that program. The program ( MSIL) does not correspond to a machine language but are instructions that are made up for the virtual machine ( CLR ) . We write a program in a high level language such as Visual Basic, CSharp and it gets compiled to MSIL code that is then run by the CLR. A project can be written in different languages because they will be compiled to MSIL and interoperate with each other. When writing the .Net program say in C# we have at our disposal a wide variety of libraries consisting of classes. Microsoft provides Visual Studio that is a software tool that can be used to develop programs. It also provides IIS Server for Web Application development. If you have programmed in Java with the Java Virtual Machine and Byte Codes then the .Net concept is very similar.
Let us see how a small C++ program is compiled and run.
File: "hello.cpp"
#include <iostream>
using namespace std ;
int main()
{
cout << "Hello World" << endl ;
return( 0 ) ;
}
We compile it:
g++ -o hello.exe hello.cpp
Now we have a "hello.exe" . We can run this on our machine. It contains machine language code. If we give it to someone with the same operating system and the same CPU as ours then that person can run the file. The other person does not need anything else ( software installed to run it ) . However if we give it to a person who is on a different operating system or a different CPU then the program will not run. The executable contains operating system specific code as well as our CPU machine code.
C++
Program --> Compiler --> Executable ( machine language code , operating system specific instructions )
This is slightly different from the way a DotNet program runs
C# Program --> Compiler --> MSIL Code ( Dotnet runtime runs the code )
The compiler in a Dot Net environment does not produce an executable but rather a program ( can be .dll or .exe ) that contains virtual bytecodes called MSIL ( Microsoft Intermediate Language) . We then run the program and the DotNet environment takes those MSIL codes and transforms them into executable machine language code and runs them. We can even take the MSIL code and be able to run it on another operating system with another CPU though Microsoft has not been as successful in compatibility on different operating systems in reality .
To download the .Net core for Mac, Unix , Windows.
https://dotnet.microsoft.com/download
The .Net Framework can be used to develop certain kind of applications for Windows that are not
available for .Net core.
https://dotnet.microsoft.com/download/dotnet-framework
We will be working with C# 3.0 so you can obtain .Net Framework 2.0 or higher or .Net Core 2.0 or higher. We will be
using command line "csc.exe" from .Net Framework to compile our programs. Please view the documentation on
.Net Core and Visual Studio to obtain more information about compilation than what is covered in this course.
Hello World on Mac .Net core
After installation your .Net will probably be installed in the folder "/usr/local/share/dotnet/" . Create a
folder for your
dotnet project. We will call our folder "csharp" . Open up the terminal and make sure our path
includes the "dotnet" path.
We can do something like:
PATH=$PATH:/usr/local/share/dotnet/
After running the above command we can "cd" to our "csharp" folder and then run the command:
dotnet new console -o hello
This will create a new project that is console based in a new directory called "hello" . Let's "cd" to that
folder and the run the command.
dotnet run
This will run our project and the output would be:
2030008434:hello ajay.mittal$ dotnet run
Hello World
In the "hello" folder we should be able to find the file:
File: "Program.cs"
using System;
namespace hello
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
Hello World on Windows .Net Framework
After installation the "csc.exe" will be in the folder
"Microsoft.NET\Framework\" under the main "Windows" folder. Add this path to the command prompt
or to the
environment variable. We can create a folder say "CSharp" somewhere on our drive. It doesn't
really matter where. Let's assume the path to the "csc.exe" of the .Net installation is at:
"C:\Windows\Microsoft.NET\Framework\v4.0.30319"
Once we open the Dos command prompt and navigate to the folder where we want to keep our source files such as in "C:\Csharp" we can set the path with the command:
set PATH=C:\Windows\Microsoft.NET\Framework\v4.0.30319;%PATH%;
If we don't do this we will receive an error of the form:
'csc' is not recognized as an internal or external command, operable program or batch file. We are allowing the command prompt to locate the file "csc.exe" .by setting the path. We can also place the command in a file say "set1.bat" and place this file in the same folder as the source files "C:\Csharp" . If we open the command prompt again then we just need to run the batch file with the command:
C:\CSharp>set1.bat
Create a file called "Program.cs" with the following contents:
File: "Program.cs"
using System;
namespace hello
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
Compile this file with the following command:
csc Program.cs
This will produce an executable called "Program.exe" . This consists of MSIL code but contains an
entry point to the DotNet framework. When we
run the program the DotNet framework takes the MSIL code and translates them on the fly to
machine code and runs the code. We cannot just
hand over the ".exe" to another computer that does not have the DotNet environment and
expect that our program will be run.
Create a new Project and we can call it anything we want. Say we call it "ConsoleApplication1" .
Choose
"Visual C#/Windows/Console Application" from the installed templates
Use the above program to "Program.cs" to print "Hello World" to the console.
We will briefly examine the simple program that prints "Hello World" to the screen.
File: "hello.cs"
using System;
namespace hello
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
The name of the file can be anything. It does not have to be the name of the class defined in the
program.
The line :
using System:
states that we want to use the classes inside the namespace "System" . A namespace is like a box that we can place our classes in order to avoid
name conflicts . It allows for better organization of our programs.
The line:
namespace hello
states that we are declaring our own namespace called "hello" and in this namespace we are
creating our class
"Program" .
The line:
class Program
declares a class by the name "Program". This is a user defined type .
The line:
static void Main(string[] args)
defines the entry point for the program. This is where the execution starts. The method has the word "static" before it. This means that we can call the method directly and do not need to create an object of the class that the method is in. The "static" keyword will be studied later on.
The line:
Console.WriteLine("Hello World!");
uses the class "Console" that has a static method called "WriteLine" to write a string to the console.
Let us examine the different options we have with "csc.exe" .
File: "hello.cs"
using System; public class Program { static void Main(string[] args) { printDemo.printHelloWorld() ; } }
File: "printDemo.cs"
using System; public class printDemo { public static void printHelloWorld() { Console.WriteLine("Hello World!"); } }
In the above we have 2 files and the "hello.cs" is actually calling a function in the other file. So we have 2 files that
need to be compiled.
csc hello.cs printDemo.cs
This produces the executable file "hello.exe" that can then be run.
C:\CSharp\MultipleFiles>hello.exe
Hello World!
We can choose to name the
output executable to something other than the default.
csc -out:my.exe hello.cs printDemo.cs
We can use the "-out:" option to state that the output file should be "my.exe" instead of the default..
We have created the executable above. This executable is an assembly. An assembly is a collection
of classes The assembly can be an ".exe" or it can be a ".dll" . Let us create an example that produces a "dll" instead of an "exe". The "dll" is more of a library that can be reused.
File: "student.cs"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class student
{
public static void printStudent()
{
Console.WriteLine( "Inside the printStudent method." ) ;
}
static void Main(string[] args)
{
Console.WriteLine("Hello, world!");
Console.ReadLine();
}
}
Run the below command:
csc -out:student.dll -t:library student.cs
The "-t:library" option specifies that we want the resulting file to be a library.
File: "myprogram.cs"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class myprogram
{
static void Main(string[] args)
{
student.printStudent() ;
Console.ReadLine();
}
}
The above file is created in the same folder with the file "student.cs" . We can see that it uses the student class.
csc /r:student.dll myprogram.cs
In the above command we are stating that with the "/r" option we are referencing the library "student.dll". The file "myprogram.cs" is compiled to an executable that can be run.
C:\CSharp\assembly>myprogram.exe
Inside the "printStudent" method.
.
Comments are placed in a program to improve readability . They are not part of the compiled code and do not affect the logic in your program. Their purpose is to help the person reading the code. There are two types of comments in C#; the single line and multiple line comments.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/*
This is a simple C# program that
prints a line to console.
*/
class hello
{
static void Main(string[] args)
{
Console.WriteLine("Hello, world!");
Console.ReadLine();
}
}
//This is a single line comment.
1)
Convert the hexadecimal "DA" to decimal.
Convert the binary "1011" to decimal .
Convert the decimal number 15 to binary.
2)
Correct the errors in the below program:
using System class hello ; { static void Main( ) { Console.WriteLine("Hello, world!"); Console.ReadLine( //Comment ); } }
1)
18
11
1111
2)
using System ;
class hello
{
static void Main( string[] args )
{
Console.WriteLine("Hello, world!");
Console.ReadLine( ); //Comment
}
}