{"id":179,"date":"2014-08-14T13:09:02","date_gmt":"2014-08-14T13:09:02","guid":{"rendered":"http:\/\/mairwa.com\/wordpress\/?p=179"},"modified":"2014-08-14T13:09:02","modified_gmt":"2014-08-14T13:09:02","slug":"aaa","status":"publish","type":"post","link":"http:\/\/mairwa.com\/wordpress\/?p=179","title":{"rendered":"Delegate and constructor"},"content":{"rendered":"<p>Delegate is a reference type that holds the reference of a class method. Any method which has the same signature as delegate can be assigned to delegate. It is very similar to the function pointer but with a difference that delegates are a type-safe. We can say that it is the object-oriented implementation of function pointers.<\/p>\n<p>There are three steps for defining and using delegates:<\/p>\n<ol>\n<li>Declaration<\/li>\n<\/ol>\n<p>A delegate is declared by using the keyword delegate, otherwise it resembles a method declaration.<\/p>\n<ol>\n<li>Instantiation<\/li>\n<\/ol>\n<p>To create a delegate instance, we need to assign a method (which has same signature as delegate) to delegate.<\/p>\n<ol>\n<li>Invocation<\/li>\n<\/ol>\n<p>Invoking a delegate is like as invoking a regular method.<\/p>\n<div>\n<pre><b>1.\u00a0 <\/b><b>\/\/1. Declaration<\/b><b><\/b><\/pre>\n<pre><b>2.\u00a0\u00a0 <\/b><b>public<\/b><b> delegate <\/b><b>int<\/b><b> <\/b><b>MyDelagate<\/b><b>(<\/b><b>int<\/b><b> a<\/b><b>,<\/b><b> <\/b><b>int<\/b><b> b<\/b><b>);<\/b><b> <\/b><b>\/\/delegates having same signature as method <\/b><b><\/b><\/pre>\n<pre><b>3.\u00a0\u00a0 <\/b><b>\u00a0<\/b><b><\/b><\/pre>\n<pre><b>4.\u00a0\u00a0 <\/b><b>public<\/b><b> <\/b><b>class<\/b><b> <\/b><b>Example<\/b><b><\/b><\/pre>\n<pre><b>5.\u00a0\u00a0 <\/b><b>{<\/b><b><\/b><\/pre>\n<pre><b>6.\u00a0\u00a0 <\/b><b>\u00a0<\/b><b>\/\/ methods to be assigned and called by delegate<\/b><b><\/b><\/pre>\n<pre><b>7.\u00a0\u00a0 <\/b><b>\u00a0<\/b><b>public<\/b><b> <\/b><b>int<\/b><b> <\/b><b>Sum<\/b><b>(<\/b><b>int<\/b><b> a<\/b><b>,<\/b><b> <\/b><b>int<\/b><b> b<\/b><b>)<\/b><b><\/b><\/pre>\n<pre><b>8.\u00a0\u00a0 <\/b><b>\u00a0<\/b><b>{<\/b><b><\/b><\/pre>\n<pre><b>9.\u00a0\u00a0 <\/b><b>\u00a0<\/b><b>return<\/b><b> a <\/b><b>+<\/b><b> b<\/b><b>;<\/b><b><\/b><\/pre>\n<pre><b>10.\u00a0 <\/b><b>\u00a0<\/b><b>}<\/b><b><\/b><\/pre>\n<pre><b>11.\u00a0 <\/b><b>\u00a0<\/b><b><\/b><\/pre>\n<pre><b>12.\u00a0 <\/b><b>\u00a0<\/b><b>public<\/b><b> <\/b><b>int<\/b><b> <\/b><b>Difference<\/b><b>(<\/b><b>int<\/b><b> a<\/b><b>,<\/b><b> <\/b><b>int<\/b><b> b<\/b><b>)<\/b><b><\/b><\/pre>\n<pre><b>13.\u00a0 <\/b><b>\u00a0<\/b><b>{<\/b><b><\/b><\/pre>\n<pre><b>14.\u00a0 <\/b><b>\u00a0<\/b><b>return<\/b><b> a <\/b><b>-<\/b><b> b<\/b><b>;<\/b><b><\/b><\/pre>\n<pre><b>15.\u00a0 <\/b><b>\u00a0<\/b><b>}<\/b><b><\/b><\/pre>\n<pre><b>16.\u00a0 <\/b><b>\u00a0<\/b><b>}<\/b><b><\/b><\/pre>\n<pre><b>17.\u00a0 <\/b><b>\u00a0<\/b><b>class<\/b><b> <\/b><b>Program<\/b><b><\/b><\/pre>\n<pre><b>18.\u00a0 <\/b><b>\u00a0<\/b><b>{<\/b><b><\/b><\/pre>\n<pre><b>19.\u00a0 <\/b><b>\u00a0<\/b><b>static<\/b><b> <\/b><b>void<\/b><b> <\/b><b>Main<\/b><b>()<\/b><b><\/b><\/pre>\n<pre><b>20.\u00a0 <\/b><b>\u00a0<\/b><b>{<\/b><b><\/b><\/pre>\n<pre><b>21.\u00a0 <\/b><b>\u00a0<\/b><b>Example<\/b><b> obj <\/b><b>=<\/b><b> <\/b><b>new<\/b><b> <\/b><b>Example<\/b><b>();<\/b><b><\/b><\/pre>\n<pre><b>22.\u00a0 <\/b><b>\u00a0<\/b><b><\/b><\/pre>\n<pre><b>23.\u00a0 <\/b><b>\u00a0<\/b><b>\/\/ 2. Instantiation : As a single cast delegate<\/b><b><\/b><\/pre>\n<pre><b>24.\u00a0 <\/b><b>\u00a0<\/b><b>MyDelagate<\/b><b> sum <\/b><b>=<\/b><b> <\/b><b>new<\/b><b> <\/b><b>MyDelagate<\/b><b>(<\/b><b>obj<\/b><b>.<\/b><b>Sum<\/b><b>);<\/b><b> <\/b><b><\/b><\/pre>\n<pre><b>25.\u00a0 <\/b><b>\u00a0<\/b><b>MyDelagate<\/b><b> diff <\/b><b>=<\/b><b> <\/b><b>new<\/b><b> <\/b><b>MyDelagate<\/b><b>(<\/b><b>obj<\/b><b>.<\/b><b>Difference<\/b><b>);<\/b><b><\/b><\/pre>\n<pre><b>26.\u00a0 <\/b><b>\u00a0<\/b><b><\/b><\/pre>\n<pre><b>27.\u00a0 <\/b><b>\u00a0<\/b><b>\/\/ 3.Invocation<\/b><b><\/b><\/pre>\n<pre><b>28.\u00a0 <\/b><b>\u00a0<\/b><b>Console<\/b><b>.<\/b><b>WriteLine<\/b><b>(<\/b><b>\"Sum of two integer is = \"<\/b><b> <\/b><b>+<\/b><b> sum<\/b><b>(<\/b><b>10<\/b><b>,<\/b><b> <\/b><b>20<\/b><b>));<\/b><b> <\/b><b><\/b><\/pre>\n<pre><b>29.\u00a0 <\/b><b>\u00a0<\/b><b>Console<\/b><b>.<\/b><b>WriteLine<\/b><b>(<\/b><b>\"Difference of two integer is = \"<\/b><b> <\/b><b>+<\/b><b> diff<\/b><b>(<\/b><b>20<\/b><b>,<\/b><b> <\/b><b>10<\/b><b>));<\/b><b><\/b><\/pre>\n<pre><b>30.\u00a0 <\/b><b>\u00a0<\/b><b>}<\/b><b><\/b><\/pre>\n<pre><b>31.\u00a0 <\/b><b>\u00a0<\/b><b>}<\/b><b><\/b><\/pre>\n<pre><b>32.\u00a0 <\/b><b>\u00a0<\/b><b><\/b><\/pre>\n<pre><b>33.\u00a0 <\/b><b>\/* Out Put <\/b><b><\/b><\/pre>\n<pre><b>34.\u00a0 <\/b><b>\u00a0<\/b><b><\/b><\/pre>\n<pre><b>35.\u00a0 <\/b><b>\u00a0Sum of two integer is = 30<\/b><b><\/b><\/pre>\n<pre><b>36.\u00a0 <\/b><b>\u00a0Difference of two integer is = 10<\/b><b><\/b><\/pre>\n<pre><b>37.\u00a0 <\/b><b>*\/<\/b><b><\/b><\/pre>\n<\/div>\n<h3>Key points about delegates<\/h3>\n<ol>\n<li>Delegates are like C++ function pointers but are type safe.<\/li>\n<li>Delegates allow methods to be passed as parameters.<\/li>\n<li>Delegates are used in event handling for defining callback methods.<\/li>\n<li>Delegates can be chained together i.e. these allow defining a set of methods that executed as a single unit.<\/li>\n<li>Once a delegate is created, the method it is associated will never changes because delegates are immutable in nature.<\/li>\n<li>Delegates provide a way to execute methods at run-time.<\/li>\n<li>All delegates are implicitly derived from System.MulticastDelegate, class which is inheriting from System.Delegate class.<\/li>\n<li>Delegate types are incompatible with each other, even if their signatures are the same. These are considered equal if they have the reference of same method.<\/li>\n<li>Single cast delegate<\/li>\n<\/ol>\n<h2>Types of delegates<\/h2>\n<p>A single cast delegate holds the reference of only single method. In previous example, created delegate is a single cast delegate.<\/p>\n<ol>\n<li>Multi cast delegate<\/li>\n<\/ol>\n<p>A delegate which holds the reference of more than one method is called multi-cast delegate. A multicast delegate only contains the reference of methods which return type is void. The + and += operators are used to combine delegate instances. Multicast delegates are considered equal if they reference the same methods in the same order.<\/p>\n<div>\n<pre><b>1.\u00a0 <\/b><b>\/\/1. Declaration<\/b><b><\/b><\/pre>\n<pre><b>2.\u00a0\u00a0 <\/b><b>public<\/b><b> delegate <\/b><b>void<\/b><b> <\/b><b>MyDelagate<\/b><b>(<\/b><b>int<\/b><b> a<\/b><b>,<\/b><b> <\/b><b>int<\/b><b> b<\/b><b>);<\/b><b> <\/b><b><\/b><\/pre>\n<pre><b>3.\u00a0\u00a0 <\/b><b>public<\/b><b> <\/b><b>class<\/b><b> <\/b><b>Example<\/b><b><\/b><\/pre>\n<pre><b>4.\u00a0\u00a0 <\/b><b>{<\/b><b><\/b><\/pre>\n<pre><b>5.\u00a0\u00a0 <\/b><b>\u00a0<\/b><b>\/\/ methods to be assigned and called by delegate<\/b><b><\/b><\/pre>\n<pre><b>6.\u00a0\u00a0 <\/b><b>\u00a0<\/b><b>public<\/b><b> <\/b><b>void<\/b><b> <\/b><b>Sum<\/b><b>(<\/b><b>int<\/b><b> a<\/b><b>,<\/b><b> <\/b><b>int<\/b><b> b<\/b><b>)<\/b><b><\/b><\/pre>\n<pre><b>7.\u00a0\u00a0 <\/b><b>\u00a0<\/b><b>{<\/b><b><\/b><\/pre>\n<pre><b>8.\u00a0\u00a0 <\/b><b>\u00a0<\/b><b>Console<\/b><b>.<\/b><b>WriteLine<\/b><b>(<\/b><b>\"Sum of integers is = \"<\/b><b> <\/b><b>+<\/b><b> <\/b><b>(<\/b><b>a <\/b><b>+<\/b><b> b<\/b><b>));<\/b><b><\/b><\/pre>\n<pre><b>9.\u00a0\u00a0 <\/b><b>\u00a0<\/b><b>}<\/b><b><\/b><\/pre>\n<pre><b>10.\u00a0 <\/b><b>\u00a0<\/b><b><\/b><\/pre>\n<pre><b>11.\u00a0 <\/b><b>\u00a0<\/b><b>public<\/b><b> <\/b><b>void<\/b><b> <\/b><b>Difference<\/b><b>(<\/b><b>int<\/b><b> a<\/b><b>,<\/b><b> <\/b><b>int<\/b><b> b<\/b><b>)<\/b><b><\/b><\/pre>\n<pre><b>12.\u00a0 <\/b><b>\u00a0<\/b><b>{<\/b><b><\/b><\/pre>\n<pre><b>13.\u00a0 <\/b><b>\u00a0<\/b><b>Console<\/b><b>.<\/b><b>WriteLine<\/b><b>(<\/b><b>\"Difference of integer is = \"<\/b><b> <\/b><b>+<\/b><b> <\/b><b>(<\/b><b>a <\/b><b>-<\/b><b> b<\/b><b>));<\/b><b><\/b><\/pre>\n<pre><b>14.\u00a0 <\/b><b>\u00a0<\/b><b>}<\/b><b><\/b><\/pre>\n<pre><b>15.\u00a0 <\/b><b>}<\/b><b><\/b><\/pre>\n<pre><b>16.\u00a0 <\/b><b>class<\/b><b> <\/b><b>Program<\/b><b><\/b><\/pre>\n<pre><b>17.\u00a0 <\/b><b>{<\/b><b><\/b><\/pre>\n<pre><b>18.\u00a0 <\/b><b>\u00a0<\/b><b>static<\/b><b> <\/b><b>void<\/b><b> <\/b><b>Main<\/b><b>()<\/b><b><\/b><\/pre>\n<pre><b>19.\u00a0 <\/b><b>\u00a0<\/b><b>{<\/b><b><\/b><\/pre>\n<pre><b>20.\u00a0 <\/b><b>\u00a0<\/b><b>Example<\/b><b> obj <\/b><b>=<\/b><b> <\/b><b>new<\/b><b> <\/b><b>Example<\/b><b>();<\/b><b><\/b><\/pre>\n<pre><b>21.\u00a0 <\/b><b>\u00a0<\/b><b>\/\/ 2. Instantiation<\/b><b><\/b><\/pre>\n<pre><b>22.\u00a0 <\/b><b>\u00a0<\/b><b>MyDelagate<\/b><b> multicastdel <\/b><b>=<\/b><b> <\/b><b>new<\/b><b> <\/b><b>MyDelagate<\/b><b>(<\/b><b>obj<\/b><b>.<\/b><b>Sum<\/b><b>);<\/b><b> <\/b><b><\/b><\/pre>\n<pre><b>23.\u00a0 <\/b><b>\u00a0multicastdel <\/b><b>+=<\/b><b> <\/b><b>new<\/b><b> <\/b><b>MyDelagate<\/b><b>(<\/b><b>obj<\/b><b>.<\/b><b>Difference<\/b><b>);<\/b><b><\/b><\/pre>\n<pre><b>24.\u00a0 <\/b><b>\u00a0<\/b><b><\/b><\/pre>\n<pre><b>25.\u00a0 <\/b><b>\u00a0<\/b><b>\/\/ 3. Invocation<\/b><b><\/b><\/pre>\n<pre><b>26.\u00a0 <\/b><b>\u00a0multicastdel <\/b><b>(<\/b><b>50<\/b><b>,<\/b><b> <\/b><b>20<\/b><b>);<\/b><b><\/b><\/pre>\n<pre><b>27.\u00a0 <\/b><b>\u00a0<\/b><b>}<\/b><b><\/b><\/pre>\n<pre><b>28.\u00a0 <\/b><b>}<\/b><b><\/b><\/pre>\n<pre><b>29.\u00a0 <\/b><b>\u00a0<\/b><b><\/b><\/pre>\n<pre><b>30.\u00a0 <\/b><b>\/* Out put<\/b><b><\/b><\/pre>\n<pre><b>31.\u00a0 <\/b><b>\u00a0<\/b><b><\/b><\/pre>\n<pre><b>32.\u00a0 <\/b><b>\u00a0Sum of integers is = 70 <\/b><b><\/b><\/pre>\n<pre><b>33.\u00a0 <\/b><b>\u00a0Difference of integer is = 30<\/b><b><\/b><\/pre>\n<pre><b>34.\u00a0 <\/b><b>\u00a0<\/b><b><\/b><\/pre>\n<pre><b>35.\u00a0 <\/b><b>*\/<\/b><b><\/b><\/pre>\n<\/div>\n<p>Static Constructors<\/p>\n<p>A static constructor is used to initialize any <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/98f28cdx.aspx\">static<\/a> data, or to perform a particular action that needs to be performed once only. It is called automatically before the first instance is created or any static members are referenced. Static constructors have the following properties:<\/p>\n<ul>\n<li>A static constructor does not take access modifiers or have parameters.<\/li>\n<li>A static constructor is called automatically to initialize the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/0b0thckt.aspx\">class<\/a> before the first instance is created or any static members are referenced.<\/li>\n<li>A static constructor cannot be called directly.<\/li>\n<li>The user has no control on when the static constructor is executed in the program.<\/li>\n<li>A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.<\/li>\n<li>If a static constructor throws an exception, the runtime will not invoke it a second time, and the type will remain uninitialized for the lifetime of the application domain in which your program is running.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>class Cl1<\/p>\n<p>{<\/p>\n<p>\/\/create static readonly object for new instance of<\/p>\n<p>\/\/this class (cl1).it is use for calling private constructor<\/p>\n<p>Public static readonly ss3 Instance = new cl1 ();<\/p>\n<p>Public int A;<\/p>\n<p>Static cl1 (){<\/p>\n<p>Console.WriteLine(&#8220;static&#8221;);<\/p>\n<p>}<\/p>\n<p>private cl1 ()<\/p>\n<p>{<\/p>\n<p>this.A = 5;<\/p>\n<p>Console.WriteLine(&#8220;public1&#8221;);<\/p>\n<p>}<\/p>\n<p>public cl1 (string rt) {<\/p>\n<p>Console.WriteLine(&#8220;public&#8221;); }<\/p>\n<p>public static void MyMethod()<\/p>\n<p>{<\/p>\n<p>Console.WriteLine(&#8220;MyMethod invoked.&#8221;);<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>class Program<\/p>\n<p>{<\/p>\n<p>static void Main(string[] args){<\/p>\n<p>\/\/if we create new instance of class cl1 then call<\/p>\n<p>\/\/private,static and public constructor in sequence .static and private \/\/constructor call only one time in entire assembly<\/p>\n<p>cl1 pr = new cl1(&#8220;io&#8221;);<\/p>\n<p>cl1.MyMethod();<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p><b>Private Constructors<\/b><\/p>\n<p>A private constructor is a special instance constructor. It is commonly used in classes that contain static members only. If a class has one or more private constructors and no public constructors, then other classes (except nested classes) are not allowed to create instances of this class.<\/p>\n<p>The declaration of the empty constructor prevents the automatic generation of a default constructor. Note that if we don&#8217;t use an access modifier with the constructor it will still be private by default. However, the <b>private<\/b> modifier is usually used explicitly to make it clear that the class cannot be instantiated.<\/p>\n<p>Private constructors are useful to prevent creation of a class when there are no instance fields or methods, such as the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.math(v=vs.71).aspx\">Math class<\/a>, or when a method is called to obtain an instance of a class.<\/p>\n<p>Example&#8211;<\/p>\n<p>using System;<\/p>\n<p>&nbsp;<\/p>\n<p>public class MyClass<\/p>\n<p>{<\/p>\n<p>private MyClass() {}<\/p>\n<p>public static int counter;<\/p>\n<p>public static int IncrementCounter()<\/p>\n<p>{<\/p>\n<p>return ++counter;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>class MainClass<\/p>\n<p>{<\/p>\n<p>static void Main()<\/p>\n<p>{<\/p>\n<p>\/\/ If you uncomment the following statement, it will generate<\/p>\n<p>\/\/ an error because the constructor is inaccessible:<\/p>\n<p>\/\/ MyClass myObject = new MyClass();\u00a0\u00a0 \/\/ Error<\/p>\n<p>MyClass.counter = 100;<\/p>\n<p>MyClass.IncrementCounter();<\/p>\n<p>Console.WriteLine(&#8220;New count: {0}&#8221;, MyClass.counter);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\">Delegate is a reference type that holds the reference of a class method. Any method which has the same signature as delegate can be assigned to delegate. It is very similar to the function pointer but with a difference that delegates are a type-safe. We can say that it is the object-oriented implementation of function pointers. There are three steps&hellip; <a href=\"http:\/\/mairwa.com\/wordpress\/?p=179\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-179","post","type-post","status-publish","format-standard","hentry","category-c-vb","xfolkentry"],"_links":{"self":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/179","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=179"}],"version-history":[{"count":0,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/179\/revisions"}],"wp:attachment":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=179"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=179"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=179"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}