Introduction to C#
What is a C#?
C# is one of the object-oriented language that enables developers to build applications. You can use C# to create Windows client applications, XML Web services, distributed components, client-server applications, database applications, and more.
What tools do you need to develop C# application?
There I is a wide variety of tools that developers can use while developing
applications based on C#, such as visual studio which is the preferred one, MonoDevelop, TextPad, and notepad.
Why Visual Studio is the preferred tool for developing C# applications?
Visual Studio is an integrated development environment (IDE) that provides an advanced code editor, convenient user interface designers, integrated debugger, and many other tools to make it easier to develop applications based on the C# language.
Where to download Visual Studio?
You can download visual studio on the SAIT website by clicking on this link:https://e5.onthehub.com/WebStore/ProductsByMajorVersionList.aspx?cmi_cs=1&cmi_mnuMain=bdba23cf-e05e-e011-971f-0030487d8897&ws=74d8e40a-fe6f-e011-971f-0030487d8897&vsro=8 .
After downloading and installing visual studio, you can create your first C# application.
Your first C# application:
To create an application, follow these steps:
· Start Visual Studio.
· On the menu bar, choose File -> New -> Project.
· Choose Visual C# from templates, and then choose Windows.
· Choose Console Application.
· Specify a name for your project.
If everything is fine, you should a window like the one bellow:
Click OK button.
This is your first program. Let’s add some line of code to print “Hello Word to the console”
Add this code in the main method:
/* my first program in C# */
Console.WriteLine (“Hello World”);
Console.ReadKey ();
Click on start to run your program.
As you can see, your program says “hello word in the console”.
After creating our first program, let’s look at some concepts.
What is the structure of a C# program?
A C# program consist of the following parts:
- Variables
These are identifiers that you create to hold values or references to objects in your code. A variable is essentially a named memory location
- Methods
Methods are pieces of functionality in an application. In C#, the main method is the starting point of all programs.
- Classes
As we have seen with java, classes are the blueprints for reference types. A class contains the data and method definitions that a program uses.
· Namespaces
A namespace is a collection of classes. It helps to avoid naming collisions in applications that may contain classes with the same name.
Example: in our first program the namespace is helloWord and this namespace contains the class program.
Good to know:
· C# is a case sensitive language.
· All statements and expression must end with a semicolon (;).
· Unlike Java, class name could be different from program file name.
What does means CLR (common Language Run-Time)?
A little bit history about C#:
Before C#, we had two languages in the C family, which are C/C++.
With either of these languages, when we compile our applications, the compiler translate our code into the native code for the machine on which it was running. That means if I wrote an application in C++ on a windows machine, the compiler will translate that code into the native code for that machine.
The problem is that we have different operation systems and different hardware. That application will not run on Linux for instance.
C# language and the .NET framework came up with an idea that they borrow from java. In java, when you compile your code it is not translated in machine code, it is translated into an intermediate language called bytecode, and we have the exact same concept in C# called IL (Intermediate Language) code.
When you compile your C# application, the result is IL code. CLR is an application sitting in the memory that translate IL code to machine code.
In the previous paragraph, I talked about C# and .NET framework. You might want to know the difference between C #a .NET framework.
C# vs .NET?
C# is an object oriented programming language as we have seen before, while .NET is a framework for building applications on windows.
.NET framework is not limited to C#. There are different languages that can target .NET framework and build applications, such as F#, VB.NET.
If you are new to C#, you might want to know the data type available on C#?
Primitive data types
This table shows the common used primitive types in C#
Good to know:
· If you want to declare a float, you have to add “f” at the end of the number.
Float number = 2.324f;
· If you want to declare a decimal, you have to add “m” at the end of the number.
decimal number = 2.324m;
Complex Data Structures:
· Array (same as we have seen in java)
· Enumeration (Enum)
· Structure (Struct)
Thank you for reading my post. I hope that you have learned something interesting.
What coming next?
In my second post, after explaining complex data structure Structure, I will also explain some concepts of the object oriented programming in C# (Inheritance, polymorphism, Encapsulation, and more).