Tuesday, 27 March 2018

C # tutorial Operator Overloading

Operator Overloading
C# oprator have some special meaning ,but you can change and redefine operator meaning.

Some Point to remember before define Operator Overloading
  • The method should be a public and static method
  • The method is marked by keyword "operator" followed by the operator symbol

Below is the example of of unary(+) operator overloading

 
Example 1

using System;
public class Program
{
                  public static void Main()
                  {

                         Area obj1 = new Area(10,10);
                         Area obj2= new Area(15, 12);

                         Area overlod_obj = obj1 + obj2;

                         Console.WriteLine("Value of mX :"+overlod_obj.GetValX().ToString());
                         Console.WriteLine("Value of mY :" + overlod_obj.GetValY().ToString());

                         Console.ReadKey();
                  }
}


class Area
{
            int mX;
            int mY;

            public Area(int x, int y)
            {
                mX = x;
                mY = y;
            }

            public static Area operator +(Area m1, Area m2)
            {
                return new Area(m1.mX * m2.mX, m2.mY * m2.mY);
            }

           public int GetValX()
          {
              return mX;
          }

          public int GetValY()
         {
            return mY;
         }

}

Output :
Value of mX :150
Value of mY :144


Below is the table , showing  Operator,Operator Type and Overloading Possible or not

Operator Type Operator Overloading Possible
unary +, -, !, ~, ++, -- Yes
binary +, -, *, /, % Yes
comparison ==, !=, <, >, <=, >= Yes
conditional &&, || Yes
assignment +=, -=, *=, /=, %= No
Misc.. =, ., ?:, ->, new, is, sizeof, typeof No




Below is the example of of unary(++) operator overloading


Example 2

using System;

public class Program
{
           public static void Main()
           {
     
                 Area obj1 = new Area(10);

                Console.WriteLine("Value of m :" + obj1.GetValueX().ToString());

                Area obj2 = obj1++;

                Console.WriteLine("Value of m :" + obj2.GetValueX().ToString());

               Console.ReadKey();
          }
}


class Area
{

         int m;

        public Area(int x)
         {
             m = x;
        }

         public static Area operator ++(Area obj)
        {
             obj.m++;
             return obj;
        }

        public int GetValueX()
        {
              return m;
        }
}

Output :
Value of m :10
Value of m :11


Monday, 26 March 2018

040 C# Preprocessor Directives

Preprocessor directive
Preprocessor directive gives instruction compiler , before the actual compilation starts.  Preprocessor directives begin with #, but do not ends with semicolon (;) . C and C++  have separate processor, which create macros . C# do not have any separate Preprocessor.  C# considers Preprocessor directive as  instruction.

A simple program with Preprocessor directive

Example  
#define mySysmbol
using System;
public class Program
{
              public static void Main()
               {
                   #if (mySysmbol)
                      Console.WriteLine("mySysmbol is defined");
                  #endif
                 Console.ReadKey();
            }
}
 Output :
mySysmbol is defined

Here is the most common used Preprocessor directive.

directive What is ?
#define define symbol Preprocessor. The symbol maybe sequence of character
#undefine undefine the symbol which was defined by the #define.
#if test or check particular symbol or symbol with compound statement with operator return true or false. If true is return , some statement is executed
#else if #if returns false , this code block is executed with some statement
#elif checking of compound conditional statement on define by the #define
#endif close or end code block started from #if directives
#line compiler line number can be customize
#error customized error , is generated from desired location
#warning customize warning is generated from desired location
#region specify an area code blocks. Expand and collapse functionalities is by defauly in .Net framework
#endregion end code block area define by #define

A Preprocessor directive is defined at the top of a program as it is defined before the compilation of program . Preprocessor directive symbols  and same name variable can be declared, both do not conflict each other.

A simple program with Preprocessor directive , directive symbol and variable are same name.

Example  

#define mySysmbol
using System;
public class Program
{
          public static void Main()
         {
                    string mySysmbol = string.Empty;
                    mySysmbol = "I Am Learning C#";

                   #if (mySysmbol)
                               Console.WriteLine("mySysmbol is defined");
                  #endif
                  Console.ReadKey();
           }
}
Output :
mySysmbol is defined

If you are writing check statement (if) , you can use operator like =, == ,!=,&&,||,! . #region  is generally written when long term code is written , code is  separated  with different part of logic.

A simple program with Preprocessor directive , with operator.

Example
#define mySysmbol1
#define mySysmbol2
using System;
            public class Program
            {
                          public static void Main()
                         {
                                #if (mySysmbol1 && mySysmbol2)
                                {
                                           Console.WriteLine("mySysmbol1 and mySysmbol2 defined");
                                }
                               #endif
                               Console.ReadKey();
                    }
}
Output :
mySysmbol1 and mySysmbol2 defined



045 C# Keywords

Keyword  
C#  keywords  are predefined and reserved for a language usage.Each keyword  have its own meaning and instruction to the compiler. If you use the same name identifier, it will conflict and  throw error. You can use the same name of a keyword  adding @ at the start of a identifier name.

Here  is the example of how error occurred  if we use same name identifier and keyword.
Example 1

using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
           class Program
           {
                     static void Main(string[] args)
                   {
                         student case = new student();
                    }
          }
          class student
         {
              string Name;
             int Roll_Number;
          }
}
Output :Error
Invalid expression term 'case'    
Here 'case' is reserve keyword , we used as identifier , causes error.


Here is the example how we can use same name identifier and keyword without error.
Example 1
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
                  class Program
                 {
                        static void Main(string[] args)
                        {
                                   student @case = new student();
                         }
                 }
               class student
               {
                 string Name;
                 int Roll_Number;
             }
}



There are two types of Keyword, reserve and contextual.







Here is the list of reserved keyword commonly used.

abstract explicit const event
return new override extern
readonly sealed static unsafe
virtual volatile public private
internal protected if else
switch case do foreach
in while break continue
default goto throw try
catch finally unchecked checked
fixed lock ref out
using awit true  enum
void bull bite case
class decimal int double
float string unit sbyte
struc long double  

Contextual keyword : keyword are valid for certain context only. This keyword are not reserved, are reserved for certain context only.

add async await dynamic
get global partial remove
set value var when
where yield    

Here is the example of contextual keyword , 'global' keyword here used for current context only.
Example 1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
          class Program
         {
             static void Main(string[] args)
              {
                        global::System.Console.WriteLine("Your Specific Text");
                         Console.ReadKey();
             }
        }
     class System
     {
      }
}

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

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