<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Carlos Eduardo Pérez Villanueva</title>
    <description>The latest articles on DEV Community by Carlos Eduardo Pérez Villanueva (@charlexmachina).</description>
    <link>https://dev.to/charlexmachina</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F186302%2F08446b13-83d5-4659-91a3-cedcc22de880.jpg</url>
      <title>DEV Community: Carlos Eduardo Pérez Villanueva</title>
      <link>https://dev.to/charlexmachina</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/charlexmachina"/>
    <language>en</language>
    <item>
      <title>Unity's Monobehaviours by Example</title>
      <dc:creator>Carlos Eduardo Pérez Villanueva</dc:creator>
      <pubDate>Wed, 17 Jun 2020 23:09:31 +0000</pubDate>
      <link>https://dev.to/charlexmachina/unity-s-monobehaviours-by-example-1hdn</link>
      <guid>https://dev.to/charlexmachina/unity-s-monobehaviours-by-example-1hdn</guid>
      <description>&lt;p&gt;Hello users of DEV!&lt;/p&gt;

&lt;p&gt;Last week, I wrote my first post to this platform, called &lt;a href="https://dev.to/oldmancharles/c-collections-by-example-31lp"&gt;C# Collections by Example&lt;/a&gt;, where I shared some small tips on using methods such as &lt;strong&gt;First()&lt;/strong&gt;, and its difference with &lt;strong&gt;FirstOrDefault()&lt;/strong&gt;, and adding real examples of how I use them on apps I build on my job.&lt;/p&gt;

&lt;p&gt;Today I want to talk about another topic, and share real examples from games I've worked on using the Unity Game Engine. Although I work as a programmer at an hospital here in Nicaragua, I spend most of my free evenings playing or developing videogames. I have plenty of experience using Unity, I even got to publish a game for iOS and Android; and some on itch.io! So here I'll share some useful tips regarding monobehaviours that you might find useful when developing games!&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's &lt;strong&gt;Start()&lt;/strong&gt; with some coroutines!
&lt;/h2&gt;

&lt;p&gt;Coroutines are pretty useful in Unity to execute code over time, or delays. Now, it's a standard practice to declare coroutines as a separate function, to finally execute it like &lt;strong&gt;StartCoroutine(MyCoroutine());&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is fine and it's simply the best approach to it. I don't like to write much code to be honest. I like to use inline functions whenever I can, because long, vertical walls of text make me feel uncomfortable. Sometimes, I used GameObjects that called a Coroutine as their only function, and they did it on spawn.&lt;/p&gt;

&lt;p&gt;It looked more or less like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;SerializeField&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;timeToWait&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Start&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; 
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;StartCoroutine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Explode&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;IEnumerator&lt;/span&gt; &lt;span class="nf"&gt;Explode&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; 
&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;WaitForSeconds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;timeToWait&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

   &lt;span class="c1"&gt;// ... do some bomb stuff&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Oh my God!&lt;/strong&gt; Just look at all that code! Two entire blocks just to make a simple bomb! I have enough with the explosion logic itself! Well, fortunately, someone at Unity Technologies might have thought the same, so you can use this little trick to save some lines of code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;SerializeField&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt; &lt;span class="n"&gt;timeToWait&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;IEnumerator&lt;/span&gt; &lt;span class="nf"&gt;Start&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; 
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;WaitForSeconds&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;timeToWait&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

   &lt;span class="c1"&gt;// ... do some bomb stuff&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Beautiful, is it not? Yeah, yeah, you can still write the explosion logic on a separate function, and, in theory it would be the same two blocks of code again. But my point is, that you don't have to declare a new coroutine. Unity &lt;strong&gt;allows for declaring Start() as a coroutine!&lt;/strong&gt; Go figure! I don't know about you, but I find this very useful. And as soon as I discovered this trick, which I have not found documented on Unity's official documentation, I never stopped using it. I hope it turns out to be useful to you!&lt;/p&gt;

&lt;p&gt;Why this happens? Beats me ¯\&lt;em&gt;(ツ)&lt;/em&gt;/¯ Unity isn't open source so I don't know for sure. But, basically, if Unity finds the &lt;strong&gt;Start()&lt;/strong&gt; method declared to return an IEnumerator(), it will automatically call it as we call coroutines, so it will behave accordingly.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use what you need
&lt;/h2&gt;

&lt;p&gt;I used to be one of those guys who often left their &lt;strong&gt;Update()&lt;/strong&gt; methods empty when I didn't need it for anything. Well, when you declare these so called &lt;em&gt;"magic methods"&lt;/em&gt; in Unity. They are added to the game loop and are being called, even when they're empty. Which means that &lt;strong&gt;Update()&lt;/strong&gt; method you're leaving empty is causing overhead. Yes, it's small. But if you are like I used to be and leave &lt;em&gt;every&lt;/em&gt; unused &lt;strong&gt;Update()&lt;/strong&gt; method empty, then &lt;em&gt;eventually&lt;/em&gt; you will feel the difference, especially on low-end systems.&lt;/p&gt;

&lt;p&gt;My point is, just use the magic methods you &lt;em&gt;actually need&lt;/em&gt; for your game behaviour to be the intended one. If you won't use it, just delete it. Because if not, &lt;em&gt;it will definitely be called&lt;/em&gt;, even when empty.&lt;/p&gt;

&lt;h2&gt;
  
  
  Don't animate with strings, use hash instead!
&lt;/h2&gt;

&lt;p&gt;We all have hardcoded strings more than once when working on games. A common example in the games I've made is with animations. Unity has an animation system I'm not too fond of. Especially because you trigger animations by name on a string. I'm always afraid of declaring stuff that way because, what happens if I miss a letter and write "isWaling" instead of "isWalking"? Disasters will happen!&lt;/p&gt;

&lt;p&gt;Thankfully (and honestly, it should be the norm), animations can not only be declared in an intellisense-friendly manner, but &lt;strong&gt;it can also be less expensive to call them!&lt;/strong&gt; So it not only makes your life as a programmer easier, but it also helps you with some small, free performance gains!&lt;/p&gt;

&lt;p&gt;Unity's own &lt;strong&gt;Animator&lt;/strong&gt; class has a cool, little method called &lt;strong&gt;StringToHash()&lt;/strong&gt;, which will generate the right ID for our animation, which then we can use to call our animations just like we always do. What's so different, then?&lt;/p&gt;

&lt;h3&gt;
  
  
  StringToHash() 101
&lt;/h3&gt;

&lt;p&gt;What does Unity do when we use something like this, for example?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;Animator&lt;/span&gt; &lt;span class="n"&gt;_animator&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Start&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;_animator&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;GetComponent&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Animator&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;AnimateFruitCatch&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="c1"&gt;// called by hard-coded string. Also, it produces garbage which may cause stutters in our game if done too much&lt;/span&gt;
   &lt;span class="n"&gt;_animator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SetTrigger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"CatchedFruit"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Well, for the uninitiated, Unity will first look for a match between the animation you want to call, and the ones you've assigned to your Animator component. This will be done through string matching, which, is slow compared to number matching. When it finds a match, then it will generate a hash value from animation's name. This numeric value is the actual ID for the animation you want to play, then it will look for the animation that matches this generated hash.&lt;/p&gt;

&lt;p&gt;Unity basically performs two searches, for little actual gain. Since, on the first place, typos may happen, &lt;a href="https://www.bbc.com/news/newsbeat-44846477#:~:text=An%20infamously%20dreadful%202013%20Aliens,Xbox%20360%20to%20terrible%20reviews"&gt;and you don't want to suffer the consequences of misspelling in gaming.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ok, so what can we do to reduce this overhead and also save ourselves from an Alien: Colonial Marines situation? Well, it's as simple as pre-computing the animation's hash code! This can be easily done with the &lt;strong&gt;StringToHash()&lt;/strong&gt; method I mentioned earlier.&lt;/p&gt;

&lt;p&gt;This is a snippet from a game I developed for mobile devices called Eden:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;Animator&lt;/span&gt; &lt;span class="n"&gt;_animator&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// here, we pre-compute the hash and use the animation's name JUST ONCE&lt;/span&gt;
&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;CatchedFruit&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Animator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;StringToHash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Catched_Fruit"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Start&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;_animator&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;GetComponent&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Animator&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;AnimateFruitCatch&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="c1"&gt;// since we call the variable directly, it becomes intellisense-friendly!&lt;/span&gt;
   &lt;span class="n"&gt;_animator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SetTrigger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;CatchedFruit&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By animating our GameObjects this way, we significantly reduce the overhead caused for looking up the animation we want to use by string matching, which like I said earlier, it is very slow compared to int matching. This will help you save some precious processing cycles and also make your life a little bit easier.&lt;/p&gt;

&lt;h2&gt;
  
  
  This is it for now, folks!
&lt;/h2&gt;

&lt;p&gt;I don't have anything more to share about the subject &lt;strong&gt;at the moment.&lt;/strong&gt; I hope any of these are useful for you, fellow game developer!&lt;/p&gt;

&lt;p&gt;I'll post more of these regularly, so make sure to &lt;a href="https://twitter.com/CharlExMachina"&gt;follow me&lt;/a&gt; and check out when I post more articles like this one!&lt;/p&gt;

</description>
      <category>unity3d</category>
      <category>csharp</category>
      <category>gamedev</category>
      <category>todayisearched</category>
    </item>
    <item>
      <title>C# collections by Example</title>
      <dc:creator>Carlos Eduardo Pérez Villanueva</dc:creator>
      <pubDate>Fri, 12 Jun 2020 04:08:14 +0000</pubDate>
      <link>https://dev.to/charlexmachina/c-collections-by-example-31lp</link>
      <guid>https://dev.to/charlexmachina/c-collections-by-example-31lp</guid>
      <description>&lt;p&gt;I've been thinking for a very long time about writing for this awesome community of developers. I think it's finally time, and I'll start by sharing some practical examples about the different things you can do with C# collections, and when you might like to use certain features.&lt;/p&gt;

&lt;p&gt;I will mostly describe the ones I use the most at work. I work at an hospital, so I work with collections &lt;strong&gt;A LOT&lt;/strong&gt;. I spend most of my day using LINQ and querying information from our database with the help of the Entity Framework. I hope to make a series of posts like this one, where I can show you examples you can refer to about different programming languages functions, libraries, and more. All with examples both theoretical and real!&lt;/p&gt;

&lt;p&gt;Without further ado, let's get started!&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's get the &lt;strong&gt;first&lt;/strong&gt; item, shall we?
&lt;/h2&gt;

&lt;p&gt;C# Collections' IEnumerable interface, gives you two methods to get the first item of a given collection. This is done through either the &lt;strong&gt;First()&lt;/strong&gt;, or &lt;strong&gt;FirstOrDefault()&lt;/strong&gt; methods. And I will explain both:&lt;/p&gt;

&lt;h3&gt;
  
  
  First()
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;firstPerson&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;myCollectionOfPersons&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;First&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;firstPerson&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the most basic example of how to get the first item from an IEnumerable source, such as a &lt;strong&gt;List&lt;/strong&gt;, for example.&lt;/p&gt;

&lt;p&gt;Now, you might find a problem with this little guy, and it is the fact that, &lt;strong&gt;when there aren't any items in your collection, it will throw an exception. Which you have to write code for! (&lt;em&gt;shivers...&lt;/em&gt;)&lt;/strong&gt; It has happened to me more times than what I'd like to admit and it annoys me a lot. Fortunately enough, the guys at Microsoft seem to have thought the same, so we have another alternative way of calling this method. Let me introduce you to &lt;strong&gt;First()'s&lt;/strong&gt; little brother: &lt;strong&gt;FirstOrDefault()&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  FirstOrDefault()
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;firstPerson&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;myCollectionOfPersons&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;FirstOrDefault&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Status&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="n"&gt;Status&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Married&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;firstPerson&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;firstPerson&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When using &lt;strong&gt;First()&lt;/strong&gt; we may had to wrap the code in a try...catch block just to make sure we can handle the situation when the List is empty, which means that there isn't any &lt;strong&gt;first&lt;/strong&gt; element to work with in the first place. &lt;strong&gt;FirstOrDefault()&lt;/strong&gt; will return either the first element of the List, or a default value. Which can be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;null&lt;/em&gt; for any reference type (like Lists or our classes!)&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;false&lt;/em&gt; for boolean types&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Zero (0)&lt;/em&gt; for any numeric type (integer, float, double, etc.)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;structs&lt;/strong&gt; are interesting. To all its properties, default values will be assigned depending on their types&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I think this is all I have to say for the methods available to obtain the first value of a given collection in C#. I use it mostly when I'm 100% sure that I want to get specifically the first element of a collection. I use it a lot with LINQ's &lt;strong&gt;OrderBy()&lt;/strong&gt; method. For example, I order reports by date on ascending order, then getting the first one which will, in turn, be the earliest one.&lt;/p&gt;

&lt;h4&gt;
  
  
  Practical example of how I use it at my job
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;earliestReport&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;db&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;sp_GetAllReports&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;Where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CreatedBy&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="n"&gt;myUsername&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;OrderBy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CreationDate&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;First&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// this gets the first report, which is the earliest made by this user&lt;/span&gt;
&lt;span class="c1"&gt;// do business stuff with it...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Let's check if &lt;strong&gt;any&lt;/strong&gt; item in the collection meets our criteria!
&lt;/h2&gt;

&lt;p&gt;Something I generally find on code written by my workmates that really grinds my gears, is them checking if a given item of a certain criteria exists by looping the collection, and then storing the knowledge of the existance of such item in a boolean value called &lt;strong&gt;itemExists&lt;/strong&gt;. Oh my God, the times I had to refactor just to clean up such mess...&lt;/p&gt;

&lt;p&gt;Thankfully, collections in C# also come with a handy little method called &lt;strong&gt;Any()&lt;/strong&gt;. With &lt;strong&gt;Any()&lt;/strong&gt;, we can check in a single line if an item that meets our criteria exists on a given collection. Let's see how to use it then!&lt;/p&gt;

&lt;h3&gt;
  
  
  Any()
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myCollectionOfPersons&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Any&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Age&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="m"&gt;65&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"This person is an elder!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Plain and simple, we can check Collections under any criteria with this really useful method!&lt;/p&gt;

&lt;h4&gt;
  
  
  Practical example of how I use it at my job
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;patientsList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Any&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HasInsurance&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="c1"&gt;// do stuff with patients who possess insurance&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  More to come!
&lt;/h2&gt;

&lt;p&gt;Well, the post is getting a bit long and I have no other interesting tidbits to share about the subject at this moment. Let me know if this information was of any help! I'll try to write these frequently about different topics so stay tuned!&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>tutorial</category>
      <category>todayisearched</category>
    </item>
  </channel>
</rss>
