Jan 21, 2020

MULTICAST DELEGATE IN C#

delegate, multicast delegate,c# delegate, c# Multicast delegate example,example of multicast delegate, definition of multicast delegate
multicast delegate

MULTICAST DELEGATE

            “A delegate holds more than one method reference and can be call by that delegate name”.
            If we have more than one method with same signature, here signature take as the same return type and same parameter accepts. Than for those methods we use multicast delegate to call all that method with one delegate name.
            In this topic we will discuss about three step to perform multicast delegate.
    1.    Defining the delegate
It is same like as delegate that’s the delegate signature should match with method
     signature except name. The multi task will perform in creating instance part.
      SYNTAX
                        [< Modifier >] delegate <return type> Name (parameter list)
    2.    Instantiating the instance of multicast delegate
The main difference between delegate and multicast delegate is going to declare here. We will create multiple method reference by using one delegate name like this.
            <Delegate name> d.name = new <delegate name> (reference method)
This syntax can be return like this
            <Delegate name> d.name = <1st reference method>
            d.name += <2nd reference method >
Here we use d.name for both methods and when we give one time values theses method will execute by same value see the example to clarify.
    3.    Calling the multicast delegate
It is also same like delegate calling. Calling will do in two different ways.
1st way:   <d.name> (value1, value2… value n)         //as above mention d.name
2nd way:  <d.name>.invoke (value1, value2… value n)
When we call one time all the linked method will execute using same values.
Points to be noted while going to perform multicast delegate
            There some points that should keep in mind when decide to make multicast delegate that’s
1.    We use multicast delegate in such a time where multi method has to execute by same values.
2.    Before using of this delegate make sure that all your methods are non-value returning type otherwise only the last method result will be shown and rest all are overwrites by the last one. If your methods are value returning type it better to use delegate instead of multicast delegate.

Jan 19, 2020

DELEGATE IN C#


c# delegate example,delegate,delegate meaning,multicast delegate in c#,use of delegates in c#
c# delegate

DELEGATE

            “Delegate is a point safe function pointer which is responsible to call a method and hold the reference to execute the method”.
            It is a user type and reference type which should be declares under a namespace. To perform delegate we have to do three steps

          1.    Defining a delegate

The syntax of delegate same like as method signature but we have to add delegate word extra see this,
            [<Modifier>] delegate <return type> <Name> [(parameter list)];
Here some point to be noted,
First point is the return type of the delegate must same to the method and the
Parameter list also must same to build delegate of a method.
            Second point is the name of the method and delegate must different for which we are going to make a delegate.
            If the method is static you need to declare the delegate also static because by default delegates are static. 
        2.    Instantiating instance of the delegate
Due to the delegate is reference type we have to instantiate the instance of delegate. This step is also similar to the method instantiating like this
<delegate name> db= <new> <delegate name> (method name)
db is nothing just name like we use the object name. example will clear it.
Here also two point to be noted
            First one while instantiating you have to pass the method name for which we build the delegate instead of giving parameter values.
            Second point is if the method is static it is directly call in method name if the main and delegate are in same class otherwise you need to give the class name as we did in the static method to execute, if it is non-static you should have to go with the reference or instance to call the method name in delegate method name.
        3.    Call the delegate
Where we call the delegate by passing the required values, but internally actually the method that is bound with the delegate get execute and give us the results. Like this
<Delegate name ‘db’> (parameter);   //db as we use for delegate instance.

Jan 18, 2020

C# INDEXER

indexer, indexer in c#, c# foreach with index,c# list indexof,index of c#,
c# indexer

INDEXERS IN C#

“Indexer is a class member, which gives access to a value of class just like array, and class behaves like virtual array not like an original array.” It is used to provide access to the class’s fields outside form the class like properties but that is another case.

SYNTAX

[<modifier>] <type> this [<parameter list>]
{
  [get{statements}]                    //Get accessor
  [set{statements}]                    // Set accessor  
}
Syntax Explanation
            -       Here the modifier may be public private or other.
            -       In type what type of value will provide that may be string, int, decimal or double but the best type is object because it return all the possible type int string double all.
            -       Where this pointing that we are going to define a indexer on the current class in which we declaring indexer.
            -       And the last get and set block will include your logic of getting and setting the values of class. Either you can permit to getting only or setting only but you can permit the both like properties in c#.    
            -       Parameter list may be parameter type list which can be string type, double type, care type or int type etc. and will write like this <int index> or <string name>, but what is the difference between this means if you use int type the index parameter will be number and if you use string type the indexing parameter will be some name on behalf of name we can access the values to get and set. But there is problem with name that is c# is case sensitive and conditioner is most of it if we make any name case diff. to the original then the assignment does not success and return any null name, this issue will be solved by convert the given name into either upper case and check or convert into lower case and check see example to understand.
            We use ‘else if’ construct to provide get and set accessor to the values which will clear in example see below. According to the index number mean 0, 1, 2 or 1, 2, 3 the values will be accessible to get and set.

Jan 16, 2020

Why multiple inheritance not support through class and support through interface

multiple inheritance in c#,c# inheritance,classes multiple inheritance
multiple inheritance does not support through classes

Why multiple inheritance not support through class and support through interface

            Before the explanation you should know all the points about multiple inheritances.         
The multiple inheritances is not support through class, look why we use inheritance to class to consume the member of a class to another where two class may have same name and signature like this
multiple inheritance, multiple inheritance through class, inheritance through classes,class in c#, inheritance
multiple inheritance support through interfaces not through class

When we call the Demo () method then here the ambiguity rise which Demo () method should call that’s why multiple inheritance is not supported through class.
            Where in case of interfaces contain only abstract members and we are going to inherit the interfaces not for consuming the member but for the implementation of the members. In this case we can built or perform multiple inheritance because we are going to implement the abstract member or method. Hera also the interface can contain multiple methods of same name and signature like
multiple inheritance, multiple inheritance through interfaces, inheritance through interface,interface
multiple inheritance support through interfaces not through class-
But when the word come to implementation there is no chance of raise ambiguity because we are not consuming any method we are just implementing the method and ambiguity is not raise for implementation that’s why multiple inheritance support  through interface but not by class.

Performing multiple inheritance process

            We will see three cases in this topic
Case 1:
When we have multiple class in our program and zero interface in the case we only able to perform single or multi-level inheritance not chance to perform multiple inheritance.
class inheritance, multiple inheritance, inheritance,class,
why classes not support multiple inheritance 
Case 2:
            Case two state if we have multiple interfaces and zero class in this case we can perform multiple inheritance as want. Also can perform single and multi-level inheritance like this  
interfaces inheritance,inheritance,interface in c#
why interface support multiple inheritance
Case 3:
        Case 3 deals if we have class as well as interface then the possibilities will be. We can use only one class and set of interfaces when building inheritance like, 
multiple inheritance support by interface inc#,multiple inheritance does not support by class
multiple inheritance is not support through classes but support through interfaces
Or we can skip the class and use multiple interfaces, but in this case also you can use only one class and multiple interfaces at a time. 
In simple words we perform inheritance between the classes to consuming the members and  inheritance perform to interfaces to implementation, where while consuming ambiguity problem can raise and no problem when implementing that’s why multiple inheritance is not support through class but still support through interfaces in c#/ CSharp.




Jan 15, 2020

OPERATOR OVERLOADING IN C#

operator overloading, c# operator overloading, overloading,operator overloading c#
operator overloading 

OPERATOR OVERLOADING

            “The approaches to defining multiple behaviors to an operator, the behavior will vary based on the operand used between the operators’”

For examples

            Let take the ‘+’ addition operator when it is used between two numeric operand it play the role of addition and when it use between two string operand it play concatenation. Like this
            Number + Number => addition
            String + String        => concatenation    // concatenation operator join the string.
Note: we cannot apply ‘string – string ‘ it is invalid and compiler give you error. Because for the ‘-‘ of two string inside compiler no class and logic are defined.

Code Example


class OperatorTest
    {
        public int Add(int a, int b)
        {
            return a + b;
        }
        public string Join(string a, string b)
        {
            return a + b;
        }
operator overloading in c++, operator overloading example, define operator overloading,method overloading and operator overloading
difference between method
and operator overloading
        static void Main()
        {
            int a, b;

            Console.WriteLine("enter sentence one");
            string str1 =Console.ReadLine();
            Console.WriteLine("enter sentence two");
            string str2 = Console.ReadLine();
            if (str1 != null)
                a = 1;
            else
                a = 0;
            if (str2 != null)
               b = 1;
            else
                b = 0;
            OperatorTest o = new OperatorTest();
            Console. WriteLine( o.Join(str1, str2));
            Console.WriteLine("totel {0} statement are joined", o.Add(a, b));
            Console.ReadKey();
        }
    }

Jan 14, 2020

STRUCTURE IN C-SHARP


STRUCTURE IN C-SHARP
structure in c#,c# data structures,c# struct inheritance,c# program structure,c# struct example,c# struct example
 c# struct

               " Structure is a user define type, which contain almost all the member that a class have such as in structure it can contain field, method , constructor, properties and operator method etc".
                Where in C language the structure contain only the fields.

DEFINING A STRUCTURE

                It almost similar to a class having modifiers  Structure name and of course struct it is the keyword to define a structure.
Syntax
[<modifiers>]  struct <name>
{
   Member definition;
}
The only one difference to defining a class and structure is the keyword class use class as keyword and structure use struct as keyword to defining a structure and the rest all same in both.
Instantiating instance of class and calling process of class and structure is also same we did it like this
Classname Obj=new Classname;                                     //instantiating the instance in class
Obj. CLassmembers                                                        // calling the members of class
                This gona same in structure also like this
Structurename Obj=new Structurename;                         //instantiating the instance of structure
Obj. CLassmembers                                                        // calling the members of structure

Example of structure:

Case 1 (used new keyword to creating instance)
               Field x is initialized by` constructor
namespace Teststrucrture
{
    public struct Airthemetic
    {
         int x;
        public void Add(int a, int b)
        {
            Console.WriteLine(a + b);
        }
        public void Sub(int a,int b)
        {
            Console.WriteLine(a - b);
        }
        static void Main()
        {
            Airthemetic a = new Airthemetic();
            a.Add(21, 20);
            a.Sub(34, 21);
            Console.ReadKey();
        }
    }
}

Case 2(without use new keyword)
                Field x is explicitly initialized
namespace Teststrucrture
{
    public struct Airthemetic
    {
         int x;
        public void Add(int a, int b)
        {
            Console.WriteLine(a + b);
        }
        public void Sub(int a,int b)
        {
            Console.WriteLine(a - b);
        }
        static void Main()
        {
            Airthemetic a;  a.x = 3;
            a.Add(21, 20);
            a.Sub(34, 21);
            Console.ReadKey();
        }
    }
}

Jan 11, 2020

METHOD HIDING /SHADOWING


METHOD HIDING /SHADOWING

            Method hiding is also known as method shadowing type of polymorphism which deals with the concept.
“Method hiding is a process of re-implementation the parent class method in child class by changing its behavior while keep the same name and signature”.
Whereas the parent class need not to declare as virtual but still the child class has to declared with ‘new’ keyword like this. Not use of override.
Suppose if I have parent class and its method
public class Parentclass
method hiding,polymorphism, method shadowing, After re-implementing the parent classes under child class how I call the parent class?, difference between overriding and hiding,
polymorphism method hiding
{
  public void Test()
  {

     Statements;
   }
 }
public class Childclass: Parentclas
{
  public new void Test()
  {
     New Statements;
   }
 }
Here we use new keyword to shadowing but it can be possible without using new keyword, but the compiler raise a warning to you. We use to this keyword to make sure that the parent method also active otherwise without using the new keyword child class method hide the parent’s method and make it not active, if we doesn’t use new then in future it may be chance to forget that the parent also have a same name and signature method, and it can leads to our program fail.

Difference between method overriding and method hiding

            We have two approaches to re-implementing a parent class method under child class method which are.
1.    Method overriding ‘
2.    Method hiding/shadowing
Here the detail of these differences.
METHOD OVERRIDING

       -       In this case the parent’s method must be  virtual to re-implement under child class.
       -       The child’s method must be overridden to change the behavior. 
       -       Method can re-implement with the permission of parent class that is need to  declare ‘virtual’.
       -       Due to permission the overridden member cannot considered as a pure child member.
       -       The overridden member/method can be accessible to the parent instance. Because it built with parent permission and not a pure child class method.
METHOD HIDING/SHADOWING

       -       In this case the parent’s method needs not to virtual to re-implement under child class. We can re-implement the normal method.
       -       The child’s method must be declared with new keyword change the behavior.
       -       Method can re-implement without the permission of parent class.
       -       The method creates in the child as using new keyword considered as a pure child class member.
       -       The parent reference can’t access the hiding method, because it purely a child method and pure member can’t e accessible for the parent

Jan 10, 2020

DIFFERENT KIND OF VARIABLE OF A CLASS C#


DIFFERENT KIND OF VARIABLE OF A CLASS

constant variable, non static variable, static variable, variable, variable kinds in c#, kind of variable of a class
kind of variable consist in a class
            You should know about variable before its kind so.

            “Variable is a name of memory storage which we used to store a value.”
Whenever we a have a value of any nature then we need a memory space from where we can store our value thus the concept of variable come into play at this time we declare a storage space to store that value the memory location are called as variable.
            We can differ its kind according its size and behavior according to this in c# we have four main kind of variable that are.
1.     Non-static variable
2.    Static variable
3.    Constant variable
4.    Read-Only variable

Jan 9, 2020

DIFFERENCE BETWEEN STATIC AND NON-STATIC CONSTRUCTOR

DIFFERENCE BETWEEN STATIC AND NON-STATIC CONSTRUCTOR

            There are some important point about static and non-static constructor it cover the definition, advantages and differences.
1.    The entire constructor that explicitly defined by the keyword static will be static constructor and the rest of all constructor will non-static constructor.
2.    Static constructor never defined implicitly it always defined explicitly by programmer as needed. Where the non-static constructor such as default/parameter-less constructor defined implicitly, if non-static constructor does not define explicitly.
3.    If the variable in class is static then it always initialize by static constructor, non- static constructor never initialize the static variable or static instance variables. Non-static constructor responsible to initialize the non-static variable/field of the class.
4.    Static constructor are implicitly called if we does not call in main class even then it execute immediately after class execution start, where the non-static constructor execute if and only if we called it by instantiating its object and execute each and every time after instantiating the class.
5.    Static constructor always execute before executing the code of main class but even then the main class will be the entry point of project not the static members, because static member does not to instantiating the class object to execute the member. Where the non-static constructor and member always execute after the instantiating/creating the class object.
6.    Static constructor execute only for one time in life cycle of a program , where the non-static constructor execute 0 times if the instance does not created otherwise it execute each and every time the class instance are created.
7.    Static constructor never is parameterized it must be parameter less, because the static constructor implicitly called and always has fixed values and that are declare in that class by static constructor. Whereas the non-static constructor can be parameterized.
8.    Static member can directly access with in a static block. But the non-static always access using class instance.
9.    Static constructor never overloaded, because overloading is done by parameter where static constructor are parameter less constructor. But we can perform overloading in non-static constructor because it can be parameterized.
10. All the class having implicit constructor, here the static constructor is implicitly defied if and only if the class having any static field, otherwise all the implicit constructor of any class will be non-static implicit constructor.
static vs non static constructor, static vs non static members, c# static constructor, c# non static constructor
difference between static and non-static constructor c#



WHY CONSTRUCTOR IS NEED IN OUR CLASS C# PROGRAMMING

WHY CONSTRUCTOR IS NEED IN OUR CLASS C# PROGRAMMING


            We already know about constructor what is it and how can we use it the need of constructor in our class due to following reasons.
advantages of constructor in c#, c# constructor, constructor,  use of constructor in programming, class member in programming
why constructor is need to our class in c#
1.    The very important first need of constructor is, every class requires a constructor in that class if we want to create the instance of that class. Lucky every class has its own constructor implicitly by the compiler if the constructor is not defined explicitly.
2.    The second point that comes our mind that what the need of explicit constructor if already that class have implicit constructor. The answer is because of the implicit constructor initialize the instance with default/same value even we create multiple instances in that class. But most time we need different value by changing instance. There for implicit constructor also known as default constructor.
3.    Now the third point the advantage of explicit constructor, is if we defined parametric constructor explicitly (a constructor that take argument) then we get a chance to change of initializing field of the class with new values every time to create instance of that class. Like this
public class test
{
  public int x;
  public test (int x)
  {
    this. x = x;   //this x refer to class variable x and x refer to the coming           
                         argument / local variable.
  }
}
Now when we create instance of the class we have opportunity to give different values with the time we create its instance. If the class needs a value then always try to create an explicit constructor.
4.    This point should note that whenever we defined a class, then every class need a value to execute that value can pass through the constructor only.

What is class member in c#      Constructor overloading    Static constructor