<?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>xiaoma de blog</title>
	<atom:link href="http://www.floatingvectors.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.floatingvectors.com/blog</link>
	<description></description>
	<lastBuildDate>Thu, 03 Jun 2010 15:45:14 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>WPF path animation as opacity mask</title>
		<link>http://www.floatingvectors.com/blog/357/</link>
		<comments>http://www.floatingvectors.com/blog/357/#comments</comments>
		<pubDate>Thu, 03 Jun 2010 15:45:14 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[软件[Code]]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.floatingvectors.com/blog/?p=357</guid>
		<description><![CDATA[CODE
&#60;Window x:Class=&#8221;WpfApplication1.MainWindow&#8221;
       xmlns=&#8221;http://schemas.microsoft.com/winfx/2006/xaml/presentation&#8221;
       xmlns:x=&#8221;http://schemas.microsoft.com/winfx/2006/xaml&#8221;
       x:Name=&#8221;Window&#8221;
       Title=&#8221;Path Opacity Mask Animation Demo&#8221;
       Width=&#8221;640&#8243; Height=&#8221;480&#8243; xmlns:System=&#8221;clr-namespace:System;assembly=mscorlib&#8221;&#62;
       &#60;Window.Resources&#62;
              &#60;Storyboard x:Key=&#8221;Timeline1&#8243;&#62;
                     &#60;DoubleAnimationUsingKeyFrames BeginTime=&#8221;00:00:00&#8243; Storyboard.TargetName=&#8221;path1&#8243;      Storyboard.TargetProperty=&#8221;(Shape.StrokeDashOffset)&#8221;&#62;
                           &#60;SplineDoubleKeyFrame KeyTime=&#8221;00:00:01&#8243; Value=&#8221;25&#8243;/&#62;
                           &#60;SplineDoubleKeyFrame KeyTime=&#8221;00:00:02&#8243; Value=&#8221;0&#8243;/&#62;
                     &#60;/DoubleAnimationUsingKeyFrames&#62;
                     &#60;DoubleAnimationUsingKeyFrames BeginTime=&#8221;00:00:00&#8243; Storyboard.TargetName=&#8221;path2&#8243;  Storyboard.TargetProperty=&#8221;(Shape.StrokeDashOffset)&#8221;&#62;
                           &#60;SplineDoubleKeyFrame KeyTime=&#8221;00:00:03&#8243; Value=&#8221;25&#8243;/&#62;
                           &#60;SplineDoubleKeyFrame KeyTime=&#8221;00:00:04&#8243; Value=&#8221;0&#8243;/&#62;
                     &#60;/DoubleAnimationUsingKeyFrames&#62;
    &#60;/Storyboard&#62;
       &#60;/Window.Resources&#62;
       &#60;Window.Triggers&#62;
              &#60;EventTrigger RoutedEvent=&#8221;FrameworkElement.Loaded&#8221;&#62;
                     &#60;BeginStoryboard Storyboard=&#8221;{StaticResource Timeline1}&#8221;/&#62;
              &#60;/EventTrigger&#62;
       &#60;/Window.Triggers&#62;
       &#60;Grid x:Name=&#8221;LayoutRoot&#8221;&#62;
              &#60;Image Source=&#8221;Hydrangeas.jpg&#8221; Stretch=&#8221;Fill&#8221;&#62;
      &#60;Image.OpacityMask&#62;
    &#60;VisualBrush&#62;
             &#60;VisualBrush.Visual&#62;
               &#60;Path [...]]]></description>
			<content:encoded><![CDATA[<p>CODE</p>
<p>&lt;Window x:Class=&#8221;WpfApplication1.MainWindow&#8221;<br />
       xmlns=&#8221;<a href="http://schemas.microsoft.com/winfx/2006/xaml/presentation">http://schemas.microsoft.com/winfx/2006/xaml/presentation</a>&#8221;<br />
       xmlns:x=&#8221;<a href="http://schemas.microsoft.com/winfx/2006/xaml">http://schemas.microsoft.com/winfx/2006/xaml</a>&#8221;<br />
       x:Name=&#8221;Window&#8221;<br />
       Title=&#8221;Path Opacity Mask Animation Demo&#8221;<br />
       Width=&#8221;640&#8243; Height=&#8221;480&#8243; xmlns:System=&#8221;clr-namespace:System;assembly=mscorlib&#8221;&gt;</p>
<p>       &lt;Window.Resources&gt;<br />
              &lt;Storyboard x:Key=&#8221;Timeline1&#8243;&gt;<br />
                     &lt;DoubleAnimationUsingKeyFrames BeginTime=&#8221;00:00:00&#8243; Storyboard.TargetName=&#8221;path1&#8243;      Storyboard.TargetProperty=&#8221;(Shape.StrokeDashOffset)&#8221;&gt;<br />
                           &lt;SplineDoubleKeyFrame KeyTime=&#8221;00:00:01&#8243; Value=&#8221;25&#8243;/&gt;<br />
                           &lt;SplineDoubleKeyFrame KeyTime=&#8221;00:00:02&#8243; Value=&#8221;0&#8243;/&gt;<br />
                     &lt;/DoubleAnimationUsingKeyFrames&gt;<br />
                     &lt;DoubleAnimationUsingKeyFrames BeginTime=&#8221;00:00:00&#8243; Storyboard.TargetName=&#8221;path2&#8243;  Storyboard.TargetProperty=&#8221;(Shape.StrokeDashOffset)&#8221;&gt;<br />
                           &lt;SplineDoubleKeyFrame KeyTime=&#8221;00:00:03&#8243; Value=&#8221;25&#8243;/&gt;<br />
                           &lt;SplineDoubleKeyFrame KeyTime=&#8221;00:00:04&#8243; Value=&#8221;0&#8243;/&gt;<br />
                     &lt;/DoubleAnimationUsingKeyFrames&gt;<br />
    &lt;/Storyboard&gt;<br />
       &lt;/Window.Resources&gt;<br />
       &lt;Window.Triggers&gt;<br />
              &lt;EventTrigger RoutedEvent=&#8221;FrameworkElement.Loaded&#8221;&gt;<br />
                     &lt;BeginStoryboard Storyboard=&#8221;{StaticResource Timeline1}&#8221;/&gt;<br />
              &lt;/EventTrigger&gt;<br />
       &lt;/Window.Triggers&gt;</p>
<p>       &lt;Grid x:Name=&#8221;LayoutRoot&#8221;&gt;<br />
              &lt;Image Source=&#8221;Hydrangeas.jpg&#8221; Stretch=&#8221;Fill&#8221;&gt;<br />
      &lt;Image.OpacityMask&gt;<br />
    &lt;VisualBrush&gt;<br />
             &lt;VisualBrush.Visual&gt;<br />
               &lt;Path x:Name=&#8221;path1&#8243; Stretch=&#8221;Fill&#8221; Stroke=&#8221;#FF000000&#8243; StrokeDashOffset=&#8221;1000&#8243; StrokeEndLineCap=&#8221;Round&#8221; StrokeStartLineCap=&#8221;Round&#8221; StrokeThickness=&#8221;20&#8243; HorizontalAlignment=&#8221;Left&#8221; Margin=&#8221;228,145.336,0,188.972&#8243; Width=&#8221;71&#8243; Data=&#8221;M294,171 C289,162 283,147 268,151 253,155 244,164.99992 244,177.00008 244,189.00024 284,196.00033 287,207.00047 290,218.00062 286,249.00103 259,250.00104 232,251.00105 235,225.00071 233,219.00063&#8243; StrokeDashArray=&#8221;25 1000&#8243;/&gt;<br />
             &lt;/VisualBrush.Visual&gt;<br />
           &lt;/VisualBrush&gt;<br />
    &lt;/Image.OpacityMask&gt;<br />
     &lt;/Image&gt;<br />
    <br />
    &lt;Path x:Name=&#8221;path2&#8243; Stretch=&#8221;Fill&#8221; Stroke=&#8221;#FF000000&#8243; StrokeDashOffset=&#8221;1000&#8243; StrokeEndLineCap=&#8221;Round&#8221; StrokeStartLineCap=&#8221;Round&#8221; StrokeThickness=&#8221;20&#8243; HorizontalAlignment=&#8221;Left&#8221; Margin=&#8221;228,145.336,0,188.972&#8243; Width=&#8221;71&#8243; Data=&#8221;M294,171 C289,162 283,147 268,151 253,155 244,164.99992 244,177.00008 244,189.00024 284,196.00033 287,207.00047 290,218.00062 286,249.00103 259,250.00104 232,251.00105 235,225.00071 233,219.00063&#8243; StrokeDashArray=&#8221;25 1000&#8243;/&gt;<br />
          <br />
  &lt;/Grid&gt;<br />
&lt;/Window&gt;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.floatingvectors.com/blog/357/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Heatsink Calculation</title>
		<link>http://www.floatingvectors.com/blog/353/</link>
		<comments>http://www.floatingvectors.com/blog/353/#comments</comments>
		<pubDate>Sun, 14 Mar 2010 11:15:38 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[电子[Electronics]]]></category>

		<guid isPermaLink="false">http://www.floatingvectors.com/blog/?p=353</guid>
		<description><![CDATA[When design power electronics, to prevent devices from overheating and damage, heatsinks are commonly used. How to calculate heatsinks is thus an issue.
The basic theory is a thermal equivalent of Ohm’s Law. For a power transistor:
T(j-a) = Pd x Rth(j-a)
T(j-a) (in degrees Celsius) is the temperature rise of the transistor above ‘ambient ’ temperature.
Pd (in [...]]]></description>
			<content:encoded><![CDATA[<p>When design power electronics, to prevent devices from overheating and damage, heatsinks are commonly used. How to calculate heatsinks is thus an issue.</p>
<p>The basic theory is a thermal equivalent of Ohm’s Law. For a power transistor:</p>
<p>T(j-a) = Pd x Rth(j-a)</p>
<p>T(j-a) (in degrees Celsius) is the temperature rise of the transistor above ‘ambient ’ temperature.</p>
<p>Pd (in watts)is the power disspipated.</p>
<p>Rth(j-a) (in degress Celsius per watt)is the total thermal resistance between the junction and the ambient.</p>
<p>A design example:</p>
<p><a href="http://www.floatingvectors.com/blog/wp-content/uploads/2010/03/heatsink_selection.gif"><img class="aligncenter size-full wp-image-354" title="heatsink_selection" src="http://www.floatingvectors.com/blog/wp-content/uploads/2010/03/heatsink_selection.gif" alt="" width="640" height="400" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.floatingvectors.com/blog/353/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Unity3D with Windows 7 Multitouch</title>
		<link>http://www.floatingvectors.com/blog/331/</link>
		<comments>http://www.floatingvectors.com/blog/331/#comments</comments>
		<pubDate>Fri, 12 Feb 2010 07:08:41 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[软件[Code]]]></category>
		<category><![CDATA[Multitouch]]></category>
		<category><![CDATA[Unity]]></category>
		<category><![CDATA[Windows 7]]></category>

		<guid isPermaLink="false">http://www.floatingvectors.com/blog/?p=331</guid>
		<description><![CDATA[[There is a video that cannot be displayed in this feed. Visit the blog entry to see the video.]
Here it goes. Build Unity 3D multitouch application on Windows 7 machines.
This video demonstrates an application built with Unity 3D with Windowns 7 multitouch input.
Thanks to Unity explosion framework Detonator. Nice work!
]]></description>
			<content:encoded><![CDATA[<p>[There is a video that cannot be displayed in this feed. <a href="http://www.floatingvectors.com/blog/331/">Visit the blog entry to see the video.]</a></p>
<p>Here it goes. Build Unity 3D multitouch application on Windows 7 machines.</p>
<p>This video demonstrates an application built with Unity 3D with Windowns 7 multitouch input.</p>
<p>Thanks to Unity explosion framework <a href="http://unity3d.com/support/resources/unity-extensions/explosion-framework.html">Detonator</a>. Nice work!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.floatingvectors.com/blog/331/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="http://www.floatingvectors.com/blog/wp-content/uploads/2010/02/UnityWindows7Multitouch.mov" length="3904385" type="video/quicktime" />
		</item>
		<item>
		<title>Unity 3D First Try</title>
		<link>http://www.floatingvectors.com/blog/316/</link>
		<comments>http://www.floatingvectors.com/blog/316/#comments</comments>
		<pubDate>Wed, 10 Feb 2010 02:51:38 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[软件[Code]]]></category>
		<category><![CDATA[Unity]]></category>

		<guid isPermaLink="false">http://www.floatingvectors.com/blog/?p=316</guid>
		<description><![CDATA[Please view the full post to see the Unity content.
This is part of a tutorial in  the book &#8220;Unity Game Development Essentials&#8221;
Use mouse and keyboard to control walk through.
Up/W: Forward
Down/S: Backward
Left/A: Left
Right/D: Right
Mouse: Look around and turn the player
]]></description>
			<content:encoded><![CDATA[<p><em>Please view the full post to see the Unity content.</em></p>
<p>This is part of a tutorial in  the book &#8220;Unity Game Development Essentials&#8221;</p>
<p>Use mouse and keyboard to control walk through.</p>
<p>Up/W: Forward</p>
<p>Down/S: Backward</p>
<p>Left/A: Left</p>
<p>Right/D: Right</p>
<p>Mouse: Look around and turn the player</p>
]]></content:encoded>
			<wfw:commentRss>http://www.floatingvectors.com/blog/316/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Motor Control Tutorial(Part I)</title>
		<link>http://www.floatingvectors.com/blog/282/</link>
		<comments>http://www.floatingvectors.com/blog/282/#comments</comments>
		<pubDate>Sun, 31 Jan 2010 12:40:30 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[电子[Electronics]]]></category>
		<category><![CDATA[Motor control]]></category>

		<guid isPermaLink="false">http://www.floatingvectors.com/blog/?p=282</guid>
		<description><![CDATA[Motor Control Tutorial (Part I)
 
After working on several motor control projects, I decided to write this article about PID controller. This article will tell how to implement control loops (PID loops) in software. The hardware part will come soon.
PID Controller
A PID (proportional-integral-derivative) controller is a generic controller widely used in control systems. It calculates the [...]]]></description>
			<content:encoded><![CDATA[<h1>Motor Control Tutorial (Part I)</h1>
<p> </p>
<p>After working on several motor control projects, I decided to write this article about PID controller. This article will tell how to implement control loops (PID loops) in software. The hardware part will come soon.</p>
<p><strong>PID Controller</strong></p>
<p>A PID (proportional-integral-derivative) controller is a generic controller widely used in control systems. It calculates the difference value between a given set-point and measured value and attempts to minimize the difference.</p>
<p>The terminology “PID” tells that it includes three terms. Each term has a different effect on the controlled system. Figure 1 shows a block diagram of a basic PID controller.</p>
<p><a href="http://www.floatingvectors.com/blog/wp-content/uploads/2010/01/basic_pid.png"><img class="aligncenter size-full wp-image-283" title="basic_pid" src="http://www.floatingvectors.com/blog/wp-content/uploads/2010/01/basic_pid.png" alt="" width="446" height="236" /></a> </p>
<p style="text-align: center;">Figure 1 Basic PID Controller</p>
<p>The standard mathematic form is</p>
<p><a href="http://www.floatingvectors.com/blog/wp-content/uploads/2010/01/pid_math.png"><img class="aligncenter size-full wp-image-284" title="pid_math" src="http://www.floatingvectors.com/blog/wp-content/uploads/2010/01/pid_math.png" alt="" width="416" height="97" /></a></p>
<p>The three terms are</p>
<p><a href="http://www.floatingvectors.com/blog/wp-content/uploads/2010/01/pid_terms.png"><img class="aligncenter size-full wp-image-285" title="pid_terms" src="http://www.floatingvectors.com/blog/wp-content/uploads/2010/01/pid_terms.png" alt="" width="416" height="109" /></a></p>
<p>Implementation of a PID controller in a MCU chip requires a discrete form however.</p>
<p><a href="http://www.floatingvectors.com/blog/wp-content/uploads/2010/01/pid_discrete.png"><img class="aligncenter size-full wp-image-286" title="pid_discrete" src="http://www.floatingvectors.com/blog/wp-content/uploads/2010/01/pid_discrete.png" alt="" width="416" height="187" /></a></p>
<p>Where</p>
<p> T is the sample period (sec).</p>
<p> Ti is the integral period (sec).</p>
<p> Td is the derivative period (sec).</p>
<p>Then the three terms finally become</p>
<p style="text-align: center;"><a href="http://www.floatingvectors.com/blog/wp-content/uploads/2010/01/pid_final.png"></a><a href="http://www.floatingvectors.com/blog/wp-content/uploads/2010/01/pid_final1.png"><img class="aligncenter size-full wp-image-312" title="pid_final" src="http://www.floatingvectors.com/blog/wp-content/uploads/2010/01/pid_final1.png" alt="" width="416" height="124" /></a></p>
<p>Integrator Anti-windup</p>
<p>There are limitations in actuators. For a system that operates over a wide range of conditions, it may happen that the control variable reaches the actuator limits. When this happens, the feedback loop is broken. The integral term will build up since the error is nonzero. The controller output will become very large. When the control input changes, it may take a long time before the integrator and the controller output come back inside the saturation range. To avoid this situation, some anti-windup correction is need.</p>
<p>Below is one way to implement anti-windup in a conventional PID controller.</p>
<p><a href="http://www.floatingvectors.com/blog/wp-content/uploads/2010/01/pid_antiwindup.png"><img class="aligncenter size-full wp-image-313" title="pid_antiwindup" src="http://www.floatingvectors.com/blog/wp-content/uploads/2010/01/pid_antiwindup.png" alt="" width="558" height="192" /></a></p>
<p>If the actuator is saturated, the error will decrease integrator and thus prevent windup.</p>
<p><a href="http://www.floatingvectors.com/blog/wp-content/uploads/2010/01/pid_antiwindup_final.png"><img class="aligncenter size-full wp-image-314" title="pid_antiwindup_final" src="http://www.floatingvectors.com/blog/wp-content/uploads/2010/01/pid_antiwindup_final.png" alt="" width="416" height="156" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.floatingvectors.com/blog/282/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>C# Passing Parameters</title>
		<link>http://www.floatingvectors.com/blog/276/</link>
		<comments>http://www.floatingvectors.com/blog/276/#comments</comments>
		<pubDate>Sun, 31 Jan 2010 12:01:42 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[软件[Code]]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.floatingvectors.com/blog/?p=276</guid>
		<description><![CDATA[C# Passing Parameters

Passing Value-Type Parameters
A value-type variable contains its data directly as opposed to a reference-type variable, which contains a reference to its data. Therefore, passing a value-type variable to a method means passing a copy of the variable to the method. Any changes to the parameter that take place inside the method have no [...]]]></description>
			<content:encoded><![CDATA[<h1>C# Passing Parameters</h1>
<p><strong></strong></p>
<p><strong>Passing Value-Type Parameters</strong></p>
<p>A value-type variable contains its data directly as opposed to a reference-type variable, which contains a reference to its data. Therefore, passing a value-type variable to a method means passing a copy of the variable to the method. Any changes to the parameter that take place inside the method have no affect on the original data stored in the variable. If you want the called method to change the value of the parameter, you have to pass it by reference, using the ref or out keyword.</p>
<p>The following example demonstrates passing value-type parameters by value. The variable n is passed by value to the method SquareIt. Any changes that take place inside the method have no affect on the original value of the variable.</p>
<pre class="brush: c#">

class PassingValByVal
{
    static void SquareIt(int x)
    // The parameter x is passed by value.
    // Changes to x will not affect the original value of x.
    {
        x *= x;
        System.Console.WriteLine(&quot;The value inside the method: {0}&quot;, x);
    }
    static void Main()
    {
        int n = 5;
        System.Console.WriteLine(&quot;The value before calling the method: {0}&quot;, n);

        SquareIt(n);  // Passing the variable by value.
        System.Console.WriteLine(&quot;The value after calling the method: {0}&quot;, n);

        // Keep the console window open in debug mode.
        System.Console.WriteLine(&quot;Press any key to exit.&quot;);
        System.Console.ReadKey();
    }
}
/* Output:
    The value before calling the method: 5
    The value inside the method: 25
    The value after calling the method: 5
*/
</pre>
<p>The variable n, being a value type, contains its data, the value 5. When SquareIt is invoked, the contents of n are copied into the parameter x, which is squared inside the method. In Main, however, the value of n is the same, before and after calling the SquareIt method. In fact, the change that takes place inside the method only affects the local variable x.</p>
<p>The following example is the same as the previous example, except for passing the parameter using the <strong>ref</strong> keyword. The value of the parameter is changed after calling the method.</p>
<pre class="brush: c#">

class PassingValByRef
{
    static void SquareIt(ref int x)
    // The parameter x is passed by reference.
    // Changes to x will affect the original value of x.
    {
        x *= x;
        System.Console.WriteLine(&quot;The value inside the method: {0}&quot;, x);
    }
    static void Main()
    {
        int n = 5;
        System.Console.WriteLine(&quot;The value before calling the method: {0}&quot;, n);

        SquareIt(ref n);  // Passing the variable by reference.
        System.Console.WriteLine(&quot;The value after calling the method: {0}&quot;, n);

        // Keep the console window open in debug mode.
        System.Console.WriteLine(&quot;Press any key to exit.&quot;);
        System.Console.ReadKey();
    }
}
/* Output:
    The value before calling the method: 5
    The value inside the method: 25
    The value after calling the method: 25
*/
</pre>
<p><strong>Passing Reference-Type Parameters</strong></p>
<p>A variable of a reference type does not contain its data directly; it contains a reference to its data. When you pass a reference-type parameter by value, it is possible to change the data pointed to by the reference, such as the value of a class member. However, you cannot change the value of the reference itself; that is, you cannot use the same reference to allocate memory for a new class and have it persist outside the block. To do that, pass the parameter using the ref or out keyword.</p>
<p>The following example demonstrates passing a reference-type parameter, arr, by value, to a method, Change. Because the parameter is a reference to arr, it is possible to change the values of the array elements. However, the attempt to reassign the parameter to a different memory location only works inside the method and does not affect the original variable, arr.</p>
<pre class="brush: c#">

class PassingRefByVal
{
    static void Change(int[] pArray)
    {
        pArray[0] = 888;  // This change affects the original element.
        pArray = new int[5] {-3, -1, -2, -3, -4};   // This change is local.
        System.Console.WriteLine(&quot;Inside the method, the first element is: {0}&quot;, pArray[0]);
    }

    static void Main()
    {
        int[] arr = {1, 4, 5};
        System.Console.WriteLine(&quot;Inside Main, before calling the method, the first element is: {0}&quot;, arr [0]);

        Change(arr);
        System.Console.WriteLine(&quot;Inside Main, after calling the method, the first element is: {0}&quot;, arr [0]);
    }
}
/* Output:
    Inside Main, before calling the method, the first element is: 1
    Inside the method, the first element is: -3
    Inside Main, after calling the method, the first element is: 888
*/
</pre>
<p>In the preceding example, the array, arr, which is a reference type, is passed to the method without the <em>ref</em> parameter. In such a case, a copy of the reference, which points to arr, is passed to the method. The output shows that it is possible for the method to change the contents of an array element, in this case from 1 to 888. However, allocating a new portion of memory by using the new operator inside the Change method makes the variable pArray reference a new array. Thus, any changes after that will not affect the original array, arr, which is created inside Main. In fact, two arrays are created in this example, one inside Main and one inside the Change method.</p>
<p>This example is the same as the previous example, except for using the <strong>ref</strong> keyword in the method header and call. Any changes that take place in the method will affect the original variables in the calling program.</p>
<pre class="brush: c#">

class PassingRefByRef
{
    static void Change(ref int[] pArray)
    {
        // Both of the following changes will affect the original variables:
        pArray[0] = 888;
        pArray = new int[5] {-3, -1, -2, -3, -4};
        System.Console.WriteLine(&quot;Inside the method, the first element is: {0}&quot;, pArray[0]);
    }

    static void Main()
    {
        int[] arr = {1, 4, 5};
        System.Console.WriteLine(&quot;Inside Main, before calling the method, the first element is: {0}&quot;, arr[0]);

        Change(ref arr);
        System.Console.WriteLine(&quot;Inside Main, after calling the method, the first element is: {0}&quot;, arr[0]);
    }
}
/* Output:
    Inside Main, before calling the method, the first element is: 1
    Inside the method, the first element is: -3
    Inside Main, after calling the method, the first element is: -3
*/
</pre>
<p>All of the changes that take place inside the method affect the original array in Main. In fact, the original array is reallocated using the <strong>new</strong> operator. Thus, after calling the Change method, any reference to arr points to the five-element array, which is created in the Change method.</p>
<p>Swapping strings is a good example of passing reference-type parameters by reference. In the example, two strings, str1 and str2, are initialized in Main and passed to the SwapStrings method as parameters modified by the <strong>ref</strong> keyword. The two strings are swapped inside the method and inside Main as well.</p>
<pre class="brush: c#">

class SwappingStrings
 {
     static void SwapStrings(ref string s1, ref string s2)
     // The string parameter is passed by reference.
     // Any changes on parameters will affect the original variables.
     {
         string temp = s1;
         s1 = s2;
         s2 = temp;
         System.Console.WriteLine(&quot;Inside the method: {0} {1}&quot;, s1, s2);
     }

     static void Main()
     {
         string str1 = &quot;John&quot;;
         string str2 = &quot;Smith&quot;;
         System.Console.WriteLine(&quot;Inside Main, before swapping: {0} {1}&quot;, str1, str2);

         SwapStrings(ref str1, ref str2);   // Passing strings by reference
         System.Console.WriteLine(&quot;Inside Main, after swapping: {0} {1}&quot;, str1, str2);
     }
 }
 /* Output:
     Inside Main, before swapping: John Smith
     Inside the method: Smith John
     Inside Main, after swapping: Smith John
*/
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.floatingvectors.com/blog/276/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>C# Boxing and Unboxing</title>
		<link>http://www.floatingvectors.com/blog/256/</link>
		<comments>http://www.floatingvectors.com/blog/256/#comments</comments>
		<pubDate>Sun, 31 Jan 2010 10:33:26 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[软件[Code]]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.floatingvectors.com/blog/?p=256</guid>
		<description><![CDATA[C# Boxing and Unboxing
 
Boxing is the process of converting a value type to the type object or to any interface type implemented by this value type. When the CLR boxes a value type, it wraps the value inside a System.Object and stores it on the managed heap. Unboxing extracts the value type from the object.
In [...]]]></description>
			<content:encoded><![CDATA[<h1>C# Boxing and Unboxing</h1>
<p> </p>
<p>Boxing is the process of converting a value type to the type <strong>object</strong> or to any interface type implemented by this value type. When the CLR boxes a value type, it wraps the value inside a System.Object and stores it on the managed heap. Unboxing extracts the value type from the object.</p>
<p>In relation to simple assignments, boxing and unboxing are computationally expensive processes. When a value type is boxed, a new object must be allocated and constructed. To a lesser degree, the cast required for unboxing is also expensive computationally.</p>
<h2>Boxing</h2>
<p>Boxing is used to store value types in the garbage-collected heap. Boxing is an implicit conversion of a value type to the type <strong>object</strong> or to any interface type implemented by this value type. Boxing a value type allocates an object instance on the heap and copies the value into the new object.</p>
<p>Consider the following declaration of a value-type variable:</p>
<pre class="brush: c#">

int i=123;
</pre>
<p>The following statement implicitly applies the boxing operation on the variable i:</p>
<pre class="brush: c#">

object o=i;      //Implicit boxing
</pre>
<p>The result of this statement is creating an object reference o, on the stack, that references a value of the type <strong>int</strong>, on the heap. This value is a copy of the value-type value assigned to the variable i. The difference between the two variables, i and o, is illustrated in the following figure.</p>
<p><a href="http://www.floatingvectors.com/blog/wp-content/uploads/2010/01/boxing.png"><img class="aligncenter size-full wp-image-272" title="boxing" src="http://www.floatingvectors.com/blog/wp-content/uploads/2010/01/boxing.png" alt="" width="250" height="170" /></a></p>
<h2>Unboxing</h2>
<p>Unboxing is an explicit conversion from the type <strong>object</strong> to a value type or from an interface type to a value type that implements the interface. An unboxing operation consists of:</p>
<p>Checking the object instance to make sure that it is a boxed value of the given value type.</p>
<p>Copying the value from the instance into the value-type variable.</p>
<p>The following statements demonstrate both boxing and unboxing operations:</p>
<pre class="brush: c#">

int  i = 123;

object o = i;  //boxing

int j = (int) o;   //unboxing
</pre>
<p>The following figure demonstrates the result of the previous statements.</p>
<p><a href="http://www.floatingvectors.com/blog/wp-content/uploads/2010/01/unboxing.png"><img class="aligncenter size-full wp-image-273" title="unboxing" src="http://www.floatingvectors.com/blog/wp-content/uploads/2010/01/unboxing.png" alt="" width="250" height="219" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.floatingvectors.com/blog/256/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>C# Common Type System</title>
		<link>http://www.floatingvectors.com/blog/249/</link>
		<comments>http://www.floatingvectors.com/blog/249/#comments</comments>
		<pubDate>Sun, 31 Jan 2010 08:26:29 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[软件[Code]]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.floatingvectors.com/blog/?p=249</guid>
		<description><![CDATA[C# Common Type System
 
The C# typing system contains the following categories:
Value types
Reference types
Pointer types
Variables that are value types store data, and those that are reference types store references to the actual data. Reference types are also referred to as objects. Pointer types can be used only in unsafe mode.
It is possible to convert a value [...]]]></description>
			<content:encoded><![CDATA[<h1>C# Common Type System</h1>
<p> </p>
<p>The C# typing system contains the following categories:</p>
<p>Value types</p>
<p>Reference types</p>
<p>Pointer types</p>
<p>Variables that are value types store data, and those that are reference types store references to the actual data. Reference types are also referred to as objects. Pointer types can be used only in unsafe mode.</p>
<p>It is possible to convert a value type to a reference type, and back again to a value type, by using boxing and unboxing. With the exception of a boxed value type, you cannot convert a reference type to a value type.</p>
<p>Types can derive from other types, called <em>base types</em>. The derived type inherits (with some restrictions) the methods, properties, and other members of the base type. The base type can in turn derive from some other type, in which case the derived type inherits the members of both base types in its inheritance hierarchy. All types, including built-in numeric types such as System.Int32 (C# keyword: int), derive ultimately from a single base type, which is System.Object (C# keyword: object). This unified type hierarchy is called the Common Type System (CTS).</p>
<p>Each type in the CTS is defined as either a <em>value type</em> or a <em>reference type</em>. This includes all custom types in the .NET Framework class library and also your own user-defined types. Types that you define by using the struct keyword are value types; all the built-in numeric types are <strong>structs</strong>. Types that you define by using the class keyword are reference types. Reference types and value types have different compile-time rules, and different run-time behavior.</p>
<p>The following illustration shows the relationship between value types and reference types in the CTS.</p>
<p><a href="http://www.floatingvectors.com/blog/wp-content/uploads/2010/01/csharpCTS.png"></a></p>
<p><strong><a href="http://www.floatingvectors.com/blog/wp-content/uploads/2010/01/csharpCTS.png"><img class="aligncenter size-full wp-image-250" title="csharpCTS" src="http://www.floatingvectors.com/blog/wp-content/uploads/2010/01/csharpCTS.png" alt="" width="514" height="410" /></a></strong></p>
<p><strong>Value Types</strong></p>
<p>Value types derive from System.ValueType, which derives from System.Object. Types that derive from System.ValueType have special behavior in the CLR. Value type variables directly contain their values, which means that the memory is allocated inline in whatever context the variable is declared. There is no separate heap allocation or garbage collection overhead for value-type variables.</p>
<p>There are two categories of value types: struct and enum.</p>
<p>Value types are <em>sealed</em>, which means, for example, that you cannot derive a type from System..::.Int32, and you cannot define a struct to inherit from any user-defined class or struct because a struct can only inherit from System.ValueType. However, a struct can implement one or more interfaces. You can cast a struct type to an interface type; this causes a <em>boxing</em> operation to wrap the struct inside a reference type object on the managed heap. Boxing operations occur when you pass a value type to a method that takes a System.Object as an input parameter.</p>
<p><strong>Reference Types</strong></p>
<p>A type that is defined as a class, delegate, array, or interface is a <em>reference type</em>. At run time, when you declare a variable of a reference type, the variable contains the value null until you explicitly create an instance of the object by using the new operator, or assign it an object that has been created elsewhere by using <strong>new.</strong></p>
<p>When the object is created, the memory is allocated on the managed heap, and the variable holds only a reference to the location of the object. Types on the managed heap require overhead both when they are allocated and when they are reclaimed by the automatic memory management functionality of the CLR, which is known as <em>garbage collection</em>. However, garbage collection is also highly optimized, and in most scenarios it does not create a performance issue.<span id="_marker"> </span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.floatingvectors.com/blog/249/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>纸飞机之王Sky King</title>
		<link>http://www.floatingvectors.com/blog/214/</link>
		<comments>http://www.floatingvectors.com/blog/214/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 01:32:56 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[生活［Life］]]></category>
		<category><![CDATA[玩具]]></category>
		<category><![CDATA[Sky King]]></category>

		<guid isPermaLink="false">http://www.floatingvectors.com/blog/http:/www.floatingvectors.com/blog/sound</guid>
		<description><![CDATA[纸飞机之王Sky King
 
Fold your own Sky King paper airplane.

]]></description>
			<content:encoded><![CDATA[<h1>纸飞机之王Sky King</h1>
<p> </p>
<p>Fold your own Sky King paper airplane.</p>
<p><img class="aligncenter size-medium wp-image-208" title="Plane_1_4" src="http://www.floatingvectors.com/blog/wp-content/uploads/2009/11/Plane_1_4-201x300.jpg" alt="Plane_1_4" width="201" height="300" /><img class="aligncenter size-medium wp-image-209" title="Plane_5_8" src="http://www.floatingvectors.com/blog/wp-content/uploads/2009/11/Plane_5_8-299x300.jpg" alt="Plane_5_8" width="299" height="300" /><img class="aligncenter size-medium wp-image-211" title="Plane_9" src="http://www.floatingvectors.com/blog/wp-content/uploads/2009/11/Plane_9-300x198.jpg" alt="Plane_9" width="300" height="198" /><img class="aligncenter size-medium wp-image-212" title="Plane_10_12" src="http://www.floatingvectors.com/blog/wp-content/uploads/2009/11/Plane_10_12-300x292.jpg" alt="Plane_10_12" width="300" height="292" /><img class="aligncenter size-medium wp-image-213" title="Plane_final" src="http://www.floatingvectors.com/blog/wp-content/uploads/2009/11/Plane_final-300x174.jpg" alt="Plane_final" width="300" height="174" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.floatingvectors.com/blog/214/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Good riddance</title>
		<link>http://www.floatingvectors.com/blog/181/</link>
		<comments>http://www.floatingvectors.com/blog/181/#comments</comments>
		<pubDate>Sat, 03 Oct 2009 07:04:48 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[生活［Life］]]></category>

		<guid isPermaLink="false">http://www.floatingvectors.com/blog/?p=181</guid>
		<description><![CDATA[Good riddance
 
折腾了一年，单纯地努力去做一件事。无论这次我做什么样的决定，都是在跟着心在走。
Little things make life great. 谢谢你，小胖子。
偶然看到:  http://en.wikipedia.org/wiki/Good_Riddance_(Time_of_Your_Life)
]]></description>
			<content:encoded><![CDATA[<h1>Good riddance</h1>
<p> </p>
<p>折腾了一年，单纯地努力去做一件事。无论这次我做什么样的决定，都是在跟着心在走。</p>
<p>Little things make life great. 谢谢你，小胖子。</p>
<p>偶然看到:  <a href="http://en.wikipedia.org/wiki/Good_Riddance_(Time_of_Your_Life">http://en.wikipedia.org/wiki/Good_Riddance_(Time_of_Your_Life</a>)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.floatingvectors.com/blog/181/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
