A place where you can find latest technology updates new developments in the IT industry as well as complete learning material regarding different software technologies.

Logo

Logo

Hey! we are glad to announce that we Omistic moved on to our official website at www.omistic.com

C-Sharp Step by Step Learning Program

Content:

- Getting Started With C#
- Premitive Data Types
- Variables
- Casting
- Enumerations
- Arrays in C-Sharp
- 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#

                                                                                                                               


Getting Started With C#

1. How to write on screen in C#.


using System;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("I have been studying C# for 4 weeks!");
        }
    }
}


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


     Console.WriteLine("I have been studying C# for 4 weeks!");
     Console.ReadLine();

Now it will remain on screen, unless we press 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.


        static void Main(string[] args)
        {
            string a = "John ";
            string b = "Smith";
            string c = a + b;
            Console.WriteLine(c);
            Console.ReadLine();
        }

3. Adding two numbers:


        static void Main(string[] args)
        {
            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.

static void Main(string[] args) { 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

static void Main(string[] args) { 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.


        static void Main(string[] args)
        {
            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.


        static void Main(string[] args)
        {
            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.


        static void Main(string[] args)
        {
            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.

Data Types (Premitive/Pre-defined)

What are Data Types?

The type of data that a variable contains is called Data Type (type). A Data Type is a classification of things that share similar type of qualities or characteristics or behavior.
C# is strongly typed language so every variable and object must have a type.

There are two types of data type in C#

  • Primitive types or Pre-defined
Such as:  byte, short, int, float, double, long ,char, bool, DateTime, string, object etc..

  • Non-primitive types or User Defined (Which we'll cover in our future topics)
Such as: class, struct, enum, interface, delegate, array.

Primitive data types are FCL types
Primitives type are directly from base class library and can be used interchangeably.
All these premitive data types are objects.

C# Type .NetFramework Type/FCL Signed Bytes Possible Values
sbyte System.sbyte Yes 1 -128 to 127
short System.Int16 Yes 2 -32768 to 32767
int System.Int32 Yes 4 231 to 231 - 1
long System.Int64 Yes 8 263 to 263 - 1
byte System.Byte No 1 0 to 255
ushort System.Uint16 No 2 0 to 65535
uint System.Uint32 No 4 0 to 232 - 1
ulong System.Uint64 No 8 0 to 264 - 1
float System.Single Yes 4
±1.5 x 10-45 to ±3.4 x 1038 with 7
significant figures
double System.Double Yes 8
±5.0 x 10-324 to ±1.7 x 10308 with 15 or
16 significant figures
decimal System.Decimal Yes 12
±1.0 x 10-28 to ±7.9 x 1028 with 28 or 29
significant figures
char System.Char N/A 2 Any Unicode character
bool System.Boolean N/A 1/2 true or false


Variables:

• Variables temporarily holds data for re-use. 
• Variables can be declare as being of a particular type, and each variable is constrained to hold only values of its declared type.
• Variables can hold either value types or reference types, or they can be pointers.
• A variable of value types directly contains only an object with the value.
• A variable of reference type directly contains a reference to an object. Another variable may contain a reference to the same object.
• It is possible in C# to define your own value types by declaring enumerations or structs.

Construction:

Declaring a variable
   DataType VariableName;

Assigning the value of variable
   VariableName Operator;

For example,
Declaring a variable


        static void Main(string[] args)
        {
            int Number;
        }

Assigning the value of variable


            Number = 9;

OR you can do it in a single line


        static void Main(string[] args)
        {
            int Number = 9;
        }

For Printing on Console


        static void Main(string[] args)
        {
            int Number = 9;
            Console.WriteLine("Result Is " + Number);
        }

Casting:

• Casting means to move data from one type to another data type.
• All small Data Types can be cast into bigger types called 'implicit casting'.
• All big Data Types can be cast into smaller types called 'explicit casting'.

Construction:
   Implicit Casting..
   SmallDataType VariableName = 0;
   BigDataType VariableName = SmallTypeVariableName;

   Explicit Casting..
   BigDataType VariableName = 0;
   SmallDataType VariableName = (SmallDataType)BigTypeVariableName;

for example,
Casting implicitly


        static void Main(string[] args)
        {
            int a = 2;
            long b = a;
            Console.WriteLine("b: " + b);
        }

Result:
d: 2

Casting explicitly


        static void Main(string[] args)
        {
            long b = 3;
            int d = (int)b;
            Console.WriteLine("d: " + d);
        }

Result:
d: 3

Note: Possible loss of precision or accuracy in explicit casting.

Enumerations:

• Enums are special types which we make ourselves to name some constant values
• Enums can't be declared inside a method.
• Enums are type with a fixed set of possible values.
• By default all enums are of string type.
By default enumeration members also have integer values according to their index/position, but we can also assign our customized int values.


Construction:
Write the keyword enum, followed by the name of the enum, followed by a pair of curly-brackets containing a comma-separated list of the enumeration member names.

enum EnumName { Spring, Summer, Fall, Winter }

for example,


    class Program
    {
        public enum ChildrenTypes
        {
            Smart, Fat, Skinny
        }
        static void Main(string[] args)
        {
            System.Console.WriteLine(ChildrenTypes.Smart);
        }
    }

Result:
Smart

integer casting in enum must be explicit, like:


    class Program
    {
        public enum ChildrenTypes
        {
            Smart, Fat, Skinny
        }
        public static void Main(string[] args)
        {
            int cTypes = (int)ChildrenTypes.Smart;
            Console.WriteLine(cTypes);
        }
    }


Result:
0

By default enumeration members have integer values according to their index/position
such as: 0 for C, 1 for B, 2 for A & 3 for A1
BUT, we can also assign our custom int values, like:


        public enum AgeRange
        {
            teenager = 20, young = 30, old = 50
        }


we can also set the enum data type at the time of declaring an enum, like:


    public class Program
    {
        public enum AgeRange : byte
        {
            teenager = 20, young = 30, old = 50
        }
        public static void Main(string[] args)
        {
            System.Console.WriteLine((byte)AgeRange.teenager);
            Console.ReadLine();
        }
    }


Result:
20

Array in C#

Construction
variable type [] variable name = new variable type[length]
Array of type integer with constant values


        public static void Main(string[] args)
        {
            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


        public static void Main(string[] args)
        {
            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).


        public static void Main(string[] args)
        {
            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).


        public static void Main(string[] args)
        {
            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).


        public static void Main(string[] args)
        {
             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.


        public static void Main(string[] args)
        {
            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).


        public static void Main(string[] args)
        {
            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();
        }

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,


        public static void Main(string[] args)
        {
            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,


        public static void Main(string[] args)
        {
            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,


        public static void Main(string[] args)
        {
            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 in C#:

Construction:
switch (integer a)
{
case 1:
statement
break;
case 2:
statement
break;
default:
statement
break;
}
NOTE: The default in switch statement is equivalent to else in if statement.

For example,


        public static void Main(string[] args)
        {
            int week = Convert.ToInt16(Console.ReadLine());
            switch (week)
            {
                case 1:
                    Console.WriteLine("Monday");
                    break;
                case 2:
                    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("Unknown");
                    break;
            }
            Console.ReadLine();
        }

Looping in C#:

  • for loop, while Loop, do while loop
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.


        public static void Main(string[] args)
        {
            for (int i = 1; i < 21; i++)
            {
                Console.WriteLine(i);
            }
            Console.ReadLine();
        }

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


        public static void Main(string[] args)
        {
            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.


        public static void Main(string[] args)
        {
            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.


        public static void Main(string[] args)
        {
            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.


        public static void Main(string[] args)
        {
            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.


        public static void Main(string[] args)
        {
            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.


        public static void Main(string[] args)
        {
            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.


        public static void Main(string[] args)
        {
            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.


        public static void Main(string[] args)
        {
            int num = Convert.ToInt16(Console.ReadLine());
            do
            {
                Console.WriteLine(num);
                num = num + 2;
            }
            while (num < 30);
            Console.ReadLine();
        }
_________________________________________________________________________________
Hey! we are glad to announce that we Omistic moved on to our official website at www.omistic.com

0 comments:

Post a Comment