Delegates in C#

INTRODUCTION:-

Delegates is variable type that allow you to use function indirectly. The some delegates can be used to call any function  that matches a specific signature., giving you ability to choose between several function during the run time.
Delegates are declare much like function but with no function body and using the delegates keyword. The delegate declaration specified a function signature consisting of a return type and the parameter list. Delegates same as pointer to function in C and C++. Class declaration is then necessary condition for implementing delegates.

WHY DELEGATE?

Delegate improve the application performance.
Delegate use for encapsulation to the method calling from user.
Its use to call method asynchronously with in program. Delegates commonly used for Multithreading and Event handling.

SYNTAX:-

delegate <return type> delegate_name <parameter param_name>;//param is optional 

Create object of Delegates

After delegate type has been declared, a delegate object must be created with the new keyword and be associated with a particular method. When creating a delegate, the argument passed to the new expression is written like a method call, but without the arguments to the method. For example:

delegate double process_delegate(double num1, double num2);
process_delegate pd1 = new process_delegate(Add);
process_delegate pd2 = new process_delegate(Mult);

Program for Delegate:-

using System;
namespace C#DelegatesDemo
  {
      class delegate_program
      {
        delegate double process_delegate(double num1, double num2);; // Declare the delegate.

        //Method for addition
        Static double Add(double num1, double num2)
          {
           return num1 + num2;
          }

         //Method for multiplication
         Static double Mult(double num1, double num2)
          {
           return num1 * num2;
          }

         Static Void mail (String [] args)
          {
            process_delegate process;    // object creation for delegate.
            Console.WriteLine("Enter the choice, A for Addition and M for multiplication?");
            String input = Console.ReadLine(); // Read the user choice.
            if(input.ToUpper()=="A")
                      process = new process_delegate(delegate_program.Add); // Create delegate instance.
            Else
                      process = new process_delegate(delegate_program.Mul); // Create delegate instance.
          
            Console.WriteLine("Enter two numbers");
            
            double a = convert.ToDouble(Console.ReadLine());
            double b = convert.ToDouble(Console.ReadLine());

            Console.WriteLine("Result={0}",process(a,b)); // Calling the delegate.
            COnsole.ReadLine(); //Prevent the screen for vanish.

           }
        }
    }

OUTPUT:-

Enter the choice, A for Addition and M for multiplication
A
Enter to numbers
20 10
Result=30

Multicasting of a Delegate

Multicasting means delegates inside delegates. means using single object we can call two or more delegates by passing delegates object as a parameter. let me explain by example:-

 

using System;

delegate int process_delegate(int val);
namespace C#DelegatesDemo
{
   class delegate_program
   {
      static int num = 10;
      public static int Add(int val1)
      {
         return num+=val1;
      }

      public static int Mult(int val2)
      {
         return num*=val2;
      }
      public static int printval()
      {
         return num;
      }

      static void Main(string[] args)
      {
         process_delegate pd; //create delegate object
         
         process_delegaten pd1 = new process_delegate(Add); // Create delegate instance.
         process_delegaten pd2= new process_delegate(Mult); // Create delegate instance.
        
         pd = pd1;//assign pd1 delegates object  
         pd += pd2;//Delegate objects can be composed using the "+" operator.
         
         //calling multicast delegates
         pd(20);//it will call both delegates (invoke both method)
         
         Console.WriteLine("Value of Val: {0}", printNum());
         COnsole.ReadLine(); //Prevent the screen for vanish.
      }
   }
}

OUTPUT:-

Value of Val:600

CONCLUSION:-

This article would help to understand delegates in C# using simple examples. If you have any query regarding this article, you can reach me by comment here.

Program for switch case in C#

If multiple options are available and out of them a single options need to be executed, this can be efficiently done by using the switch case.
the value of the variable decide the particular case statement to be executed. i.e. depending upon the value. the program execution would directly enter one of the case statement, its execution takes place and the control of the program direct comes out of the switch statement. If matching case is not found the default block is executed.   

Syntax:- 
  switch(choice)
   {
      case const1:
             ------------------;
             ------------------;
             break;
      case const2:
             ------------------;
             ------------------;
             break;
      case const-n:
             ------------------;
             ------------------;
             break;
      default:
             ------------------;
             ------------------;

   }

* 
1) The brake statement is compulsory at the end of each case statement.
2) The contacts in a case statement can be integer character but float constants are not allowed.
3) The multiple case statement case statement can be written in any order without affecting the execution of the s   witch case statement.
4) The constant may not be in a perfect sequence and can be selected as per the requirement of the program.
5) The default block is optional and can be ignored.
6) The variable can be replaced by an expression but the expression should be solved to a single value.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

&nbsp;

namespace ConsoleATM

{

class Program

{

static void Main(string[] args)

{

double Ope_Balance = 5000;

int choice = print_msg();

while (true)

{

switch (choice)

{

case 1:

Ope_Balance = deposit(Ope_Balance);

Console.WriteLine("Do you want to continue Y/N ?\n");

if (Console.ReadLine().ToUpper() == "Y")

{

choice = print_msg();

}

break;

case 2:

Ope_Balance = withdrawal(Ope_Balance);

Console.WriteLine("Do you want to continue Y/N ?\n");

if (Console.ReadLine().ToUpper() == "Y")

{

choice = print_msg();

}

break;

case 3:

Console.WriteLine("your Balance is:- " + Ope_Balance + "\n");

Console.WriteLine("Do you want to continue Y/N ?\n");

if (Console.ReadLine().ToUpper() == "Y")

{

choice = print_msg();

}

break;

case 4:

Environment.Exit(0);

break;

default:

Console.WriteLine("Please enter the correct choice.");

choice = print_msg();

break;

}

}

Console.Read();

}

private static double withdrawal(double Ope_Balance)

{

Console.WriteLine("Enter the amount for withdrawal\n");

double withdrawal_amt = (Convert.ToDouble(Console.ReadLine()));

if (withdrawal_amt <= Ope_Balance)

{

Ope_Balance = Ope_Balance - withdrawal_amt;

}

else

{

Console.WriteLine("Your balance is less than withdrawal amount.\n");

}

return Ope_Balance;

}

private static double deposit(double op_bal)

{

Console.WriteLine("Enter the amount for deposite\n");

op_bal = op_bal + Convert.ToDouble(Console.ReadLine());

return op_bal;

}

private static int print_msg()

{

int result = 0;

Console.WriteLine("\n Well come to my application. Please enter your choice\n");

Console.WriteLine("1. Deposit the amount");

Console.WriteLine("2. Withdrawal the amount");

Console.WriteLine("3. Check your balance");

Console.WriteLine("4. Exit\n");

try

{

result = Convert.ToInt32(Console.ReadLine());

}

catch { }

return result;

}

}

}

switchCase
o/p:-
Well come to my application. Please enter your choice
1. Deposit the amount
2. Withdrawal the amount
3. Check your balance
4. Exit

What does mean by Console Application

An application that runs in a command prompt window same as a C and C++ program is known as consol application.
It doesn’t have any graphical user interface like mouse. Console Applications will have character based interface.

To work with console applications in .NET you have to use a class called Console that is available within the namespace System.

There are two methods for writing to the console output.

Console.Write() — print the value of variable, like int,char,float etc.

Console.WriteLine() — This does the same, but adds a newline at

the end of the output.

Console.ReadLine()-The below C#.NET code lets the user input a line of text.

To write C# console program
Open Visual Studio2010 ->File -> New Project -> C# Lang-> select Console Applications

consoleApp1

Program:-

consoleApp2


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace demo

{

class Program

{

static void Main(string[] args)

{

string your_name;//variable declaration

Console.WriteLine("Please enter your NAME"); //print the sentence

your_name = Console.ReadLine(); //read the character from user.

Console.WriteLine("Well-Come to my first application Mr. {0} ", your_name);

Console.Read();//Prevent to disappear the screen

}

}

}

Output:-

Please enter your NAME

Pradeep Atkari

Well-Come to my first application Mr. Pradeep Atkari

consoleApp3

How to use the Google Maps API on Windows 8

How to use the Google Maps API on Windows 8

Something that hasn’t been very well documented yet is how to implement the Google Maps API with Windows 8. There are lots of questions about how to do it, but not a lot of answers. Hopefully, this tutorial can help with that. 

 

EMAIL BROADCASTER

MAIL BROADCASTER
MAIL BROADCASTER

Mail Broadcaster allows you to send thousands of e-mails with the help of few clicks only that means you can broadcast several mails. This is very usefull in e-mail marketing.   Mail is a directly commercial message to a group of people using email.   In its broadest sense, every email sent to a potential or current customer could be considered email marketing.

Email marketing can be done to either cold lists or current customer database.

using it we can send mail with many domains like gmail, yahoo, rediffmail etc. no need to open your gmail account for send the mail. you can create as much as mail IDs for send the mail. you can use different different mail IDs for send the mail.

Front End:- Visual Studio 2008, c#.net

Back End :-  Sql Server 2005/2008

For more details feel free to contact us at info@shardainfotech.com or call us at (091) 879-387-2448,  (091) 976-621-0353 & (091) 957-909-9071.

Click here visit product page at our website.

Follow us on Facebook : Mail Broadcaster