<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd" MadCap:lastBlockDepth="2" MadCap:lastHeight="120" MadCap:lastWidth="624" MadCap:disableMasterStylesheet="true" MadCap:tocPath="Platform Specific Issues" MadCap:InPreviewMode="false" MadCap:RuntimeFileType="Topic" MadCap:TargetType="WebHelp" MadCap:PathToHelpSystem="../../" MadCap:HelpSystemFileName="index.xml" MadCap:SearchType="Stem"> <head><title>Automatic Properties</title> <script type="text/javascript">/* <![CDATA[ */ window.onload = function(){ var pathToFlash = $('html').attr('MadCap:PathToHelpSystem') + 'Content/Resources/Code/ZeroClipboard.swf'; ZeroClipboard.setMoviePath(pathToFlash); function bindToClipBord(element,content){ var clip = new ZeroClipboard.Client(); clip.setText(content); clip.glue(element); }; if(location.protocol==='file:'){ $('.copylink-marker').remove(); } else{ $('.copylink-marker').each(function(){ var text = $(this).parent().parent().children('.prettyprint').html(); $(this).hover(function(){ bindToClipBord(this,text); }, function(){}); }); } prettyPrint(); }; /* ]]> */</script> <link href="../SkinSupport/MadCap.css" rel="stylesheet" /> <link href="../Resources/Stylesheets/OnlineStyle.css" rel="stylesheet" /> <script src="../Resources/Code/prettify.js"> </script> <script src="../Resources/Code/lang-vb.js"> </script> <script src="../Resources/Code/jquery.min.js"> </script> <script src="../Resources/Code/ZeroClipboard.js"> </script> <script src="../SkinSupport/MadCapAll.js" type="text/javascript"> </script> </head> <body> <p class="MCWebHelpFramesetLink" style="display: none;"><a href="../../index_CSH.html#platform_specific_issues/automatic_properties.htm" style="">Open topic with navigation</a> </p> <div class="MCBreadcrumbsBox"><span class="MCBreadcrumbsPrefix">You are here: </span><a class="MCBreadcrumbsLink" href="../platform_specific_issues.htm">Platform Specific Issues</a><span class="MCBreadcrumbsDivider"> > </span><span class="MCBreadcrumbs">Automatic Properties</span> </div> <p> <script type="text/javascript">/*<![CDATA[*/document.write('<a href="' + location.href +'">'); document.write("Direct Link"); document.write('</a>');/*]]>*/</script> </p> <p> </p> <h1><a name="kanchor108"></a>Automatic Properties</h1> <p>Automatic properties simply allow a user to skip the step of introducing private field variables when the property simply sets and gets the field value:</p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">class Person { public string Name { get; set; } }</pre> <div class="codesnippet-meta">AutoProperties.cs: Auto property <div class="codedownload"><a href="../CodeExamples/platform/autoproperties/Example-platform-autoproperties-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <div class="codesnippet" MadCap:conditions="Primary.VB.NET"> <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Public Class Person Public Property Name() As String End Class</pre> <div class="codesnippet-meta">AutoProperties.vb: Auto property <div class="codedownload"><a href="../CodeExamples/platform/autoproperties/Example-platform-autoproperties-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <p class="">Behind the scenes the compiler creates a field and the regular property code. The field-name depends on the compiler. For C# the field-name of auto-property is <span class="method"><PropertyName>k__BackingField</span>. For example the property FirstName will be stored in the field <span class="method"><FirstName>k__BackingField</span>. For <a href="http://msdn.microsoft.com/en-us/library/dd293589.aspx" target="_parent">VB.Net the field-name of auto-property</a> is <span class="method">_PropertyName</span>. For example the property FirstName will be stored in the field <span class="method">_FirstName</span>.</p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">public class Person { // the name is actually <Name>k__BackingField // but you cannot express that in C# code private string name; public string Name { get { return name; } set { name = value; } } }</pre> <div class="codesnippet-meta">AutoProperties.cs: Auto property behind the scenes <div class="codedownload"><a href="../CodeExamples/platform/autoproperties/Example-platform-autoproperties-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <div class="codesnippet" MadCap:conditions="Primary.VB.NET"> <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Public Class Person Private _Name As String Public Property Name() As String Get Return _Name End Get Set(ByVal value As String) _Name = value End Set End Property End Class</pre> <div class="codesnippet-meta">AutoProperties.vb: Auto property behind the scenes <div class="codedownload"><a href="../CodeExamples/platform/autoproperties/Example-platform-autoproperties-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <h2>Issues With Auto Properties</h2> <p>Note that db4o always uses the field name for its configuration. This also applies to auto-properties. You need to use the field-name to specify index etc. </p> <p>For C# the name of the backing field is not specified and a compiler implementation detail. Therefore it's possible that it actually changes in the future.</p> <p>For example to index the auto property Name:</p> <div class="codesnippet" MadCap:conditions="Primary.c#"> <pre class="prettyprint" xml:space="preserve">IEmbeddedConfiguration configuration = Db4oEmbedded.NewConfiguration(); configuration.Common.ObjectClass(typeof(Person)).ObjectField("<Name>k__BackingField").Indexed(true);</pre> <div class="codesnippet-meta">AutoProperties.cs: Configure auto properties <div class="codedownload"><a href="../CodeExamples/platform/autoproperties/Example-platform-autoproperties-csharp.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <div class="codesnippet" MadCap:conditions="Primary.VB.NET"> <pre class="prettyprint lang-vb" MadCap:conditions="Primary.Online" xml:space="preserve">Dim configuration As IEmbeddedConfiguration = Db4oEmbedded.NewConfiguration() configuration.Common.ObjectClass(GetType(Person)).ObjectField("_Name").Indexed(True)</pre> <div class="codesnippet-meta">AutoProperties.vb: Configure auto properties <div class="codedownload"><a href="../CodeExamples/platform/autoproperties/Example-platform-autoproperties-vb.zip" class="codedownload" MadCap:conditions="Primary.Online">Download Code</a></div><div class="codedownload copylink-marker" MadCap:conditions="Primary.Online"><a href="#copy">Copy Code</a></div></div> </div> <p class=""> </p> <script type="text/javascript" src="../SkinSupport/MadCapBodyEnd.js"> </script> </body> </html>