Unit-1
What is C#?
Introduction
C# is a general-purpose, modern and object-oriented programming language pronounced as “C sharp”. It was developed by Microsoft led by Anders Hejlsberg and his team within the .Net initiative. For users who are familiar with C, C++, or Java, C# is simple since it shares many syntactical similarities with Java a little bit of The .Net Framework .Net applications are cross-platform programs, and their frameworks support C++, C#, Visual Basic, COBOL, and other languages.
Why C#?
C# has many other reasons for being popular and in demand. Few of the reasons are mentioned below:
Easy to start: C# is a high-level language so it is closer to other popular programming languages like C, C++, and Java and thus becomes easy to learn for anyone.
Game Development: C# is widely used in game development and will continue to dominate. C# integrates with Microsoft and thus has a large target audience. The C# features such as Automatic Garbage Collection, interfaces, object-oriented, etc. make C# a popular game developing language.
Widely used for developing Desktop and Web Application: C# is widely used for developing web applications and Desktop applications. It is one of the most popular languages that is used in professional desktop. If anyone wants to create Microsoft apps, C# is their first choice.
Community:The larger the community the better it is as new tools and software will be developing to make it better. C# has a large community so the developments are done to make it exist in the system and not become extinct.
Structure of C# program
A C# program consists of the following parts −
Namespace declaration
A class
Class methods
Class attributes
A Main method
Statements and Expressions
Comments
A simple code that prints the words "Hello World" to show the structure of C# programming.
using System;
namespace HelloWorldApplication {
class HelloWorld {
static void Main(string[] args) {
/* my first program in C# */
Console.WriteLine("Hello World");
Console.ReadKey();
}
}
}
Output
“Hello World”
* Let us look at the various parts of the given program −
The first line of the program using System; - the using keyword is used to include the System namespace in the program. A program generally has multiple using statements.
The next line has the namespace declaration. A namespace is a collection of classes. The HelloWorldApplication namespace contains the class HelloWorld.
The next line has a class declaration, the class HelloWorld contains the data and method definitions that your program uses. Classes generally contain multiple methods. Methods define the behavior of the class. However, the HelloWorld class has only one method Main.
The next line defines the Main method, which is the entry point for all C# programs. The Main method states what the class does when executed.
The next line /*...*/ is ignored by the compiler and it is put to add comments in the program.
The Main method specifies its behavior with the statement Console.WriteLine("Hello World");
WriteLine is a method of the Console class defined in the System namespace. This statement causes the message "Hello, World!" to be displayed on the screen.The last line Console.ReadKey(); is for the VS.NET Users. This makes the program wait for a key press and it prevents the screen from running and closing quickly when the program is launched from Visual Studio .NET.
C# Variables
A variable is nothing but a name given to a storage area that our programs can manipulate. Each variable in C# has a specific type, which determines the size and layout of the variable's memory the range of values that can be stored within that memory and the set of operations that can be applied to the variable.
The basic value types provided in C# can be categorized as −
C# also allows defining other value types of variable such as enum and reference types of variables such as class, which we will cover in subsequent chapters.
Defining Variables
Syntax for variable definition in C# is −
<data_type> <variable_list>;
Here, data_type must be a valid C# data type including char, int, float, double, or any user-defined data type, and variable_list may consist of one or more identifier names separated by commas.
Some valid variable definitions are shown here −
int i, j, k;
char c, ch;
float f, salary;
double d;
You can initialize a variable at the time of definition as −
int i = 100;
C# Data Types
A data type specifies the type of data that a variable can store such as integer, floating, character etc.
There are 3 types of data types in C# language.
Value Data Type
The value data types are integer-based and floating-point based. C# language supports both signed and unsigned literals.
There are 2 types of value data type in C# language.
1) Predefined Data Types - such as Integer, Boolean, Float, etc.
2) User defined Data Types - such as Structure, Enumerations, etc.
The memory size of data types may change according to 32 or 64 bit operating system.
Let's see the value data types. It size is given according to 32 bit OS.
Reference Data Type
The reference data types do not contain the actual data stored in a variable, but they contain a reference to the variables.
If the data is changed by one of the variables, the other variable automatically reflects this change in value.
There are 2 types of reference data type in C# language.
1) Predefined Types - such as Objects, String.
2) User defined Types - such as Classes, Interface.
Pointer Data Type
The pointer in C# language is a variable, it is also known as locator or indicator that points to an address of a value.
Control Statements
The control statements are used to control the flow of execution of the program. If you want to execute a specific block of instructions only when a certain condition is true, then control statements are useful. If you want to execute a block repeatedly, then loops are useful.
If …else statement
The if statement is used to check for a condition and based on it decide whether or not to execute a code block. The if code block is executed only if the condition is true otherwise further instruction or else block gets executed.
switch statement
The switch statement executes only one statement from multiple given statements associated with conditions. If any condition returns true, then the code statements below it get executed. The following diagram shows the basic flow of a switch statement.
Looping in a programming language is a way to execute a statement or a set of statements multiple times depending on the result of the condition to be evaluated to execute statements. The result condition should be true to execute statements within loops.
while loop
The test condition is given in the beginning of the loop and all statements are executed till the given Boolean condition satisfies when the condition becomes false, the control will be out from the while loop.
Syntax:
while (boolean condition)
{
loop statements...
}
for loop
for loop has similar functionality as while loop but with different syntax. for loops are preferred when the number of times loop statements are to be executed is known beforehand. The loop variable initialization, condition to be tested, and increment/decrement of the loop variable is done in one line in for loop thereby providing a shorter, easy to debug structure of looping.
Syntax
for (loop variable initialization ; testing condition;
increment / decrement)
{
// statements to be executed
}
do-while loop
do while loop is similar to while loop with the only difference that it checks the condition after executing the statements, i.e it will execute the loop body one time for sure because it checks the condition after executing the statements.
Syntax :
do
{
statements..
}while (condition);
C# Jump Statements
In C#, Jump statements are used to transfer control from one point to another point in the program due to some specified code while executing the program. There are five keywords in the Jump Statements:
break
continue
goto
return
throw
break statement
The break statement is used to terminate the loop or statement in which it present. After that, the control will pass to the statements that present after the break statement, if available. If the break statement present in the nested loop, then it terminates only those loops which contains break statement.
continue statement
This statement is used to skip over the execution part of the loop on a certain condition. After that, it transfers the control to the beginning of the loop. Basically, it skips its following statements and continues with the next iteration of the loop.
goto statement
This statement is used to transfer control to the labeled statement in the program. The label is the valid identifier and placed just before the statement from where the control is transferred.
return statement
This statement terminates the execution of the method and returns the control to the calling method. It returns an optional value. If the type of method is void, then the return statement can be excluded.
throw statement
This is used to create an object of any valid exception class with the help of new keyword manually. The valid exception must be derived from the Exception class.