Thursday, 1 February 2018

006 C# Loop

C # tutorial 006 Loop 
Loop is a part of a program to do anything continuously and repeatedly until a condition satisfied. Sometimes we do some work, until we reach the goal, sometimes we repeatedly do something to reach any conclusion. For example, to find , how many 3 are there in 15. We can go adding 3 until we reach 15. The first  3 + 3 will give us 6 , the Second time 6 + 3 will give us 9, condition is to reach 15. Then we go adding 3 until you reach 15. This is the concept of loops. Continuously doing the same thing until the condition is reached. 

There are mainly three kind of loop
  •   While loop 
  •   For loop 
  •   Do while loop 
Another kind of loop, that is nested loop. Nested loop can be any loop , do while, while, for.

 1)While loop 

It is a control structure loop where entry is controlled once. In while loop, the condition of exit the loop comes under while statement. Until the condition is false  the loop will execute repeatedly. And under this loop, if there is any program statement , that will execute repeatedly.






inti = 1; 

Example 1
while (true)
{
     //required code
}
 
Example 2

while (i < 4)
{
       Console.WriteLine(i.ToString());
}
//Output:1,2,3

2)For loop

For loop also entry control loop which will execute repeatedly. It's action is predefined number of times of repeatation. The syntax of for loop as below. Fast a variable is defined and initialise, then

the exist condition  is written, then the variable which was initialised, step is written incremental / decremental to satisfy the exit condition. One major difference for for loop other than , while and do while loop,  that variable can be defined in the loop statement directly. Here is the example of follow which show how a variable is defined within the loop.


Syntax

for(initialization; condition; step)

Example 


for (int i = 1; i <5 font="" i="i+1)">
{
      Console.WriteLine(i.ToString());
}
//Output :1,2,3,4
 

3)Do while loop

Do while loop also loop which to repeat work until the condition satisfied. It is an exit control loop. Depending upon the condition, if satisfied, the control send back to the loop again. In this kind of loop , the condition is checked finally and decision is taken whether the loop will continue or exit the loop.

Syntax 


do
{
    //statement
} while (C
ondition is true)
 
    
int i = 1;
do
{
         Console.WriteLine(i.ToString());
} while (i <= 10);


//Output :1,2,3..10

Here is the difference between loops


while

for

do while
Entry control Entry control Exit control
Not predefined reparation Predefined reparation Not predefined reparation


Infinite Loop


An infinite loop is a loop which is executed endlessly, no termination condition satisfy to terminate the loop or the loop was instructed continuous repeat. Infinite loop maybe while loop, do loop, for loop. In this kind of loop, if we do not write termination condition properly, the loop will execute endlessly. Here are the example of infinite loop.

Example 1

while (true)
{
}

Example 2
for (; ; )
{
}

Example 3
do
{
} while (true);



Break and Continue
 
Break is a statement to break a loop which is running. Break statement break the existing loop and return that control to the program. Generally break is written when are in a particular situation the loop need to be stop. Continue is a statement to continue the loop to next Iteration . When the continue statement is executed the control of the loop the to next Iteration . In the below example, continue statement execute, it goes to the Iteration work equal to 26.

for (int i = 1; i < 100; i++)
{
if (i>50)
        break;//
if (i==25)
        continue;
         Console.WriteLine(i.ToString());
}


Nested loop
Nested loop is loop which comes under any loop. Can be a for loop under a while loop, while loop under a do loop. The below is example of nested loop. The nesting of loops level is not constant, there may be multiple level of nesting depending upon the situation, . Net I have no control on restriction over the level of nesting. Break and continue statement can be implemented each level of nesting.

Example 1 
for (int i = 1; i < 100; i++)
{
         for (int l = 1; l < 10; l++)
         {
           //statment
         }
}

Example 2 
int[,] myArray = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };

for (int i = 0; i < 4; i++)
{
                   for (int l = 1; l < 2; l++)
                    {
                          //statment
                            var val = myArray[i, l];
                           Response.Write(val+"");
                     }
}

//Output:
2
4
6
8


The below example is a sample of nested loops. And multidimensional array is initialized with data 
and nested loop is created to access this data. This a nested loop can be created any level.
 Generally,to access long data set , array nested loop is used. Below is a example of nested loop 
with break statement.

Example


int[,] myArray = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
for (int i = 0; i < 4; i++)
  {
          for (int l = 1; l < 2; l++)
           {
               //statment
                var val = myArray[i, l];
                if (val==6)
                 break;//
           }
}


Wednesday, 31 January 2018

005 C# Structure & Enum

Structure
Structure is a user defined type which is lightweight than a class. Structure instance 
cannot be created. Here are some features of a structure
  • Structured contain member , like fields and methods.
  • An instance of structure cannot be created. 
  • Structure is value type 
  • Interface can be implemented 
  • Structure does not support inheritance, private, protected, public cannot be defined.
  • C# use the keyword struct , to define a structure. 
  • Structure allow nested type. 
During structures initialization, when a new keyword is called , an appropriate structure constructor 
is called to initialize the structure. We do not need to declare constructor separately.  
Here is the example of structure.  
struct Student
{
       private int m_Age;
       private string m_Name;
      public int Age
      {
      get
       {
        return m_Age;
        }
       set
      {
      m_Age = value;
        }
      }
     public string Name
    {
    get
   {
    return m_Name;
   }
   set
   {
    m_Name = value;
   }
    }

}

Student obj = new Student();
obj.Age = 12;
obj.Name = "John";


Console.WriteLine("Age :" + obj.Age);
Console.WriteLine("Name :" + obj.Name);

Here, the structure contain fields and properties. Age,Name can be assign to the structure and get 
the value of the name and age when required. Structure is value type and no instance can be created, 
it is lightweight compare two class. 

Here is a difference between class and structure 

  • Class is reference type but the structured is value type.
  • Class is created on the heap, structure is created on the stack. 
  • Long heavy weight object, persist for long time, required more instance, then class is used. 
Small object, lightweight, little less data passes for small time, then structure is used. 

As we have discussed earlier, we can implement interface on a structure. Here is a example. 
An interface is defined, School. Structure is defined, named as student. The example shows that, 
how the GetAttenDance method is implemented on the structure.

interface School
{
int GetAttenDance();
}

struct Student : School
{
          public int GetAttenDance()
         {
          //implementation ommitted
         return 50;
          }
}

Student obj = new Student();
int att = obj.GetAttenDance();
Console.WriteLine("Attandace :" + att.ToString());

Enum : It is a value type, data type. The full form is enumeration. This content a set all values 
which are constant. Each value are associated with a numeric instant. If we do not assign any value, 
the default first value is 0, then it is increased by 1 for each value. It is value type and created 
on the stack. It comes under the namespace system.enum The default value type of enum is integer, 
using the cast operator, we can cast the value to long, you int. 

Here is the example of a enum type. An enum is declared named days.

enum Days { SunDay = 1, MonDay, TueDay, WedDay, TruDay, FriDay };
int i=(int)Days.TueDay;
When it is called The returns the days number.





Monday, 29 January 2018

004 C# Class

C # tutorial 004 Class
Object oriented programming C# is the most preferable language in .Net Framework. 
.Net Framework support class and instance of a class. A class is template of blueprint of an object 
which define the object completely for software development purpose. For example we can define a 
class student . A student required age, gender, name, height, class, address, parents name, roll number,
date of birth. Now if we want to software for school, the above attribute of a student are almost all for 
a school. When we are going to develop a software for school, we will consider this as attribute and 
use when required. The student class will content this attribute to represent a student completely. 
Like this car, boar, employee, athletes many more class can be created. We must remember that a 
class will not represent a particular student rather it will represent an object of a student. This object will
contain the property or attribute for a student. The behavior or property can be define by the member 
of a class. Member means feels, methods, properties of a particular class. Here is example of a class. 

 
public class student
{
          //fields
          string student_name;
          int student_age;
 
        //property
         public string sname
        {
               get
              {
                   return this.sname;
               }
             set
            {
                 this.student_name = value;
           }
       }
       //method
         public void calculate_fees()
         {
        }
}

Here are some points about the class

Fields : The first section of the student class represent the fields. This field maybe kind of variable types 
integer, string, byte etc. This field can have different access modifier private, public, protected. 

Property : Property of a class is to assign value to different variable. If we declare a variable with private 
access modifier, the variable would not be visible to outside of the class, property are required to assign 
this variables. Get set property assign value from outside world to a variable. 

Method : Method part of a class which is responsible for do some work. A Method can calculate student 
pending fees, marks calculation attendance calculation. 

Now we are considering with a calculator class.

public class calculator
{
       //fields
          int a;
          int b;
          //property
          public int _a
         {
          get
          {
             return this._a;
            }
           set
           {
             this.a = value;
            }
          }
 
           //property
           public int _b
            {
           get
           {
            return this._b;
           }
          set
          {
            this.b = value;
          }
           }


           //method
            public int doSum()
           {
           return (a + b);
             }
}

How to use the class
calculator obj = new calculator();
obj._a = 10;
obj._b = 20;
int x = obj.doSum();

The calculator class have two variable which was assign value by property and the calculation was 
done by method to calculate and output to another variable. 


Nested class : A Nested class is a class which is defined within a class. For example a class a 
content another class. The second class is nested class. A class retain it's all feature during nested
to another class. A good example of a nested class is club and club member. A club is a class , club 
member is also class under club. Club object has some attribute and properties and methods and
club member has some attribute properties and method. Both class return the property as per oops 
principles. 

class outerclass
{ 
          class Innerclass
          {
         }
}

class outerclass
{
     class Innerclass
     {
     }
     public Innerclass GetNestedClass()
      {
          Innerclass obj = new Innerclass();
          return obj;
        }
}
Constructor : Constructor is a method which is fast called when an object instance is created.
 An object instance is an process of allocating memory, when this memory allocation is done the 
constructor is called. In VB.Net it is called new keyword. A constructor can be.

  • Parameterized Constructor
  • Overloaded Constructor
  • Private Constructor
  • Copy Constructor
  • Static Constructor
Here is example of constructor. 
public class student
{
      public student()
       {
        }
}
In.Net framework C# , if we do not declare constructor, .Net manage it itself. If you write a
class and do not write the constructor of the class, .Net Framework internally create the constructor
when called for initialization of the class.

Parameterized Constructor :
A constructor can define with many definition, depending upon the parameters and data type,  the constructor will be called. If we do not call constructor, we create an instance of the class, C# will automatically create constructor with blank parameter and initialise accordingly. If we call a class with passing parameter, the appropriate matching parameter constructor will be called. It is possible to declare more than one constructor in a class, and initialise constructor depending about the calling parameters. Here is the example of a parameterized constructor.


public class Student
{
          private int s_Age;
          private string s_Name;
          public Student()
         {
          s_Age = 12;
          s_Name = "John";
         }
        public Student(int age, string name)
        {
         s_Age = age;
         s_Name = name;
        }
}
Student p1 = new Student();
Student p1 = new Student(15,"Rohit");

Overloaded Constructor :

public class Student
{
          private int s_Age;
          private string s_Name;
         public Student()
        {
         s_Age = 12;
        s_Name = "John";
         }
        public Student(int age, string name)
        {
         s_Age = age;
         s_Name = name;
        }
}
Student p1 = new Student();
Student p1 = new Student(15,"Rohit");


Private Constructor :

A constructor can be declared with access modifier private. When we declare constructor with private modifier, the class loose some functionality. A class with private constructor, instance cannot be created for this class. This class will behave like a singleton pattern. In design pattern singleton pattern is a pattern, where an object instance create once  for the whole application. The class cannot be inherited also.


  class Student
  {
  //private constructor
      private Student()
     {
     }
  }
class Student
 {
    static void Main(string[] args)
     {
      Student obj = new Student();//Error cannot access private constructor
    }
}

Copy Constructor :

As we know a construct a special is a method which is called fast during the instance creation of a class. After the instance of a class is created, this instant use to initialize another class by copying the class. When pass, the object instance of the first class into the second class through parameter. here
 is the example of copy constructor.


           public class Student
          {
                   private int s_Age;
                   private string s_Name;
                   public Student()
                  {
                   s_Age = 12;
                   s_Name = "John";
                 }
                  public Student(int age, string name)
                  {
                  s_Age = age;
                  s_Name = name;
               }
                //copy constructor
                 public Student(Student obj)
                {
                  s_Age = obj.s_Age;
                 s_Name = obj.s_Name;
               }
   }

/ Instance constructor.
Student p1 = new Student(12,"John");

// Copy Constructor
Student p2 = new Student(p1);

Static Constructor : Static constructor is a special type of constructor which is used to initialize static data during instance creation of a class. When member of a class static, then static constructor is created. Which is either data member of function of a class static, static constructor is used. Static constructor cannot be call directly. It is called before the main constructor is called. When a constructor is declared static, the whole class loose inheritance functionality.
 
class Student
{
         static int s_Age;
        static Student()
        {
              Console.WriteLine("Static Constructor Executed");
              s_Age = 12;
          }
        private int ShowData()
       {
         Console.WriteLine("A = " + A);
        }

      static void Main(string[] args)
      {
      Student P1 = new Student(20);
     Console.Write(P1.ShowData());
    //10
     }
}

Destructor : Destructor is a method which is the last method of the class. It is declared as the same name of the class name. In general purpose software engineer no need to call destructor of a class,.Net automatically call destructor of the objects when instance have no use. It is a part of a garbage collection and memory management technique of. Net framework. .Net automatically call the cleanup command to clean and destroy of an object instance, software engineer cannot handle or cannot invoke the cleanup command of. Net framework. We will learn in details in the letter chapter.

class Student
{
      public Student()
      {
        Console.WriteLine("Constructor");
       }
     ~Student()
     {
    Console.WriteLine("Destructor");
     }
}


class Student
{
      static void Main()
      {
        Student x = new Student();
       }
}


//Output
//Constructor
//Destructor

Some More about class 

Static Class: When a class have when is static member, it may be data member or maybe Member function, it is called static class. As like static constructor class instance cannot be created. That a static class is created to make some direct Access to the data member and function. Static class also loose its its functionality inheritance. During instance creation of a static class , the static constructor is called fast, then the original constructor is called. If during the static constructor initialization , error occurred ,  error cannot be handle as it does not throw any exception. Example of static class

 
public static class School
 {
    public static int CalculateArea(int width, int height)
    {
    return width * height;
    }
}
Singleton Class :
During singleton Design pattern , it is necessary to create an object instance in such a way that no duplicate object instance can not be created of the same type. Singleton Design pattern can be achieved by declaring constructor private and declaring static variable to consume the object instance of the same class. Singleton class has only a single entry to the class. No other instance can be created and access of the member .


public sealed class Student
{
     private static readonly Student instance = new Student();
    static Student()
    {
    }
   private Student()
   {
   }
   public static Student Instance
   {
       get
         {
            return instance;
         }
   }
}
Sealed Class : A class will lose is inheritance functionality , when we declare the class as sealed. This class cannot be used as base class, no child class cannot be defined of it. Here is the example of Sealed class.


class School
{
         virtual public void printSchool()
         {
          Console.WriteLine("School");
           }
}


class Student : School
{
              sealed override public void printStudent()
               {
               Console.WriteLine("Student");
               }
}


class Class : Student
{
//Error: cannot override inherited member
//Student.printStudent because it is sealed.
}


Parcial Class :
Partial class as the define, class split into many parts. For example class is defined in a place a, the class is defined again in a place b again. When the software Run, the class act as a whole, from place a and please b,  work as a whole class. If the class is declared as sealed class in any place, and the least part of the class are define in other place, the whole class will act as a sealed class. Partial class can be nested. That means definition of class and its nested class can be separate part of the project, during run time will act as a whole class and nested class. Here is the example of partial class.



public partial class Student
{
        public Student()
        {
        }
       public void CalculateAttandance(int val)
        {
        Console.WriteLine(val);
        }
}
public partial class Student
{
          public void CalculateFees(int val)
         {
          Console.WriteLine(val);
         }
}


public partial class Student
{
      public Student()
       {
       }
      public void CalculateAttandance(int val)
        {
         Console.WriteLine(val);
         }
       public void CalculateFees(int val)
       {
        Console.WriteLine(val);
        }
}
Internal Class : An internal class is a class only visible through the namespace. Internal access modifier basic functionality of a class in .Net framework. Internal access modifier is used to hide a class from outside namespace class and assembly. Class may be visible only in the namespace where it was designed for.  


Static Member :Static main is a class which Run when a program start. It is the main entry point of a program. It is the main method of a program.
 

বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...