Showing posts with label C# Training / Tutorial. Show all posts
Showing posts with label C# Training / Tutorial. Show all posts

CSharp (C#) If..else..if ladder


An If..Else..If ladder is implemented in most of the programming logics. The execution happens from top to bottom. Once a condition is satisfied, remaining if else statements are all bypassed and the control moves to the next statement outside the if..else..if ladder. If none of the conditions are satisfied, then the final else will be executed.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter the number between 2 and 10 : ");
            int number = Convert.ToInt32(Console.ReadLine());
            if (number % 2 == 0)
            {
                Console.WriteLine("The smallest factor of {0} is 2",number);
            }
            else if (number % 3 == 0)
            {
                Console.WriteLine("The smallest factor of {0} is 3",number);
            }
            else if (number % 5 == 0)
            {
                Console.WriteLine("The smallest factor of {0} is 5",number);
            }
            else if (number % 7 == 0)
            {
                Console.WriteLine("The smallest factor of {0} is 7",number);
            }
            else
            {
                Console.WriteLine("The number isnt between 1 and 10");
            }

            Console.ReadLine();
        }
    }

}

Output:

C Sharp ( C# ) Control Statements

CSharp (C#) Nested If..else loop Statement


Nested If is having an if condition within another if condition. When there are many conditions to be verified we could go for Nested..If

Using the similar example from If..Else

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            //If a number is positive only then find if its an even number or not.
            int number = Convert.ToInt32(Console.ReadLine());
            if (number > 0)
            {
                int value = number % 2; //If the remainder is zero its an even number.
                if (value == 0)
                {
                    Console.WriteLine("The number {0} is positive and is an even number.", number);
                }
                else
                {
                    Console.WriteLine("The number {0} is positive and is an odd number.", number);
                }
            }
            else
            {
                Console.WriteLine("The number {0} is not a positive number.", number);
            }
            Console.ReadLine();
        }
    }
}

Output :



C Sharp ( C# ) Control Statements

C Sharp ( C# ) If..Else Statement


If..Else Statement is used whenever a particular condition is true, a certain set of tasks needs to be done and if the condition isn't true then another set of tasks are performed.

Syntax :
if (condition)
    {
        set of tasks or statements; (Set 1)
    }
else
    {
        set of tasks or statements; (Set 2)
    }

If the condition is satisfied, then the Set 1 statements are executed. If the condition fails, then the Set 2 statements are executed.

Example : Find if a value is even or odd. If even then print a statement, if not then print another statement.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            int number = Convert.ToInt32(Console.ReadLine());
            int value = number % 2; //If the remainder is zero its an even number.
            if (value == 0)
            {
                Console.WriteLine("The number {0} is an even number.", number);
            }
            else
            {
                Console.WriteLine("The number {0} is an odd number.", number);
            }

            Console.ReadLine();
        }
    }
}

Output :


C Sharp ( C# ) Control Statements

C Sharp ( C# ) Control Statements


Control Statements in C Sharp are statements that will help control the execution a set of statements.

Selection Statements : (Click on links below)
Iteration Statements :
  • For Loop
  • Foreach
  • While Loop
  • Do While Loop
Jump Statements :
  • break
  • continue
  • goto
  • return
  • throw

C Sharp ( C# ) Literals


C Sharp Literals is assigning a value to a variable.

In C#, literals refer to fixed values that are assigned to different variables  C# literals can be of any simple or primitive type. The way a variable is defined using a certain datatype tells us how the literal should be given.

Here are examples how literals are assigned.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            //Assigning a char
            char a = 'Z';
            //Assigning a string
            string s = "Hello... how are you ?";
            //Assigning an integer
            int i = 10;
            //Assigning a uint
            uint j = 10u;
            uint k = 10U;
            //Assigning a float
            float f = 10.5678F;
            float g = 10.567f;
            //Assinging a long
            long l = 100l;
            long m = 100L;
            //Assigning a decimal
            decimal d = 5.6m;
            decimal e = 6.7M;
            //Assigning a double
            double z = 1.24d;
            double y = 3.456979879D;
            //Assigning a byte;
            byte b = 10;
            //Assiging a bool;
            bool value = true;
            bool value1 = false;
            //Assigning unsigned values for long
            ulong ul = 487UL;
            //Assigning signed byte
            sbyte sb = 20;
            //Assigning HexaDecimal Values
            int hexa = 0x2a; //This represents a decimal value of 2a which is 32+10 = 42           
        }
    }
}

C Sharp ( C# ) Decimal Data Type


The Decimal datatype is used in C# to accept number (both as plain integers and fractional values) but is mostly used for monetary calculations. The decimal type utilizes 128 bits to represent values within the range 1E–28 to 7.9E+28. The Decimal datatype eliminates most of the rounding errors that could happen in other datatypes. Thats the reason its mostly used for money calculations.

While assigning a value to the decimal datatype, suffix the value by "M" or "m".

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            decimal price = 25.8M;
            decimal discountpercent = 10;
            decimal actualprice;

            actualprice = price - (price * discountpercent / 100);
           
            Console.WriteLine("The final price of ${0} price product after {1} percent discount is : ${2}",price,discountpercent,actualprice);
            Console.ReadLine();
        }
    }
}

Output:

C Sharp Datatypes Home Page

C Sharp (C#) Double datatype


The Double datatype is used whenever fractional values need to be assigned to a variable. Its similar to an integer datatype but has a very big range of values which can accept fractional values. Double is the most popular datatype used in most of the mathematical functions in C# while using the Math library or just assigning values.

When a variable is assigned a value which is double, then it needs to be suffix with "D" or "d".

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            double a = 22D;
            double b = 7D;
            double pivalue = a / b;// float answer = (a * a) + (b * b) + (2 * a * b); //a squared + b squared + 2ab
            Console.WriteLine("The final value of {0} / {1} is : {2}", a, b, pivalue);
            Console.ReadLine();

            double number = 1.2345D;
            double power = 2.5D;
            double finalanswer = Math.Pow(number, power);
            Console.WriteLine("The {0} to the power of {1} is {2}", number, power, finalanswer);
            Console.ReadLine();
        }
    }
}

Output:

C Sharp Datatypes Home Page

C Sharp (C#) Float Data type


Float data type is a numeric datatype that will accept numbers. Its a 32 bit sized datatype. The difference from integer datatype is that, float accepts fractional values. The float values when assigned to a variable are generally followed by a "F" at the end of the value.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            float a = 3.4567F;
            float b = 5.7679F;
            float answer = (a * a) + (b * b) + (2 * a * b); //a squared + b squared + 2ab
            Console.WriteLine("The final value is : {0}", answer);
            Console.ReadLine();
        }
    }
}

Output:

C Sharp Datatypes Home Page

C Sharp (C#) Long and ulong datatype


Long and ulong are very similar to Integer type ( int ) but has a very high range of numbers that it can accept. Long has a range of about –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 and ulong has a range of about 0 to 18,446,744,073,709,551,615.

While using long datatype and assigning a long value to a variable, its recommended to suffix L with the number being assigned.
long x = 1234567890L; (something like this for long)
ulongx = 1234567890uL; (something like this for ulong)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            long a = 1234567890L;
            long b = 1234567890L;
            long c = a * b;
            Console.WriteLine("The addition of two signed long numbers {0} and {1} is : {2}", a, b, c);
            Console.ReadLine();

            ulong d = 1234567890uL;
            ulong e = 1234567890uL;
            ulong f = d * e;
            Console.WriteLine("The addition of two unsigned long numbers {0} and {1} is : {2}", d, e, f);
            Console.ReadLine();
        }
    }
}

Output :

C Sharp Datatypes Home Page

C Sharp (C#) Short and ushort datatypes


The short and ushort datatypes that are used in C# are similar to int. The only difference between them is the range thats supported. Short supports from –32,768 to 32,767 and ushort supports from 0 to 65,535.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            short num1 = 20000;
            short num2 = 20000;
            short num3 = (short)(num1 + num2);
            Console.WriteLine("The addition of num1 and num2 is : {0}", num3);
            Console.ReadLine();

            //Here the numbers that can be accommodated by ushort but not by short.
            ushort num4 = 20000;
            ushort num5 = 20000;
            ushort num6 = (ushort)(num4 + num5);
            Console.WriteLine("The addition of num4 and num5 is : {0}", num6);
            Console.ReadLine();           
        }
    }
}

Output:

C Sharp Datatypes Home Page

C Sharp (C#) Unsigned Integer (unit) data type


This is similar to Integer datatype that is explained here : int but the only difference is , it cannot hold any negative values. It can hold only 0 and positive values. So the max number it could hold will be higher than and int datatype. (Limit being : 0 to 4,294,967,295)

Using the same sample as listed in the integer datatype, for bigger number, have used uint instead of int and it works perfectly fine as the limit falls within the range of uint.

While defining an unit datatype , its recommended to use "u" as a suffix to the value thats getting assigned.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            int num1 = 200000000;
            int num2 = 200000000;
            int num3 = num1 + num2;
            Console.WriteLine("The addition of num1 and num2 is : {0}", num3);
            Console.ReadLine();

            //Here the numbers are more than the limit that int variable could hold.
            uint num4 = 2000000001u;
            uint num5 = 2000000003u;
            uint num6 = num4 + num5;
            Console.WriteLine("The addition of num4 and num5 is : {0}", num6);
            Console.ReadLine();           
        }
    }
}

Output :


C Sharp Datatypes Home Page

C Sharp (C#) Integer Data Type


Integer datatype is most widely used datatype to store numbers in C#. Variables of type int are mostly used to control loops, to index arrays and for other math functions. Depending on the requirements remaining datatypes that hold numbers are chosen (example: short or long). This is a signed datatype, so it can hold both negative and positive numbers with a range from –2,147,483,648 to 2,147,483,647.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            int num1 = 200000000;
            int num2 = 200000000;
            int num3 = num1 + num2;
            Console.WriteLine("The addition of num1 and num2 is : {0}", num3);
            Console.ReadLine();

            //Here the numbers are more than the limit that int variable could hold.
            int num4 = 2000000001;
            int num5 = 2000000003;
            int num6 = num4 + num5;
            Console.WriteLine("The addition of num4 and num5 is : {0}", num6);
            Console.ReadLine();           
        }
    }
}

Output :

The numbers in the code, num1 and num2 values are within the range of int datatype. So they did not throw any garbage value. But the num4 and num5 are outside the range of int datatype, hence when added, it gave a garbage value as output. We could avoid it by using a datatype which could hold higher numbers. (like a long datatype)

C Sharp Datatypes Home Page

C Sharp (C#) Char Data type


Char datatype is used when variables need to store characters. A character is assigned to a variable using single quotes. Char datatype allows 16 bits hence can accommodate Chinese, Japanese or any other language that uses Unicode characters. Though it belongs to integer category, numbers cannot be assigned to a character variable.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            char a = 'A';
            //assigning a number to char will take the ASCII value.
            char b = (char)104;
            Console.WriteLine("The variable a value is : {0}", a);
            Console.WriteLine("The variable b value is : {0}", b);
            Console.ReadLine();           
        }
    }
}

Output:

C Sharp Datatypes Home Page

C Sharp (C#) Byte and sbyte Data Type


Byte datatype has a range from 0 to 255 and can be assigned as below :
byte b = 255

In the preceding declaration, the integer literal 255 is implicitly converted from int to byte. If the integer literal exceeds the range of byte, a compilation error will occur. There is a predefined implicit conversion from byte to short, ushort, int, unit, long, ulong, float, double, decimal.


Similarly an sbyte is a signed byte which has a limit from -128 to +127. The following example shows the usage of both signed and unsigned byte when values are within and outside the limits.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            byte a = 100;
            byte b = 100;
            byte c = (byte)(a + b);
            Console.WriteLine("The addition of two unsigned byte numbers {0} and {1} is : {2}", a, b, c);
            Console.ReadLine();

            sbyte d = 100;
            sbyte e = 100;
            sbyte f = (sbyte)(d + e);
            Console.WriteLine("The addition of two signed byte numbers {0} and {1} is : {2}", d, e, f);
            Console.ReadLine();
        }
    }
}

Output :

C Sharp Datatypes Home Page