Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecturer: Mukhtar Mohamed Ali “Hakaale”

Similar presentations


Presentation on theme: "Lecturer: Mukhtar Mohamed Ali “Hakaale”"— Presentation transcript:

1 Lecturer: Mukhtar Mohamed Ali “Hakaale”
Introduction to C# Lecturer: Mukhtar Mohamed Ali “Hakaale”

2 Introduction Welcome to the C#. With the introduction of the .NET framework, Microsoft included a new language called C# (pronounced C Sharp). C# is designed to be a simple, modern, general-purpose, object-oriented programming language, borrowing key concepts from several other languages, most notably Java. 

3 Cont.. C# could theoretically be compiled to machine code, but in real life, it's always used in combination with the .NET framework. Therefore, applications written in C#, requires the .NET framework to be installed on the computer running the application. While the .NET framework makes it possible to use a wide range of languages, C# is sometimes referred to as THE .NET language, perhaps because it was designed together with the framework.  C# is an Object Oriented language and does not offer global variables or functions. Everything is wrapped in classes, even simple types like int and string, which inherits from the System.Object class. 

4 Visual C# Express C# can be written with any text editor, like Windows Notepad, and then compiled with the C# Command line compiler, csc.exe, which comes with the .NET framework. However, most people prefer to use an IDE (Integrated Development Environment), and Microsoft offers several options for this. Their flagship is Visual Studio, which can be used to work on every possible aspect of the .NET framework. This product is very advanced, and comes in several editions. Visual Studio is not exactly cheap, and might even be too advanced for hobby programmers.  For C# programming, you should download the Visual C# Express from  Install it, and you're ready to write your first C# application!

5 First App (Hello, world!)
If you have ever learned a programming language, you know that they all start with the "Hello, world!" example, and who are we to break such a fine tradition? Start Visual C# Express (introduced in the last chapter), and select File -> New project… From the project dialog, select the Console application. This is the most basic application type on a Windows system, but don't worry, we won't stay here for long. Once you click Ok, Visual C# Express creates a new project for you, including a file called Program.cs. This is where all the fun is, and it should look something like this: 

6 Cont …

7 Hello world explained using is a keyword, highlighted with blue by the editor. The using keyword imports a namespace, and a namespace is a collection of classes. Classes brings us some sort of functionality, and when working with an advanced IDE like Visual C# Express, it will usually create parts of the trivial code for us. 

8 Cont … As you can see, we even get our own namespace:
The namespace ConsoleApplication1 is now the main namespace for this application, and new classes will be a part of it by default. Obviously, you can change this, and create classes in another namespace. In that case, you will have to import this new namespace to use it in your application, with the using statement, like any other namespace.  Next, we define our class. Since C# is truly an Object Oriented language, every line of code that actually does something, is wrapped inside a class. In the case, the class is simply called Program:

9 We can have more classes, even in the same file
We can have more classes, even in the same file. For now, we only need one class. A class can contain several variables, properties and methods, concepts we will go deeper into later on. For now, all you need to know is that our current class only contains one method and nothing else. It's declared like this: This line is probably the most complicated one in this example, so let's split it up a bit. The first word is static. The static keyword tells us that this method should be accesible without instantiating the class, but more about this in our chapter about classes. 

10 Cont … The next keyword is void, and tells us what this method should return. For instance, int could be an integer or a string of text, but in this case, we don't want our method to return anything, or void, which is the same as no type.  The next word is Main, which is simply the name of our method. This method is the so-called entry-point of our application, that is, the first piece of code to be executed, and in our example, the only piece to be executed. 


Download ppt "Lecturer: Mukhtar Mohamed Ali “Hakaale”"

Similar presentations


Ads by Google