<?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>C# Help</title>
	<atom:link href="http://www.csharphelp.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.csharphelp.com</link>
	<description>C# Tutorial and Guides</description>
	<lastBuildDate>Tue, 07 Feb 2012 01:03:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>C# Strings &#8211; Getting Started with Strings</title>
		<link>http://www.csharphelp.com/2011/06/c-strings-getting-started-with-strings/</link>
		<comments>http://www.csharphelp.com/2011/06/c-strings-getting-started-with-strings/#comments</comments>
		<pubDate>Tue, 21 Jun 2011 06:26:03 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C# Language]]></category>
		<category><![CDATA[Stringbuilder]]></category>
		<category><![CDATA[Strings]]></category>

		<guid isPermaLink="false">http://www.csharphelp.com/?p=2634</guid>
		<description><![CDATA[Working with strings is a very common task for most C# developers. The .NET Framework offers good variety of tools for working with strings, but care must be taken as there are several gotchas to trip up the beginner. The first thing to note about strings in .NET is that they are Reference Types. Reference [...]]]></description>
			<content:encoded><![CDATA[<p>Working with strings is a very common task for most C# developers. The .NET Framework offers good variety of tools for working with strings, but care must be taken as there are several gotchas to trip up the beginner.</p>
<p>The first thing to note about strings in .NET is that they are Reference Types. Reference types are live on the managed heap in .NET and so they are managed by the .NET Garbage Collector and unlike Value types they are not automatically  destroyed when they are out of scope.</p>
<p>Strings can be easily instantiated using the below syntax:</p>
<pre>string s1;</pre>
<p>Note that there is no default value for a string, thus you need to assign a value to the string before attempting to access it:</p>
<pre>string s1;
Literal1.Text = s1; //Will give run-time error 

string s2 = ""; //This is as close as you can get to assigning nothing to a string;
Literal1.Text = s2;</pre>
<p>There are numerous inbuilt methods in the .NET framework for dealing with strings. Namely:</p>
<p>Note that strings are immutable and thus the original string can never be changed. Therefore if you need to alter the string you will need to re-assign it a new string value:</p>
<pre>string s1 = "all lower case";
s1.ToUpper() ; //s1 is unchanged
Literal1.Text = s1;  //outputs 'all lower case' since s1 is unchanged

string s2 = "all lower case";
s2 = s2.ToUpper() ; //s2 is now re-assigned the new uppercase string
Literal2.Text = s2; //outputs 'ALL LOWER CASE'</pre>
<p>For more on string immutability see Strings are Immutable.</p>
<h2>String Concatenation</h2>
<p>C# offers a convenient method for concatenating a string using &#8216;+&#8217; :</p>
<pre>string s1 = "Hong ";
string s2 = "Kong";
Literal1.Text = s1 + s2;</pre>
<p>Whilst this method is convenient it is not efficient for performing a large number of concatenations (since strings are immutable and are refenece types created on the managed heap each concatenation results in the creation of an extra string which is not automatically destroyed and stays in memory until the Garbage Collector destroys it).</p>
<p>To get around this limitation the .NET Framework provides the StringBuilder class which can efficiently concatenate strings using its Append() method:</p>
<pre> StringBuilder sb = new StringBuilder();
 sb.Append("Hong ");
 sb.Append("Kong");

Literal1.Text = sb.ToString();</pre>
<p>This is by way of demonstration since there would not be any memory saving from concatentating two strings (in fact this can consume more memory since the StringBuilder consumes memory). In general concatenating more than four strings is normally when a StringBuilder should be used.</p>
<h2>Escape Characters</h2>
<p>In common with other C-based languages, C# strings may contain escape characters which qualify how the string should be output.<br />
Escape characters begin with a backslash followed by a specific character.</p>
<p>Below are listed the various escape characters and their output:</p>
<p>\&#8217;   Inserts a single quote<br />
\&#8221;    Inserts a double quote<br />
\\    Inserts a backslash<br />
\a    Triggers a system alert<br />
\n    Inserts a new line (on Windows systems)<br />
\r    Inserts a carriage return<br />
\t    Inserts a horizontal tab</p>
<h2>Verbatim Strings</h2>
<p>To preserve a string exactly as it is exactly as it was added simply add the @ character at the start of the string.</p>
<p>For example:</p>
<pre>string outputStr =   "This site is named \'C# Help\'  ";
Response.Write(outputStr);</pre>
<p>This outputs </p>
<pre>This site is named 'C# Help'</pre>
<p>To output the string verbatim add the @ character at the start of the string.</p>
<pre>string outputStr =   @"This site is named \'C# Help\'  ";
Response.Write(outputStr);</pre>
<p>This outputs </p>
<pre>This site is named \'C# Help\'</pre>
<p>Note that this will also preserve white space:</p>
<pre>string outputStr =   @"This is a                  broken
up
             string";  </pre>
]]></content:encoded>
			<wfw:commentRss>http://www.csharphelp.com/2011/06/c-strings-getting-started-with-strings/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Passing Parameters in C#</title>
		<link>http://www.csharphelp.com/2011/06/passing-parameters-in-c/</link>
		<comments>http://www.csharphelp.com/2011/06/passing-parameters-in-c/#comments</comments>
		<pubDate>Tue, 21 Jun 2011 05:21:24 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C# Language]]></category>
		<category><![CDATA[Out]]></category>
		<category><![CDATA[Parameters]]></category>
		<category><![CDATA[Ref]]></category>
		<category><![CDATA[Reference Types]]></category>
		<category><![CDATA[Value Types]]></category>

		<guid isPermaLink="false">http://www.csharphelp.com/?p=2640</guid>
		<description><![CDATA[There are three primary methods of passing parameters to C# methods: Regular Parameter Passing This is passing parameters with no modifying keywords : void MyMethod(Student studentObj, int aNumber) { aNumber += 5; studentObj.Name = "Jon"; } In the above example MyMethod takes two parameters &#8211; a Student object and an Integer. Note the difference between [...]]]></description>
			<content:encoded><![CDATA[<p>There are three primary methods of passing parameters to C# methods:</p>
<h2>Regular Parameter Passing</h2>
<p>This is passing parameters with no modifying keywords :</p>
<pre>
 void MyMethod(Student studentObj, int aNumber)
    {
        aNumber += 5;
        studentObj.Name = "Jon";
    }
</pre>
<p>In the above example MyMethod takes two parameters &#8211; a Student object and an Integer. Note the difference between the passing a Value Type (such as an Integer) and a Reference Type (such as any class such as a &#8216;Student&#8217; object in the above example). Any operations performed on a value type will no be reflected in the original variable whereas operations performed on a reference type are reflected in the original variable. For example, assuming calling the above MyMethod method note the below code (note the comment explanation at the end of the code block):</p>
<pre>
int myInt = 8;
Student aStudent = new Student();
aStudent.Name = "Mike";

MyMethod(aStudent, myInt);
// myInt value is still 8 since it is a Value Type and immutable
// aStudent.Name is now Jon since it is a Reference Type
// and the variable aStudent just points to a Student object
// on the Managed Heap which is then operated upon by the MyMethod(0 method.
</pre>
<h2>Parameter Passing By Reference</h2>
<p>By default all paramteres are passed by value &#8211; i.e. only the value of the variable is passed to method and the object itself is not passed. Thus operations within the method can only operate on the value of the parameter it receives and cannot make changes to the original variable (do note that this only applies to Value Types as noted above since Reference Types will have their state changes when they are passed to methods).<br />
Alternatively, parameters can be passed by <b>Reference</b> using the ref keyword and their values will be changed by the method:</p>
<pre>
   void MyMethod(ref int aNumber)
    {
        aNumber += 5;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
      int myInt = 8;
      MyMethod(ref myInt);
      //myInt is now 13
    }
</pre>
<p>In the above code the aNumber parameter in MyMethod now has the ref keyword, note that the ref keyword is always required when calling the method.<br />
The integer which is passed to the method now has its value changed by the method.</p>
<h2>Parameter Passing Using the out Keyword</h2>
<p>The out keyword has a similar impact to the ref keyword as the parameters that are passed to it can have their values changed.<br />
The primary difference is that when using the out keyword the objects do not need to be initialized. In the above example of passing by reference there would be a compile-time error if the integer had simply been declared by not initialized with a value as below:</p>
<pre>
 int myInt;
</pre>
<p>If the ref keyword was replaced with the out keyword this issue would be resolved:</p>
<pre>
    void MyMethod(out int aNumber)
    {
        aNumber = 3;
        aNumber += 5;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
      int myInt;
      MyMethod(out myInt);
      //myInt is now 8
	}
</pre>
<p>Note that the integer is now initialized with a value in the method body. </p>
<p>You might wonder what is the point of the out keyword. It can be very useful in situations where you want a method to perform several operations and return multiple values. In this circumstance you can simply declare multiple variables and pass them to void method to have their values assigned.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.csharphelp.com/2011/06/passing-parameters-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C# Custom Number Formatting</title>
		<link>http://www.csharphelp.com/2011/06/c-custom-number-formatting/</link>
		<comments>http://www.csharphelp.com/2011/06/c-custom-number-formatting/#comments</comments>
		<pubDate>Thu, 16 Jun 2011 06:24:34 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C# Language]]></category>
		<category><![CDATA[Formatting]]></category>
		<category><![CDATA[String]]></category>
		<category><![CDATA[String.Format]]></category>

		<guid isPermaLink="false">http://www.csharphelp.com/?p=2631</guid>
		<description><![CDATA[Very often the inbuilt numerical formatting in C# will be insufficent and you will want to apply the custom formatting for your numbers. The String.Format method is very flexible and can be used to apply custom formatting rules. The # character informs the Format method how to format the numerical value, for example to forma [...]]]></description>
			<content:encoded><![CDATA[<p>Very often the inbuilt numerical formatting in C# will be insufficent and you will want to apply the custom formatting for your numbers.</p>
<p>The String.Format method is very flexible and can be used to apply custom formatting rules. The # character informs the Format method how to format the numerical value, for example to forma the number 12000.12 as 12,000.12 use can use the format &#8220;#,#.##&#8221; as in the below code:</p>
<pre>double dbl1 = 12000.12;
string outputStr;
outputStr = string.Format("This is a custom formatting example  {0:#,#.##} ", dbl1);
Response.Write(outputStr);</pre>
<p>In this example the , character is used to denote the thousand separator and the ## after the decimal place denotes that a maximum of 2 numbers should appear after the decimal place. Therefore the double 12000.1 would be formatted as 12,000.1, if you wish two decimals places always to be shown (ie 12,100.10 in this case) you should use the &#8217;0&#8242; character after the decimal place:</p>
<pre>outputStr = string.Format("This is a custom formatting example  {0:#,#.00} ", 12000.1);</pre>
<p>You can also add additional characters before and after the formatting string. Thus to format the value as $ 12,000.12 use the format string &#8220;$ #,#.##&#8221; , or even &#8220;#,#.## US Dollars&#8221; would output &#8220;12,000.12 US Dollars&#8221;.</p>
<p>The % character will multiply the value by 100 and then add % character to the end of the string. Thus for the double value 12 the format string &#8220;{0:0.00 %}&#8221; will output &#8220;1200.00 %&#8221;. Note that you can use the # character in place of the 0 character as per the formatting rules above and so &#8220;#,#.## %&#8221; in this case would output &#8220;1,200 %&#8221;.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.csharphelp.com/2011/06/c-custom-number-formatting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Formatting Numerical Data in C#</title>
		<link>http://www.csharphelp.com/2011/06/formatting-numerical-data-c/</link>
		<comments>http://www.csharphelp.com/2011/06/formatting-numerical-data-c/#comments</comments>
		<pubDate>Tue, 14 Jun 2011 06:23:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C# Language]]></category>
		<category><![CDATA[Format]]></category>
		<category><![CDATA[Numbers]]></category>
		<category><![CDATA[String.Format]]></category>

		<guid isPermaLink="false">http://www.csharphelp.com/?p=2629</guid>
		<description><![CDATA[C# ships with several inbuilt formatting specifies which can be used to quickly format a number, for example the &#8216;c&#8217; specifier will format the number as a currency: double dbl1 = 9999999.9999999; outputStr = string.Format("This is the currency format {0:c}", dbl1); This will output the numerical value as a currency based on the user&#8217;s current [...]]]></description>
			<content:encoded><![CDATA[<p>C# ships with several inbuilt formatting specifies which can be used to quickly format a number, for example the &#8216;c&#8217; specifier will format the number as a currency:</p>
<pre>double dbl1 = 9999999.9999999;
outputStr = string.Format("This is the currency format {0:c}", dbl1);</pre>
<p>This will output the numerical value as a currency based on the user&#8217;s current currency setting. In my case this will output:</p>
<pre>This is the currency format $10,000,000.00</pre>
<p>Note firstly that String.Format is a static method &#8211; ie you can not use it from an instance of a string object so the below code will generate a compile error:</p>
<pre>string outputStr;
outputStr.Format("This is the currency format {0:c}", dbl1);</pre>
<p>You must always use it at the class level (ie <code>String.Format</code>)</p>
<p>Below are the inbuilt formatting specifiers:<br />
c &#8211; Currency<br />
d &#8211; Decimal<br />
e &#8211; Exponential notation<br />
f &#8211; Fixed point formatting<br />
n &#8211; Numerical formatting (with commas)<br />
x &#8211; Hexidecimal</p>
<p>The below code shows an implementation of the n formatting speficier:</p>
<pre>double dbl1 = 9999999.9999999;
string outputStr;
outputStr = string.Format("This is the numerical format {0:n}", dbl1);
Response.Write(outputStr);</pre>
<p>This outputs</p>
<pre>This is the numerical format 10,000,000.00</pre>
<p>Note that there will be compile errors if you attempt to format a double using the decimal format. Also, note that hexidecimal expects an integer type.</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.csharphelp.com/2011/06/formatting-numerical-data-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Strings are Immutable!</title>
		<link>http://www.csharphelp.com/2011/06/strings-are-immutable/</link>
		<comments>http://www.csharphelp.com/2011/06/strings-are-immutable/#comments</comments>
		<pubDate>Thu, 09 Jun 2011 05:29:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C# Language]]></category>
		<category><![CDATA[garbage collection]]></category>
		<category><![CDATA[Garbage Collector]]></category>
		<category><![CDATA[String]]></category>

		<guid isPermaLink="false">http://www.csharphelp.com/?p=2624</guid>
		<description><![CDATA[Working with strings is a common task in most apps but there are several &#8216;gotchas&#8217; that can arise due to the immutable nature of a  String in .NET. Immutable simply means that once created it doesn&#8217;t change. This seems strange at first since the value of a string variable can indeed be altered : string [...]]]></description>
			<content:encoded><![CDATA[<p>Working with strings is a common task in most apps but there are several &#8216;gotchas&#8217; that can arise due to the immutable nature of a  String in .NET.</p>
<p>Immutable simply means that once created it doesn&#8217;t change. This seems strange at first since the value of a string variable can indeed be altered :</p>
<pre>
string s1 = "Initial Value";
s1 = "Updated Value";
Literal1.Text = s1;</pre>
<p>In the above code the Literal will output the text &#8220;Updated Value&#8221; which seems to demonstrate the String s1 has been changed. However, behind the scenes a new object has actually been created by the .NET Framework and the variable s1 has been pointed to that (note the the original string object is still held in memory until the Garbage Collection is automatically run by the .NET Framework and so working with strings can consume a lot of memory if care isn&#8217;t taken).</p>
<p>The impact of strings being immutable is normally felt when manipulating them through the methods provided in the Framework:</p>
<pre>
string s1 = "Initial Value";
s1.ToUpper();
Literal1.Text = s1;</pre>
<p>In the above snippet, we may think calling ToUpper() should convert the string to upper case and so the Literal should output &#8220;INITIAL VALUE&#8221; but instead the output is still &#8220;Initial Value&#8221;. A string is immutable! It cannot be changed, all the method ToUpper() does is return a new string of upper case values for the s1 string. The original string (s1 in this case is left unchanged).<br />
To alter the actual string we will need to reassign its value:</p>
<pre>
string s1 = "Initial Value";
s1 = s1.ToUpper();
Literal1.Text = s1;</pre>
<p>This results in the expected output of uppercase.</p>
<p>Note that the same holds true all the common methods of string manipulation such as Replace(), Remove(), Trim(), Insert().</p>
<p>For example, the below code will leave the original string s1 totally unchanged:</p>
<pre>
string s1 = "Initial xx";
s1.Replace("xx", "Value");
Literal1.Text = s1;</pre>
<p>Instead it needs to be rewritten:</p>
<pre>
string s1 = "Initial xx";
s1 = s1.Replace("xx", "Value");
Literal1.Text = s1;</pre>
<p>Note that it is  more efficient from both a code and memory perspective to leave the original string unaltered and simply output the new string values as required:</p>
<pre>
string s1 = "Initial xx";
Literal1.Text = s1.Replace("xx", "Value");</pre>
<p>Hope this helps clarify a common issue in handling strings in .NET.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.csharphelp.com/2011/06/strings-are-immutable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using the .NET Stopwatch class to Profile Your Code</title>
		<link>http://www.csharphelp.com/2011/02/using-the-net-stopwatch-class-to-profile-your-code/</link>
		<comments>http://www.csharphelp.com/2011/02/using-the-net-stopwatch-class-to-profile-your-code/#comments</comments>
		<pubDate>Tue, 15 Feb 2011 13:37:47 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C# Language]]></category>
		<category><![CDATA[Profiling]]></category>
		<category><![CDATA[Stowatch]]></category>
		<category><![CDATA[System.Diagnostics]]></category>

		<guid isPermaLink="false">http://www.csharphelp.com/?p=2616</guid>
		<description><![CDATA[The Stopwatch class in the System.Diagnostics namespace can be used a as a basic tool to profile blocks of .NET code. System.Diagnostics.Stopwatch timerObj = new System.Diagnostics.Stopwatch(); timer.Start(); Decimal totalDec = 0; int limit = 1000000; for (int i = 0; i &#60; limit; ++i) { totalDec = totalDec + (Decimal)Math.Sqrt(i); } timerObj.Stop(); Console.WriteLine(“Sum of square [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://www.csharphelp.com/2011/01/using-the-stopwatch-class-in-c/">Stopwatch class</a> in the System.Diagnostics namespace can be used a as a basic tool to profile blocks of .NET code.</p>
<pre>System.Diagnostics.Stopwatch timerObj = new System.Diagnostics.Stopwatch();
timer.Start();
Decimal totalDec = 0;
int limit = 1000000;
for (int i = 0; i &lt; limit; ++i)
{
totalDec = totalDec + (Decimal)Math.Sqrt(i);
}
timerObj.Stop();
Console.WriteLine(“Sum of square roots: {0}”,totalDec);
Console.WriteLine(“Milliseconds elapsed : {0}”,
timerObj.ElapsedMilliseconds);
Console.WriteLine(“Time elapsed : {0}”, timerObj.Elapsed);</pre>
<p><em>This outputs the below:</em><br />
Sum of square roots : 666666166.45882210823608<br />
Milliseconds elapsed: 282<br />
Time elapsed : 00:00:00.2828692</p>
<p>When using the Stopwatch for debugging you can to utilize the IDisposable interface to automate  the stopwatch:</p>
<pre>
class AutoStopwatchDemo : System.Diagnostics.Stopwatch, IDisposable
{
public AutoStopwatchDemo()
{
Start();
}
public void Dispose()
{
Stop();
Console.WriteLine(“Elapsed : {0}”, this.Elapsed);
}
}</pre>
<p>Now, you can use {} syntax:</p>
<pre>using (new AutoStopwatchDemo())
{
Decimal totalObj2 = 0;
int limitObj2 = 1000000;
for (int i = 0; i &lt; limit2; ++i)
{
totalObj2 = limitObj2  + (Decimal)Math.Sqrt(i);
}
}</pre>
<p>Besides the Stopwatches Start() and Stop() methods, there is also Reset(), which stops the timer and then sets Elapsed to 0. In addition there is also the Restart() method, which sets Elapsed to 0 but the timer continues to run.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.csharphelp.com/2011/02/using-the-net-stopwatch-class-to-profile-your-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ensure a .NET App Works in Both 32 bit and 64 bit Environments</title>
		<link>http://www.csharphelp.com/2011/02/ensure-a-net-app-works-in-both-32-bit-and-64-bit-envirnoments/</link>
		<comments>http://www.csharphelp.com/2011/02/ensure-a-net-app-works-in-both-32-bit-and-64-bit-envirnoments/#comments</comments>
		<pubDate>Thu, 03 Feb 2011 12:40:12 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C# Language]]></category>
		<category><![CDATA[32 bit]]></category>
		<category><![CDATA[64 bit]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://www.csharphelp.com/?p=2610</guid>
		<description><![CDATA[With 64 bit versions of Windows fast becoming the standard it is imperative that an app work in both 32 bit and 64 bit environments. This done using a basic configuration setting, with a few caveats of course. In Visual Studio in the project’s build options you can select the app&#8217;s Platform target, which can [...]]]></description>
			<content:encoded><![CDATA[<p>With 64 bit versions of Windows fast becoming the standard it is imperative that an app work in both 32 bit and 64 bit environments. This done using a basic configuration setting, with a few caveats of course. In Visual Studio in the project’s build options you can select the app&#8217;s Platform target, which can be x86, x64, Itanium, or Any CPU (the listing you see may vary, depending on what you have installed with Visual Studio).</p>
<p>Unless there is a good reason not to, you should always select <strong>Any CPU</strong> see below:</p>
<p><img class="alignnone size-full wp-image-2614" title="32 bit or 64 bit targeting" src="http://www.csharphelp.com/wp-content/uploads/2011/01/ScreenHunter_02-Jan.-18-20.51.gif" alt="32 bit or 64 bit targeting" width="611" height="387" /></p>
<p>Since .NET assemblies consist of byte code that is  JIT (just-in-time) compiled to the current platform at runtime, an app&#8217;s assemblies will function on any architecture without needing to rebuild from the source code.</p>
<p>However, the choice is not always that simple. If your .NET assembly references or interops with another managed assembly or an unmanaged DLL which was compiled for a certain architecture, then your app&#8217;s .NET assembly will need to match the assembly or dll&#8217;s.</p>
<p>Take for example a scenario where you  have the following:</p>
<ul>
<li>.NET assembly: Any CPU</li>
<li>32-bit COM component</li>
</ul>
<p>On a 32-bit OS this scenario will work fine since the .NET assembly will be JIT  compiled as 32 bits. However, this will fail on a 64-bit OS since the .NET assembly will be 64 bits and when it attempts to call into the COM component the app will break. In such a scenario, it best to specify that the assembly should always be 32 bit.</p>
<p>Note that x64 OSs  run 32-bit processes just fine. Also bear in  mind  that while .NET itself mostly insulates you from platform architecture considerations, when you using unsafe code, pointers, or relying on the size of IntPtr, you will likely encounter issues when running on multiple architectures.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.csharphelp.com/2011/02/ensure-a-net-app-works-in-both-32-bit-and-64-bit-envirnoments/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Call C++ Functions in a DLL using C#</title>
		<link>http://www.csharphelp.com/2011/01/call-c-functions-in-dll-using-c/</link>
		<comments>http://www.csharphelp.com/2011/01/call-c-functions-in-dll-using-c/#comments</comments>
		<pubDate>Mon, 31 Jan 2011 11:53:17 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C# Language]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[P/Invoke]]></category>

		<guid isPermaLink="false">http://www.csharphelp.com/?p=2607</guid>
		<description><![CDATA[P/Invoke is an efficient method for calling native code functions in an unmanaged DLL. The below code samples show the source code a native code library which defines a very simple C++ function which accepts a char* argument. MyLib.h __declspec(dllexport) int Hello(char* pszBuffer, int nLengthObj); MyLib.cpp #include “stdafx.h” #include “MyLib.h” #include __declspec(dllexport) int SayHello(char* pszBuffer, [...]]]></description>
			<content:encoded><![CDATA[<p>P/Invoke is an efficient method for calling native code functions in an unmanaged DLL.   The below code samples show the source code  a native code library which defines a very simple C++ function which accepts a char* argument.<br />
MyLib.h</p>
<pre>__declspec(dllexport) int Hello(char* pszBuffer, int nLengthObj);</pre>
<p>MyLib.cpp</p>
<pre>#include “stdafx.h”
#include “MyLib.h”
#include
__declspec(dllexport) int SayHello(char* pszBuffer, int nLengthObj)
{
::strcpy_s(pszBuffer, nLength, “Hello, from the C++ DLL”);
return strlen(pszBuffer);
}
LIBRARY “MyLib”
EXPORTS
Hello</pre>
<p>From  C#, calling this   function is relatively simple. The below code shows this as well as when calling a method that takes char* you first need to convert the string to bytes.</p>
<pre>[DllImport(“MyLib.dll”, ExactSpelling=false,
CallingConvention=CallingConvention.Cdecl, EntryPoint=”Hello”)]
public static extern int Hello(
[MarshalAs(UnmanagedType.LPArray)] byte[] buffer,int length);
static void Main(string[] args)
{
int size = 32;
//we need to manually marshal the bytes to a String
byte[] buffer = new byte[size];
int returnVal = Hello(buffer, size);
string result = Encoding.ASCII.GetString(buffer,0, returnVal);
Console.WriteLine(“”{0}”, return value: {1}”, result, returnVal);
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.csharphelp.com/2011/01/call-c-functions-in-dll-using-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Measure Memory Usage of .NET Applications</title>
		<link>http://www.csharphelp.com/2011/01/measure-memory-usage-of-net-applications/</link>
		<comments>http://www.csharphelp.com/2011/01/measure-memory-usage-of-net-applications/#comments</comments>
		<pubDate>Mon, 31 Jan 2011 11:09:03 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C# Language]]></category>
		<category><![CDATA[Garbage Collector]]></category>
		<category><![CDATA[GC]]></category>
		<category><![CDATA[Memory]]></category>
		<category><![CDATA[Peformance]]></category>
		<category><![CDATA[System.Diagnostics]]></category>

		<guid isPermaLink="false">http://www.csharphelp.com/?p=2598</guid>
		<description><![CDATA[There are two main methods for measuring the memory usage of a .NET application, using the GC class or using System.Diagnostics Retrieve Your App&#8217;s Memory Utilization using the GC class The .NET Framework&#8217;s GC class contains many useful memory-related methods, including GetTotalMemory(), which returns the amount of memory the garbage collector believes is allocated to [...]]]></description>
			<content:encoded><![CDATA[<p>There are two main methods for measuring the memory usage of a .NET application, using the GC class or using System.Diagnostics</p>
<h2>Retrieve Your App&#8217;s Memory Utilization using the GC class</h2>
<p>The .NET Framework&#8217;s GC class contains many useful memory-related methods, including GetTotalMemory(), which returns the amount of memory the garbage collector believes is allocated to your app. The number  not be exact right since some objects may not have been garbage collected yet. This, however does have the advantage of being able to inform you of the amount of memory a certain part of your app uses, as opposed to the entire process:</p>
<pre>long memAvailable = GC.GetTotalMemory(false);
Console.WriteLine(“Before allocations: {0:N0}”, memAvailable);

int allocSizeObj = 40000000;
byte[] bigArrayObj = new byte[allocSizeObj];

memAvailable= GC.GetTotalMemory(false);
Console.WriteLine(“After allocations: {0:N0}”, memAvailable);</pre>
<p>The above code will output the following:</p>
<pre>Before allocations: 651,064
After allocations: 40,690,080</pre>
<h2>Retrieve Your App&#8217;s Memory Utilization using System.Diagnostics</h2>
<p>You can also request the OS report on info about your process using the Process class  in the System.Diagnostics namespace:</p>
<pre>
Process procObj = Process.GetCurrentProcess();
Console.WriteLine(“Process Info: “+Environment.NewLine+
“App's Private Memory Size : {0:N0}”+Environment.NewLine +
“App's Virtual Memory Size : {1:N0}” + Environment.NewLine +
“App's Working Set Size: {2:N0}” + Environment.NewLine +
“App's Paged Memory Size: {3:N0}” + Environment.NewLine +
“App's Paged System Memory Size: {4:N0}” + Environment.NewLine +
“App's Non-paged System Memory Size: {5:N0}” + Environment.NewLine,
procObj.PrivateMemorySize64,
procObj.VirtualMemorySize64,
procObj.WorkingSet64,
procObj.PagedMemorySize64,
procObj.PagedSystemMemorySize64,
procObj.NonpagedSystemMemorySize64 );
</pre>
<p>Here’s the output:</p>
<pre>
Process Info:
Private Memory Size: 75,935,744
Virtual Memory Size: 590,348,288
Working Set Size: 29,364,224
Paged Memory Size: 75,935,744
Paged System Memory Size: 317,152
Non-paged System Memory Size: 37,388
</pre>
<p>These numbers are not necessarily intuitive, and you can consult a good operating system book, such as Windows Internals from Microsoft Press for more on how virtual memory works. Performance counters can also be used to track this as well as other info on your app or its environment.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.csharphelp.com/2011/01/measure-memory-usage-of-net-applications/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C# Dynamic Binding &#8211; Language Binding</title>
		<link>http://www.csharphelp.com/2011/01/c-dynamic-binding-language-binding/</link>
		<comments>http://www.csharphelp.com/2011/01/c-dynamic-binding-language-binding/#comments</comments>
		<pubDate>Thu, 27 Jan 2011 11:25:48 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C# Language]]></category>
		<category><![CDATA[DLR]]></category>
		<category><![CDATA[Dynamic]]></category>
		<category><![CDATA[Dynamic Binding]]></category>
		<category><![CDATA[Language Binding]]></category>

		<guid isPermaLink="false">http://www.csharphelp.com/?p=2522</guid>
		<description><![CDATA[Language binding is a form of dynamic binding which occurs when a dynamic object does not implement IDynamicMetaObjectProvider. Language binding comes in handy when working around imperfectly designed types or the inherent limitations in the .NET type system. A common problem is that when using numeric types is they have no common interface. Using dynamic [...]]]></description>
			<content:encoded><![CDATA[<p>Language binding is a form of dynamic binding which occurs when a dynamic object does not implement IDynamicMetaObjectProvider. Language binding comes in handy when working around imperfectly designed types or the inherent limitations in the .NET type system. A common problem is that when using numeric types is they have no common interface. Using dynamic binding, methods can be bound dynamically and the same also applies for operators:</p>
<pre>
static dynamic Mean (dynamic xObj, dynamic yObj)
{
return (xObj + yObj) / 2;
}
static void Main()
{
int xObj = 3, yObj = 4;
Console.WriteLine (Mean (xObj, yObj));
}</pre>
<p>The benefit here is clear — you can avoid duplicating code for each numeric type. However, in doing so you will lose static type safety, risking runtime exceptions as opposed to getting compile time errors.<br />
Note that dynamic binding only circumvents static type safety, runtime type safety is still perserved. In contrast when using reflection, you cannot circumvent member accessibility rules using dynamic binding.</p>
<p>Language runtime binding behaves very similarly to static binding when the  runtime types of the dynamic objects are known at compile time. In the above example, the behavior of the  program would be identical if Mean was hardcoded  to work with the int type.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.csharphelp.com/2011/01/c-dynamic-binding-language-binding/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

