Learn C-Sharp step by step

  • C# basics
  • Conditions in C#
    • The if / else statement
    • The switch statement
  • Loops in C-Sharp
    • The for loop in C#
    • The while loop in C#
    • The do-while loop in C#
  • Arrays in C-Sharp
  • Functions in C-Sharp
  • Finding String Length in C#
  • Array Sorting in C#
  • Print Pattern in C#

C# (C sharp) basics:

1. How to write on screen in C#.

Console.WriteLine(“I have benn studying C# for 4 weeks!”);

This will appear on screen, and will disappear immediately. So, We are writing another statement.

Console.WriteLine(“I have benn studying C# for 4 weeks!”);
Console.ReadLine();

Now it will remain on screen, unless we press any Enter key.
Here we see that we can write letters, numbers and special characters (#) on screen.

String: String is name of variable, and it could be anything letters, numbers and special charecters. We can use string for numbers also, but when we need any arithmetical operation, we use integers, decimals etc as variables

2. String Concatenation:
We can add two strings, called string concatenation. For example, we are adding first name and second name to get full name.

string a = “John “;
string b = “Smith”;
string c = a + b;
Console.WriteLine(c);
Console.ReadLine();

3. Adding two numbers:

int a=10;
int b=12;
int c=a+b;
Console.WriteLine(c);
Console.ReadLine();

Here, we use integers as variables, because we want to add them (arithmetical operation).
Integers does NOT take fractional (decimal) values. If we want to perform arithmetical operation of fractional values; we can take double as variables.

double a = 3.4;
double b = 5.2;
double c = a + b;
Console.WriteLine(c);
Console.ReadLine();

4. How C# take input:
The program will take input from user, and will display it on screen

string name = Console.ReadLine();
Console.WriteLine(name);
Console.ReadLine();

Integer as input:
Input is always in string. So for integer, we need to convert it first.

int number = Convert.ToInt16(Console.ReadLine());
Console.WriteLine(number);
Console.ReadLine();

Remember: As the input is always in string, so for integers as input we need to convert it first.

5. Take two inputs (integers) from user and add them.

int number1 = Convert.ToInt16(Console.ReadLine());
int number2 = Convert.ToInt16(Console.ReadLine());
int number3 = number1 + number2;
Console.WriteLine(number3);
Console.ReadLine();

6. Take two inputs (string) from user, and add them.

string firstname = Console.ReadLine();
string lastname = Console.ReadLine();
string fullname = firstname + lastname;
Console.WriteLine(fullname);
Console.ReadLine();

As, input is always in string, so we did not need to convert it.

Conditional Statements in C#:

  • The if, if / else, if / else if / else Statement
  • The switch Statement

The if Statement:
Construction:

if (condition)
{statement}

For example,

int a = Convert.ToInt16(Console.ReadLine());
if (a > 10)
{Console.WriteLine(“The number is greater than 10”);
Console.ReadLine();

The if / else Statement:
Construction:

if (condition)
{statement}
else
{statement}

For example,

int a = Convert.ToInt16(Console.ReadLine());
if (a > 10)
{Console.WriteLine(“The number is greater than 10”);}
else
{ Console.WriteLine(“The number is 10 or less than 10”);}
Console.ReadLine();

The if / else if / else Statement- (also called nested if)
Construction:

if (condition)
{statement}
else if (condition)
{statement}
else
{statement}

For example,

int a = Convert.ToInt16(Console.ReadLine());
if (a > 10)
{Console.WriteLine(“The number is greater than 10”);}
else if (a == 10)
{Console.WriteLine(“The number is 10”);}
else
{ Console.WriteLine(“The number is less than 10”);}
Console.ReadLine();

NOTE: We write = two times.

The switch Statement:
Construction:

switch (integer a)
{
case 1:
statement
break;
case 2:
statement
break;
default:
statement
break;
}

NOTE: The default in switch statement is equaivalent to else in if statement.
For example,

int week = Convert.ToInt16(Console.ReadLine());
switch (week)
{
case 1:
Console.WriteLine(“Monday”);
break;
Console.WriteLine(“Tuesday”);
break;
case 3:
Console.WriteLine(“Wednesday”);
break;
case 4:
Console.WriteLine(“Thursday”);
break;
case 5:
Console.WriteLine(“Friday”);
break;
case 6:
Console.WriteLine(“Saturday”);
break;
case 7:
Console.WriteLine(“Sunday”);
break;
default:
Console.WriteLine(“NOT KNOWN”);
break;
}
Console.ReadLine();

The for loop in C#
Construction

for (initial point; ending point; increament)
{
Statement(s)
}

For example, the following program will write counting from 1 to 20.

for (int i = 1; i < 21; i++)
{
Console.WriteLine(i);
}
Console.ReadLine();

Q. Write table of 2 using for loop in C#.

for (int i = 1; i < 11; i++)
{
int tab = 2 * i;
Console.WriteLine(tab);
}
Console.ReadLine();

Q. Write a program that print even numbers from 1 to 100.

for (int i = 1; i < 101; i++)
{
if (i % 2 == 0)
{
Console.WriteLine(i);
}
}
Console.ReadLine();

Q. Write a program that take input from user, and write table of that number.

Console.WriteLine(“Enter a number:”);
int num = Convert.ToInt16(Console.ReadLine());
for (int i = 1; i < 11; i++)
{
int tab = i * num;
Console.WriteLine(tab);
}
Console.ReadLine();

Q. Write a program in C sharp, to find the factorial of 5.

int fact = 1;
for (int i = 5; i > 0; i–)
{
fact=fact*i;
}
Console.WriteLine(fact);
Console.ReadLine();

Q. Write a program that take input from user, and find factorial of that number.

Console.WriteLine(“Enter a number:”);
int num = Convert.ToInt16(Console.ReadLine());
Console.WriteLine(“Its factorial is:”);
int fact = 1;
for (int i = num; i > 0; i–)
{
fact=fact*i;
}
Console.WriteLine(fact);
Console.ReadLine();

The while Loop
Construction:

while (condition)
{
statement(s)
}

Q. Write a program in C# using while loop, that take a number from user and return cube of that number. And the program ends when the input is 11.

int num = Convert.ToInt16(Console.ReadLine());
int cube = num * num * num;
while (num != 11)
{
Console.WriteLine(cube);
break;
}
Console.ReadLine();

Q. Write a program that starts from 0, increase 1 by 1, and end before 10 using while loop in C Sharp.

int number = 0;
while (number < 10)
{
Console.WriteLine(number);
number = number + 1;
}
Console.ReadLine();

The do – while loop
Construction

do
{
statement(s)
}
while
{
statement(s)
}

Q. Write a program in C Sharp using do – while loop, that take a number, and increase it 2 by 2, and ends before 30.

int num = Convert.ToInt16(Console.ReadLine());
do
{
Console.WriteLine(num);
num = num + 2;
}
while (num < 30);
Console.ReadLine();

Array in C#
Construction

variable type [] variable name = new variable type [length]

Array of type integer with constant values

int[] myarray = new int[3] { 2, 5, 9 };
Console.WriteLine(myarray[0]);
Console.ReadLine();

NOTE:index 0 inside Console.WriteLine statement represents index of array, that is 2.
In the above myarray; index 0 = 2, index 1 = 5, index 2 = 9.
If we replace 0 by 1, the program will show 5, and for 2, the program will show 9.

Array of type string with constant values

string[] name = new string [3] { “Bilal”, “Sohail”, “Afzal” };
Console.WriteLine(name[0]);
Console.ReadLine();

Q. Write an array in C# of type integer that take 3 numbers as input (the program must close after taking 3 inputs).

int[] myarray = new int[3];
myarray[0] = Convert.ToInt16(Console.ReadLine());
myarray[1] = Convert.ToInt16(Console.ReadLine());
myarray[2] = Convert.ToInt16(Console.ReadLine());
Console.WriteLine(myarray);
Console.ReadLine();

Q. Write an array in C Sharp of type string that take 3 strings (names) as input (the program must ends after taking 3 inputs).

string[] name = new string[3];
name[0] = Console.ReadLine();
name[1] = Console.ReadLine();
name[2] = Console.ReadLine();
Console.WriteLine(name);
Console.ReadLine();

Q. Write an array in C# of type integer that take 10 numbers as input (Use for loop for simplicity).

int[] myarray = new int[10];
for (int i = 0; i < 10; i++)
{
myarray[i] = Convert.ToInt16(Console.ReadLine());
}
Console.WriteLine(myarray);
Console.ReadLine();

Q. Write a program in C Sharp that take 10 inputs from user, and show their sum.

int[] myarray = new int[10];
for (int i = 0; i < 10; i++)
{
myarray[i] = Convert.ToInt16(Console.ReadLine());
}
int a = 0;
for (int j = 0; j < 10; j++)
{
a = a + myarray[j];
}
Console.WriteLine(a);
Console.ReadLine();

Q. Write a program in C# that take 10 numbers, and show their average (input could be decimal or fractional value also).

double[] myarray = new double[10];
for (int i = 0; i < 10; i++)
{
myarray[i] = Convert.ToInt16(Console.ReadLine());
}
double a = 0;
double b = 0;
for (int j = 0; j < 10; j++)
{
a = a + myarray[j];
b = a / 10;
}
Console.WriteLine(b);
Console.ReadLine();

Introduction to Windows Forms Application

Q. Take two inputs from user (first name and second name) and concatenates them and print it, using windows forms application in C#.
For this, take 2 textboxes and 1 button from toolbox menue.

string firstname = textBox1.Text;
string secondname = textBox2.Text;
string fullname = firstname + secondname;
MessageBox.Show(fullname);

Q. Take 2 inputs from user and add them using windows forms application in C#.

int a = Convert.ToInt16(textBox1.Text);
int b = Convert.ToInt16(textBox2.Text);
int c = a + b;
MessageBox.Show(Convert.ToString(c));

Q. Write a program that take input from user and show whether the number is odd or even, using windows forms application in C Sharp.
For this, take 1 textbox and 1 button.

int number = Convert.ToInt16(textBox1.Text);
if (number % 2 == 0)
{
MessageBox.Show(“EVEN”);
}
else
{
MessageBox.Show(“ODD”);
}

Functions in C#
Construction

output type (input type)
{
return (program);
}

Now call the function

output type = function name;

The following example will make the matter clear.

Q. Write a function that take 2 numbers and add them using windows forms application in C#.

Function
int add(int a, int b)
{
return (a + b);
}
Now, call the function
int c = add(Convert.ToInt16(textBox1.Text), Convert.ToInt16(textBox2.Text));
MessageBox.Show(Convert.ToString(c));

Q. Write  a function in C Sharp which takes three number as input parameter and return the largest of three.

Function
int largest(int a, int b, int c)
{
if (a > b && a > c)
{
return a;
}
else if (b > a && b > c)
{
return b;
}
else
{
return c;
}
}
Call the function
int result = largest(Convert.ToInt16(textBox1.Text), Convert.ToInt16(textBox2.Text), Convert.ToInt16(textBox3.Text));
MessageBox.Show(Convert.ToString(result));

Q. Write a program in C# that take Temperature in Fahrenheight, and convert it to Centigrate.

Console.WriteLine(“Enter Temperature in Fahrenheight:”);
double ftemp = Convert.ToDouble(Console.ReadLine());
Console.WriteLine(“Equivalent Temperature in Centigrate is:”);
double ctemp= (ftemp-32) * 5 / 9;
Console.WriteLine(ctemp);
Console.ReadLine();

Q. Write a program in C Sharp that take Month and Date, and show number of days from the start of the year to that date.

Console.WriteLine(“Enter Month”);
Console.WriteLine(“Enter Date”);
int b = Convert.ToInt16(Console.ReadLine());
int c = Convert.ToInt16(Console.ReadLine());
Console.WriteLine(“Number of days from the start of the year are:”);
int a = 0;
int d = 0;
int[] month = new int[12] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
for (int i = 0; i < b – 1; i++)
{
a = a + month[i];
d = a + c;
}
Console.WriteLine(d);
Console.ReadLine();

 

Finding String Length in C-Sharp

Q. Write a program in C# that take string input, and print its number of characters.

string name = Console.ReadLine();
Console.WriteLine(name.Length);
Console.ReadLine();

Q. Write a program in C Sharp that take a sentense as input, and show the number of “a” used in the sentense.

string name = Console.ReadLine();
int counter = 0;
for (int i = 0; i < name.Length; i++)
{
if (name[i] == ‘a’)
{
counter++;
}
}
Console.WriteLine(counter);
Console.ReadLine();

Q. Write a program in C# taht take name and password. If the name and password are correct, the program show “you are logged in”, otherwise, “incorrect name or password”.

Console.WriteLine(“Enter your Name”);
Console.WriteLine(“Enter your Pswrd”);
string name = Console.ReadLine();
string pswrd = Console.ReadLine();
string myname = “bilal”;
string mypswrd = “123456”;
if (name == myname && pswrd == mypswrd)
{
Console.WriteLine(“You are logged in”);
}
else
{
Console.WriteLine(“Incorrect name or pswrd”);
}
Console.ReadLine();

Sorting Arrays in C Sharp

Q. Write a string array of length 3, and sort them.

string[] name = new string[] { “We”, “He”, “Us”};
Array.Sort(name);
foreach (string i in name)
{
Console.WriteLine(i);
}
Console.ReadLine();

Q. Write a string array in C# that take 5 inputs, and sort them.

string[] name = new string[5];
for (int i = 0; i < 5; i++)
{
name[i] = Console.ReadLine();
}
Array.Sort(name);
foreach (string i in name)
{
Console.WriteLine(i);
}
Console.ReadLine();

Q. Write an array in C Sharp of length 3, and sort it.

int[] numbers = new int [] { 4, 3, 8, 0, 5 };
Array.Sort(numbers);
foreach (int i in numbers)
{
Console.WriteLine(i);
}
Console.ReadLine();

Q. Write a program in C# that take 5 integers, and sort them.

int[] numbers = new int[5];
for (int i = 0; i < 5; i++)
{
numbers[i] = Convert.ToInt16(Console.ReadLine());
}
Array.Sort(numbers);
foreach (int i in numbers)
{
Console.WriteLine(i);
}
Console.ReadLine();

Print Pattern in C-Sharp

Q. Print * 10 times vertically usinf C# Console Application.

for (int i = 1; i < 11; i++)
{
Console.WriteLine(“*”);
}
Console.ReadLine();

Q. Print * 10 times Horizontally usinf C# Console Application.

for (int i = 1; i < 11; i++)
{
Console.Write(“*”);
}
Console.ReadLine();

Q. Print * 10 times Horizontally with spaces between them usinf C# Console Application.

for (int i = 1; i < 11; i++)
{
Console.Write(“* “);
}
Console.ReadLine();

Q. Write a program in C# that take string input and print the result vertically.

string name = Console.ReadLine();
for (int i = 0; i < name.Length; i++)
{
Console.WriteLine(name[i]);
}
Console.ReadLine();

Q. Print the following pattern using C# Console Application.
*
**
***
****
*****

for (int i = 1; i < 6; i++)
{
for (int j = 1; j <= i; j++)
{
Console.Write(“*”);
}
Console.WriteLine(“”);
}
Console.ReadLine();

Q. Print the following pattern using C-Sharp Console Application.
*****
****
***
**
*

for (int i = 5; i > 0; i–)
{
for (int j = 1; j <= i; j++)
{
Console.Write(“*”);
}
Console.WriteLine(“”);
}
Console.ReadLine();

Q. Print pyramid using C# Console Application, like this:

    *
   * *
  * * *
 * * * *
* * * * *

for (int i = 1; i < 6; i++)
{
for (int j = 4; j >= i; j–)
{
Console.Write(” “);
}
for (int k = 1; k <= i; k++)
{
Console.Write(“* “);
}
Console.WriteLine(“”);
}
Console.ReadLine();

History of Programming Languages

 Programming languages:

A programming language is an artificial language designed to express computations that can be performed by a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine, to express algorithms precisely, or as a mode of human communication.

World’s first computer programmer: (Ada Lovelace)

The progression of computer programming languages was made possible by the programmer’s search for efficient translation of human language into something that can be read and understood by computers. The languages generated, called machine code, have high levels of abstraction, which hide the computer hardware and make use of representations that are more convenient to programmers.

As programs evolve and become more sophisticated, programmers found out that certain types of computer languages are easier to support. As expected in a dynamic discipline, there is no standard for categorizing the languages used in programming. There are, in fact, dozens of categories. One of the most basic ways to categorize the languages is through a programming paradigm, which gives the programmer’s view of code execution. Among the languages classifications according to programming paradigm are:

o Object-Oriented Programming Languages:
Known as the newest and most powerful paradigms, object-oriented programming requires the designer to specify the data structures as well as the types of operations to be applied on those data structures. The pairing of data, and the operations that can be done on it is called an object. A program made using this language is therefore made up of a set of cooperating objects instead of an instructions list.

The most famous object-oriented programming these days are C#, C , Visual Basic, Java, and Python. 

o Structured Programming Languages:

 An exceptional type of procedural programming, structured programming provides programmers with additional tools to handle the problems created by larger programs. When using this language, programmers are required to cut program structure into small pieces of code that can easily be understood. Instead of using global variables, it employs variables that are local to every subroutine. Among the popular features of structured programming is that it doesn’t accept GOTO statement which is usually associated with the top-down approach. Such approach starts with an opening overview of the system with minimal details about the various parts. To add these details, design iterations are then included to complete the design.

Commonly used structured languages include C, Pascal, and ADA.

o Procedural Programming Languages:

Procedural Programming involves a list of operations the program needs to complete to be able to attain the preferred state. It is a simple programming paradigm where every program comes with a starting phase, a list of tasks and operations, and an ending stage. Also called imperative programming, this approach comes with small sections of code that perform certain functions. These sections are made up of procedures, subroutines, or methods. A procedure is made up of a list of computations that should be done. Procedural programming lets a part of the code to be used again without the need to make several copies. It achieves this by dividing programmatic tasks into small sections. Because of this, programmers are also capable of maintaining and understanding program structure.

Among the known procedural languages are BASIC and FORTRAN.

These are the different types of computer programming languages that you can consider when planning to make a computer program. Procedural programming splits the program’s source code into smaller fragments. Structured languages require more constraints in the flow and organization of programs. And object-oriented programs arrange codes and data structures into objects. 

Some important languages that were developed in this period include:

  • 1951 – Regional Assembly Language
  • 1952 – Autocode
  • 1954 – FORTRAN (Formula Translator)
  • 1954 – IPL (forerunner to LISP)
  • 1955 – FLOW-MATIC (forerunner to COBOL)
  • 1957 – COMTRAN (forerunner to COBOL)
  • 1958 – LISP (List Processor)
  • 1958 – ALGOL 58
  • 1959 – FACT (forerunner to COBOL)
  • 1959 – COBOL (Common Business Oriented language)
  • 1962 – APL
  • 1962 – Simula
  • 1962 – SNOBOL
  • 1963 – CPL (forerunner to C)
  • 1964 – BASIC (Beginner’s All-purpose Symbolic Instruction Code)
  • 1964 – PL/I
  • 1967 – BCPL (forerunner to C)
  • 1968 – Logo
  • 1970 – Pascal
  • 1970 – Forth
  • 1972 – C
  • 1972 – Smalltalk
  • 1972 – Prolog
  • 1973 – ML
  • 1975 – Scheme
  • 1978 – SQL (initially only a query language, later extended with programming constructs)
  • 1980 – C++ (as C with classes, name changed in July 1983)
  • 1983 – Objective-C
  • 1983 – Ada
  • 1984 – Common Lisp
  • 1985 – Eiffel
  • 1986 – Erlang
  • 1987 – Perl
  • 1988 – Tcl
  • 1989 – FL (Backus)
  • 1990 – Haskell
  • 1991 – Python
  • 1991 – Visual Basic
  • 1993 – Ruby
  • 1993 – Lua
  • 1994 – CLOS (part of ANSI Common Lisp)
  • 1995 – Java
  • 1995 – Delphi (Object Pascal)
  • 1995 – JavaScript
  • 1995 – PHP
  • 1997 – Rebol
  • 1999 – D
  • 2001 – C#
  • 2001 – Visual Basic .NET
  • 2002 – F#
  • 2003 – Scala
  • 2003 – Factor
  • 2006 – Windows Power Shell
  • 2007 – Clojure
  • 2007 – Groovy
  • 2009 – Go

Prominent people in the history of programming languages:

  • John Backus,  inventor of Fortran.
  • Alan Cooper, developer of Visual Basic.
  • Edsger W. Dijkstra, developed the framework for structured programming.
  • James Gosling, developer of Oak, the precursor of Java.
  • Anders Hejlsberg, developer of Turbo Pascal, Delphi and C#.
  • Grace Hopper, developer of Flow-Matic, influencing COBOL.
  • Kenneth E. Iverson, developer of APL, and co-developer of J along with Roger Hui.
  • Bill Joy, inventor of vi, early author of BSD Unix, and originator of SunOS, which became Solaris.
  • Alan Kay, pioneering work on object-oriented programming, and originator of Smalltalk.
  • Brian Kernighan, co-author of the first book on the C programming language with Dennis Ritchie, coauthor of the AWK and AMPL programming languages.
  • John McCarthy, inventor of LISP.
  • John von Neumann, originator of the operating system concept.
  • Dennis Ritchie, inventor of C (programming language). Unix Operating System , Plan 9 Operating System.
  • Bjarne Stroustrup, developer of C++.
  • Ken Thompson, inventor of B , Go Programming Language , Inferno Programming Language.
  • Niklaus Wirth, inventor of Pascal, Modula and Oberon.
  • Larry Wall, creator of Perl and Perl 6
  • Guido van Rossum, creator of Python
  • Yukihiro Matsumoto, creator of Ruby