<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<title>Snipplr</title>
<link>http://snipplr.com/language/c-sharp/tags/csharp</link>
<description>Recent snippets posted on Snipplr.com</description>
<language>en-us</language>
<pubDate>Fri, 24 May 2013 19:12:47 GMT</pubDate>
<item>
<title>(C#) Get the Host Name in ASP.NET C# - jasonseney</title>
<link>http://snipplr.com/view/50795/get-the-host-name-in-aspnet-c/</link>
<description><![CDATA[ <p>Will return the host name that the site is running on. Example:

`http://mysite.com` or `http://myspecialsite.com:102`</p> ]]></description>
<pubDate>Fri, 18 Mar 2011 07:33:58 GMT</pubDate>
<guid>http://snipplr.com/view/50795/get-the-host-name-in-aspnet-c/</guid>
</item>
<item>
<title>(C#) isDate in C# - stewshack</title>
<link>http://snipplr.com/view/42244/isdate-in-c/</link>
<description><![CDATA[ <p>Here is my function to check if a string is a date in C#.</p> ]]></description>
<pubDate>Thu, 14 Oct 2010 12:32:11 GMT</pubDate>
<guid>http://snipplr.com/view/42244/isdate-in-c/</guid>
</item>
<item>
<title>(C#) Reverse a String with LINQ - stewshack</title>
<link>http://snipplr.com/view/42243/reverse-a-string-with-linq/</link>
<description><![CDATA[ <p>Here's how to reverse a string in one line using C# and LINQ.</p> ]]></description>
<pubDate>Thu, 14 Oct 2010 12:28:51 GMT</pubDate>
<guid>http://snipplr.com/view/42243/reverse-a-string-with-linq/</guid>
</item>
<item>
<title>(C#) Reusable embedded keep session alive with HtmlHelper for ASP.NET MVC 1.0 C# - jeremybiros</title>
<link>http://snipplr.com/view/39877/reusable-embedded-keep-session-alive-with-htmlhelper-for-aspnet-mvc-10-c/</link>
<description><![CDATA[ <p>This is an embedded, mvc method of keeping your asp.net session alive using the iframe approach that many people have used, but allowing it to be instantly added to a page via an HtmlHelper method, and allowing it to be packaged in a library that can be referenced by any of your projects.</p> ]]></description>
<pubDate>Thu, 02 Sep 2010 10:38:33 GMT</pubDate>
<guid>http://snipplr.com/view/39877/reusable-embedded-keep-session-alive-with-htmlhelper-for-aspnet-mvc-10-c/</guid>
</item>
<item>
<title>(C#) String Comparison C# - stewshack</title>
<link>http://snipplr.com/view/38608/string-comparison-c/</link>
<description><![CDATA[ <p></p> ]]></description>
<pubDate>Fri, 06 Aug 2010 13:59:36 GMT</pubDate>
<guid>http://snipplr.com/view/38608/string-comparison-c/</guid>
</item>
<item>
<title>(C#) Calculate a CRC32 like the one used in zip files - pckujawa</title>
<link>http://snipplr.com/view/37249/calculate-a-crc32-like-the-one-used-in-zip-files/</link>
<description><![CDATA[ <p></p> ]]></description>
<pubDate>Wed, 14 Jul 2010 03:33:44 GMT</pubDate>
<guid>http://snipplr.com/view/37249/calculate-a-crc32-like-the-one-used-in-zip-files/</guid>
</item>
<item>
<title>(C#) Persisting values in an application between executions and upgrades (.NET) - pckujawa</title>
<link>http://snipplr.com/view/29302/persisting-values-in-an-application-between-executions-and-upgrades-net/</link>
<description><![CDATA[ <p>Read the [best article on the subject - Exploring Secrets of Persistent Application Settings](http://www.devx.com/dotnet/Article/33944/1954) - and then the following links if desired. (Note, I recommend reading [this forum post about settings not being upgraded](http://social.msdn.microsoft.com/Forums/en-US/msbuild/thread/790980e8-eb89-492c-976b-4f93dc7c1caf/).)

It might help to understand [What is app.config for?](http://stackoverflow.com/questions/1431742/what-is-app-config-for)

Also of note: [Saving out a Form's Size and Location using the Application Settings feature](http://blogs.msdn.com/rprabhu/archive/2005/11/28/497792.aspx). Be sure to read the first couple of comments as well.

Lastly, if you are interested in import/export functionality, check out [my post on the subject](http://stackoverflow.com/questions/2389290/how-to-load-a-separate-application-settings-file-dynamically-and-merge-with-curre).</p> ]]></description>
<pubDate>Thu, 04 Mar 2010 17:08:30 GMT</pubDate>
<guid>http://snipplr.com/view/29302/persisting-values-in-an-application-between-executions-and-upgrades-net/</guid>
</item>
<item>
<title>(C#) C# Strip XML/HTML from string - jasonseney</title>
<link>http://snipplr.com/view/20590/c-strip-xmlhtml-from-string/</link>
<description><![CDATA[ <p>[Credit - Sam Allen, Dot Net Perls](http://dotnetperls.com/remove-html-tags)</p> ]]></description>
<pubDate>Fri, 02 Oct 2009 12:31:52 GMT</pubDate>
<guid>http://snipplr.com/view/20590/c-strip-xmlhtml-from-string/</guid>
</item>
<item>
<title>(C#) C# tricks : regex, encoding, log, format date - manatlan</title>
<link>http://snipplr.com/view/19673/c-tricks--regex-encoding-log-format-date/</link>
<description><![CDATA[ <p></p> ]]></description>
<pubDate>Mon, 14 Sep 2009 03:13:40 GMT</pubDate>
<guid>http://snipplr.com/view/19673/c-tricks--regex-encoding-log-format-date/</guid>
</item>
<item>
<title>(C#) Strip Decimal and Zero Fill - rtipton</title>
<link>http://snipplr.com/view/15676/strip-decimal-and-zero-fill/</link>
<description><![CDATA[ <p></p> ]]></description>
<pubDate>Sun, 07 Jun 2009 19:27:24 GMT</pubDate>
<guid>http://snipplr.com/view/15676/strip-decimal-and-zero-fill/</guid>
</item>
<item>
<title>(C#) Array in C# - rtipton</title>
<link>http://snipplr.com/view/15315/array-in-c/</link>
<description><![CDATA[ <p></p> ]]></description>
<pubDate>Mon, 25 May 2009 21:54:12 GMT</pubDate>
<guid>http://snipplr.com/view/15315/array-in-c/</guid>
</item>
<item>
<title>(C#) Pass a string from C# to a C++ DLL in .NET (and get a pointer to the string's chars) - pckujawa</title>
<link>http://snipplr.com/view/12509/pass-a-string-from-c-to-a-c-dll-in-net-and-get-a-pointer-to-the-strings-chars/</link>
<description><![CDATA[ <p>Specifically, this code enables you to pass a string from C# into C++ managed code and then get a C++ pointer to the string's chars.

[Marshal.StringToHGlobalAnsi Method](http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.stringtohglobalansi.aspx) from MSDN.

Good [information about native vs. managed types](http://blog.rednael.com/2008/08/29/MarshallingUsingNativeDLLsInNET.aspx).

Related [forum post](http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/5fe46030-76f7-4988-9ce3-fc21a30ee3a2/).</p> ]]></description>
<pubDate>Mon, 23 Feb 2009 11:38:49 GMT</pubDate>
<guid>http://snipplr.com/view/12509/pass-a-string-from-c-to-a-c-dll-in-net-and-get-a-pointer-to-the-strings-chars/</guid>
</item>
<item>
<title>(C#) .NET Getting and Setting the Application Version Number and other application attributes - pckujawa</title>
<link>http://snipplr.com/view/11761/net-getting-and-setting-the-application-version-number-and-other-application-attributes/</link>
<description><![CDATA[ <p>If you are just looking for the version information in the assembly, you can use `Application.ProductVersion` (at least for GUI applications). I don't think you'll get the reflection performance hit this way.

You can also add an "About" box to your GUI and see what code is generated for all of that information (product name, copyright, version, etc).</p> ]]></description>
<pubDate>Mon, 02 Feb 2009 13:52:29 GMT</pubDate>
<guid>http://snipplr.com/view/11761/net-getting-and-setting-the-application-version-number-and-other-application-attributes/</guid>
</item>
<item>
<title>(C#) Create IEnumerable&amp;amp;lt;string&amp;amp;gt; to manually enumerate over string values - pckujawa</title>
<link>http://snipplr.com/view/11153/create-ienumerableltstringgt-to-manually-enumerate-over-string-values/</link>
<description><![CDATA[ <p>Ever need to enumerate over a collection of strings? Simple, just `foreach(string str in stringContainer)` and you've got them, right? Well, what if you want to enumerate over them manually? Basically, you can't (as far as I've found). You can do `stringContainer.GetEnumerator()`, but that returns a System.Collections.IEnumerator, which holds objects, not strings (thus requiring an explicit cast to get the string value). That is, you cannot do  
    `IEnumerator en = stringContainer.GetEnumerator();  
    en.MoveNext();  
    string myString = en.Current;`  
You have to do  
    `string myString = Convert.ToString(en.Current);`

I don't know why C# doesn't have this built in, but it doesn't (and trying to cast to IEnumerator&amp;lt;string&amp;gt; throws an exception). So, there are a lot of homegrown solutions out there, but this one is the easiest for my application. I needed to read in values from a file and assign them to string variables. Since there are a lot of variables, and their order is sometimes changed between versions, it is a pain to assign each variable by its array index (the array being created by reading in each line and splitting it with a comma). It is much easier to change the ordering of variables by simply moving the line up or down in the source code. Thus the need for an enumerator and my dilemma.

The URL refers to similar code from an O'Reilly book.

The 'yield return' statement essentially returns one value from a collection each time a value is needed (such as in a foreach or enum.MoveNext), but keeps track of ordering for you. Pretty slick. Google it for more info.</p> ]]></description>
<pubDate>Tue, 13 Jan 2009 19:04:35 GMT</pubDate>
<guid>http://snipplr.com/view/11153/create-ienumerableltstringgt-to-manually-enumerate-over-string-values/</guid>
</item>
<item>
<title>(C#) C# Extension Method To Add ForEach To Any IEnumerable - hoffstein</title>
<link>http://snipplr.com/view/10554/c-extension-method-to-add-foreach-to-any-ienumerable/</link>
<description><![CDATA[ <p>This will allow you to use the LINQ ForEach method on any IEnumerable.</p> ]]></description>
<pubDate>Wed, 17 Dec 2008 11:34:47 GMT</pubDate>
<guid>http://snipplr.com/view/10554/c-extension-method-to-add-foreach-to-any-ienumerable/</guid>
</item>
<item>
<title>(C#) Getting Application Data and other Special Folders Paths - pckujawa</title>
<link>http://snipplr.com/view/10434/getting-application-data-and-other-special-folders-paths/</link>
<description><![CDATA[ <p>Thanks to Jon Koon for this one. Other special folders can be obtained through the System.Environment.SpecialFolder enumeration (such as the desktop, my computer, my docs, etc).</p> ]]></description>
<pubDate>Fri, 12 Dec 2008 13:42:16 GMT</pubDate>
<guid>http://snipplr.com/view/10434/getting-application-data-and-other-special-folders-paths/</guid>
</item>
<item>
<title>(C#) Example of subscribing to and firing events - pckujawa</title>
<link>http://snipplr.com/view/10291/example-of-subscribing-to-and-firing-events/</link>
<description><![CDATA[ <p>See [Raise and Handle Events Generically](http://snipplr.com/view/15033/raise-and-handle-events-generically/) for a better way to use events (generically) and links to best practices in .NET events.

This example should be used as a Console program and will simply write a line of text to the console. It shows how to create an event handler in a class, have that event handler fire an event with custom information, and how to subscribe to that event and use its data. I hope it is straightforward enough.</p> ]]></description>
<pubDate>Fri, 05 Dec 2008 14:12:19 GMT</pubDate>
<guid>http://snipplr.com/view/10291/example-of-subscribing-to-and-firing-events/</guid>
</item>
<item>
<title>(C#) C# Get Enum Members - hoffstein</title>
<link>http://snipplr.com/view/10188/c-get-enum-members/</link>
<description><![CDATA[ <p>Retrieves an array of strings corresponding to members of the given enum.  Useful for looping through them at runtime.</p> ]]></description>
<pubDate>Mon, 01 Dec 2008 16:34:59 GMT</pubDate>
<guid>http://snipplr.com/view/10188/c-get-enum-members/</guid>
</item>
<item>
<title>(C#) Scroll to the bottom of a text box control programmatically - pckujawa</title>
<link>http://snipplr.com/view/9922/scroll-to-the-bottom-of-a-text-box-control-programmatically/</link>
<description><![CDATA[ <p>This technique is useful for when you are displaying text back to the user in a GUI and want the most recent text to be displayed at the bottom of the text control, but you want that information to be visible.

Another nice function that works well if you aren’t moving up and down in the text box is the textbox.AppendText function.  It will add the text to the bottom and keep the textbox from going to the top. (Thanks to Charlie Mann for that one.)</p> ]]></description>
<pubDate>Wed, 19 Nov 2008 12:17:29 GMT</pubDate>
<guid>http://snipplr.com/view/9922/scroll-to-the-bottom-of-a-text-box-control-programmatically/</guid>
</item>
<item>
<title>(C#) How to tell what form control a context menu was over when you clicked one of its items - pckujawa</title>
<link>http://snipplr.com/view/9460/how-to-tell-what-form-control-a-context-menu-was-over-when-you-clicked-one-of-its-items/</link>
<description><![CDATA[ <p>If you add a ContextMenuStrip (the menu that shows up when you right-click on something) to your design, you can conveniently have more than one form control use that context menu. The only problem is that you don't know which control the context menu was over (in other words, what data the user right-clicked on) when you get the event for one of the ToolStripMenuItems on the ContextMenuStrip being clicked. Many others have monitored the control for mouse click events to tell which control fired the event, but I've found a better, more abstract way of determining this information.

In my case, I have two DataGridViews with the same context menu. The only items on that menu are Copy and Paste. The code below shows that, when Copy is clicked, the "sender" argument can be cast as a ToolStripDropDownItem, the Owner of which is a ContextMenuStrip. This Owner can be cast as the control which holds the data that the user clicked on, e.g. a DataGridView as in my case. From that point, the control can be manipulated in any way necessary.

I still haven't found a good way to have right-click select the item (cell, row, column, table) in the DGV as if the user left-clicked. Currently, the user needs to select the data with left-clicks and then right-click inside the DGV for the context-menu to know what data to use.</p> ]]></description>
<pubDate>Mon, 03 Nov 2008 14:00:47 GMT</pubDate>
<guid>http://snipplr.com/view/9460/how-to-tell-what-form-control-a-context-menu-was-over-when-you-clicked-one-of-its-items/</guid>
</item>
</channel>
</rss>