Chapter 2
Chapter 2
Basics of C# Programming
By
Dr. Sara Tarik
The C# Language
This is a sample text, Insert your desired text here this is a sample text.
■ The C# programs consist of one or several files with a .cs extension, which contain definitions of
classes and other types.
■ These files are compiled by the C# compiler (csc) to executable code and as a result assemblies are
created, which are files with the same name but with a different extension (.exe or .dll).
■ For example, if we compile HelloCSharp.cs, we will get a file with the name HelloCSharp.exe.
■ The main program elements in C# are classes, methods, operators, expressions, conditional
statements, loops, data types, exceptions and few others.
■ C# has a rich set of keywords that are reserved and cannot be used as identifiers (variable names,
class names, etc.). These keywords are used in the definition of the program elements. Below are
examples of the main keywords in C#:
■ Be careful when writing the keywords like class, static, void, int, etc.
■ The same thing, written in upper case, lower-case or a mix of both, means different things in C#.
■ Writing Class is different from class and void is different from VOID.
■ This rule applies to all elements of your program: keywords, names of variables, class names etc.
■ All C# keywords are lowercase (e.g., public, class, void), while namespaces, class begin (by
convention) with an initial capital letter and have capitalized the first letter of any embedded words
(e.g., Console.WriteLine, System.Windows. MessageBox, System.Data.SqlClient).
WhirlWind POW ERPOINT TEMPLATE | Email : [email protected] | Web : www.example.com 4
Integrated Development Environment
This is a sample text, Insert your desired text here this is a sample text.
■ IDE is a tool where we write code, compile it, run it, test it, debug it, etc. and everything is integrated
into a single place.
■ Development environments integrate in them a text editor for writing code, a programming
language, a compiler or an interpreter and a runtime environment for executing programs, a
debugger for tracking the program and seeking out errors, tools for user interface design and other
tools.
■ For programming with the C# language the most commonly used IDE is Visual Studio, which is
developed and distributed freely by Microsoft and can be downloaded from: https://www.
visualstudio.com/downloads.
WhirlWind POW ERPOINT TEMPLATE | Email : [email protected] | Web : www.example.com 5
What Is Visual Studio?
This is a sample text, Insert your desired text here this is a sample text.
■ Visual Studio is a powerful IDE for developing software applications for Windows and the .NET
Framework platform.
■ Visual Studio supports different programming languages (for example C#, VB.NET and C++) and
different software development technologies such as Win32, ASP.NET, ADO.NET, Windows Forms,
WPF, Silverlight, Windows Store apps and many more Windows and .NET technologies.
4) Click Next.
6) Click Create.
■ The name 1Class is not a valid identifier because it begins with a digit, and the
name My Class is not a valid identifier because it contains a space.
WhirlWind POW ERPOINT TEMPLATE | Email : [email protected] | Web : www.example.com 20
Main Method Definition
This is a sample text, Insert your desired text here this is a sample text.
■ Modern communication methods are many and various: they can be through graphical or web-based
interface, console or others.
■ The console remains an irreplaceable tool for communication with the user espicially when:
● We write small and simple programs where it is necessary to focus the attention on the specific
problem to be solved, rather than the elegant representation of the result to the user.
● We want to test a small piece of code for a larger application. Due to simplicity of the operation of
the console application we can isolate this part of the code easily and comfortably without having
to go through a complex user interface and a number of screens to get to the desired code for
testing.
■ In C#, the console output is used to display information to the console window.
■ The Console class in the System namespace provides methods to output text and data to the console.
■ Console.WriteLine(…) method prints a line of text and moves the cursor to the next line. It is used to
print various data types because for each type there is a predefined version of this method in the
Console class.
Output:
■ On the other hand, Console.Write(…) method prints a line of text without moving the cursor to the
next line.
Output:
■ The string in the parentheses, between the double quotation marks, in line 8 is the argument to the
method.
WhirlWind POW ERPOINT TEMPLATE | Email : [email protected] | Web : www.example.com 31
Program 1: Displaying a Single Line of Text
This is a sample text, Insert your desired text here this is a sample text.
■ Formatting is adding characters such as spaces, tabs and new lines, which are insignificant to the
compiler and they give the code a logical structure and make it easier to read.
■ The program code must be correctly formatted:
● All class and method names must start with a capital letter.
● Variable names must begin with a lower-case letter.
● We must follow several important rules regarding indentation: when some piece of code is
logically inside another piece of code, it is indented (moved) on the right with a single [Tab]. For
example, 1) Methods are indented inside the definition of the class (move to the right by one or
more [Tab] characters). 2) Method contents are indented inside the definition of the method. 3)
The opening curly bracket { must be on its own line and placed exactly under the method or class it
refers to. 4) The closing curly bracket } must be on its own line, placed exactly vertically under the
respective opening bracket (with the same indentation).
WhirlWind P O W E R P O I N T T E M P L A T E | Email : [email protected] | Web : www.example.com 35
Main Formatting Rules
This is a sample text, Insert your desired text here this is a sample text.
■ The examples below show the difference between the formatted and unformatted code.
A properly formatted code The code is written without tabs
■ The unformatted code with inconsistent indentation, spacing, and alignment will compile and run
exactly like the formatted code but they are more difficult to read and understand, and therefore
difficult to modify and maintain.
WhirlWind POW ERPOINT TEMPLATE | Email : [email protected] | Web : www.example.com 36
Compiling the Source Code
This is a sample text, Insert your desired text here this is a sample text.
■ When our program contains errors, also known as bugs, we must find and remove them, i.e. we need
to debug the program.
■ To stop the execution of the program at designated positions we can place breakpoints.
■ The breakpoint is associated with a line of the program.
■ The program stops its execution on the lines with breakpoints, allowing for the rest of the code to be
executed step by step.
■ On each step, we can check and even change the values of the current variables.
■ Debugging is a sort of step by step slow motion execution of the program. It gives us the opportunity
to easily understand the details of the code and see where exactly and why the errors have occurred.
■ To do so, first, stop the execution of the program before it is finished. We select Debug –> Stop
Debugging or press [Shift+F5]. After that we delete the problem line and start the program in
normal mode (without debugging) by pressing [Ctrl+F5].
■ Comments are explanatory statements that can be included in the C# code to help anyone reading
the source code (improve code readability).
■ All characters available inside any comment are ignored by the C# compiler. So, they do not cause the
computer to perform any action when the app executes.
■ There are two common types of comments in C#: single-line comments and multi-line comments.
■ The single-line comment starts with // , terminates at the end of the line on which it appears, and can
be placed anywhere in the line.
■ The multiline (delimited) comments starts with the delimiter /* and ends with the delimiter */.
■ Delimited comments can be split over several lines and all text between the delimiters is ignored by
the compiler.