<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Programming in C#</title>
	<atom:link href="http://programmingincsharp.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://programmingincsharp.com</link>
	<description>A Guide To C# and ASP.NET Programming</description>
	<lastBuildDate>Thu, 17 May 2012 16:31:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.4</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Using CSS in ASP.NET</title>
		<link>http://programmingincsharp.com/using-css-in-asp-net/</link>
		<comments>http://programmingincsharp.com/using-css-in-asp-net/#comments</comments>
		<pubDate>Thu, 17 May 2012 16:31:01 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[div]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[smartphones]]></category>
		<category><![CDATA[Table]]></category>
		<category><![CDATA[tablets]]></category>
		<category><![CDATA[WebForms]]></category>

		<guid isPermaLink="false">http://programmingincsharp.com/?p=60</guid>
		<description><![CDATA[An often ignored part of ASP.NET development is building a good web based user interface. I&#8217;ve found this to be especially true when ASP.NET developers get called upon to construct public facing web sites. After all, historically most ASP.NET web apps are created for internal, not external, consumption and it&#8217;s expected, if not demanded, that [...]]]></description>
			<content:encoded><![CDATA[<p>An often ignored part of ASP.NET development is building a good web based user interface. I&#8217;ve found this to be especially true when ASP.NET developers get called upon to construct public facing web sites. After all, historically most ASP.NET web apps are created for internal, not external, consumption and it&#8217;s expected, if not demanded, that all visitors to use IE, the &#8220;corporate standard&#8221; browser.</p>
<p>But, today we&#8217;re seeing a shift where organizations have a need to present customized solutions to the outside world. This can be anything from a fully public website to a site restricted to vendors, dealers or the like. This also means that we have to be able to present designs that hold up well across a wide variety of browsers and look good doing it, even on internal facing web apps. This is especially true as internal users are increasing usage of Apple and Android smartphones and tablets.</p>
<h3>CSS Based vs. Table Based Page Layouts</h3>
<p>From what I&#8217;ve seen, most ASP.NET developers tend to get stuck around 1998 when it comes to laying out a web site. Typically, they&#8217;ll use tables, including nested tables, to layout screen elements. Many ASP.NET developers have told me that they have trouble with understanding DIVs and other CSS elements and therefore avoid them.</p>
<p>The most common argument you&#8217;ll see against using tables for layout purposes is that tables are intended for displaying tabular data, not laying out the web page. But, the reason to use a CSS based design rather than an old fashioned table layout goes deeper than that.</p>
<p>First of all, using CSS makes it easy to maintain visual and code consistency throughout a site and across other related sites and makes the site more visually appealing, usable and accessible to visitors. One place I often see this is with data entry screens where spacing of entry controls and matching labels can lead to a hodgepodge of layouts that are difficult to modify and maintain. One developer may use many levels of nested tables and in-line styling while another uses a combination of  &lt;br&gt;, &amp;nbsp; and transparent gifs to handle spacing. The positioning of each and every screen element is handled differently on each and every page, making the site difficult for developers as well as users.</p>
<p>Next, a good CSS design insures that redesigns and even simple changes can be done more efficiently and less expensively. When things like nested tables and transparent gifs are used to layout pages, it can create a situation where even simple modifications can become time consuming exercises. For example, changing a company logo design or position to meet the latest whims of the marketing department can mean hours, if not days, of tedious work. With a consistent CSS design, you can prevent this kind of headache.</p>
<p>Lastly, using a standard and modern CSS design helps present your site better across multiple browsers as well as improving performance. As more and more visitors show up with Apple iOS and Android based smartphone and tablet devices this aspect becomes more important. Table based sites, especially those that use nested tables, tend to perform poorly on mobile browsers.</p>
<p>So, should you never use tables for web page layouts? While it is something to avoid in general, you may encounter situations where a table layout is the best solution. This is sort of like finding situations where a &#8220;GoTo&#8221; statement is useful, although a little bit more common. If you must use a table layout, do make sure that you avoid nested tables and using HTML elements or gifs to handle spacing. Instead, use CSS code, such as margins and padding, to handle as much positioning as possible.</p>
<h3>Basic CSS Design Rules For ASP.NET Sites</h3>
<p>Now, let&#8217;s go over a few things that will help you design better ASP.NET web pages.</p>
<p>First of all, I highly recommend building a template site that uses your CSS framework and contains your master pages and empty or stripped down templates of the different types of web pages you&#8217;ll be displaying. This prototyping technique works well for both WebForms projects and MVC projects. Having this kind of guideline to follow will greatly help new people coming into your project and help keep all team members on the same &#8216;page&#8217; so to speak. Things can get really ugly on a site when everyone is doing their own thing rather than following a common user interface standard template.</p>
<p>I also suggest that you use a naming convention for CSS elements. This makes it easier for other people on your team to figure out what element to use in a particular situation. Along with this, I suggest using the &#8216;id&#8217; rather than &#8216;class&#8217; attribute for common elements like the page header and footer. This helps identify their unique status on the page and can prevent accidental usage of a style.</p>
<p>Next, avoid inline style attributes as much as possible. There can be special cases when this is necessary but, as much as possible, keep CSS in the CSS file(s). Another thing to avoid using are older HTML styling elements like &lt;font&gt;, &lt;b&gt; and &lt;i&gt;. Instead using spans and CSS styles. This insures visual consistency and maintainability.</p>
<p>Finally, don&#8217;t fall into the trap of replacing empty cells and spacer GIFs with spacing divs. (Yes, I&#8217;ve made this mistake myself.) Instead using margins and padding to position your screen elements. If you layout your page designs well it&#8217;s very unlikely you&#8217;ll need to have spacing crutches on your pages.</p>
<h3>Common CSS Grid Frameworks</h3>
<p>It may seem like quite a task to come up with a style sheet that handles a page layout easily and consistently. There&#8217;s a lot that goes into it ranging from properly resetting HTML elements to selecting common sizing and typography to cross-browser compatibility. Fortunately, a lot of this work has been done for you. All you need to do is download the CSS frameworks and plug in your own styling where applicable.</p>
<p>The two systems I&#8217;ve used are the <a href="http://960.gs">960 Grid System</a> and the <a href="http://www.blueprintcss.org">Blueprint CSS system</a>. Both are easy to understand and apply to a site. There are also several modifications to them around that you can download for use in custom situations like mobile device sites and floating layouts. Using one of these common CSS grid based systems is perhaps the easiest way to simplify using CSS in your ASP.NET site designs.</p>
<p>By doing a little planning up front and by using a common CSS grid framework you can get away from using antiquated table based layouts and improve the quality of your ASP.NET websites.</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-knowledge">
<ul class="socials">
		<li class="shr-twitter">
			<a href="http://www.shareaholic.com/api/share/?title=Using+CSS+in+ASP.NET&amp;link=http://programmingincsharp.com/using-css-in-asp-net/&amp;notes=An%20often%20ignored%20part%20of%20ASP.NET%20development%20is%20building%20a%20good%20web%20based%20user%20interface.%20I%27ve%20found%20this%20to%20be%20especially%20true%20when%20ASP.NET%20developers%20get%20called%20upon%20to%20construct%20public%20facing%20web%20sites.%20After%20all%2C%20historically%20most%20ASP.NET%20web%20apps%20are%20created%20for%20internal%2C%20not%20external%2C%20consumpt&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=%24%7Btitle%7D+-+%24%7Bshort_link%7D&amp;service=7&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.shareaholic.com/api/share/?title=Using+CSS+in+ASP.NET&amp;link=http://programmingincsharp.com/using-css-in-asp-net/&amp;notes=An%20often%20ignored%20part%20of%20ASP.NET%20development%20is%20building%20a%20good%20web%20based%20user%20interface.%20I%27ve%20found%20this%20to%20be%20especially%20true%20when%20ASP.NET%20developers%20get%20called%20upon%20to%20construct%20public%20facing%20web%20sites.%20After%20all%2C%20historically%20most%20ASP.NET%20web%20apps%20are%20created%20for%20internal%2C%20not%20external%2C%20consumpt&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=5&amp;tags=&amp;ctype=" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-dzone">
			<a href="http://www.shareaholic.com/api/share/?title=Using+CSS+in+ASP.NET&amp;link=http://programmingincsharp.com/using-css-in-asp-net/&amp;notes=An%20often%20ignored%20part%20of%20ASP.NET%20development%20is%20building%20a%20good%20web%20based%20user%20interface.%20I%27ve%20found%20this%20to%20be%20especially%20true%20when%20ASP.NET%20developers%20get%20called%20upon%20to%20construct%20public%20facing%20web%20sites.%20After%20all%2C%20historically%20most%20ASP.NET%20web%20apps%20are%20created%20for%20internal%2C%20not%20external%2C%20consumpt&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=102&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to DZone">Add this to DZone</a>
		</li>
		<li class="shr-slashdot">
			<a href="http://www.shareaholic.com/api/share/?title=Using+CSS+in+ASP.NET&amp;link=http://programmingincsharp.com/using-css-in-asp-net/&amp;notes=An%20often%20ignored%20part%20of%20ASP.NET%20development%20is%20building%20a%20good%20web%20based%20user%20interface.%20I%27ve%20found%20this%20to%20be%20especially%20true%20when%20ASP.NET%20developers%20get%20called%20upon%20to%20construct%20public%20facing%20web%20sites.%20After%20all%2C%20historically%20most%20ASP.NET%20web%20apps%20are%20created%20for%20internal%2C%20not%20external%2C%20consumpt&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=61&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Submit this to SlashDot">Submit this to SlashDot</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.shareaholic.com/api/share/?title=Using+CSS+in+ASP.NET&amp;link=http://programmingincsharp.com/using-css-in-asp-net/&amp;notes=An%20often%20ignored%20part%20of%20ASP.NET%20development%20is%20building%20a%20good%20web%20based%20user%20interface.%20I%27ve%20found%20this%20to%20be%20especially%20true%20when%20ASP.NET%20developers%20get%20called%20upon%20to%20construct%20public%20facing%20web%20sites.%20After%20all%2C%20historically%20most%20ASP.NET%20web%20apps%20are%20created%20for%20internal%2C%20not%20external%2C%20consumpt&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=38&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.shareaholic.com/api/share/?title=Using+CSS+in+ASP.NET&amp;link=http://programmingincsharp.com/using-css-in-asp-net/&amp;notes=An%20often%20ignored%20part%20of%20ASP.NET%20development%20is%20building%20a%20good%20web%20based%20user%20interface.%20I%27ve%20found%20this%20to%20be%20especially%20true%20when%20ASP.NET%20developers%20get%20called%20upon%20to%20construct%20public%20facing%20web%20sites.%20After%20all%2C%20historically%20most%20ASP.NET%20web%20apps%20are%20created%20for%20internal%2C%20not%20external%2C%20consumpt&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=88&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
</ul><div style="clear: both;"></div><div class="shr-getshr" style="visibility:hidden;font-size:10px !important"><a target="_blank" href="http://www.shareaholic.com/?src=pub">Get Shareaholic</a></div><div style="clear: both;"></div></div>

]]></content:encoded>
			<wfw:commentRss>http://programmingincsharp.com/using-css-in-asp-net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What is a Static Class in C#?</title>
		<link>http://programmingincsharp.com/what-is-a-static-class-in-c/</link>
		<comments>http://programmingincsharp.com/what-is-a-static-class-in-c/#comments</comments>
		<pubDate>Sat, 05 May 2012 15:13:29 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[C# Questions and Answers]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[constructor]]></category>
		<category><![CDATA[singleton]]></category>
		<category><![CDATA[static]]></category>

		<guid isPermaLink="false">http://programmingincsharp.com/?p=29</guid>
		<description><![CDATA[In this article we&#8217;ll be taking a look at static classes in C# and when to use them in your programs and libraries. First of all, the difference between a non-static and a static class in C# is simply that a static class cannot be instantiated. This means that you don&#8217;t use the new keyword [...]]]></description>
			<content:encoded><![CDATA[<p>In this article we&#8217;ll be taking a look at static classes in C# and when to use them in your programs and libraries.</p>
<p>First of all, the difference between a non-static and a static class in C# is simply that a static class cannot be instantiated. This means that you don&#8217;t use the new keyword to instantiate an instance of the class. Instead, you will access members of the static class by using the class name. Basically, a static class is intended to be a container for a set of methods that only work with supplied parameters and don&#8217;t have a need to store or retrieve data that is unique to them. An example of a static class that&#8217;s built into the .NET Framework is the System.Math class. It contains methods which perform various mathematical operations.</p>
<p>A static class is like a regular class that only has static members and has a private constructor. If you have a class like this in your design it&#8217;s best to make it a static class in order to use the features in the compiler which will insure that instances of the class cannot be created or inherited. By taking this step it will make your code more solid.</p>
<p>Sometimes there is confusion between a static class and a singleton class where only one instance is allowed. The way to separate them is to understand that the static class should not have any internal variables, each method should function independently, like a function library. However, a singleton class may expose internal values as properties and allow the storage and retrieval of these values. </p>
<p>As I mentioned above static classes cannot be instantiated. Instead, the Framework will call the private constructor for the static class prior to the first place the class is referenced in code. This allows you to blend in your function library seamlessly into your program so if you had a static class like this&#8230;</p>
<pre class="csharpcode">public static class MyFunctions
    {
        public static string Test1(string myValue)
        {
            // Code
        }

		public static int Test2(int myValue)
        {
            // Code
        }
    }
</pre>
<p>&#8230;you can call it like so&#8230;</p>
<pre class="csharpcode">MyFunctions.Test1(CurrentValue);</pre>
<p>&#8230;without having to create an instance of the object using the new keyword (which you can&#8217;t do with a static class anyway). Also, a static class will remain in memory for the lifetime of the application domain where it is created.</p>
<p>As you can see in the example above, a static class can only contain static members. The program won&#8217;t compile if you don&#8217;t remember to mark each of them as static. Also, static classes are sealed, which means that they cannot be inherited and cannot inherit from any class other than System.Object. While a static class cannot have an instance constructor, they can have a private, static, constructor. This routine will be called when the Framework creates the instance of the object. </p>
<p>One thing to bear in mind here is that if your static class requires a lot of extensive initialization it may not be a good candidate for a static class. In my view, these classes are intended for groups of related, but essentially standalone, functions. Having a substantial initialization routine may indicate that the class might work better as a singleton class or as a regular class. Taking this into consideration early on can prevent you from having to recode a static class into something else.</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-knowledge">
<ul class="socials">
		<li class="shr-twitter">
			<a href="http://www.shareaholic.com/api/share/?title=What+is+a+Static+Class+in+C%23%3F&amp;link=http://programmingincsharp.com/what-is-a-static-class-in-c/&amp;notes=In%20this%20article%20we%27ll%20be%20taking%20a%20look%20at%20static%20classes%20in%20C%23%20and%20when%20to%20use%20them%20in%20your%20programs%20and%20libraries.%0D%0A%0D%0AFirst%20of%20all%2C%20the%20difference%20between%20a%20non-static%20and%20a%20static%20class%20in%20C%23%20is%20simply%20that%20a%20static%20class%20cannot%20be%20instantiated.%20This%20means%20that%20you%20don%27t%20use%20the%20new%20keyword%20to%20ins&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=%24%7Btitle%7D+-+%24%7Bshort_link%7D&amp;service=7&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.shareaholic.com/api/share/?title=What+is+a+Static+Class+in+C%23%3F&amp;link=http://programmingincsharp.com/what-is-a-static-class-in-c/&amp;notes=In%20this%20article%20we%27ll%20be%20taking%20a%20look%20at%20static%20classes%20in%20C%23%20and%20when%20to%20use%20them%20in%20your%20programs%20and%20libraries.%0D%0A%0D%0AFirst%20of%20all%2C%20the%20difference%20between%20a%20non-static%20and%20a%20static%20class%20in%20C%23%20is%20simply%20that%20a%20static%20class%20cannot%20be%20instantiated.%20This%20means%20that%20you%20don%27t%20use%20the%20new%20keyword%20to%20ins&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=5&amp;tags=&amp;ctype=" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-dzone">
			<a href="http://www.shareaholic.com/api/share/?title=What+is+a+Static+Class+in+C%23%3F&amp;link=http://programmingincsharp.com/what-is-a-static-class-in-c/&amp;notes=In%20this%20article%20we%27ll%20be%20taking%20a%20look%20at%20static%20classes%20in%20C%23%20and%20when%20to%20use%20them%20in%20your%20programs%20and%20libraries.%0D%0A%0D%0AFirst%20of%20all%2C%20the%20difference%20between%20a%20non-static%20and%20a%20static%20class%20in%20C%23%20is%20simply%20that%20a%20static%20class%20cannot%20be%20instantiated.%20This%20means%20that%20you%20don%27t%20use%20the%20new%20keyword%20to%20ins&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=102&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to DZone">Add this to DZone</a>
		</li>
		<li class="shr-slashdot">
			<a href="http://www.shareaholic.com/api/share/?title=What+is+a+Static+Class+in+C%23%3F&amp;link=http://programmingincsharp.com/what-is-a-static-class-in-c/&amp;notes=In%20this%20article%20we%27ll%20be%20taking%20a%20look%20at%20static%20classes%20in%20C%23%20and%20when%20to%20use%20them%20in%20your%20programs%20and%20libraries.%0D%0A%0D%0AFirst%20of%20all%2C%20the%20difference%20between%20a%20non-static%20and%20a%20static%20class%20in%20C%23%20is%20simply%20that%20a%20static%20class%20cannot%20be%20instantiated.%20This%20means%20that%20you%20don%27t%20use%20the%20new%20keyword%20to%20ins&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=61&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Submit this to SlashDot">Submit this to SlashDot</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.shareaholic.com/api/share/?title=What+is+a+Static+Class+in+C%23%3F&amp;link=http://programmingincsharp.com/what-is-a-static-class-in-c/&amp;notes=In%20this%20article%20we%27ll%20be%20taking%20a%20look%20at%20static%20classes%20in%20C%23%20and%20when%20to%20use%20them%20in%20your%20programs%20and%20libraries.%0D%0A%0D%0AFirst%20of%20all%2C%20the%20difference%20between%20a%20non-static%20and%20a%20static%20class%20in%20C%23%20is%20simply%20that%20a%20static%20class%20cannot%20be%20instantiated.%20This%20means%20that%20you%20don%27t%20use%20the%20new%20keyword%20to%20ins&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=38&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.shareaholic.com/api/share/?title=What+is+a+Static+Class+in+C%23%3F&amp;link=http://programmingincsharp.com/what-is-a-static-class-in-c/&amp;notes=In%20this%20article%20we%27ll%20be%20taking%20a%20look%20at%20static%20classes%20in%20C%23%20and%20when%20to%20use%20them%20in%20your%20programs%20and%20libraries.%0D%0A%0D%0AFirst%20of%20all%2C%20the%20difference%20between%20a%20non-static%20and%20a%20static%20class%20in%20C%23%20is%20simply%20that%20a%20static%20class%20cannot%20be%20instantiated.%20This%20means%20that%20you%20don%27t%20use%20the%20new%20keyword%20to%20ins&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=88&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
</ul><div style="clear: both;"></div><div class="shr-getshr" style="visibility:hidden;font-size:10px !important"><a target="_blank" href="http://www.shareaholic.com/?src=pub">Get Shareaholic</a></div><div style="clear: both;"></div></div>

]]></content:encoded>
			<wfw:commentRss>http://programmingincsharp.com/what-is-a-static-class-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Random Extensions for C#</title>
		<link>http://programmingincsharp.com/random-extensions-for-c/</link>
		<comments>http://programmingincsharp.com/random-extensions-for-c/#comments</comments>
		<pubDate>Fri, 04 May 2012 13:40:28 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[C# Code Examples]]></category>
		<category><![CDATA[Extensions]]></category>
		<category><![CDATA[Generic]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[List]]></category>
		<category><![CDATA[Queue]]></category>
		<category><![CDATA[Random]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://programmingincsharp.com/?p=33</guid>
		<description><![CDATA[Today I thought I&#8217;d discuss some extensions I use to the .NET Random object. Previously, I had these functions is a separate wrapper class but I found that moving them to a static extension class would be a better fit across my projects. To start with, let&#8217;s take a look at what Extension methods are. [...]]]></description>
			<content:encoded><![CDATA[<p>Today I thought I&#8217;d discuss some extensions I use to the .NET Random object. Previously, I had these functions is a separate wrapper class but I found that moving them to a static extension class would be a better fit across my projects.</p>
<p>To start with, let&#8217;s take a look at what Extension methods are. Extension methods in C# allow you &#8220;roll your own&#8221; methods into existing types without having to create a new derived type. Extension methods are placed in a static class, but, by using a special syntax, they are called as if though they belonged to the original type. In your C# code calling an extension method looks the same as calling a method defined by the base type.</p>
<p>So, let&#8217;s take a look at how your would define a class that held Extension methods. Here&#8217;s how I&#8217;m starting out my Random Extensions class.</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">class</span> RandomExtensions
{</pre>
<p>You don&#8217;t have to define the whole class as static. You can define it normally if you want. I&#8217;ve just found it better myself to treat these classes just like I would <a href="http://programmingincsharp.com/what-is-a-static-class-in-c/">static function classes</a> I&#8217;ve done before. I also create a separate class for each method I&#8217;m extending instead of doing a &#8216;grab bag&#8217; of unrelated functions. This helps keep things better organized, something that&#8217;s quite important when using extension methods.</p>
<p>Now let&#8217;s look at some of my functions.</p>
<p>One thing I like to do is to reseed the randomization on each call. There is, of course, some overhead in doing this but I find the improved level of random results to be worth it. To do this, I create a function called Increment that pushes the random counter a variable distance using the Next method and the current time in milliseconds and seconds. Here&#8217;s the function&#8230;</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">void</span> Increment(<span class="kwrd">this</span> Random rand)
{
<span class="kwrd">    int</span> <span class="kwrd">value</span> = DateTime.Now.Millisecond + DateTime.Now.Second;
<span class="kwrd">    for</span> (<span class="kwrd">int</span> Counter = 0; Counter &lt;= <span class="kwrd">value</span>; Counter++)
    {
        rand.Next();
    }
}</pre>
<p>Notice how the extension method is defined with the &#8216;this&#8217; followed by the type name and then variable name. This is what tells the .Net framework that this is an extension of the Random type.</p>
<p>One of my long running gripes about the .NET Random type is with the range overload built into the Next method, specifically that the maximum value isn&#8217;t inclusive. Therefore, if you do RandomInstance.Next(1,6), like you would to simulate a dice, you would only get values back from 1 to 5. Instead, you would need to use (1,7) to get values from 1 to 6 back. It&#8217;s too late for Microsoft to change this behavior since it would  break too much existing code. But, if you want, you can use extension  methods to change it for you in your code while leaving the default behavior intact. I did my own function that included the given max value in the range. Here&#8217;s the function&#8230;</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">int</span> Range(<span class="kwrd">this</span> Random rand,<span class="kwrd">int</span> low, <span class="kwrd">int</span> high)
{
    rand.Increment();
<span class="kwrd">    return</span> rand.Next(low, high + 1);
}</pre>
<p>Here you&#8217;ll see I increment the random value prior to the value between retrieved via the Next method. This gives an extra touch of randomization.</p>
<p>Another useful function I&#8217;ve needed from time to time is a way to build a random sequence of integers I can use to access data randomly. For example, if I wanted to select 10 random records from a database table that had an integer primary key, this function would help me to grab a random selection of records. Another use, which I&#8217;ll show below, is to help in scrambling the contents of a list.</p>
<p>To create the random sequence, I&#8217;m using a Queue generic type. This allows me to automatically remove used number from the list and thus makes it easier to manage. Here&#8217;s the function&#8230;</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">static</span> Queue SequenceQueue(<span class="kwrd">this</span> Random rand, <span class="kwrd">int</span> max)
{
    Queue ReturnValue = <span class="kwrd">new</span> Queue();
<span class="kwrd">    while</span> (ReturnValue.Count &lt;= max)
    {
<span class="kwrd">        int</span> Result = rand.Range(0, max);
<span class="kwrd">        if</span> (!ReturnValue.Contains(Result))
        {
            ReturnValue.Enqueue(Result);
        }
    }
<span class="kwrd">    return</span> ReturnValue;
}</pre>
<p>As you can see, I use my Range method to load in values to the Queue.</p>
<p>I also have a related method called Sequence that returns a generic List instance rather than a Queue. This is useful when you want to use a random sequence in a LINQ query. Here&#8217;s an example of this technique working with a LINQ-to-SQL datasource&#8230;</p>
<pre class="csharpcode">Random Rnd = <span class="kwrd">new</span> Random();
TestDataContext TestData = <span class="kwrd">new</span> TestDataContext();
<span class="kwrd">int</span> MaxTest = (from items <span class="kwrd">in</span> TestData.TestItems select items.ID).Max();
List RandomIDs = Rnd.Sequence(1,MaxTest,10);
List ItemList = (from item <span class="kwrd">in</span>
                 TestData.TestItems.Where(item =&gt; RandomIDs.Contains(item.ID))
                 select item).ToList();</pre>
<p>My last Random extension method, Scramble, scrambles up the contents of a generic list. One place a routine like this could be used is in a card game, where the members of the original list were the card objects.</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">static</span> List Scramble(<span class="kwrd">this</span> Random rand,List inputList) <span class="kwrd">where</span> T : IComparable
{
    List ReturnList = <span class="kwrd">new</span> List();
    Queue RandomQueue = rand.SequenceQueue(inputList.Count-1);
<span class="kwrd">    while</span> (RandomQueue.Count != 0)
    {
        ReturnList.Add(inputList[RandomQueue.Dequeue()]);
    }
<span class="kwrd">    return</span> ReturnList;
}</pre>
<p>You can download my full <a title="Random Extensions Class Download" href="http://programmingincsharp.com/wp-content/uploads/2011/06/RandomExtensions.zip">RandomExtensions class</a> here. In addition to the methods shown above, it also contains some additional overloads that I&#8217;ve found useful.</p>
<p>That wraps up this example of an extension to the Random type. Let me know if you see any problems with it or if you have any questions or thoughts about it by leaving a comment below.</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-knowledge">
<ul class="socials">
		<li class="shr-twitter">
			<a href="http://www.shareaholic.com/api/share/?title=Random+Extensions+for+C%23&amp;link=http://programmingincsharp.com/random-extensions-for-c/&amp;notes=Today%20I%20thought%20I%27d%20discuss%20some%20extensions%20I%20use%20to%20the%20.NET%20Random%20object.%20Previously%2C%20I%20had%20these%20functions%20is%20a%20separate%20wrapper%20class%20but%20I%20found%20that%20moving%20them%20to%20a%20static%20extension%20class%20would%20be%20a%20better%20fit%20across%20my%20projects.%0D%0A%0D%0ATo%20start%20with%2C%20let%27s%20take%20a%20look%20at%20what%20Extension%20methods%20&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=%24%7Btitle%7D+-+%24%7Bshort_link%7D&amp;service=7&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.shareaholic.com/api/share/?title=Random+Extensions+for+C%23&amp;link=http://programmingincsharp.com/random-extensions-for-c/&amp;notes=Today%20I%20thought%20I%27d%20discuss%20some%20extensions%20I%20use%20to%20the%20.NET%20Random%20object.%20Previously%2C%20I%20had%20these%20functions%20is%20a%20separate%20wrapper%20class%20but%20I%20found%20that%20moving%20them%20to%20a%20static%20extension%20class%20would%20be%20a%20better%20fit%20across%20my%20projects.%0D%0A%0D%0ATo%20start%20with%2C%20let%27s%20take%20a%20look%20at%20what%20Extension%20methods%20&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=5&amp;tags=&amp;ctype=" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-dzone">
			<a href="http://www.shareaholic.com/api/share/?title=Random+Extensions+for+C%23&amp;link=http://programmingincsharp.com/random-extensions-for-c/&amp;notes=Today%20I%20thought%20I%27d%20discuss%20some%20extensions%20I%20use%20to%20the%20.NET%20Random%20object.%20Previously%2C%20I%20had%20these%20functions%20is%20a%20separate%20wrapper%20class%20but%20I%20found%20that%20moving%20them%20to%20a%20static%20extension%20class%20would%20be%20a%20better%20fit%20across%20my%20projects.%0D%0A%0D%0ATo%20start%20with%2C%20let%27s%20take%20a%20look%20at%20what%20Extension%20methods%20&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=102&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to DZone">Add this to DZone</a>
		</li>
		<li class="shr-slashdot">
			<a href="http://www.shareaholic.com/api/share/?title=Random+Extensions+for+C%23&amp;link=http://programmingincsharp.com/random-extensions-for-c/&amp;notes=Today%20I%20thought%20I%27d%20discuss%20some%20extensions%20I%20use%20to%20the%20.NET%20Random%20object.%20Previously%2C%20I%20had%20these%20functions%20is%20a%20separate%20wrapper%20class%20but%20I%20found%20that%20moving%20them%20to%20a%20static%20extension%20class%20would%20be%20a%20better%20fit%20across%20my%20projects.%0D%0A%0D%0ATo%20start%20with%2C%20let%27s%20take%20a%20look%20at%20what%20Extension%20methods%20&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=61&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Submit this to SlashDot">Submit this to SlashDot</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.shareaholic.com/api/share/?title=Random+Extensions+for+C%23&amp;link=http://programmingincsharp.com/random-extensions-for-c/&amp;notes=Today%20I%20thought%20I%27d%20discuss%20some%20extensions%20I%20use%20to%20the%20.NET%20Random%20object.%20Previously%2C%20I%20had%20these%20functions%20is%20a%20separate%20wrapper%20class%20but%20I%20found%20that%20moving%20them%20to%20a%20static%20extension%20class%20would%20be%20a%20better%20fit%20across%20my%20projects.%0D%0A%0D%0ATo%20start%20with%2C%20let%27s%20take%20a%20look%20at%20what%20Extension%20methods%20&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=38&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.shareaholic.com/api/share/?title=Random+Extensions+for+C%23&amp;link=http://programmingincsharp.com/random-extensions-for-c/&amp;notes=Today%20I%20thought%20I%27d%20discuss%20some%20extensions%20I%20use%20to%20the%20.NET%20Random%20object.%20Previously%2C%20I%20had%20these%20functions%20is%20a%20separate%20wrapper%20class%20but%20I%20found%20that%20moving%20them%20to%20a%20static%20extension%20class%20would%20be%20a%20better%20fit%20across%20my%20projects.%0D%0A%0D%0ATo%20start%20with%2C%20let%27s%20take%20a%20look%20at%20what%20Extension%20methods%20&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=88&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
</ul><div style="clear: both;"></div><div class="shr-getshr" style="visibility:hidden;font-size:10px !important"><a target="_blank" href="http://www.shareaholic.com/?src=pub">Get Shareaholic</a></div><div style="clear: both;"></div></div>

]]></content:encoded>
			<wfw:commentRss>http://programmingincsharp.com/random-extensions-for-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Is a C# String a Value Type or a Reference Type?</title>
		<link>http://programmingincsharp.com/is-a-c-string-a-value-type-or-a-reference-type/</link>
		<comments>http://programmingincsharp.com/is-a-c-string-a-value-type-or-a-reference-type/#comments</comments>
		<pubDate>Sat, 28 Apr 2012 06:31:17 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[C# Questions and Answers]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[Reference Type]]></category>
		<category><![CDATA[String]]></category>
		<category><![CDATA[struct]]></category>
		<category><![CDATA[Value Type]]></category>

		<guid isPermaLink="false">http://programmingincsharp.com/?p=14</guid>
		<description><![CDATA[First, let&#8217;s establish the difference between a value type and a reference type in C#. There are two types in C#, class and struct, and the .NET Framework deals with them in different ways. Structs are the value type. In addition to defined structs this also includes primitive value types like bool, int, and char. [...]]]></description>
			<content:encoded><![CDATA[<p>First, let&#8217;s establish the difference between a value type and a reference type in C#.</p>
<p>There are two types in C#, class and struct, and the .NET Framework deals with them in different ways. </p>
<p>Structs are the value type. In addition to defined structs this also includes primitive value types like bool, int, and char.  When a Struct, or value type, instance is created a single memory space is allocated to store the value. When the framework is dealing with a value type it deals directly with the memory location. This is quite efficient for simple data such as numbers and single characters.</p>
<p>Classes are the reference type. Unlike structs, memory for classes is accessed indirectly through pointers. Each instance points to a different section of memory. This allows a greater degree of flexibility while incurring a performance cost. Fortunately in C#, unlike C++, we don&#8217;t have to worry about managing pointers to our data, the runtime handles it for us.</p>
<p>When we consider strings in C# it can get confusing since, although they&#8217;re a reference type, they behave somewhat like a value type too. For example, we can use the equal sign to set the value of a string from another string just like we do for integers. This is different than how we set the value of a new object we&#8217;ve defined in our code or, for that matter, other classes in the Framework. However, behind the scenes, the .NET Framework is allocating memory for separate instances for each string for us. </p>
<p>This &#8216;shorthand&#8217; can cause you to think that a string is a value type and not a reference type. That&#8217;s why we have to be careful in order to avoid performance sapping operations like looping string concatenations. Each iteration of such a loop causes new instances to be created in a different memory area. As the string builds, considerable resources are used to construct the string over and over again. That&#8217;s why using StringBuilder is preferred in this situation since it doesn&#8217;t allocate a new string for each concatenation operation.</p>
<p>I hope this answers the question, &#8220;Is a C# String a Value Type or a Reference Type?&#8221; for you. </p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-knowledge">
<ul class="socials">
		<li class="shr-twitter">
			<a href="http://www.shareaholic.com/api/share/?title=Is+a+C%23+String+a+Value+Type+or+a+Reference+Type%3F&amp;link=http://programmingincsharp.com/is-a-c-string-a-value-type-or-a-reference-type/&amp;notes=First%2C%20let%27s%20establish%20the%20difference%20between%20a%20value%20type%20and%20a%20reference%20type%20in%20C%23.%0D%0A%0D%0AThere%20are%20two%20types%20in%20C%23%2C%20class%20and%20struct%2C%20and%20the%20.NET%20Framework%20deals%20with%20them%20in%20different%20ways.%20%0D%0A%0D%0AStructs%20are%20the%20value%20type.%20In%20addition%20to%20defined%20structs%20this%20also%20includes%20primitive%20value%20types%20lik&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=%24%7Btitle%7D+-+%24%7Bshort_link%7D&amp;service=7&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.shareaholic.com/api/share/?title=Is+a+C%23+String+a+Value+Type+or+a+Reference+Type%3F&amp;link=http://programmingincsharp.com/is-a-c-string-a-value-type-or-a-reference-type/&amp;notes=First%2C%20let%27s%20establish%20the%20difference%20between%20a%20value%20type%20and%20a%20reference%20type%20in%20C%23.%0D%0A%0D%0AThere%20are%20two%20types%20in%20C%23%2C%20class%20and%20struct%2C%20and%20the%20.NET%20Framework%20deals%20with%20them%20in%20different%20ways.%20%0D%0A%0D%0AStructs%20are%20the%20value%20type.%20In%20addition%20to%20defined%20structs%20this%20also%20includes%20primitive%20value%20types%20lik&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=5&amp;tags=&amp;ctype=" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-dzone">
			<a href="http://www.shareaholic.com/api/share/?title=Is+a+C%23+String+a+Value+Type+or+a+Reference+Type%3F&amp;link=http://programmingincsharp.com/is-a-c-string-a-value-type-or-a-reference-type/&amp;notes=First%2C%20let%27s%20establish%20the%20difference%20between%20a%20value%20type%20and%20a%20reference%20type%20in%20C%23.%0D%0A%0D%0AThere%20are%20two%20types%20in%20C%23%2C%20class%20and%20struct%2C%20and%20the%20.NET%20Framework%20deals%20with%20them%20in%20different%20ways.%20%0D%0A%0D%0AStructs%20are%20the%20value%20type.%20In%20addition%20to%20defined%20structs%20this%20also%20includes%20primitive%20value%20types%20lik&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=102&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to DZone">Add this to DZone</a>
		</li>
		<li class="shr-slashdot">
			<a href="http://www.shareaholic.com/api/share/?title=Is+a+C%23+String+a+Value+Type+or+a+Reference+Type%3F&amp;link=http://programmingincsharp.com/is-a-c-string-a-value-type-or-a-reference-type/&amp;notes=First%2C%20let%27s%20establish%20the%20difference%20between%20a%20value%20type%20and%20a%20reference%20type%20in%20C%23.%0D%0A%0D%0AThere%20are%20two%20types%20in%20C%23%2C%20class%20and%20struct%2C%20and%20the%20.NET%20Framework%20deals%20with%20them%20in%20different%20ways.%20%0D%0A%0D%0AStructs%20are%20the%20value%20type.%20In%20addition%20to%20defined%20structs%20this%20also%20includes%20primitive%20value%20types%20lik&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=61&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Submit this to SlashDot">Submit this to SlashDot</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.shareaholic.com/api/share/?title=Is+a+C%23+String+a+Value+Type+or+a+Reference+Type%3F&amp;link=http://programmingincsharp.com/is-a-c-string-a-value-type-or-a-reference-type/&amp;notes=First%2C%20let%27s%20establish%20the%20difference%20between%20a%20value%20type%20and%20a%20reference%20type%20in%20C%23.%0D%0A%0D%0AThere%20are%20two%20types%20in%20C%23%2C%20class%20and%20struct%2C%20and%20the%20.NET%20Framework%20deals%20with%20them%20in%20different%20ways.%20%0D%0A%0D%0AStructs%20are%20the%20value%20type.%20In%20addition%20to%20defined%20structs%20this%20also%20includes%20primitive%20value%20types%20lik&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=38&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.shareaholic.com/api/share/?title=Is+a+C%23+String+a+Value+Type+or+a+Reference+Type%3F&amp;link=http://programmingincsharp.com/is-a-c-string-a-value-type-or-a-reference-type/&amp;notes=First%2C%20let%27s%20establish%20the%20difference%20between%20a%20value%20type%20and%20a%20reference%20type%20in%20C%23.%0D%0A%0D%0AThere%20are%20two%20types%20in%20C%23%2C%20class%20and%20struct%2C%20and%20the%20.NET%20Framework%20deals%20with%20them%20in%20different%20ways.%20%0D%0A%0D%0AStructs%20are%20the%20value%20type.%20In%20addition%20to%20defined%20structs%20this%20also%20includes%20primitive%20value%20types%20lik&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=88&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
</ul><div style="clear: both;"></div><div class="shr-getshr" style="visibility:hidden;font-size:10px !important"><a target="_blank" href="http://www.shareaholic.com/?src=pub">Get Shareaholic</a></div><div style="clear: both;"></div></div>

]]></content:encoded>
			<wfw:commentRss>http://programmingincsharp.com/is-a-c-string-a-value-type-or-a-reference-type/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Casting Variables in C#</title>
		<link>http://programmingincsharp.com/casting-variables-in-c/</link>
		<comments>http://programmingincsharp.com/casting-variables-in-c/#comments</comments>
		<pubDate>Fri, 27 Apr 2012 05:19:21 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[C# Programming Concepts]]></category>
		<category><![CDATA[as]]></category>
		<category><![CDATA[casting]]></category>
		<category><![CDATA[conversion]]></category>
		<category><![CDATA[exception]]></category>
		<category><![CDATA[nullable]]></category>

		<guid isPermaLink="false">http://programmingincsharp.com/?p=21</guid>
		<description><![CDATA[C# is a strongly typed language. This means that when a variable is defined we have to specify what type of data the variable will hold. Languages which aren&#8217;t strongly typed attempt to do the type conversion within their compiler or interpreter themselves. While this can make it easier on the casual programmer, it does [...]]]></description>
			<content:encoded><![CDATA[<p>C# is a strongly typed language. This means that when a variable is defined we have to specify what type of data the variable will hold. Languages which aren&#8217;t strongly typed attempt to do the type conversion within their compiler or interpreter themselves. While this can make it easier on the casual programmer, it does allow subtle bugs to enter the program if considerable care isn&#8217;t exercised. By having a strongly typed language, variable type bugs are eliminated. However, there can be times where you want to move a value from one type of variable to another. This is where casting comes in. </p>
<p>In C#, there are several different kinds of casting you can use depending upon the situation.</p>
<p>The first type of casting we&#8217;ll look at is the standard C++ and Java syntax style casting. C#, being akin to these languages, uses this same syntax.</p>
<pre class="csharpcode">string MyString = (string)MyObject;</pre>
<p>This is sometimes called explicit casting. The casting operation is applied to the full equation to the right. So, in this example, Var1 would not be cast by itself but the entire equation, Var1+Var2 would be.</p>
<pre class="csharpcode">string MyString = (string)Var1+Var2;</pre>
<p>There may also be occasions when you do only want to cast part of an equation chain. In that circumstance, you can use parentheses to indicate the part you wish to cast, as shown here:</p>
<pre class="csharpcode">string MyString = ((Form)MyObject).Text;</pre>
<p>In this example, we&#8217;ll be converting the variable MyObject to a Form and then extracting the Text property from the newly cast Form object.</p>
<p>It is important to note that if a particular value cannot be cast as a particular variable type either the program won&#8217;t compile or a runtime exception will occur. Which happens depends on if the compiler can detect the problem. </p>
<p>The second type of C# casting is the &#8216;as&#8217; casting operator. It works just like the explicit casting except that, in the case of a conversion failure, it will cause the value to be set to null rather than throwing an exception. However, it is important to remember that you can only use this kind of casting for reference and nullable variable types and not for non-nullable types like integers. Here&#8217;s an example of using the as casting operator.</p>
<pre class="csharpcode">Object MyObject = new TextBox();
Button MyButton = MyObject as Button;</pre>
<p>I hope this has helped go over the basics of casting variables in C# for you.</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-knowledge">
<ul class="socials">
		<li class="shr-twitter">
			<a href="http://www.shareaholic.com/api/share/?title=Casting+Variables+in+C%23&amp;link=http://programmingincsharp.com/casting-variables-in-c/&amp;notes=C%23%20is%20a%20strongly%20typed%20language.%20This%20means%20that%20when%20a%20variable%20is%20defined%20we%20have%20to%20specify%20what%20type%20of%20data%20the%20variable%20will%20hold.%20Languages%20which%20aren%27t%20strongly%20typed%20attempt%20to%20do%20the%20type%20conversion%20within%20their%20compiler%20or%20interpreter%20themselves.%20While%20this%20can%20make%20it%20easier%20on%20the%20casua&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=%24%7Btitle%7D+-+%24%7Bshort_link%7D&amp;service=7&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.shareaholic.com/api/share/?title=Casting+Variables+in+C%23&amp;link=http://programmingincsharp.com/casting-variables-in-c/&amp;notes=C%23%20is%20a%20strongly%20typed%20language.%20This%20means%20that%20when%20a%20variable%20is%20defined%20we%20have%20to%20specify%20what%20type%20of%20data%20the%20variable%20will%20hold.%20Languages%20which%20aren%27t%20strongly%20typed%20attempt%20to%20do%20the%20type%20conversion%20within%20their%20compiler%20or%20interpreter%20themselves.%20While%20this%20can%20make%20it%20easier%20on%20the%20casua&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=5&amp;tags=&amp;ctype=" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-dzone">
			<a href="http://www.shareaholic.com/api/share/?title=Casting+Variables+in+C%23&amp;link=http://programmingincsharp.com/casting-variables-in-c/&amp;notes=C%23%20is%20a%20strongly%20typed%20language.%20This%20means%20that%20when%20a%20variable%20is%20defined%20we%20have%20to%20specify%20what%20type%20of%20data%20the%20variable%20will%20hold.%20Languages%20which%20aren%27t%20strongly%20typed%20attempt%20to%20do%20the%20type%20conversion%20within%20their%20compiler%20or%20interpreter%20themselves.%20While%20this%20can%20make%20it%20easier%20on%20the%20casua&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=102&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to DZone">Add this to DZone</a>
		</li>
		<li class="shr-slashdot">
			<a href="http://www.shareaholic.com/api/share/?title=Casting+Variables+in+C%23&amp;link=http://programmingincsharp.com/casting-variables-in-c/&amp;notes=C%23%20is%20a%20strongly%20typed%20language.%20This%20means%20that%20when%20a%20variable%20is%20defined%20we%20have%20to%20specify%20what%20type%20of%20data%20the%20variable%20will%20hold.%20Languages%20which%20aren%27t%20strongly%20typed%20attempt%20to%20do%20the%20type%20conversion%20within%20their%20compiler%20or%20interpreter%20themselves.%20While%20this%20can%20make%20it%20easier%20on%20the%20casua&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=61&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Submit this to SlashDot">Submit this to SlashDot</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.shareaholic.com/api/share/?title=Casting+Variables+in+C%23&amp;link=http://programmingincsharp.com/casting-variables-in-c/&amp;notes=C%23%20is%20a%20strongly%20typed%20language.%20This%20means%20that%20when%20a%20variable%20is%20defined%20we%20have%20to%20specify%20what%20type%20of%20data%20the%20variable%20will%20hold.%20Languages%20which%20aren%27t%20strongly%20typed%20attempt%20to%20do%20the%20type%20conversion%20within%20their%20compiler%20or%20interpreter%20themselves.%20While%20this%20can%20make%20it%20easier%20on%20the%20casua&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=38&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.shareaholic.com/api/share/?title=Casting+Variables+in+C%23&amp;link=http://programmingincsharp.com/casting-variables-in-c/&amp;notes=C%23%20is%20a%20strongly%20typed%20language.%20This%20means%20that%20when%20a%20variable%20is%20defined%20we%20have%20to%20specify%20what%20type%20of%20data%20the%20variable%20will%20hold.%20Languages%20which%20aren%27t%20strongly%20typed%20attempt%20to%20do%20the%20type%20conversion%20within%20their%20compiler%20or%20interpreter%20themselves.%20While%20this%20can%20make%20it%20easier%20on%20the%20casua&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=88&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
</ul><div style="clear: both;"></div><div class="shr-getshr" style="visibility:hidden;font-size:10px !important"><a target="_blank" href="http://www.shareaholic.com/?src=pub">Get Shareaholic</a></div><div style="clear: both;"></div></div>

]]></content:encoded>
			<wfw:commentRss>http://programmingincsharp.com/casting-variables-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What is the Difference Between an Abstract Class and an Interface in C#?</title>
		<link>http://programmingincsharp.com/what-is-the-difference-between-an-abstract-class-and-an-interface-in-c/</link>
		<comments>http://programmingincsharp.com/what-is-the-difference-between-an-abstract-class-and-an-interface-in-c/#comments</comments>
		<pubDate>Thu, 26 Apr 2012 17:06:35 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[C# Questions and Answers]]></category>
		<category><![CDATA[abstract class]]></category>
		<category><![CDATA[events]]></category>
		<category><![CDATA[Indexers]]></category>
		<category><![CDATA[inheritance]]></category>
		<category><![CDATA[Interfaces]]></category>
		<category><![CDATA[methods]]></category>
		<category><![CDATA[Polymorphism]]></category>

		<guid isPermaLink="false">http://programmingincsharp.com/?p=57</guid>
		<description><![CDATA[When interviewing for C# jobs often you’ll be asked what is the difference between an abstract class and&#160; interface in C#? In this article, I’ll take a look at these two techniques, their differences and when you should use which one them. Interfaces in C# An interface in C# is simply a template for a [...]]]></description>
			<content:encoded><![CDATA[<p>When interviewing for C# jobs often you’ll be asked what is the difference between an abstract class and&nbsp; interface in C#? In this article, I’ll take a look at these two techniques, their differences and when you should use which one them.</p>
<h4>Interfaces in C#</h4>
<p>An interface in C# is simply a template for a class. It’s part of <a href="http://programmingincsharp.com/polymorphism-in-c/">how C# implements polymorphism</a>.&nbsp; An interface doesn’t contain any code, only definitions of methods, properties, events and indexers. A class that implements an interface will need to define the code used for these items. Here’s a short code snippet of an example C# interface…</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">interface</span> IMyItem
{
    <span class="kwrd">int</span> Id { get; set; }

    <span class="kwrd">string</span> Description { get; set; }

    <span class="kwrd">string</span> RunTest(<span class="kwrd">int</span> testNumber);

    <span class="kwrd">event</span> EventHandler TestStatus;

    <span class="kwrd">string</span> <span class="kwrd">this</span>[<span class="kwrd">int</span> index] { get; set;}
}</pre>
<p>In this example, the interface includes two properties, a method, an event and an indexer. A class that used this interface would need to provide code for these items. While the code doesn’t have to do anything, it does have to be implemented by the class using the interface.</p>
<p>One powerful thing that you can do with interfaces is that you can have a class that implements more than one interface. One of my favorite usages is to not only implement a program specific interface but to also implement a generic List or Queue. </p>
<h4>Abstract Classes in C#</h4>
<p> An abstract class in C# is a class that can’t be instantiated and, like an interface, is intended to provide a common class definition that is shared by derived classes. Like an interface, the routines in an abstract class may be methods, properties, indexers and events. Unlike an interface, an abstract class may contain code although it may also have abstract methods that do not have code. Also, an abstract class may define constructors and destructors while an interface does not as well as internal private and protected variables.</p>
<p>Here’s a simple example of a C# abstract class…</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">abstract</span> <span class="kwrd">class</span> TestItem
{
    <span class="kwrd">private</span> <span class="kwrd">int</span> _testId;

    <span class="kwrd">public</span> TestItem(<span class="kwrd">int</span> testIdValue)
    {
        _testId = testIdValue;
    }

    <span class="kwrd">public</span> <span class="kwrd">int</span> TestId
    {
        get
        {
            <span class="kwrd">return</span> _testId;
        }
        set
        {
            _testId = <span class="kwrd">value</span>;
        }
    }

    <span class="kwrd">public</span> <span class="kwrd">abstract</span> <span class="kwrd">string</span> Description { get; set; }

    <span class="kwrd">public</span> <span class="kwrd">abstract</span> <span class="kwrd">void</span> RunTest(<span class="kwrd">int</span> i);

    <span class="kwrd">public</span> <span class="kwrd">abstract</span> <span class="kwrd">string</span> <span class="kwrd">this</span>[<span class="kwrd">int</span> index] { get; set; }
}</pre>
<p>As you can see above, there is code in the constructor and one of the properties while the rest of the routines are marked with the abstract keyword to indicate that their implementation is left up to the derived subclass. The routines marked as abstract have to be implemented in code by the derived class, they aren’t optional. However, the routines that are implemented in code by the abstract class do not have to be implemented in code by the subclass. However, routines that have code in the abstract class can be overridden if they’re marked with the virtual keyword.</p>
<p>Here’s an example of a class that is derived from the abstract class above…</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> Tester : TestItem
{
    <span class="kwrd">public</span> Tester(<span class="kwrd">int</span> testIdValue) : <span class="kwrd">base</span>(testIdValue) { }

    <span class="kwrd">public</span> <span class="kwrd">override</span> <span class="kwrd">string</span> Description { get; set; }

    <span class="kwrd">public</span> <span class="kwrd">override</span> <span class="kwrd">void</span> RunTest(<span class="kwrd">int</span> i)
    {
        <span class="rem">//call test routines</span>
    }

    <span class="kwrd">public</span> <span class="kwrd">override</span> <span class="kwrd">string</span> <span class="kwrd">this</span>[<span class="kwrd">int</span> index]
    {
        get
        {
            <span class="rem">// process and return some data</span>
        }
        set
        {
            <span class="rem">// process and assign some data</span>
        }
    }
}</pre>
<p>Note that we need to use the override keyword to indicate to the Framework that a particular routine is overriding the base abstract class’ routine definition. Also see that the class doesn’t have an implementation of TestId property, it uses the code in the abstract class instead. One tricky part is in our constructor. We have to define a constructor or else the compiler will present this error: “[classname] does not contain a constructor that takes 0 arguments”. To avoid this error, we have to define a constructor and pass the value on to the base class code as seen in the example code above.</p>
<h4>Abstract Class vs. Interface in C#</h4>
<p>But, when should you use an abstract class instead of an interface and vice versa? Either technique provides a way to have a common definition for a class although interfaces are a form of polymorphism and abstract classes are a form of inheritance, from an OOP perspective. Let’s see what this means in practical terms.</p>
<p>Let’s assume that we’re working on an inventory and invoicing system that has several different user types, such as vendor, dealer, distributor, employee and so forth. As we look at the system, it’s obvious we’ll be handling users will be the same for all users so it makes sense to have a user abstract class that we can derive our classes from for our various types. In this context, an interface probably wouldn’t make sense because there would probably be similar implementation needs across the spectrum of users. So, we might have an abstract base class called User that would have subclasses for each type of user.</p>
<p>Now, let’s assume that in our system we wanted to standardize how programmers working on the project defined common functions such as duplicating records. On a large project, it can get confusing with everybody defining things their own way, so, by using a common interface standard a lot of this confusion can be eliminated. For example, an IDuplicate interface could be used for users, invoices, inventory items and so forth in the project. The actual code might be considerably different but the interface is the same across the board in the project. Also, remember that C# is single inheritance, you can include more than one interface. Therefore, a class could implement an IDuplicate interface as well as other interfaces you might define or ones that you might borrow from the .NET Framework, such as IList. </p>
<p>I hope this has helped you get the differences between abstract classes and interfaces in C# clearer in your mind.</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-knowledge">
<ul class="socials">
		<li class="shr-twitter">
			<a href="http://www.shareaholic.com/api/share/?title=What+is+the+Difference+Between+an+Abstract+Class+and+an+Interface+in+C%23%3F&amp;link=http://programmingincsharp.com/what-is-the-difference-between-an-abstract-class-and-an-interface-in-c/&amp;notes=When%20interviewing%20for%20C%23%20jobs%20often%20you%E2%80%99ll%20be%20asked%20what%20is%20the%20difference%20between%20an%20abstract%20class%20and%26nbsp%3B%20interface%20in%20C%23%3F%20In%20this%20article%2C%20I%E2%80%99ll%20take%20a%20look%20at%20these%20two%20techniques%2C%20their%20differences%20and%20when%20you%20should%20use%20which%20one%20them.%0D%0AInterfaces%20in%20C%23%0D%0AAn%20interface%20in%20C%23%20is%20simply%20a%20t&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=%24%7Btitle%7D+-+%24%7Bshort_link%7D&amp;service=7&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.shareaholic.com/api/share/?title=What+is+the+Difference+Between+an+Abstract+Class+and+an+Interface+in+C%23%3F&amp;link=http://programmingincsharp.com/what-is-the-difference-between-an-abstract-class-and-an-interface-in-c/&amp;notes=When%20interviewing%20for%20C%23%20jobs%20often%20you%E2%80%99ll%20be%20asked%20what%20is%20the%20difference%20between%20an%20abstract%20class%20and%26nbsp%3B%20interface%20in%20C%23%3F%20In%20this%20article%2C%20I%E2%80%99ll%20take%20a%20look%20at%20these%20two%20techniques%2C%20their%20differences%20and%20when%20you%20should%20use%20which%20one%20them.%0D%0AInterfaces%20in%20C%23%0D%0AAn%20interface%20in%20C%23%20is%20simply%20a%20t&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=5&amp;tags=&amp;ctype=" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-dzone">
			<a href="http://www.shareaholic.com/api/share/?title=What+is+the+Difference+Between+an+Abstract+Class+and+an+Interface+in+C%23%3F&amp;link=http://programmingincsharp.com/what-is-the-difference-between-an-abstract-class-and-an-interface-in-c/&amp;notes=When%20interviewing%20for%20C%23%20jobs%20often%20you%E2%80%99ll%20be%20asked%20what%20is%20the%20difference%20between%20an%20abstract%20class%20and%26nbsp%3B%20interface%20in%20C%23%3F%20In%20this%20article%2C%20I%E2%80%99ll%20take%20a%20look%20at%20these%20two%20techniques%2C%20their%20differences%20and%20when%20you%20should%20use%20which%20one%20them.%0D%0AInterfaces%20in%20C%23%0D%0AAn%20interface%20in%20C%23%20is%20simply%20a%20t&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=102&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to DZone">Add this to DZone</a>
		</li>
		<li class="shr-slashdot">
			<a href="http://www.shareaholic.com/api/share/?title=What+is+the+Difference+Between+an+Abstract+Class+and+an+Interface+in+C%23%3F&amp;link=http://programmingincsharp.com/what-is-the-difference-between-an-abstract-class-and-an-interface-in-c/&amp;notes=When%20interviewing%20for%20C%23%20jobs%20often%20you%E2%80%99ll%20be%20asked%20what%20is%20the%20difference%20between%20an%20abstract%20class%20and%26nbsp%3B%20interface%20in%20C%23%3F%20In%20this%20article%2C%20I%E2%80%99ll%20take%20a%20look%20at%20these%20two%20techniques%2C%20their%20differences%20and%20when%20you%20should%20use%20which%20one%20them.%0D%0AInterfaces%20in%20C%23%0D%0AAn%20interface%20in%20C%23%20is%20simply%20a%20t&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=61&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Submit this to SlashDot">Submit this to SlashDot</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.shareaholic.com/api/share/?title=What+is+the+Difference+Between+an+Abstract+Class+and+an+Interface+in+C%23%3F&amp;link=http://programmingincsharp.com/what-is-the-difference-between-an-abstract-class-and-an-interface-in-c/&amp;notes=When%20interviewing%20for%20C%23%20jobs%20often%20you%E2%80%99ll%20be%20asked%20what%20is%20the%20difference%20between%20an%20abstract%20class%20and%26nbsp%3B%20interface%20in%20C%23%3F%20In%20this%20article%2C%20I%E2%80%99ll%20take%20a%20look%20at%20these%20two%20techniques%2C%20their%20differences%20and%20when%20you%20should%20use%20which%20one%20them.%0D%0AInterfaces%20in%20C%23%0D%0AAn%20interface%20in%20C%23%20is%20simply%20a%20t&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=38&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.shareaholic.com/api/share/?title=What+is+the+Difference+Between+an+Abstract+Class+and+an+Interface+in+C%23%3F&amp;link=http://programmingincsharp.com/what-is-the-difference-between-an-abstract-class-and-an-interface-in-c/&amp;notes=When%20interviewing%20for%20C%23%20jobs%20often%20you%E2%80%99ll%20be%20asked%20what%20is%20the%20difference%20between%20an%20abstract%20class%20and%26nbsp%3B%20interface%20in%20C%23%3F%20In%20this%20article%2C%20I%E2%80%99ll%20take%20a%20look%20at%20these%20two%20techniques%2C%20their%20differences%20and%20when%20you%20should%20use%20which%20one%20them.%0D%0AInterfaces%20in%20C%23%0D%0AAn%20interface%20in%20C%23%20is%20simply%20a%20t&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=88&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
</ul><div style="clear: both;"></div><div class="shr-getshr" style="visibility:hidden;font-size:10px !important"><a target="_blank" href="http://www.shareaholic.com/?src=pub">Get Shareaholic</a></div><div style="clear: both;"></div></div>

]]></content:encoded>
			<wfw:commentRss>http://programmingincsharp.com/what-is-the-difference-between-an-abstract-class-and-an-interface-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Variable Conversion in C#</title>
		<link>http://programmingincsharp.com/variable-conversion-in-c/</link>
		<comments>http://programmingincsharp.com/variable-conversion-in-c/#comments</comments>
		<pubDate>Wed, 25 Apr 2012 16:08:31 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[C# Programming Concepts]]></category>
		<category><![CDATA[boxing]]></category>
		<category><![CDATA[conversion]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[reference]]></category>
		<category><![CDATA[unboxing]]></category>

		<guid isPermaLink="false">http://programmingincsharp.com/?p=19</guid>
		<description><![CDATA[In this article we&#8217;ll be looking at two of the variable conversion methods in C#. Reference Conversion A reference conversion is simply converting a variable from one reference type to another. Reference conversions can be either implicit or explicit. This conversion never changes the referential identity of the object which is being converted. This means [...]]]></description>
			<content:encoded><![CDATA[<p>In this article we&#8217;ll be looking at two of the variable conversion methods in C#.</p>
<h4>Reference Conversion</h4>
<p>A reference conversion is simply converting a variable from one reference type to another. Reference conversions can be either implicit or explicit. This conversion never changes the referential identity of the object which is being converted. This means that although the conversion could change the reference type, the type or value of the referred to object is never changed.</p>
<h4>Boxing and Unboxing Conversion</h4>
<p>A boxing conversion allows any value type to be converted to the type object or to any interface type that is implemented by the value type. Boxing a value means allocating an object instance and then copying the value to that instance.</p>
<p>Unboxing is an explicit conversion. It converts from the type object to a value type or from an interface type to a value type which implements the interface. Unboxing involves checking the object instance to insure that it is a boxed value of the given value type. The next step in the unboxing operation is copying the value from the instance into the value type variable.</p>
<p>It is important to remember that you should avoid boxing/unboxing operations in cases where you&#8217;re needing to do repetitive assignments. By avoiding this overhead you can improve the performance of your program.</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-knowledge">
<ul class="socials">
		<li class="shr-twitter">
			<a href="http://www.shareaholic.com/api/share/?title=Variable+Conversion+in+C%23&amp;link=http://programmingincsharp.com/variable-conversion-in-c/&amp;notes=In%20this%20article%20we%27ll%20be%20looking%20at%20two%20of%20the%20variable%20conversion%20methods%20in%20C%23.%0D%0A%0D%0AReference%20Conversion%0D%0A%0D%0AA%20reference%20conversion%20is%20simply%20converting%20a%20variable%20from%20one%20reference%20type%20to%20another.%20Reference%20conversions%20can%20be%20either%20implicit%20or%20explicit.%20This%20conversion%20never%20changes%20the%20referent&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=%24%7Btitle%7D+-+%24%7Bshort_link%7D&amp;service=7&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.shareaholic.com/api/share/?title=Variable+Conversion+in+C%23&amp;link=http://programmingincsharp.com/variable-conversion-in-c/&amp;notes=In%20this%20article%20we%27ll%20be%20looking%20at%20two%20of%20the%20variable%20conversion%20methods%20in%20C%23.%0D%0A%0D%0AReference%20Conversion%0D%0A%0D%0AA%20reference%20conversion%20is%20simply%20converting%20a%20variable%20from%20one%20reference%20type%20to%20another.%20Reference%20conversions%20can%20be%20either%20implicit%20or%20explicit.%20This%20conversion%20never%20changes%20the%20referent&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=5&amp;tags=&amp;ctype=" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-dzone">
			<a href="http://www.shareaholic.com/api/share/?title=Variable+Conversion+in+C%23&amp;link=http://programmingincsharp.com/variable-conversion-in-c/&amp;notes=In%20this%20article%20we%27ll%20be%20looking%20at%20two%20of%20the%20variable%20conversion%20methods%20in%20C%23.%0D%0A%0D%0AReference%20Conversion%0D%0A%0D%0AA%20reference%20conversion%20is%20simply%20converting%20a%20variable%20from%20one%20reference%20type%20to%20another.%20Reference%20conversions%20can%20be%20either%20implicit%20or%20explicit.%20This%20conversion%20never%20changes%20the%20referent&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=102&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to DZone">Add this to DZone</a>
		</li>
		<li class="shr-slashdot">
			<a href="http://www.shareaholic.com/api/share/?title=Variable+Conversion+in+C%23&amp;link=http://programmingincsharp.com/variable-conversion-in-c/&amp;notes=In%20this%20article%20we%27ll%20be%20looking%20at%20two%20of%20the%20variable%20conversion%20methods%20in%20C%23.%0D%0A%0D%0AReference%20Conversion%0D%0A%0D%0AA%20reference%20conversion%20is%20simply%20converting%20a%20variable%20from%20one%20reference%20type%20to%20another.%20Reference%20conversions%20can%20be%20either%20implicit%20or%20explicit.%20This%20conversion%20never%20changes%20the%20referent&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=61&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Submit this to SlashDot">Submit this to SlashDot</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.shareaholic.com/api/share/?title=Variable+Conversion+in+C%23&amp;link=http://programmingincsharp.com/variable-conversion-in-c/&amp;notes=In%20this%20article%20we%27ll%20be%20looking%20at%20two%20of%20the%20variable%20conversion%20methods%20in%20C%23.%0D%0A%0D%0AReference%20Conversion%0D%0A%0D%0AA%20reference%20conversion%20is%20simply%20converting%20a%20variable%20from%20one%20reference%20type%20to%20another.%20Reference%20conversions%20can%20be%20either%20implicit%20or%20explicit.%20This%20conversion%20never%20changes%20the%20referent&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=38&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.shareaholic.com/api/share/?title=Variable+Conversion+in+C%23&amp;link=http://programmingincsharp.com/variable-conversion-in-c/&amp;notes=In%20this%20article%20we%27ll%20be%20looking%20at%20two%20of%20the%20variable%20conversion%20methods%20in%20C%23.%0D%0A%0D%0AReference%20Conversion%0D%0A%0D%0AA%20reference%20conversion%20is%20simply%20converting%20a%20variable%20from%20one%20reference%20type%20to%20another.%20Reference%20conversions%20can%20be%20either%20implicit%20or%20explicit.%20This%20conversion%20never%20changes%20the%20referent&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=88&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
</ul><div style="clear: both;"></div><div class="shr-getshr" style="visibility:hidden;font-size:10px !important"><a target="_blank" href="http://www.shareaholic.com/?src=pub">Get Shareaholic</a></div><div style="clear: both;"></div></div>

]]></content:encoded>
			<wfw:commentRss>http://programmingincsharp.com/variable-conversion-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Polymorphism in C#</title>
		<link>http://programmingincsharp.com/polymorphism-in-c/</link>
		<comments>http://programmingincsharp.com/polymorphism-in-c/#comments</comments>
		<pubDate>Tue, 24 Apr 2012 15:42:29 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[C# Programming Concepts]]></category>
		<category><![CDATA[abstract class]]></category>
		<category><![CDATA[Interfaces]]></category>
		<category><![CDATA[Interview Questions]]></category>
		<category><![CDATA[overloading]]></category>
		<category><![CDATA[overriding]]></category>
		<category><![CDATA[Polymorphism]]></category>

		<guid isPermaLink="false">http://programmingincsharp.com/?p=54</guid>
		<description><![CDATA[Do you have a programming interview question that trips you up sometimes? One of mine is polymorphism in C# during a discussion of Object Oriented Programming (OOP). While I know the answer and use the technique in my programs, I stumble around with this one often, trying to articulate what it means in the context [...]]]></description>
			<content:encoded><![CDATA[<p>Do you have a programming interview question that trips you up sometimes? One of mine is polymorphism in C# during a discussion of Object Oriented Programming (OOP). While I know the answer and use the technique in my programs, I stumble around with this one often, trying to articulate what it means in the context of C#. So, to help me, and you, get this embedded this in our minds, I decided to write this article on this topic.</p>
<p>The definition of polymorphism is when different types of objects use the method or properties with the same name. Therefore, the programmer doesn’t need to know the exact type of an object while coding the application. The precise behavior of a method or property is determined at run-time. In the context of C# and .NET, this primarily means interfaces. Another form of polymorphism in C# is operator overloading, for example, where the + operator can be used to add numbers or concatenate strings. But, for our discussion here we’ll primarily discuss interfaces.</p>
<h4>C# Interfaces</h4>
<p>While some other languages use other methods to provide polymorphism, C#’s single-inheritance design makes interfaces the way to accomplish this technique. Remember that unlike an abstract class, an interface only defines the behavior, it doesn’t implement it. </p>
<p>Here’s a simple example of what an interface in C# looks like…</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">interface</span> ITestItem
{
    <span class="kwrd">int</span> TestId { get; set; }

    <span class="kwrd">string</span> TestDescription { get; set; }

    <span class="kwrd">string</span> RunTest(<span class="kwrd">int</span> testNumber);
}</pre>
<p>In this interface, we’re defining two properties and one method. To implement this interface in a class, we would need to indicate that we’re using the interface and provide an implementation for the routines defined in the interface, as seen here…</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> TestGroup : ITestItem
{
    DateTime _testDateTime;

    <span class="kwrd">public</span> TestGroup()
    {
        <span class="rem">//load without a parameter</span>
    }

    <span class="kwrd">public</span> TestGroup(<span class="kwrd">string</span> testinfo)
    {
        <span class="rem">//load with a parameter</span>
    }

    <span class="kwrd">public</span> <span class="kwrd">int</span> TestId { get; set; }

    <span class="kwrd">public</span> <span class="kwrd">string</span> TestDescription { get; set; }

    <span class="kwrd">public</span> DateTime TimeStamp
    {
        get{<span class="kwrd">return</span> _testDateTime;}
    }

    <span class="kwrd">public</span> <span class="kwrd">string</span> RunTest(<span class="kwrd">int</span> testNumber)
    {

        <span class="rem">//run our tests</span>
        _testDateTime = DateTime.Now;
        <span class="kwrd">return</span> <span class="kwrd">null</span>;
    }
}</pre>
<p>To show how polymorphism would work, here’s a different class that implements the same interface but has different code…</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> TestUser : ITestItem
{
    <span class="kwrd">int</span> _userId;

    <span class="kwrd">public</span> TestUser(<span class="kwrd">int</span> UserId)
    {
        _userId = UserId;
        <span class="rem">//load with a parameter</span>
    }

    <span class="kwrd">public</span> <span class="kwrd">int</span> TestId
    {
        get{<span class="kwrd">return</span> _userId;}
        set{}
    }

    <span class="kwrd">public</span> <span class="kwrd">string</span> TestDescription { get; set; }

    <span class="kwrd">public</span> <span class="kwrd">string</span> RunTest(<span class="kwrd">int</span> testNumber)
    {
        <span class="rem">//do our tests</span>
        <span class="kwrd">return</span> <span class="kwrd">null</span>;
    }
}
</pre>
<p>Also, in C#, you can only inherit from one base class, including abstract classes, but you can use multiple interfaces as shown in this code snippet that adds a IList interface to our class that’s already using the ITestItem interface we defined…</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> TestUser : ITestItem, IList&lt;<span class="kwrd">string</span>&gt;</pre>
<h4>C# Polymorphism Confusion</h4>
<p>A common bit of confusion when it comes to polymorphism in C# is overloading and overriding. These aren’t the same as polymorphism but are sometimes confused with it. Overloading is when there are multiple definitions, also known as signatures, for the same method name within a class. Overriding occurs when a subclass replaces a parent’s implementation of a method with it’s own code. I’ve seen this used as a trick question in a C# interview so watch out for it, especially if you’re interviewing with someone who’s a stickler of OOP academia.</p>
<p>Anyway, I hope this article has helped you understand more about polymorphism in C#.</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-knowledge">
<ul class="socials">
		<li class="shr-twitter">
			<a href="http://www.shareaholic.com/api/share/?title=Polymorphism+in+C%23&amp;link=http://programmingincsharp.com/polymorphism-in-c/&amp;notes=Do%20you%20have%20a%20programming%20interview%20question%20that%20trips%20you%20up%20sometimes%3F%20One%20of%20mine%20is%20polymorphism%20in%20C%23%20during%20a%20discussion%20of%20Object%20Oriented%20Programming%20%28OOP%29.%20While%20I%20know%20the%20answer%20and%20use%20the%20technique%20in%20my%20programs%2C%20I%20stumble%20around%20with%20this%20one%20often%2C%20trying%20to%20articulate%20what%20it%20means&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=%24%7Btitle%7D+-+%24%7Bshort_link%7D&amp;service=7&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.shareaholic.com/api/share/?title=Polymorphism+in+C%23&amp;link=http://programmingincsharp.com/polymorphism-in-c/&amp;notes=Do%20you%20have%20a%20programming%20interview%20question%20that%20trips%20you%20up%20sometimes%3F%20One%20of%20mine%20is%20polymorphism%20in%20C%23%20during%20a%20discussion%20of%20Object%20Oriented%20Programming%20%28OOP%29.%20While%20I%20know%20the%20answer%20and%20use%20the%20technique%20in%20my%20programs%2C%20I%20stumble%20around%20with%20this%20one%20often%2C%20trying%20to%20articulate%20what%20it%20means&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=5&amp;tags=&amp;ctype=" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-dzone">
			<a href="http://www.shareaholic.com/api/share/?title=Polymorphism+in+C%23&amp;link=http://programmingincsharp.com/polymorphism-in-c/&amp;notes=Do%20you%20have%20a%20programming%20interview%20question%20that%20trips%20you%20up%20sometimes%3F%20One%20of%20mine%20is%20polymorphism%20in%20C%23%20during%20a%20discussion%20of%20Object%20Oriented%20Programming%20%28OOP%29.%20While%20I%20know%20the%20answer%20and%20use%20the%20technique%20in%20my%20programs%2C%20I%20stumble%20around%20with%20this%20one%20often%2C%20trying%20to%20articulate%20what%20it%20means&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=102&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to DZone">Add this to DZone</a>
		</li>
		<li class="shr-slashdot">
			<a href="http://www.shareaholic.com/api/share/?title=Polymorphism+in+C%23&amp;link=http://programmingincsharp.com/polymorphism-in-c/&amp;notes=Do%20you%20have%20a%20programming%20interview%20question%20that%20trips%20you%20up%20sometimes%3F%20One%20of%20mine%20is%20polymorphism%20in%20C%23%20during%20a%20discussion%20of%20Object%20Oriented%20Programming%20%28OOP%29.%20While%20I%20know%20the%20answer%20and%20use%20the%20technique%20in%20my%20programs%2C%20I%20stumble%20around%20with%20this%20one%20often%2C%20trying%20to%20articulate%20what%20it%20means&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=61&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Submit this to SlashDot">Submit this to SlashDot</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.shareaholic.com/api/share/?title=Polymorphism+in+C%23&amp;link=http://programmingincsharp.com/polymorphism-in-c/&amp;notes=Do%20you%20have%20a%20programming%20interview%20question%20that%20trips%20you%20up%20sometimes%3F%20One%20of%20mine%20is%20polymorphism%20in%20C%23%20during%20a%20discussion%20of%20Object%20Oriented%20Programming%20%28OOP%29.%20While%20I%20know%20the%20answer%20and%20use%20the%20technique%20in%20my%20programs%2C%20I%20stumble%20around%20with%20this%20one%20often%2C%20trying%20to%20articulate%20what%20it%20means&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=38&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.shareaholic.com/api/share/?title=Polymorphism+in+C%23&amp;link=http://programmingincsharp.com/polymorphism-in-c/&amp;notes=Do%20you%20have%20a%20programming%20interview%20question%20that%20trips%20you%20up%20sometimes%3F%20One%20of%20mine%20is%20polymorphism%20in%20C%23%20during%20a%20discussion%20of%20Object%20Oriented%20Programming%20%28OOP%29.%20While%20I%20know%20the%20answer%20and%20use%20the%20technique%20in%20my%20programs%2C%20I%20stumble%20around%20with%20this%20one%20often%2C%20trying%20to%20articulate%20what%20it%20means&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=88&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
</ul><div style="clear: both;"></div><div class="shr-getshr" style="visibility:hidden;font-size:10px !important"><a target="_blank" href="http://www.shareaholic.com/?src=pub">Get Shareaholic</a></div><div style="clear: both;"></div></div>

]]></content:encoded>
			<wfw:commentRss>http://programmingincsharp.com/polymorphism-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Indexers in C#</title>
		<link>http://programmingincsharp.com/indexers-in-c/</link>
		<comments>http://programmingincsharp.com/indexers-in-c/#comments</comments>
		<pubDate>Sat, 21 Apr 2012 12:39:40 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[C# Programming Concepts]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[brackets]]></category>
		<category><![CDATA[Indexers]]></category>
		<category><![CDATA[this]]></category>

		<guid isPermaLink="false">http://programmingincsharp.com/?p=51</guid>
		<description><![CDATA[Indexers in C# are a simple way to permit a class to be used like an array. The advantage of using indexers rather than exposing an array or generic list object is that internally, you can manage the values presented in any fashion you wish. This means that you can manage the array size, error [...]]]></description>
			<content:encoded><![CDATA[<p>Indexers in C# are a simple way to permit a class to be used like an array. The advantage of using indexers rather than exposing an array or generic list object is that internally, you can manage the values presented in any fashion you wish. This means that you can manage the array size, error handling and so forth with a lower likelihood of bugs. It allows you to better manage complex data structures, odd types of class members and so forth, hiding some complexity from other programmers who may not be familiar with a particular requirement. I’ve found indexers to be particularly useful in situations where data is being aggregated from several different sources but needs to be presented as a cohesive group. Below, I’ll present a simple example of a C# indexer and you can take it from there.</p>
<p>First, lets begin with a simple string property that’s accessed via an integer indexer. We’ll assume that there is an array of strings that’s loaded in the class constructor. Here’s what this property looks like…</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">string</span> <span class="kwrd">this</span>[<span class="kwrd">int</span> index]
{
    get
   {
        <span class="kwrd">return</span> DataItems[index];
    }
    set
   {
        DataItems[index] = <span class="kwrd">value</span>;
    }
}</pre>
<p>As you can see, we use the <font color="#0000ff">this</font> keyword to define the indexer. Also notice that we use brackets [] rather than a parenthesis () in the definition. In this case, our indexer is an integer and we’re expecting a string value to be set or returned by the property. But, the nice thing is that it can be overloaded. So, let’s say that I wanted to be able to send in a string value or an integer value. Here’s how it would be coded…</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">string</span> <span class="kwrd">this</span>[<span class="kwrd">string</span> key]
{
    get
    {
        <span class="kwrd">return</span> DataItems[GetIndexForKey(key)];
    }
    set
    {
        DataItems[GetIndexForKey(key)] = <span class="kwrd">value</span>;
    }
}</pre>
<p>In this example, we’re passing in a string value rather than an integer value to determine the return value. A private routine, GetIndexForKey, looks up the internal index value so that it can be set or returned properly in the underlying data. As you can see, overloading indexers can add a lot of flexibility to your code. The indexer could be called either way as seen in these two example lines of code… </p>
<pre class="csharpcode"> IndexTest[1] = <span class="str">"This is a Test"</span>;

 IndexTest[<span class="str">"example"</span>] = <span class="str">"This is a Test"</span>;</pre>
<p>But, you can take it even further with by having an indexer with multiple parameters. This method is effective for returning individual items within class instances contained within the indexer class or to perform special processing on a the returned value. For example, if you were returning a date as a string value, you could pass in formatting information in addition to the index value, as in this example…</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">string</span> <span class="kwrd">this</span>[<span class="kwrd">int</span> index, <span class="kwrd">string</span> formatValue]
{
    get
    {
        <span class="kwrd">return</span> DateItems[index].ToString(formatValue);
    }
}</pre>
<p>As in other situations, you will need to make sure that each overloaded indexer has an unique signature.</p>
<p>I hope this article has covered the basics of indexers in C# for you.</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-knowledge">
<ul class="socials">
		<li class="shr-twitter">
			<a href="http://www.shareaholic.com/api/share/?title=Indexers+in+C%23&amp;link=http://programmingincsharp.com/indexers-in-c/&amp;notes=Indexers%20in%20C%23%20are%20a%20simple%20way%20to%20permit%20a%20class%20to%20be%20used%20like%20an%20array.%20The%20advantage%20of%20using%20indexers%20rather%20than%20exposing%20an%20array%20or%20generic%20list%20object%20is%20that%20internally%2C%20you%20can%20manage%20the%20values%20presented%20in%20any%20fashion%20you%20wish.%20This%20means%20that%20you%20can%20manage%20the%20array%20size%2C%20error%20handl&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=%24%7Btitle%7D+-+%24%7Bshort_link%7D&amp;service=7&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.shareaholic.com/api/share/?title=Indexers+in+C%23&amp;link=http://programmingincsharp.com/indexers-in-c/&amp;notes=Indexers%20in%20C%23%20are%20a%20simple%20way%20to%20permit%20a%20class%20to%20be%20used%20like%20an%20array.%20The%20advantage%20of%20using%20indexers%20rather%20than%20exposing%20an%20array%20or%20generic%20list%20object%20is%20that%20internally%2C%20you%20can%20manage%20the%20values%20presented%20in%20any%20fashion%20you%20wish.%20This%20means%20that%20you%20can%20manage%20the%20array%20size%2C%20error%20handl&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=5&amp;tags=&amp;ctype=" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-dzone">
			<a href="http://www.shareaholic.com/api/share/?title=Indexers+in+C%23&amp;link=http://programmingincsharp.com/indexers-in-c/&amp;notes=Indexers%20in%20C%23%20are%20a%20simple%20way%20to%20permit%20a%20class%20to%20be%20used%20like%20an%20array.%20The%20advantage%20of%20using%20indexers%20rather%20than%20exposing%20an%20array%20or%20generic%20list%20object%20is%20that%20internally%2C%20you%20can%20manage%20the%20values%20presented%20in%20any%20fashion%20you%20wish.%20This%20means%20that%20you%20can%20manage%20the%20array%20size%2C%20error%20handl&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=102&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to DZone">Add this to DZone</a>
		</li>
		<li class="shr-slashdot">
			<a href="http://www.shareaholic.com/api/share/?title=Indexers+in+C%23&amp;link=http://programmingincsharp.com/indexers-in-c/&amp;notes=Indexers%20in%20C%23%20are%20a%20simple%20way%20to%20permit%20a%20class%20to%20be%20used%20like%20an%20array.%20The%20advantage%20of%20using%20indexers%20rather%20than%20exposing%20an%20array%20or%20generic%20list%20object%20is%20that%20internally%2C%20you%20can%20manage%20the%20values%20presented%20in%20any%20fashion%20you%20wish.%20This%20means%20that%20you%20can%20manage%20the%20array%20size%2C%20error%20handl&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=61&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Submit this to SlashDot">Submit this to SlashDot</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.shareaholic.com/api/share/?title=Indexers+in+C%23&amp;link=http://programmingincsharp.com/indexers-in-c/&amp;notes=Indexers%20in%20C%23%20are%20a%20simple%20way%20to%20permit%20a%20class%20to%20be%20used%20like%20an%20array.%20The%20advantage%20of%20using%20indexers%20rather%20than%20exposing%20an%20array%20or%20generic%20list%20object%20is%20that%20internally%2C%20you%20can%20manage%20the%20values%20presented%20in%20any%20fashion%20you%20wish.%20This%20means%20that%20you%20can%20manage%20the%20array%20size%2C%20error%20handl&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=38&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.shareaholic.com/api/share/?title=Indexers+in+C%23&amp;link=http://programmingincsharp.com/indexers-in-c/&amp;notes=Indexers%20in%20C%23%20are%20a%20simple%20way%20to%20permit%20a%20class%20to%20be%20used%20like%20an%20array.%20The%20advantage%20of%20using%20indexers%20rather%20than%20exposing%20an%20array%20or%20generic%20list%20object%20is%20that%20internally%2C%20you%20can%20manage%20the%20values%20presented%20in%20any%20fashion%20you%20wish.%20This%20means%20that%20you%20can%20manage%20the%20array%20size%2C%20error%20handl&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=88&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
</ul><div style="clear: both;"></div><div class="shr-getshr" style="visibility:hidden;font-size:10px !important"><a target="_blank" href="http://www.shareaholic.com/?src=pub">Get Shareaholic</a></div><div style="clear: both;"></div></div>

]]></content:encoded>
			<wfw:commentRss>http://programmingincsharp.com/indexers-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Paging Data with LINQ to SQL</title>
		<link>http://programmingincsharp.com/paging-data-with-linq-to-sql/</link>
		<comments>http://programmingincsharp.com/paging-data-with-linq-to-sql/#comments</comments>
		<pubDate>Fri, 20 Apr 2012 10:17:49 +0000</pubDate>
		<dc:creator>Frank</dc:creator>
				<category><![CDATA[C# Code Examples]]></category>
		<category><![CDATA[dataset]]></category>
		<category><![CDATA[Extensions]]></category>
		<category><![CDATA[IQueryable]]></category>
		<category><![CDATA[LINQ to SQL]]></category>
		<category><![CDATA[Math.Ceiling]]></category>
		<category><![CDATA[Paging]]></category>

		<guid isPermaLink="false">http://programmingincsharp.com/?p=47</guid>
		<description><![CDATA[In this article, I’ll be discussing how to implement the ability to page data with LINQ to SQL using an extension method. When dealing with large amounts of data, it will help your users and your applications performance to only retrieve and display small portions of a large amount of data. It’s also a good [...]]]></description>
			<content:encoded><![CDATA[<p>In this article, I’ll be discussing how to implement the ability to page data with LINQ to SQL using an extension method. When dealing with large amounts of data, it will help your users and your applications performance to only retrieve and display small portions of a large amount of data. It’s also a good user interface idea to permit users to easily move from one page to the next and to allow them to select the amount of data to view at once. Unfortunately, this hasn’t been implemented well in a lot of code I’ve seen. The nice thing about LINQ is that it offers a very easy way to do it.</p>
<p>To make things even easier, we’ll be setting this up as an extension to IQueryable, the interface that LINQ to SQL is based upon. Remember that as with any extension, you’ll need to define the method as static and use the&nbsp; ‘this’ keyword. Here’s the code…</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">static</span> IQueryable&lt;TSource&gt; Page&lt;TSource&gt;(<span class="kwrd">this</span> IQueryable&lt;TSource&gt; source,
<span class="kwrd">int</span> currentPage, <span class="kwrd">int</span> pageSize)
{
<span class="kwrd">    return</span> source.Skip((currentPage - 1) * pageSize).Take(pageSize);
}</pre>
<p>Simple, isn’t it. As you can see, we pass in the current page and the page size. The Skip method is used to skip over records up to our calculated starting point. Then, the Take method is used to retrieve our specified number of records.</p>
<p>However, there is another function that will prove useful. We need a way to get the total number of pages. Here’s the code…</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">int</span> PageCount&lt;TSource&gt;(<span class="kwrd">this</span> IQueryable&lt;TSource&gt; source, <span class="kwrd">int</span> pageSize)
{
    <span class="kwrd">return</span> (<span class="kwrd">int</span>)Math.Ceiling((<span class="kwrd">decimal</span>)source.Count() / (<span class="kwrd">decimal</span>)pageSize);
}</pre>
<p>This method returns the number of pages present based on the provide page size. We use the Math.Ceiling method to get the smallest integer that’s greater than or equal to the number of records divided by the page size. Remember when you call this function that you want to use the full dataset and not the page query instance. You can use this value to validate movement within the data, such as with a previous/next button or page number selection, and to provide user feedback, such as Page X of Y displays. If you want, you could extend this even further with another function that provided a list of page numbers.</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-knowledge">
<ul class="socials">
		<li class="shr-twitter">
			<a href="http://www.shareaholic.com/api/share/?title=Paging+Data+with+LINQ+to+SQL&amp;link=http://programmingincsharp.com/paging-data-with-linq-to-sql/&amp;notes=In%20this%20article%2C%20I%E2%80%99ll%20be%20discussing%20how%20to%20implement%20the%20ability%20to%20page%20data%20with%20LINQ%20to%20SQL%20using%20an%20extension%20method.%20When%20dealing%20with%20large%20amounts%20of%20data%2C%20it%20will%20help%20your%20users%20and%20your%20applications%20performance%20to%20only%20retrieve%20and%20display%20small%20portions%20of%20a%20large%20amount%20of%20data.%20It%E2%80%99s&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=%24%7Btitle%7D+-+%24%7Bshort_link%7D&amp;service=7&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.shareaholic.com/api/share/?title=Paging+Data+with+LINQ+to+SQL&amp;link=http://programmingincsharp.com/paging-data-with-linq-to-sql/&amp;notes=In%20this%20article%2C%20I%E2%80%99ll%20be%20discussing%20how%20to%20implement%20the%20ability%20to%20page%20data%20with%20LINQ%20to%20SQL%20using%20an%20extension%20method.%20When%20dealing%20with%20large%20amounts%20of%20data%2C%20it%20will%20help%20your%20users%20and%20your%20applications%20performance%20to%20only%20retrieve%20and%20display%20small%20portions%20of%20a%20large%20amount%20of%20data.%20It%E2%80%99s&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=5&amp;tags=&amp;ctype=" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-dzone">
			<a href="http://www.shareaholic.com/api/share/?title=Paging+Data+with+LINQ+to+SQL&amp;link=http://programmingincsharp.com/paging-data-with-linq-to-sql/&amp;notes=In%20this%20article%2C%20I%E2%80%99ll%20be%20discussing%20how%20to%20implement%20the%20ability%20to%20page%20data%20with%20LINQ%20to%20SQL%20using%20an%20extension%20method.%20When%20dealing%20with%20large%20amounts%20of%20data%2C%20it%20will%20help%20your%20users%20and%20your%20applications%20performance%20to%20only%20retrieve%20and%20display%20small%20portions%20of%20a%20large%20amount%20of%20data.%20It%E2%80%99s&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=102&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Add this to DZone">Add this to DZone</a>
		</li>
		<li class="shr-slashdot">
			<a href="http://www.shareaholic.com/api/share/?title=Paging+Data+with+LINQ+to+SQL&amp;link=http://programmingincsharp.com/paging-data-with-linq-to-sql/&amp;notes=In%20this%20article%2C%20I%E2%80%99ll%20be%20discussing%20how%20to%20implement%20the%20ability%20to%20page%20data%20with%20LINQ%20to%20SQL%20using%20an%20extension%20method.%20When%20dealing%20with%20large%20amounts%20of%20data%2C%20it%20will%20help%20your%20users%20and%20your%20applications%20performance%20to%20only%20retrieve%20and%20display%20small%20portions%20of%20a%20large%20amount%20of%20data.%20It%E2%80%99s&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=61&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Submit this to SlashDot">Submit this to SlashDot</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.shareaholic.com/api/share/?title=Paging+Data+with+LINQ+to+SQL&amp;link=http://programmingincsharp.com/paging-data-with-linq-to-sql/&amp;notes=In%20this%20article%2C%20I%E2%80%99ll%20be%20discussing%20how%20to%20implement%20the%20ability%20to%20page%20data%20with%20LINQ%20to%20SQL%20using%20an%20extension%20method.%20When%20dealing%20with%20large%20amounts%20of%20data%2C%20it%20will%20help%20your%20users%20and%20your%20applications%20performance%20to%20only%20retrieve%20and%20display%20small%20portions%20of%20a%20large%20amount%20of%20data.%20It%E2%80%99s&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=38&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.shareaholic.com/api/share/?title=Paging+Data+with+LINQ+to+SQL&amp;link=http://programmingincsharp.com/paging-data-with-linq-to-sql/&amp;notes=In%20this%20article%2C%20I%E2%80%99ll%20be%20discussing%20how%20to%20implement%20the%20ability%20to%20page%20data%20with%20LINQ%20to%20SQL%20using%20an%20extension%20method.%20When%20dealing%20with%20large%20amounts%20of%20data%2C%20it%20will%20help%20your%20users%20and%20your%20applications%20performance%20to%20only%20retrieve%20and%20display%20small%20portions%20of%20a%20large%20amount%20of%20data.%20It%E2%80%99s&amp;short_link=&amp;shortener=google&amp;shortener_key=&amp;v=1&amp;apitype=1&amp;apikey=8afa39428933be41f8afdb8ea21a495c&amp;source=Shareaholic&amp;template=&amp;service=88&amp;tags=&amp;ctype=" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
</ul><div style="clear: both;"></div><div class="shr-getshr" style="visibility:hidden;font-size:10px !important"><a target="_blank" href="http://www.shareaholic.com/?src=pub">Get Shareaholic</a></div><div style="clear: both;"></div></div>

]]></content:encoded>
			<wfw:commentRss>http://programmingincsharp.com/paging-data-with-linq-to-sql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

