<?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# 6.0 &#8211; Cognim &#8211; Internet development</title>
	<atom:link href="https://www.cognim.co.uk/tag/c-6-0/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.cognim.co.uk</link>
	<description>Enterprise system implementation. Making the complex simple</description>
	<lastBuildDate>Tue, 04 Aug 2015 17:00:17 +0000</lastBuildDate>
	<language>en-GB</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
<site xmlns="com-wordpress:feed-additions:1">91553907</site>	<item>
		<title>More concise, more readable. C# 6.0 real world gains</title>
		<link>https://www.cognim.co.uk/more-concise-more-readable-c-6-0-real-world-gains/</link>
					<comments>https://www.cognim.co.uk/more-concise-more-readable-c-6-0-real-world-gains/#comments</comments>
		
		<dc:creator><![CDATA[Darren Hall]]></dc:creator>
		<pubDate>Mon, 20 Jul 2015 10:39:58 +0000</pubDate>
				<category><![CDATA[c#]]></category>
		<category><![CDATA[C# 6.0]]></category>
		<guid isPermaLink="false">http://www.cognim.co.uk/?p=4982</guid>

					<description><![CDATA[With Visual Studio 2015 comes C# 6.0. The team at Microsoft put a whole lot of effort into the Roslyn compiler this time around and the language features are primarily syntactic sugar, but they are still extremely welcome. The Null-Conditional Operator Here is a small piece of code that I inherited recently.  It has a [&#8230;]]]></description>
										<content:encoded><![CDATA[<h3>With Visual Studio 2015 comes C# 6.0. The team at Microsoft put a whole lot of effort into the Roslyn compiler this time around and the language features are primarily syntactic sugar, but they are still extremely welcome.</h3>
<h5>The Null-Conditional Operator</h5>
<p>Here is a small piece of code that I inherited recently.  It has a very simple task, get the client&#8217;s postcode or return &#8216;unknown&#8217;</p>
<pre>public string GetPostCodeOfClientsOffice(Client client)
{
  if ((client == null) || (client.Office == null)
   || (client.Office.Address == null) || (client.Office.Address.PostCode == null))
  {
    return "unknown";
  }
  return client.Office.Address.PostCode;
}</pre>
<p>how much nicer it becomes when we use the new null conditional operator</p>
<pre>public string GetPostCodeOfClientsOffice(Client client)
{
 return client?.Office?.Address?.PostCode ?? "unknown";
}
</pre>
<p>Note that the type of client?.Office?.Address?.PostCode is nullable String.</p>
<p>This isn&#8217;t restricted to properties, the following method can also be refactored</p>
<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="beyond" data-enlighter-linenumbers="false">public int GetNumberOfClientsAwatingFeedback(List clients)
{
 if (clients != null) {
  return clients.Where(client=>client.NeedsFeedback).Count;
 }
 return 0;
}</pre>
<p>which is expressed more simply as</p>
<pre>public int GetNumberOfClientsAwatingFeedback(List clients)
{
 return clients?.Where(client=>client.NeedsFeedback).Count ?? 0;
}</pre>
<h5>Nameof Expressions</h5>
<p>When we have methods that expect non null properties we might typically see code like this</p>
<pre>public MemberParty GetMainClientAffiliation(Client client)
{
 If (client == Null)
  throw new ArgumentNullException("client");
 If (client.MemberParty == Null)
  throw new ArgumentNullException("MemberParty ");
 return client.MemberParty ;
}
</pre>
<p>Notice here that the method refers to client affiliation and the property it returns is &#8216;MemberParty &#8216;. <em><a href="http://martinfowler.com/bliki/UbiquitousLanguage.html">Not a very ubiquitous approach</a>.</em></p>
<p>When we refactor to use affiliation however, the following occurs</p>
<pre>public Affiliation GetMainClientAffiliation(Client client)
{
 If (client == Null)
  throw new ArgumentNullException("client");
 If (client.Affiliation == Null)
  throw new ArgumentNullException("MemberParty ");
 return client.Affiliation;
}
</pre>
<p>Aargh, the dreaded magic string has caught us out.  C# 6.0 to the rescue</p>
<pre>public Affiliation GetMainClientAffiliation(Client client)
{
 If (client == Null)
  throw new ArgumentNullException(nameof(client));
 If (client.Affiliation == Null)
  throw new ArgumentNullException(nameof(client.Affiliation));
 return client.Affiliation;
}
</pre>
<p>Now we have strongly typed properties and safe refactoring. Not a massive advance but something that has caught me out many times over the years.</p>
<h5>Expression bodied properties and functions</h5>
<p>Let&#8217;s say we have the following code</p>
<pre>public class Client
{
 public string FirstName { get; set; }
 public string LastName { get; set; }

 public string FullName { 
  get{ 
   return string.Format("{0} {1}",FirstName,LastName); 
  }
 }
}
</pre>
<p>we can now rewrite this as</p>
<pre>public class Client
{
 public string FirstName { get; set; }
 public string LastName { get; set; }

 public string FullName => string.Format("{0} {1}",FirstName,LastName);
}
</pre>
<p>Note that these are not lambda expressions, they are just syntactic sugar intended to simplify your code and make it more readable.</p>
<h5>String Interpolation</h5>
<p>even better, we can change this to</p>
<pre>public class Client
{
 public string FirstName { get; set; }
 public string LastName { get; set; }

 public string FullName => $"{FirstName} {LastName}";
}
</pre>
<p>Much more readable!</p>
<p>Also if we wanted to override the ToString method for this class, we might use several new features together in the following way</p>
<pre> public override string ToString() => $"Full name: {FullName} City: {Address?.City ?? "Unknown"}";
</pre>
<p>These are a few of the new language features. Like I said at the start, most of the effort this time around went into Roslyn but I really appreciate that the subtle changes they did make add to the beauty of the language.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.cognim.co.uk/more-concise-more-readable-c-6-0-real-world-gains/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">4982</post-id>	</item>
	</channel>
</rss>
