Introduction to C# |
|
C#, pronounced c sharp, is a computer language used to give instructions that tell the computer what to do, how to do it, and when to do it. This is a universal language that, although currently mostly used on the Microsoft Windows operating system, will be also used on other operating systems like Linux, BeOS, Macintosh, etc. C# is one of the languages used in the Microsoft .NET Framework. The Microsoft .NET Framework is a library of objects that create or draw things on the computer. The C# language is used to create applications that display on a black window referred to as the DOS prompt or DOS window.
An object, any object, is made of different parts. For example, a car is made of tires, doors, wires, belts, etc: In computer languages, such an object is called a class. In the C# language, everything is built as a class. Therefore, a program starts with the class keyword followed by a name. Here is an example: class Car The actual building of a class starts with an opening curly bracket "{" and ends with a closing curly bracket "}". Therefore, a class can be created as follows: class Car { } Because a class can be made of numerous parts, such as tires, doors, seats, etc, listed as sub-objects of a class, one line could become (too) crowded. Therefore, each curly bracket should be displayed on its own line: class Car { } The assignments of a program written in C# are carried in entities called methods. A method is simply a section of code that takes care of a particular detail for the functionality of the program. To create a method, you must specify its name, which starts with a letter or an underscore and can be followed by letters, digits, or underscores. The name of a method is followed by an opening and a closing parentheses. Here is an example: class Car { Drive() } A method's job is to carry a specific assignment within a program. As such, it could provide a value once the assignment has been carried. In Lesson 8, we will review the types of values that a method can provide. If on the other hand a method doesn't give back a value, then it is considered void. The type of value that a method can provide (or return) is written on the left side of the method name. An example would be: class Car { void Drive() } The assignment that a method carries is included between an opening curly bracket "{" and a closing curly bracket "}". Therefore, a method can be written as follows: class Car { void Drive() { } } Indentation is a technique of specifying where a line of code starts and where it ends. Although indentation is not part of the C# language, it makes the program easier to read. Therefore, the above lines of code can be written as follows: class Car { void Drive() { } } Note that this time, it is easier to know where the class starts and ends, and where the method starts and ends. In some occasions (we will see when and why), we will need to write a method as "static". The most regularly used method of a C# program is called Main. Therefore, the minimum you can write a program with, is as follows: class Car { static void Main() { } } A computer program is started in a regular computer file. A file for a C# program has the extension .cs. To make your programming fast, the C# language ships with many primary files that allow you to lay a foundation for your program.
When the computer starts, there is no primary visual or graphical effect. The programs must first be loaded into memory. In fact, in the beginning, the Microsoft operating system started as text-based with DOS 2.2, 3.3, 5.0, and 6.2 (6.2 was never a big hit like 5.0 because Windows 3.1, Windows 3.2, and Windows 3.3 was released at the same time, bringing the now attractive "Windows" display). To create a program, a programmer entered the source code using a text editor (there was a text editor called EDIT; it was very cute, very basic, and very wonderful; in fact, up to Windows 3.3, it was still the text editor of choice of most programmers and it was used as the editor for QBasic, the parent of Visual Basic). After creating the source file, the programmer had to use a black (or "amber") screen that displayed a blinking caret whose role was to let the user know that it was waiting for a command. A command was a word or a group of words that the programmer typed and pressed Enter (or Return). This caused the computer to produce an action. The command was typed on a line. For this reason, the area that displayed the caret was called the Command Line. Most low-level (low-level doesn't mean weak, to the contrary) programs are still using this concept. This means that, behind the scenes, computer languages such as C/C++, Pascal, Java, C#, etc, are still created and executed as if things were done at the Command Line.
To be an effective programmer, it is important to know how the Command Line works because, as mentioned already, C# programs are compiled at the Command Prompt. If you know how the Command Line works, you can have a better understanding of things that are going on behind the scenes. As its name indicates, the Command Line is a blank line with a blinking caret that expects you to type a command: There are various types of commands used on the command prompt. Some of them are as old as the operating system. Such is the case for commands used to create files or folders, copy files or folders, delete files or folders, format a drive, etc. Some other commands are created by a person like you who needs it for a particular application. For example, the C# language provides a command called csc to compile a program at the command line. In the same way, when you create a program, particularly when you create a source file, you can use some options that would be used at the command prompt.
You may have realized that all of the words we have used so far, such as class, void, or static, are regular English words that you can read and whose meaning you can find in a dictionary. Computer languages are created with meaningful words like that to make them easier to read and understand. Unfortunately, the computer doesn't understand those words and cannot execute the instructions those words represent. Like most other languages, C# is just a language that is used to give instructions to the computer. To make the computer understand these instructions, you need another, somehow more powerful, program. This program would receive the language's instructions in plain English and "translate" them in another language that the computer can understand. This program is called a compiler. A compiler is a computer program made of internal other sub-programs. One of the sub-programs, in fact probably the first, of a compiler is called a parser. A parser "scans" a file that contains (part of) the program. It checks for syntax, keywords, unknown words, and some other routines. If the parser finds a problem, which could be anything, either it stops or it continues making a list of the mistakes it found. Then it displays this list to you to fix. Sometimes it would point to the exact line where the/a problem was found. Sometimes it would point to the line where the problem showed its impact although the problem may be found somewhere else. With experience, you will know how to fix the programs or troubleshoot these problems. On this site, we will address as many issues as possible. If the parser doesn't find any problem, or after you have fixed the problems, it (the parser) passes its result(s) to the compiler. The compiler calls another program called a linker. If the program contains just one file, the linker considers it. If the program contains more than one file (because a program can contain as many files as necessary), the linker considers them. The linker gathers some of the files that the C# compiler shipped with (those files that your program needs in order to work, since your program doesn't need all possible files that C# ships with), puts them together ("links" them) with your file(s) to get your instructions in a manner that can produce a suitable result. If there is no significant problem, the compiler creates the program. This doesn't mean that everything is alright, it only means that the compiler thinks that everything is alright: it is still possible that the result may not be what you would expect. To make your life easier, all of the sub-programs (parser, linker, debugger, etc) that ship with C# are grouped in one large program: the compiler. Therefore, from now on, we will use the word "compiler" to refer to the program you use to "translate" your English-based instructions into a computer-based language. The compiler that ships with the C# version we will use, that is, the compiler of the .NET Framework is a program called csc. Like most other programs, it has the extension .exe. This csc name is not standard. This means that another C# compiler may have another name; csc.exe is just the name of the compiler we will use. |
|
Creating a Program |
On this site, we will use Microsoft Visual Studio .NET to create our programs. To use it, you have two main options. You can start from an empty project and add the necessary files, or you can create a console application using its Option. |
class OrderProcessing { static void Main() { } } |
Namespaces |
Introduction |
|
||
Copyright © 2005-2016, FunctionX | Next | |
|