Introduction
To initialize the data member of a class in runtime that means when object is created. If we create any class without constructor, the compiler will automatically create one default constructor for that class. There is always at least one constructor in every class.Introduction
Browser always preferred client by which we consumed data exposed over HTTP. So Microsoft introduced a features Web Service, WCF, Web API. But now Web API is more suitable than others because it is lightweight and when we need to use restful service it is more appropriate.Introduction
In this post I explain about Delegate in C#.
What is Delegate?
A delegate is a type that represents references to methods with a particular parameter list and return type. When you instantiate a delegate, you can associate its instance with any method with a compatible signature and return type. You can invoke (or call) the method through the delegate instance.
Why Delegate?
Delegates are used to pass methods as arguments to other methods. Event handlers are nothing more than methods that are invoked through delegates. You create a custom method, and a class such as a windows control can call your method when a certain event occurs.
How to declare a delegate?
public delegate int PerformCalculation(int x); <Access Modifier> <delegate> <Return Type> <Delegate Name>(<Parameter's>)
How to initialize a delegate?
<Delegate Name> <Delegate Object> = <new> Delegate(<Parameter's>)
Simple Example
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Anonymous { class Program { public delegate int fun(int Value); public static int del(int Value) { return Value += 7; } static void Main(string[] args) { Program okkk = new Program(); fun obj = new fun(del); Console.WriteLine(obj(50)); Type type = obj.GetType(); Console.WriteLine("\n Base Class for obj Delegate type:\t"+type.BaseType); Console.WriteLine("\n Is Class : \t"+type.IsClass); Console.WriteLine("\n Is Sealed Class : \t"+type.IsSealed); Console.ReadLine(); } } }
Example Delegate Used Method as A Parameter
By default a delegate takes a single instance function or static function as a parameter.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DelegateParam { public delegate void Del(); class delParam { public void fun() { Console.WriteLine("delParam"); } static void Main(string[] args) { delParam c1 = new delParam(); Del obj = new Del(c1.fun); obj.Invoke(); } } }
Type of Delegate
a. Singlecast Delegate
A Singlecast delegate is derived from the System.Delegate class. It can contain a reference for one method at a time. In the above example the delegate uses a Singlecast delegate. It used to invoke a single method. In the given source code example, a delegate CalculateSingleDelegate invokes a method getTotal().
class Program { // Declare a delegate delegate double CalculateSingleDelegate(double p, double t, double r); static CalculateSingleDelegate SI = getTotal; static void Main(string[] args) { double totalInterest; //Method I : Invocation of simple delegate by using Invoke keyword totalInterest = SI.Invoke(120, 1, 3.25); Console.WriteLine("Total Interest of $120 in a year at rate of 3.25% APR is {0}",totalInterest); //Method II : Invocation of simple delegate by passing method name CalculateSingleDelegate D = new CalculateSingleDelegate(getTotal); totalInterest = D(120, 1, 3.25); Console.WriteLine("Total Interest of $120 in a year at rate of 3.25% APR is {0}", totalInterest); Console.ReadKey(); } // Creating methods which will be assigned to delegate object // Gets the total interest. // The Principal. // The Time. // The Rate. //Total Interest static double getTotal(double p, double t, double r) { return (p * t * r) / 100; } }
b. Multicast Delegate
A Multicast delegate is derived from the System.MulticastDelegate class. Multicast delegate can be used to invoke the multiple methods. The delegate instance can do multicasting (adding new method on existing delegate instance) using the + operator and – operator can be used to remove a method from a delegate instance. All methods will invoke in sequence as they are assigned.
In the given source code example, a delegate instance dObjSI invokes the methods getTotal(), getRatePerYear() and getTimeSpan().
// Sample example of multicast delegate class Program { // Declare a delegate delegate double CalculateSimpleInterest(double para1, double para2, double para3); static CalculateSimpleInterest dObjSI = getTotal; static void Main(string[] args) { double SI; //Calculating simple interest SI = dObjSI.Invoke(120, 1, 3.25); //using multicast delegate by invoking method getRatePerYear() dObjSI += new CalculateSimpleInterest(getRatePerYear); double Rate=dObjSI.Invoke(SI, 120, 1); Console.WriteLine("APR rate is {0}", Rate); //using multicast delegate by invoking method getTimeSpan() dObjSI += new CalculateSimpleInterest(getTimeSpan); double TimeSpan = dObjSI.Invoke(SI, 120, 3.25); Console.WriteLine("Time Span is {0}", TimeSpan); Console.ReadKey(); } // Gets the total interest. // The Principal. // The Time. // The Rate. //Total Interest static double getTotal(double p, double t, double r) { return (p * t * r) / 100; } // Gets the interest rate per year. // The Simple Interest. // The Principal. // The Time. // Interest rate per year static double getRatePerYear(double SI, double p, double t) { return (SI * 100)/(p*t); } // Gets the interest time span. // The Simple Interest. // The Principal. // The Rate. // Interest time span static double getTimeSpan(double SI, double p, double r) { return (SI * 100) / (p * r); } }
c. Generic Delegate
Generic Delegate was introduced in .NET 3.5 that don't require to define the delegate instance in order to invoke the methods.
Conclusion
Guys hope I explain it peoperly.
Introduction
In this post I'm going to explain about the Difference between Const, ReadOnly & Static in C#.Introduction
In this post I explain about Collection, Array, ArrayList, HashTable, Stack, Queue, List, IList, IEnumerable, IQueryable, ICollection, IDictionary, Stack Generic, Queue GenericIntroduction
In this world everything are recognize to their behavior, nature or something, means we can uniquely identify them. So, in OOPS concept "Signature" is same like that. It helps to identify Methods, constructors, indexers etc. A signature makes a method look unique to the C# compiler.
Properties Of Signatures
1. The method name and the type and order of parameters all contribute to the uniqueness of signatures.
2. Signatures enable the overloading mechanism of members in classes, structs, and interfaces.
3. A method signature consists of the name of the method and the type and kind, such as value or reference.
4. An operator signature consists of the name of the operator and the type. An operator signature does not include the result type.
5. A method signature does not include the return type, nor does it include the params modifier that may be specified for the last parameter.
6. A constructor signature consists of the type and kind, such as value or reference. A constructor signature does not include the params modifier that may be specified for the last parameter.
7. An indexer signature consists of the type. An indexer signature does not include the element type.
Signatures Example
void Method(); // Method () void Method(int x); // Method (int) void Method(ref int x); // Method (ref int) void Method(out int x); // Method (out int) void Method(int x, int y); // Method (int, int) int Method(string s); // Method (string) int Method(int x); // Method (int) void Method(string[] a); // Method (string[]) void Method(params string[] a); // Method (string[])
Description
The ref and out parameter modifiers are part of a signature. Method(int), Method(ref int), and Method(out int) are all unique signatures. The return type and the params modifier are not part of a signature. Notice that there are some errors for the methods that contain duplicate signatures like Method(int) and Method(string[]) whose multiple signatures differ only by return type.
Conclusion
Hi guys I try to explain signature hope its helpful.
Introduction
In this post I try to show how to create a sandbox account. Using this account how to create a payment gateway. I write the two type of code one is form post and another is server side post.Introduction
In this post I try to show how to show data in a data table, which data from sql server and using stored procedure. Also the data table have the link (Edit, Delete, Block/Unblock). The data table alsoWhat is jQuery Data table?
DataTables is a plug-in for the jQuery Javascript library. You can add advanced interaction controls to any HTML table.Provider: Named Pipes Provider, error: 40 – Could not open a connection to SQL Server
11:57Error Message
(provider: Named Pipes Provider, error: 40 – Could not open a connection to SQL Server) (Microsoft SQL Server, Error: 5)
Follow The Below Steps:
1. Connect to SQL Server Instance using SQL Server Management Studio. Then right click on the AdventureWorks database which in this example we want to script out. From the popup menu, select "Tasks" and then "Generate Scripts…" option as shown in the snippet below.Follow the step below:
- Open your command prompt (Windows + R) and type
cmd
and press ENTER
You may need to start this as an administrator if you have UAC enabled.
To do so, locate the exe (usually you can start typing with Start Menu open), right click and select "Run as Administrator"
Problem
Here’s my PC environment :- Database : Oracle 11g
- OS Platform : Windows 7 Ultimate 64 bits
- JDK : 1.6 .0_24, 64 bits
Reason Of Error
1) If I use the NPM UI I get
The remote server returned an error: (504) Gateway Timeout.ASP.NET MVC 3 installer fails on VS2010 SP1
First I tried installing ASP.NET MVC 3 through Web Platform Installer. It seemed to work, but I didn’t get any ASP.NET MVC 3 project templates in Visual Studio.Error Page
Creating a Setup Project
This article is going to rely on screen prints since there really isn't any code for this. The output will be a *.msi file that will install the application file(s), and add a shortcut to the desktop and to theError Message
"An error occurred while getting provider information from the database. This can be caused by Entity Framework using an incorrect connection string. Check the inner exceptions for details and ensure that the connection string is correct."Standard Security
Data Source=myServerAddress; Initial Catalog=myDataBase; User Id=myUsername; Password=myPassword;