<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-5199405525386955005</id><updated>2011-04-22T06:51:40.312+06:00</updated><category term='C#'/><category term='OOP'/><category term='Software'/><category term='C#.Net'/><category term='Computer Science'/><category term='Software Engineering'/><category term='Object Oriented Programming'/><category term='Technology'/><category term='Programming'/><category term='.Net'/><title type='text'>Dark Coding Zone</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://darkcodingzone.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5199405525386955005/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://darkcodingzone.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Shiman</name><uri>http://www.blogger.com/profile/08649899361340334665</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>9</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-5199405525386955005.post-5890368365495829832</id><published>2008-11-20T18:21:00.002+06:00</published><updated>2008-11-20T19:09:42.852+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Technology'/><category scheme='http://www.blogger.com/atom/ns#' term='Object Oriented Programming'/><category scheme='http://www.blogger.com/atom/ns#' term='Programming'/><category scheme='http://www.blogger.com/atom/ns#' term='C#.Net'/><category scheme='http://www.blogger.com/atom/ns#' term='Software'/><category scheme='http://www.blogger.com/atom/ns#' term='Software Engineering'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='OOP'/><category scheme='http://www.blogger.com/atom/ns#' term='.Net'/><category scheme='http://www.blogger.com/atom/ns#' term='Computer Science'/><title type='text'>C# .NET Method Parameter Modifiers (The out Modifier)</title><content type='html'>&lt;p&gt;In a previous post I told about 4 method parameter modifiers of C#. The out modifier is one of them. In this post, I am going to tell about it and its purpose.&lt;/p&gt;&lt;p&gt;Methods that have been defined to take output parameters are under obligation to assign them to an appropriate value before exiting the method in question (if you fail to ensure this, you will receive compiler errors).&lt;/p&gt;&lt;p&gt;To illustrate, consider the Add() method that returns the sum of two integers using the C# out modifier (note the physical return value of this method is now void):&lt;/p&gt;&lt;pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%"&gt;&lt;code&gt;// Output parameters are allocated by the member.&lt;br /&gt;public static void Add(int x, int y, out int ans)&lt;br /&gt;{&lt;br /&gt;   ans = x + y;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Calling a method with output parameters also requires the use of the out modifier. Local variables passed as output variables are not required to be assigned before use (if you do so, the original value is lost after the call), for example:&lt;/p&gt;&lt;pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%"&gt;&lt;code&gt;static void Main(string[] args)&lt;br /&gt;{// No need to assign local output variables.&lt;br /&gt;   int ans;Add(90, 90, out ans);&lt;br /&gt;   Console.WriteLine("90 + 90 = {0} ", ans);&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The previous example is intended to be illustrative in nature; you really have no reason to return the value of your summation using an output parameter. However, &lt;span class="Apple-style-span" style="font-weight: bold;"&gt;the C# out modifier does serve a very useful purpose: it allows the caller to obtain multiple return values from a single method invocation.&lt;/span&gt;&lt;/p&gt;&lt;pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%"&gt;&lt;code&gt;// Returning multiple output parameters.&lt;br /&gt;public static void FillTheseValues(out int a, out string b, out bool c)&lt;br /&gt;{&lt;br /&gt;   a = 9; b = "Enjoy your string."; c = true;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The caller would be able to invoke the following method:&lt;/p&gt;&lt;pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%"&gt;&lt;code&gt;static void Main(string[] args)&lt;br /&gt;{&lt;br /&gt;   int i;&lt;br /&gt;   string str;&lt;br /&gt;   bool b;&lt;br /&gt;   FillTheseValues(out i, out str, out b);&lt;br /&gt;   Console.WriteLine("Int is: {0}", i);&lt;br /&gt;   Console.WriteLine("String is: {0}", str);&lt;br /&gt;   Console.WriteLine("Boolean is: {0}", b);&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5199405525386955005-5890368365495829832?l=darkcodingzone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://darkcodingzone.blogspot.com/feeds/5890368365495829832/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5199405525386955005&amp;postID=5890368365495829832' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5199405525386955005/posts/default/5890368365495829832'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5199405525386955005/posts/default/5890368365495829832'/><link rel='alternate' type='text/html' href='http://darkcodingzone.blogspot.com/2008/11/c-net-method-parameter-modifiers-out.html' title='C# .NET Method Parameter Modifiers (The out Modifier)'/><author><name>Shiman</name><uri>http://www.blogger.com/profile/08649899361340334665</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5199405525386955005.post-3130739356950642139</id><published>2008-11-19T18:37:00.001+06:00</published><updated>2008-11-19T18:53:21.497+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Technology'/><category scheme='http://www.blogger.com/atom/ns#' term='Object Oriented Programming'/><category scheme='http://www.blogger.com/atom/ns#' term='Programming'/><category scheme='http://www.blogger.com/atom/ns#' term='C#.Net'/><category scheme='http://www.blogger.com/atom/ns#' term='Software'/><category scheme='http://www.blogger.com/atom/ns#' term='Software Engineering'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='OOP'/><category scheme='http://www.blogger.com/atom/ns#' term='.Net'/><category scheme='http://www.blogger.com/atom/ns#' term='Computer Science'/><title type='text'>C# .NET Method Parameter Modifiers</title><content type='html'>&lt;p class="MsoNormal"&gt;&lt;span&gt;All the Methods, both static and instance level, tend  to take parameters passed in by the caller. However, unlike some programming  languages, C# provides a set of parameter modifiers that control how arguments  are sent into (and possibly returned from) a given method, as shown in the  Table.&lt;/span&gt;&lt;/p&gt; &lt;table class="MsoTableGrid" style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none; BORDER-COLLAPSE: collapse" cellspacing="0" cellpadding="0" border="1"&gt; &lt;tbody&gt; &lt;tr style="HEIGHT: 13pt"&gt; &lt;td style="PADDING-RIGHT: 5.4pt; PADDING-LEFT: 5.4pt; BACKGROUND: silver 0px 50%; PADDING-BOTTOM: 0px; WIDTH: 1.45in; PADDING-TOP: 0px; HEIGHT: 13pt" valign="top" width="139"&gt; &lt;h4&gt;&lt;span&gt;Parameter Modifier&lt;/span&gt;&lt;/h4&gt;&lt;/td&gt; &lt;td style="PADDING-RIGHT: 5.4pt; PADDING-LEFT: 5.4pt; BACKGROUND: silver 0px 50%; PADDING-BOTTOM: 0px; WIDTH: 4.7in; PADDING-TOP: 0px; HEIGHT: 13pt" valign="top" width="451"&gt; &lt;h4&gt;&lt;strong&gt;&lt;span&gt;Meaning &lt;/span&gt;&lt;/strong&gt;&lt;/h4&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td style="PADDING-RIGHT: 5.4pt; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0px; WIDTH: 1.45in; PADDING-TOP: 0px" valign="top" width="139"&gt; &lt;p class="MsoNormal"&gt;&lt;span&gt;(none)&lt;/span&gt;&lt;/p&gt;&lt;/td&gt; &lt;td style="PADDING-RIGHT: 5.4pt; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0px; WIDTH: 4.7in; PADDING-TOP: 0px" valign="top" width="451"&gt;&lt;span&gt;If a parameter is not marked with a parameter  modifier, it is assumed to be &lt;/span&gt;&lt;span&gt;passed by value, &lt;/span&gt;&lt;span&gt;meaning  the called method receives a copy of the original data.&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td style="PADDING-RIGHT: 5.4pt; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0px; WIDTH: 1.45in; PADDING-TOP: 0px" valign="top" width="139"&gt;&lt;span&gt;out&lt;/span&gt;&lt;/td&gt; &lt;td style="PADDING-RIGHT: 5.4pt; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0px; WIDTH: 4.7in; PADDING-TOP: 0px" valign="top" width="451"&gt;&lt;span&gt;Output parameters are assigned by the method being  called (and therefore &lt;/span&gt;&lt;span&gt;passed by reference&lt;/span&gt;&lt;span&gt;). If the  called method fails to assign output parameters, you are issued a compiler  error.&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td style="PADDING-RIGHT: 5.4pt; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0px; WIDTH: 1.45in; PADDING-TOP: 0px" valign="top" width="139"&gt;&lt;span&gt;params&lt;/span&gt;&lt;/td&gt; &lt;td style="PADDING-RIGHT: 5.4pt; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0px; WIDTH: 4.7in; PADDING-TOP: 0px" valign="top" width="451"&gt;&lt;span&gt;This parameter modifier allows you to send in a  variable number of identically typed arguments as a single logical parameter. A  method can have only a single &lt;/span&gt;&lt;span&gt;params &lt;/span&gt;&lt;span&gt;modifier, and it  must be the final parameter of the method.&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td style="PADDING-RIGHT: 5.4pt; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0px; WIDTH: 1.45in; PADDING-TOP: 0px" valign="top" width="139"&gt;&lt;span&gt;ref&lt;/span&gt;&lt;/td&gt; &lt;td style="PADDING-RIGHT: 5.4pt; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0px; WIDTH: 4.7in; PADDING-TOP: 0px" valign="top" width="451"&gt;&lt;span&gt;The value is initially assigned by the caller, and  may be &lt;/span&gt;&lt;span&gt;optionally &lt;/span&gt;&lt;span&gt;reassigned by the called method (as  the data is also passed by reference). No compiler error is generated if the  called method fails to assign a &lt;/span&gt;&lt;span&gt;ref  &lt;/span&gt;&lt;span&gt;parameter.&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5199405525386955005-3130739356950642139?l=darkcodingzone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://darkcodingzone.blogspot.com/feeds/3130739356950642139/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5199405525386955005&amp;postID=3130739356950642139' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5199405525386955005/posts/default/3130739356950642139'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5199405525386955005/posts/default/3130739356950642139'/><link rel='alternate' type='text/html' href='http://darkcodingzone.blogspot.com/2008/11/c-net-method-parameter-modifiers.html' title='C# .NET Method Parameter Modifiers'/><author><name>Shiman</name><uri>http://www.blogger.com/profile/08649899361340334665</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5199405525386955005.post-2387045737723793088</id><published>2008-11-19T18:29:00.003+06:00</published><updated>2008-11-19T18:53:03.077+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Technology'/><category scheme='http://www.blogger.com/atom/ns#' term='Object Oriented Programming'/><category scheme='http://www.blogger.com/atom/ns#' term='Programming'/><category scheme='http://www.blogger.com/atom/ns#' term='C#.Net'/><category scheme='http://www.blogger.com/atom/ns#' term='Software'/><category scheme='http://www.blogger.com/atom/ns#' term='Software Engineering'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='OOP'/><category scheme='http://www.blogger.com/atom/ns#' term='.Net'/><category scheme='http://www.blogger.com/atom/ns#' term='Computer Science'/><title type='text'>Uses of the Static Keyword in C#.NET (Static Classes)</title><content type='html'>&lt;p&gt;When a class has been defined as static, it is not creatable using the new keyword, and it can contain only static members or fields (if this is not the case, you receive compiler errors). Static classes are sealed class by default that means a static class cannot be inherited by other class.&lt;/p&gt;&lt;p&gt;This might seem like a very useless feature, given that a class that cannot be created and cannot be inherited does not appear all that helpful. However, if you create a class that contains nothing but static members and/or constant data, the class has no need to be allocated in the first place. Consider the following type:&lt;/p&gt;&lt;pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%"&gt;&lt;code&gt;static class UtilityClass&lt;br /&gt;{&lt;br /&gt;    public static void PrintTime()&lt;br /&gt;    { Console.WriteLine(DateTime.Now.ToShortTimeString()); }&lt;br /&gt;&lt;br /&gt;    public static void PrintDate()&lt;br /&gt;    { Console.WriteLine(DateTime.Today.ToShortDateString()); }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Given the static modifier, object users cannot create an instance of UtilityClass:&lt;/p&gt;&lt;pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%"&gt;&lt;code&gt;static void Main(string[] args)   &lt;br /&gt;{   &lt;br /&gt;    UtilityClass.PrintDate();   &lt;br /&gt;    // Compiler error! Can't create static classes.   &lt;br /&gt;    UtilityClass u = new UtilityClass();   &lt;br /&gt;    //...   &lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5199405525386955005-2387045737723793088?l=darkcodingzone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://darkcodingzone.blogspot.com/feeds/2387045737723793088/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5199405525386955005&amp;postID=2387045737723793088' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5199405525386955005/posts/default/2387045737723793088'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5199405525386955005/posts/default/2387045737723793088'/><link rel='alternate' type='text/html' href='http://darkcodingzone.blogspot.com/2008/11/uses-of-static-keyword-in-cnet-static_19.html' title='Uses of the Static Keyword in C#.NET (Static Classes)'/><author><name>Shiman</name><uri>http://www.blogger.com/profile/08649899361340334665</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5199405525386955005.post-300706664104681155</id><published>2008-11-18T18:12:00.003+06:00</published><updated>2008-11-19T18:52:22.002+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Technology'/><category scheme='http://www.blogger.com/atom/ns#' term='Object Oriented Programming'/><category scheme='http://www.blogger.com/atom/ns#' term='Programming'/><category scheme='http://www.blogger.com/atom/ns#' term='C#.Net'/><category scheme='http://www.blogger.com/atom/ns#' term='Software'/><category scheme='http://www.blogger.com/atom/ns#' term='Software Engineering'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='OOP'/><category scheme='http://www.blogger.com/atom/ns#' term='.Net'/><category scheme='http://www.blogger.com/atom/ns#' term='Computer Science'/><title type='text'>Uses of the Static Keyword in C#.NET (Static Constructors)</title><content type='html'>&lt;p&gt;As you know, constructors are used to set the value of a type’s data at the time of construction. If you were to assign the value to a piece of static data within an instance-level constructor, you would be saddened to find that the value is reset each time you create a new object! For example, assume you have updated the SavingsAccount class as so:&lt;/p&gt;&lt;pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%"&gt;&lt;code&gt;class SavingsAccount&lt;br /&gt;{&lt;br /&gt; public double currBalance;&lt;br /&gt; public static double interestRate;&lt;br /&gt; public SavingsAccount(double balance)&lt;br /&gt; {&lt;br /&gt;     currBalance = balance;&lt;br /&gt;     interestRate = 0.04;&lt;br /&gt; }&lt;br /&gt; //...&lt;br /&gt;}&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;If you execute the previous Main() method, you will see a very different output. Specifically notice how the currInterestRate variable is reset each time you create a new SavingsAccount object.&lt;/p&gt;&lt;p&gt;While you are always free to establish the initial value of static data using the member initialization syntax, what if the value for your static data needed to be obtained from a database or external file? To perform such tasks requires a method scope to author the code statements. For this very reason, C# allows you to define a static constructor:&lt;/p&gt;&lt;pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%"&gt;&lt;code&gt;class SavingsAccount&lt;br /&gt;{&lt;br /&gt;   //...  &lt;br /&gt;   // Static constructor.  &lt;br /&gt;   static SavingsAccount()&lt;br /&gt;   {&lt;br /&gt;       Console.WriteLine("In static ctor!");&lt;br /&gt;       currInterestRate = 0.04;&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Few points of interest regarding static constructors are:&lt;/p&gt; &lt;div style="PADDING-LEFT: 30px"&gt; &lt;ul&gt; &lt;li&gt;A given class (or structure) may define only one single static constructor.  &lt;/li&gt;&lt;li&gt;A static constructor is executed exactly one time, no matter of how many  objects of the type are created.  &lt;/li&gt;&lt;li&gt;A static constructor does not take an access modifier and cannot take any  parameters.  &lt;/li&gt;&lt;li&gt;The runtime invokes the static constructor when it creates an instance of  the class or before accessing the first static member invoked by the caller.  &lt;/li&gt;&lt;li&gt;The static constructor executes before any instance-level constructors.  &lt;/li&gt;&lt;/ul&gt;&lt;/div&gt; &lt;p&gt;Given this modification, when you create new SavingsAccount objects, the  value of the static data is preserved, and the output is identical to the output  of the &lt;em&gt;Static data&lt;/em&gt; program output.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5199405525386955005-300706664104681155?l=darkcodingzone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://darkcodingzone.blogspot.com/feeds/300706664104681155/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5199405525386955005&amp;postID=300706664104681155' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5199405525386955005/posts/default/300706664104681155'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5199405525386955005/posts/default/300706664104681155'/><link rel='alternate' type='text/html' href='http://darkcodingzone.blogspot.com/2008/11/uses-of-static-keyword-in-cnet-static_18.html' title='Uses of the Static Keyword in C#.NET (Static Constructors)'/><author><name>Shiman</name><uri>http://www.blogger.com/profile/08649899361340334665</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5199405525386955005.post-1102697840685217619</id><published>2008-11-18T12:07:00.004+06:00</published><updated>2008-11-18T12:19:55.133+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Technology'/><category scheme='http://www.blogger.com/atom/ns#' term='Object Oriented Programming'/><category scheme='http://www.blogger.com/atom/ns#' term='Programming'/><category scheme='http://www.blogger.com/atom/ns#' term='C#.Net'/><category scheme='http://www.blogger.com/atom/ns#' term='Software'/><category scheme='http://www.blogger.com/atom/ns#' term='Software Engineering'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='OOP'/><category scheme='http://www.blogger.com/atom/ns#' term='.Net'/><category scheme='http://www.blogger.com/atom/ns#' term='Computer Science'/><title type='text'>Uses of the Static Keyword in C#.NET (Static Methods)</title><content type='html'>&lt;p&gt;Let us consider a class named Teenager that defines a static method named Complain(), which returns a random string, obtained in part by calling a private helper function named GetRandomNumber():&lt;/p&gt;&lt;pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%"&gt;&lt;code&gt;class Teenager&lt;br /&gt;{&lt;br /&gt;    private static Random r = new Random();&lt;br /&gt;&lt;br /&gt;    private static int GetRandomNumber(short upperLimit)&lt;br /&gt;    {&lt;br /&gt;        return r.Next(upperLimit);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public static string Complain()&lt;br /&gt;    {&lt;br /&gt;        string[] messages = new string[5]{ &amp;quot;Do I have to?&amp;quot;,&lt;br /&gt;                                            &amp;quot;He started it!&amp;quot;,&lt;br /&gt;                                            &amp;quot;I'm too tired...&amp;quot;,&lt;br /&gt;                                            &amp;quot;I hate school!&amp;quot;,&lt;br /&gt;                                            &amp;quot;You are sooo wrong.&amp;quot;  &lt;br /&gt;                                        };&lt;br /&gt;        return messages[GetRandomNumber(5)];&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Notice that the System.Random member variable and the GetRandomNumber() helper function method have also been declared as static members of the Teenager class.&lt;/p&gt;&lt;p&gt;Like any static member, to call Complain(), prefix the name of the defining class:&lt;/p&gt;&lt;pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%"&gt;&lt;code&gt;// Call the static Complain method of the Teenager class.   &lt;br /&gt;static void Main(string[] args)&lt;br /&gt;{&lt;br /&gt;    for (int i = 0; i &amp;lt; 10; i++)&lt;br /&gt;        Console.WriteLine(&amp;quot;-&amp;gt; {0}&amp;quot;, Teenager.Complain());&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;And like any nonstatic method, if the Complain() method was not marked static, you would need to create an instance of the Teenager class before you could hear about the gripe of the day:&lt;/p&gt;&lt;pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%"&gt;&lt;code&gt;// Nonstatic data must be invoked at the object level.   &lt;br /&gt;Teenager joe = new Teenager();   &lt;br /&gt;joe.Complain();&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5199405525386955005-1102697840685217619?l=darkcodingzone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://darkcodingzone.blogspot.com/feeds/1102697840685217619/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5199405525386955005&amp;postID=1102697840685217619' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5199405525386955005/posts/default/1102697840685217619'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5199405525386955005/posts/default/1102697840685217619'/><link rel='alternate' type='text/html' href='http://darkcodingzone.blogspot.com/2008/11/uses-of-static-keyword-in-cnet-static.html' title='Uses of the Static Keyword in C#.NET (Static Methods)'/><author><name>Shiman</name><uri>http://www.blogger.com/profile/08649899361340334665</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5199405525386955005.post-853415703309136825</id><published>2008-11-17T12:11:00.004+06:00</published><updated>2008-11-18T12:20:50.916+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Technology'/><category scheme='http://www.blogger.com/atom/ns#' term='Object Oriented Programming'/><category scheme='http://www.blogger.com/atom/ns#' term='Programming'/><category scheme='http://www.blogger.com/atom/ns#' term='C#.Net'/><category scheme='http://www.blogger.com/atom/ns#' term='Software'/><category scheme='http://www.blogger.com/atom/ns#' term='Software Engineering'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='OOP'/><category scheme='http://www.blogger.com/atom/ns#' term='.Net'/><category scheme='http://www.blogger.com/atom/ns#' term='Computer Science'/><title type='text'>Uses of the Static Keyword in C# .NET (Static Data)</title><content type='html'>&lt;p&gt;A type may define static data. When a class defines nonstatic data, each object of this type maintains a private copy of the field. For example, assume a class that models a savings account:&lt;/p&gt;&lt;pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%"&gt;&lt;code&gt;class SavingsAccount&lt;br /&gt;{&lt;br /&gt; public double currentBalance;&lt;br /&gt; public SavingsAccount(double balance)&lt;br /&gt; { currentBalance = balance; }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;When you create SavingsAccount objects, memory for the currentBalance field is allocated for each instance. Static data, on the other hand, is allocated once and shared among all object instances of the same type. To illustrate the usefulness of static data, add a piece of static data named interestRate to the SavingsAccount class:&lt;/p&gt;&lt;pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%"&gt;&lt;code&gt;class SavingsAccount&lt;br /&gt;{&lt;br /&gt; public double currentBalance;&lt;br /&gt; public static double interestRate = 0.04;&lt;br /&gt; public SavingsAccount(double balance)&lt;br /&gt; { currentBalance = balance; }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;If you were to create three instances of SavingsAccount as so:&lt;/p&gt;&lt;pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%"&gt;&lt;code&gt;static void Main(string[] args)&lt;br /&gt;{&lt;br /&gt; // Each SavingsAccount object maintains a copy of the currBalance field.&lt;br /&gt; SavingsAccount s1 = new SavingsAccount(50);&lt;br /&gt; SavingsAccount s2 = new SavingsAccount(100);&lt;br /&gt; SavingsAccount s3 = new SavingsAccount(10000.75);&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;the in-memory data allocation would look something like the figure.&lt;/p&gt;&lt;p&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://1.bp.blogspot.com/_FqHBLEQajic/SSEM4Emt84I/AAAAAAAAAAM/SrVs2RTT-78/s1600-h/staticdata.gif"&gt;&lt;img style="float:center; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 320px; height: 156px;" src="http://1.bp.blogspot.com/_FqHBLEQajic/SSEM4Emt84I/AAAAAAAAAAM/SrVs2RTT-78/s320/staticdata.gif" border="0" alt="" id="BLOGGER_PHOTO_ID_5269507196448535426" /&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;Let’s update the SavingsAccount class to define two static methods to get and set the interest rate value. As stated, static methods can operate only on static data. However, a nonstatic method can make use of both static and nonstatic data. This should make sense, given that static data is available to all instances of the type. Given this, let’s also add two instance-level methods to interact with the interest rate variable:&lt;/p&gt;&lt;pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%"&gt;&lt;code&gt;class SavingsAccount&lt;br /&gt;{&lt;br /&gt;    public double currentBalance;&lt;br /&gt;    public static double interestRate = 0.04;&lt;br /&gt;&lt;br /&gt;    public SavingsAccount(double balance)&lt;br /&gt;    { currentBalance = balance; }&lt;br /&gt;&lt;br /&gt;    // Static methods to get/set interest rate.   &lt;br /&gt;    public static void SetInterestRate(double newRate)&lt;br /&gt;    { interestRate = newRate; }&lt;br /&gt;    public static double GetInterestRate()&lt;br /&gt;    { return interestRate; }&lt;br /&gt;&lt;br /&gt;    // Instance method to get/set current interest rate.   &lt;br /&gt;    public void SetInterestRateObj(double newRate)&lt;br /&gt;    { interestRate = newRate; }&lt;br /&gt;&lt;br /&gt;    public double GetInterestRateObj()&lt;br /&gt;    { return interestRate; }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Now, observe the following usage and the output.&lt;/p&gt;&lt;pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%"&gt;&lt;code&gt;static void Main(string[] args)&lt;br /&gt;{&lt;br /&gt;    SavingsAccount s1 = new SavingsAccount(50);&lt;br /&gt;    SavingsAccount s2 = new SavingsAccount(100);&lt;br /&gt;    // Get and set interest rate.   &lt;br /&gt;    Console.WriteLine(&amp;quot;Interest Rate is: {0}&amp;quot;, s1.GetInterestRateObj());&lt;br /&gt;    s2.SetInterestRateObj(0.08);&lt;br /&gt;    // Make new object, this does NOT 'reset' the interest rate.   &lt;br /&gt;    SavingsAccount s3 = new SavingsAccount(10000.75);&lt;br /&gt;    Console.WriteLine(&amp;quot;Interest Rate is: {0}&amp;quot;, SavingsAccount.GetInterestRate());&lt;br /&gt;    Console.ReadKey();&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5199405525386955005-853415703309136825?l=darkcodingzone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://darkcodingzone.blogspot.com/feeds/853415703309136825/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5199405525386955005&amp;postID=853415703309136825' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5199405525386955005/posts/default/853415703309136825'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5199405525386955005/posts/default/853415703309136825'/><link rel='alternate' type='text/html' href='http://darkcodingzone.blogspot.com/2008/11/uses-of-static-keyword-in-c-net-static.html' title='Uses of the Static Keyword in C# .NET (Static Data)'/><author><name>Shiman</name><uri>http://www.blogger.com/profile/08649899361340334665</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://1.bp.blogspot.com/_FqHBLEQajic/SSEM4Emt84I/AAAAAAAAAAM/SrVs2RTT-78/s72-c/staticdata.gif' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5199405525386955005.post-8017368807893219309</id><published>2008-11-17T11:22:00.000+06:00</published><updated>2008-11-17T11:54:12.879+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Technology'/><category scheme='http://www.blogger.com/atom/ns#' term='Object Oriented Programming'/><category scheme='http://www.blogger.com/atom/ns#' term='Programming'/><category scheme='http://www.blogger.com/atom/ns#' term='C#.Net'/><category scheme='http://www.blogger.com/atom/ns#' term='Software'/><category scheme='http://www.blogger.com/atom/ns#' term='Software Engineering'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='OOP'/><category scheme='http://www.blogger.com/atom/ns#' term='.Net'/><category scheme='http://www.blogger.com/atom/ns#' term='Computer Science'/><title type='text'>Uses of the Static Keyword in C# .NET</title><content type='html'>&lt;p&gt;In C#, classes, structures and their members may be defined using the static  keyword. While doing so, the static member must be invoked directly from the  class level, rather than from a type instance. To illustrate the distinction,  consider a good friend of ours, the most commonly used System.Console. As you  have seen, you do not have to invoke the WriteLine() method from the object  level:&lt;/p&gt;&lt;pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%"&gt;&lt;code&gt;// Error! WriteLine() is not an instance level method!   &lt;br /&gt;Console c = new Console();   &lt;br /&gt;c.WriteLine(&amp;quot;I can't be printed...&amp;quot;);  &lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;but instead simply prefix the type name to the static WriteLine() member:&lt;/p&gt;&lt;pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%"&gt;&lt;code&gt;// Correct! WriteLine() is a static method.   &lt;br /&gt;Console.WriteLine(&amp;quot;Thanks...&amp;quot;);   &lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;/p&gt;&lt;ul&gt; &lt;li&gt;&lt;strong&gt;Rule:&lt;/strong&gt; &lt;em&gt;Static members can operate only on static class  members.&lt;/em&gt; &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;If you attempt to make use of nonstatic class members (also called instance  data) within a static method, you receive a compiler error.The static keyword  can be used before &lt;strong&gt;Data, &lt;/strong&gt;&lt;strong&gt;Methods, Constructors&lt;/strong&gt;  and &lt;strong&gt;Classes&lt;/strong&gt;. Lets take a look at them.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;(To be continued...)&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5199405525386955005-8017368807893219309?l=darkcodingzone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://darkcodingzone.blogspot.com/feeds/8017368807893219309/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5199405525386955005&amp;postID=8017368807893219309' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5199405525386955005/posts/default/8017368807893219309'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5199405525386955005/posts/default/8017368807893219309'/><link rel='alternate' type='text/html' href='http://darkcodingzone.blogspot.com/2008/11/uses-of-static-keyword-in-c-net.html' title='Uses of the Static Keyword in C# .NET'/><author><name>Shiman</name><uri>http://www.blogger.com/profile/08649899361340334665</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5199405525386955005.post-1078313944371789629</id><published>2008-11-16T17:14:00.000+06:00</published><updated>2008-11-17T11:48:41.430+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Technology'/><category scheme='http://www.blogger.com/atom/ns#' term='Object Oriented Programming'/><category scheme='http://www.blogger.com/atom/ns#' term='Programming'/><category scheme='http://www.blogger.com/atom/ns#' term='C#.Net'/><category scheme='http://www.blogger.com/atom/ns#' term='Software'/><category scheme='http://www.blogger.com/atom/ns#' term='Software Engineering'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><category scheme='http://www.blogger.com/atom/ns#' term='OOP'/><category scheme='http://www.blogger.com/atom/ns#' term='.Net'/><category scheme='http://www.blogger.com/atom/ns#' term='Computer Science'/><title type='text'>The Basics of Object Lifetime in C#.NET</title><content type='html'>&lt;p&gt;While building your C# applications, you can feel free to assume that the managed heap will take care of itself without your direct intervention. In fact, memory management is one of those things which power the .NET platform. The golden rule of .NET memory management is quite simple:Rule: Allocate an object onto the managed heap using the new keyword and forget about it.Once “new-ed”, the garbage collector will destroy the object when it is no longer needed. The next question obviously rises, of course, is, “How does the garbage collector determine when an object is no longer needed?" The incomplete but short answer is that the garbage collector removes an object from theheap when it is unreachable by any part of your code base. Look at the following code, you have a method that allocates a local Car object:&lt;/p&gt;&lt;pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%"&gt;&lt;code&gt;public static void MakeACar()&lt;br /&gt;{&lt;br /&gt;    // If myCar is the only reference to the Car object,&lt;br /&gt;    // it may be destroyed when the method returns&lt;br /&gt;    Car myCar = new Car();&lt;br /&gt;    //...&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Notice that the Car reference (myCar) has been created directly within the MakeACar() method and has not been passed outside of the defining scope (via a return value or ref/out parameters). Thus, once this method call completes, the myCar reference is no longer reachable. And this makes the associated Car object a candidate for garbage collection. However, it is not guaranteed that this object will be reclaimed from memory immediately after MakeACar() has completed. All you can assume at this point is that when the CLR(Common Language Runtime) performs the next garbage collection, the myCar object could be(not must be) safely destroyed.As you will most certainly discover, programming in a garbage-collected environment will greatly simplify your application development. In stark contrast, C++ programmers are painfully aware that if they fail to manually delete heap-allocated objects, memory leaks are never far behind. In fact, tracking down memory leaks is one of the most time-consuming (and tedious) aspects of programming with unmanaged languages. By allowing the garbage collector to be in charge of destroying objects, the burden of memory management has been taken from your shoulders and placed onto those of the CLR.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5199405525386955005-1078313944371789629?l=darkcodingzone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://darkcodingzone.blogspot.com/feeds/1078313944371789629/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5199405525386955005&amp;postID=1078313944371789629' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5199405525386955005/posts/default/1078313944371789629'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5199405525386955005/posts/default/1078313944371789629'/><link rel='alternate' type='text/html' href='http://darkcodingzone.blogspot.com/2008/11/basics-of-object-lifetime-in-cnet.html' title='The Basics of Object Lifetime in C#.NET'/><author><name>Shiman</name><uri>http://www.blogger.com/profile/08649899361340334665</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5199405525386955005.post-3216945302325973949</id><published>2008-11-16T14:35:00.000+06:00</published><updated>2008-11-16T14:42:11.634+06:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Object Oriented Programming'/><category scheme='http://www.blogger.com/atom/ns#' term='Programming'/><category scheme='http://www.blogger.com/atom/ns#' term='OOP'/><category scheme='http://www.blogger.com/atom/ns#' term='Computer Science'/><title type='text'>Differences between an Abstract Class and an Interface</title><content type='html'>&lt;p&gt;&lt;strong&gt;An Abstract class without any implementation just looks like an  Interface, so what is the significance of using Interfaces instead of an  Abstract classes?&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;The concepts of Abstract classes and Interfaces are always confusing to the  beginners of OOP, though they have been through all the theories. Therefore, in  this post, I have tried to discuss these two things.&lt;/p&gt; &lt;p&gt;An &lt;strong&gt;Abstract class&lt;/strong&gt; is a special kind of class that cannot be  instantiated. It only allows other classes to inherit from it but cannot be  instantiated. When we create an abstract class, we are creating a base class  that might have one or more completed methods but some methods can be left  uncompleted.&lt;/p&gt; &lt;p&gt;An &lt;strong&gt;Interface&lt;/strong&gt; has no implementation; it only has the  signature or in other words, just the definition of the methods without the  body.&lt;/p&gt; &lt;p class="para"&gt;An interface is similar to an abstract class. Both types must be  inherited. You cannot create an instance of either. Abstract members require  implementation in the derived type. Interface members require implementation in  a derived type. Although abstract and interface members are similar, there are  several differences:&lt;/p&gt; &lt;ul class="itemizedlist"&gt; &lt;li class="first-listitem"&gt; &lt;p class="first-para"&gt;An abstract class can contain some implementation.  Interfaces have no implementation.&lt;/p&gt;&lt;/li&gt; &lt;li class="listitem"&gt; &lt;p class="first-para"&gt;Abstract classes can inherit other classes and interfaces.  Interfaces can only inherit other interfaces.&lt;/p&gt;&lt;/li&gt; &lt;li class="listitem"&gt; &lt;p class="first-para"&gt;Abstract classes can contain fields. Interfaces cannot have  state.&lt;/p&gt;&lt;/li&gt; &lt;li class="listitem"&gt; &lt;p class="first-para"&gt;Abstract classes have constructors and destructors.  Interfaces have neither.&lt;/p&gt;&lt;/li&gt; &lt;li class="listitem"&gt; &lt;p class="first-para"&gt;Interfaces can be inherited by structures. Abstract classes  are not inheritable by structures.&lt;/p&gt;&lt;/li&gt; &lt;li class="listitem"&gt; &lt;p class="first-para"&gt;Interfaces support multiple inheritance. Abstract classes  support single inheritance.&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;If all the methods of an abstract class are uncompleted then it is same as an  interface. And everyone knows that an abstract class can provide some defoult  behaviors to the sub-classes which an Interface cannot. But if we can have a  complete abstract class (which looks like an Interface), then why the heck do we  need Interfaces?&lt;/p&gt; &lt;p&gt;The answer is, most of the Object Oriented Languages do not support  &lt;strong&gt;multiple inheritance&lt;/strong&gt;. But, it is obvious that sometimes we need  a class to have behavior like more than one class, and the language is not  supporting that- we cannot extend more than one class. So, to provide this  feature of multiple inheritance, we need interfaces, because it is possible for  a class to implement multiple interfaces.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5199405525386955005-3216945302325973949?l=darkcodingzone.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://darkcodingzone.blogspot.com/feeds/3216945302325973949/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5199405525386955005&amp;postID=3216945302325973949' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5199405525386955005/posts/default/3216945302325973949'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5199405525386955005/posts/default/3216945302325973949'/><link rel='alternate' type='text/html' href='http://darkcodingzone.blogspot.com/2008/11/differences-between-abstract-class-and.html' title='Differences between an Abstract Class and an Interface'/><author><name>Shiman</name><uri>http://www.blogger.com/profile/08649899361340334665</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
