Writing Plug-in Methods with Delegates

A delegate variable is assigned a method dynamically. This is useful for writing plug-in methods. In this example, we have a utility method named Add,Sub that appliesa NumberOper . The NumberOper method has a delegateparameter,



///
/// delegate declaration
///
///
///
///
public delegate int NumberOperation(int fvalue, int svalue);
///
/// Button click event it will call NumberOper method
///
///
/// 
private void btnClick_Click(object sender, EventArgs e)
{
int a = Convert.ToInt16(txtfvalue.Text);
int b = Convert.ToInt16(txtsvalue.Text);
txtresult.Text = NumberOper(a, b, Add).ToString();//Dynamic we Assign Method name
}
///
/// Button click event it will call NumberOper method
///
///
/// 
private void btnSub_Click(object sender, EventArgs e)
{
int a = Convert.ToInt16(txtfvalue.Text);
int b = Convert.ToInt16(txtsvalue.Text);
txtresult.Text = NumberOper(a, b, Sub).ToString(); //Dynamic we Assign Method name
}
///
/// Main Method with delegate Parameter
///
/// first value
/// second value
/// Dynamic Delegate
/// call method add or sub and return result
public int NumberOper(int a, int b, NumberOperation AT)
{
return AT(a, b);
}
///
/// Add two numbers Methos
///
/// first Value
/// Second value
/// Result

public int Add(int a, int b)
{
return a + b;
}
///
/// Sub two number
///
/// first value
/// Second value
/// int value

public int Sub(int a, int b)
{
return a - b;
}
Add Two Number Delegate Program

public delegate int AddTwoNumberDel(int fvalue, int svalue);

private void btnClick_Click(object sender, EventArgs e)
{
AddTwoNumberDel AT = Addnumber; // create delegate instance
//is shorthand for
//AddTwoNumberDel AT = new AddTwoNumberDel(Addnumber);
int a = Convert.ToInt16(txtfvalue.Text);
int b = Convert.ToInt16(txtsvalue.Text);
txtresult.Text = AT(a, b).ToString(); // invoke Method
//is shorthand for
//txtresult.Text = AT.Invoke(a, b).ToString();
}

public int Addnumber(int fvalue, int svalue)
{
return fvalue + svalue;
}

Note:

Copy and past your VS Editor it will Work... Do you want sample application mail me ...
What is Delegate?
  • Its reference to method.
  • once method assign to delegate its work like method
  • delegate method used any other method
Where we will use real time?

If you want to call Function in Runtime .and to get data after you can call your function but method name you don't know design time but you know runtime during time you can use "Delegate"
Different type of Delegate:
Single cast Delegate
Multicast Delegate

Single cast Delegate Syntax:

Below are signature of single cast delegate

Access-modifier delegate result-type identifier ([parameters]);

Access-modifier è Its Access permission for delegate (like public ,private ..,)

Delegate è it's a Keyword

Result-type èthis is return type (like int,string,void)

Identifier è delegate name (user define)

Parameter è run time value passing

Ex :

Way 1:

public delegate void jsdelegate();

above example is no return type ,no parameter

Way 2:

public delegate string jsdelegatevalue(int x,int y);

above example is string is return type and x,y is parameter

above 2 way are delegate declaration



Disadvatage:

Disadvantage is that only HTTP protocol could be used.
Another disadvantage is all the service will run on the same port.

WCF Programming Model:

WCF service have three parts .Service,End Point,Hosting
Service it is a class written in a .net language which have some method.service have one or more endpoint
Endpoint means communicate with client.end point have 3 parts 'ABC':
· 'A' for Address,
· 'B' for Binding
· 'C' for Contracts.
Address(WHERE): Address specifies the info about where to find the service.
Binding(HOW): Binding specifies the info for how to interact with the service.
Contracts(What): Contracts specifies the info for how the service is implemented and what it offers.
· Windows Communication Foundation is a programming platform and runtime system for building, configuring and deploying network-distributed services.
· It is the latest service oriented technology.
· WCF is a combined features of Web Service, Remoting, MSMQ and COM

WCF Advantaged:

WCF is interoperable with other services when compared to .Net Remoting,where the client and service have to be .Net.

WCF services provide better reliability and security in compared to ASMX web services.
In WCF, there is no need to make much change in code for implementing the security model and changing the binding. Small changes in the configuration will make your requirements. 

WCF has integrated logging mechanism, changing the configuration file settings will provide this functionality.

WCF services can be debugged now in Visual Studio 2008 /2010. Wcfsvchost.exe will do it for you because service will be self hosted when you start debugging.

WCF vs WebService:

Wcf and Webservice have some difference

 
Features
Web Service
WCF
Hosting
It can be hosted in IIS
It can be hosted in IIS, windows activation service, Self-hosting, Windows service
Programming
[WebService] attribute has to be added to the class
[ServiceContraact] attribute has to be added to the class
Model
[WebMethod] attribute represents the method exposed to client
[OperationContract] attribute represents the method exposed to client
Operation
One-way, Request- Response are the different operations supported in web service
One-Way, Request-Response, Duplex are different type of operations supported in WCF
XML
System.Xml.serialization name space is used for serialization
System.Runtime.Serialization namespace is used for serialization
Encoding
XML 1.0, MTOM(Message Transmission Optimization Mechanism), DIME, Custom
XML 1.0, MTOM, Binary, Custom
Transports
Can be accessed through HTTP, TCP, Custom
Can be accessed through HTTP, TCP, Named pipes, MSMQ,P2P, Custom
Protocols
Security
Security, Reliable messaging, Transactions


Keywords in C#.net

C#.net have 77 Reserve word. You are not declare variable/method in reserve words .if you want to declare reserved word you can use " @ " symbol.

Example:

private void btn_Click(object sender, EventArgs e)
{
string string="Welocme to DevEnvExe"
}
Suppose if you want use above coding
private void btn_Click(object sender, EventArgs e)
{
string @string="Welocme to DevEnvExe"
}
Below list are C#.net reserve words
unchecked
unsafe
ushort
boolirtual
class
abstract
as
base
byte
char
decimal
int
sbyte
uint
ulong
break
case
catch
finally
checked
const
continue
default
delegate
do
double
else
enum
event
explicit
extern
false
fixed
float
for
foreach
goto
if
implicit
in
interface
internal
is
lock
long
namespace
new
null
object
operator
out
override
params
private
protected
public
readonly
ref
return
sealed
short
sizeof
stackalloc
static
string
struct
switch
while
this
throw
true
try
typeof
using
void
volatile






What is Web.Config File?

   It is an optional XML File which stores configuration details for a specific asp.net web application.

What is Machine.config File?

The Machine.Config file, which specifies the settings that are global to a particular machine. This file is located at the following path:

\WINNT\Microsoft.NET\Framework\[Framework Version]\CONFIG\machine.config

As web.config file is used to configure one asp .net web application, same way Machine.config file is used to configure the application according to a particular machine. That is, configuration done in machine.config file is affected on any application that runs on a particular machine. Usually, this file is not altered and only web.config is used which configuring applications.

You can override settings in the Machine.Config file for all the applications in a particular Web site by placing a Web.Config file in the root directory of the Web site as follows:

\InetPub\wwwroot\Web.Config

Macine Config VS WebConfiguration

Difference between Machine.Config and Web.Config

Machine.Config:

i) This is automatically installed when you install Visual Studio. Net.
ii) This is also called machine level configuration file.
iii)Only one machine.config file exists on a server.
iv) This file is at the highest level in the configuration hierarchy.

Web.Config:

i) This is automatically created when you create an ASP.Net web application project.

ii) This is also called application level configuration file.

iii)This file inherits setting from the machine.config

2D Array

You want to use a 2D array containing any type(int,string ,etc) of value in your C# program

2D array contain 2 pair of values
Performance PERF
2D Array are slower to index elements than 1D arrays. They are sometimes more memory
2D Array String
String[,] os=new string[,]{
{"dev","Svalue"},
{"devenvexe","JValue"}

};
2D Array Int:
int[,] os=new int[,]{ {1,2}, {3,4} };
Get the Upperbound to Loop:
string[,] ad = new string[,]
{
{"dev", "devenvexe"},
{"TCS", "Tata"},
{"CTS", "Cogn"},
};

// Get the upper bound to loop.
//Upper bound means specify the dimention (0 or 1)
for (int i = 0; i <= ad.GetUpperBound(0); i++)
{
string FDim = ad[i, 0]; // JS, TCS, CTS...
string SDim = ad[i, 1]; // J Suqare, Tata, Cogn...
MessageBox.Show(FDim);
MessageBox.Show(SDim);
}
If you want use Length keyword .it will return 6 so
string[,] ad = new string[,]
{
{"dev", "devenvexe"},
{"TCS", "Tata"},
{"CTS", "Cogn"},
};

// Get the upper bound to loop.
//Upper bound means specify the dimention (0 or 1)
for (int i = 0; i <= ad.Length/2; i++)
{
string FDim = ad[i, 0]; // JS, TCS, CTS...
string SDim = ad[i, 1]; // J Suqare, Tata, Cogn...
MessageBox.Show(FDim);
MessageBox.Show(SDim);
}
GetUpperBound VS Length:
Its faster to use Array Length

Looping Speed:

GetUpperBound: 142 ms
Length/2 : 47 ms
Arrays are references that point to fixed sizes of memory that you can store different kinds of elements in, such as values or references to other objects. In these examples, we look at simple examples of arrays.

Arrays is fixed number of memory allocate in system

It is initialize and with accessing with Square brackets []

Two dimensional array use comma with in bracket

How to create Array:

Int[] value=new int[Length]

// Int means data type suppose if you want string,bool,etc
// value is data type
// length means ,you can enter int value

Ex:

String val=new String[3];

Ex Program:

Using system;
Class Progra
{
//Public void button Click
Sting val=new String[3];
Val[0]="devenvexe";
Val[1]="Suthahar";
Val[2]="Suresh";
Foreach(string n in Val)
{
MesageBox.Show(n.Tostring);
}
}

How to use C# string Format

string Format method replace the argument Object into a text equivalent System.String.

Replace string Particular index for ex System.String.Format("Hi welcome Mr {0}","Sutahahr")

System.String.format(string format,Object arg0)

Parameters:

String format : The format String

The format String Syntax is like {indexNumber:formatCharacter}

Object arg0 : The object to be formatted.

Returns:

String : The formatted String

Examples :

Currency :

String.Format("{0:c}", 10) will return $10.00
The currency symbol ($) displayed depends on the global locale settings.

Date :

String.Format("Today's date is {0:D}", DateTime.Now)

You will get Today's date like : 01 January 2005

Time :

String.Format("The current time is {0:T}", DateTime.Now)

You will get Current Time Like : 10:10:12
--
J.SuThahar MCA

MicroSoft Technology Specialist

Microsoft Certified ProfessionalDeveloper

www.jsuthahar.tk

09943151415

Featured Post

Improving C# Performance by Using AsSpan and Avoiding Substring

During development and everyday use, Substring is often the go-to choice for string manipulation. However, there are cases where Substring c...

MSDEVBUILD - English Channel

MSDEVBUILD - Tamil Channel

Popular Posts