<?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: Gabriel Castillo</title>
    <description>The latest articles on DEV Community by Gabriel Castillo (@gabrielcastcl).</description>
    <link>https://dev.to/gabrielcastcl</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%2F812954%2Fa024a23e-429a-4282-a189-aa5dbc9de1a1.jpg</url>
      <title>DEV Community: Gabriel Castillo</title>
      <link>https://dev.to/gabrielcastcl</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gabrielcastcl"/>
    <language>en</language>
    <item>
      <title>C# Quick Sessions - Pattern matching - Chapter 1 Declaration Pattern</title>
      <dc:creator>Gabriel Castillo</dc:creator>
      <pubDate>Sun, 20 Feb 2022 14:46:44 +0000</pubDate>
      <link>https://dev.to/gabrielcastcl/c-quick-sessions-pattern-matching-chapter-1-declaration-pattern-3mko</link>
      <guid>https://dev.to/gabrielcastcl/c-quick-sessions-pattern-matching-chapter-1-declaration-pattern-3mko</guid>
      <description>&lt;p&gt;Normally in our code it's necessary to evaluate the value of a variable or an expression to make a decision relative to the result obtained, typically do you need to evaluate if the value meets with a certain condition or pattern.&lt;/p&gt;

&lt;p&gt;Maybe, you normally perform comparison or evaluation in your “if” statement, a “switch” statement, or a switch expression.&lt;/p&gt;

&lt;p&gt;C#, like other programming languages, is evolving version by version and introduces new concepts that can help us to do a better code.&lt;/p&gt;

&lt;p&gt;Since C# version 7.0 you can use pattern matching, and nowadays there are more than 7 types of pattern.&lt;/p&gt;

&lt;p&gt;This will be a full review of all of them, and we will be explored in details in several chapters.&lt;/p&gt;

&lt;p&gt;The chapter of today is Declaration Pattern.&lt;br&gt;
let's begin&lt;/p&gt;


&lt;h2&gt;
  
  
  The Declaration Pattern
&lt;/h2&gt;

&lt;p&gt;Available since C# 7.0, it is used to validate the type of an expression and if a match is ok set the result of expression in a variable.&lt;/p&gt;

&lt;p&gt;Looks the following example. On this we use the declaration pattern to evaluate if the paramaters are able to be divisble. The parameters have been declared has “int?” so the values ​​coud be “null” or a “number”.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;First we evaluate wether the value of “a parameter” is an int, if the number is null the result of the expression “a is int” will be false. By other hand wether is a number, then the value of “a” is set to variable “number”,&lt;/li&gt;
&lt;li&gt;Then we use the same logic to the parameter “b”, but setting the value to the “div” variable.&lt;/li&gt;
&lt;li&gt;And finally in the same expression we use the variable created on the step 2 (div variable) to validate if the value is greater than zero. &lt;/li&gt;
&lt;li&gt;To finally perform a standard division between two numbers.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Validating Type on runtime
&lt;/h2&gt;

&lt;p&gt;Let's see another example now. &lt;/p&gt;

&lt;p&gt;In the next example we will validate the type of an object using the declaration pattern.&lt;/p&gt;

&lt;p&gt;We are asked to create a simple solution to add an additional tax only to liquor products from a store.&lt;br&gt;
So we declare:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;IProduct Interface&lt;/strong&gt; : On this interface we will define the sigin of the method to get total price from any product.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;BaseProduct class&lt;/strong&gt;: This class implements the interface IProduct, and is an abstract class, and will be the super class for all derived kinds of products. On this class we declare the properties Name and BasePrice and define the standard code to the method getTotalPrice, that will be defined virtual, so any derived class can redefine its behavior.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;BaseProductWithTax&lt;/strong&gt;: This class inherited from BaseProduct, and will be the super class from all the product who has a tax. on this class will add the Tax propertie, and override the method GetTotalPrice to add the tax.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Electronic class&lt;/strong&gt;: derived class from BaseProduct (concrete type of product)
-** Liqueur class**: derived class from baseProductWhitTax. (specific type of a product with tax)&lt;/li&gt;
&lt;/ul&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Now we will declare a list of different types of products and will be apply the tax only to objects if they derived only the Liquer class&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;How you can see on the line &lt;code&gt;"if (product is BaseProduct baseProduct)"&lt;/code&gt; of the method &lt;strong&gt;ShwoListOfProducts()&lt;/strong&gt; this will be true for all instance of any class derived from BaseProduct, (electronic and liquier products, it's for all), and later in the line &lt;code&gt;“if (product is BaseProductWithTax productWithTax)”&lt;/code&gt; will be true only with the product of the type liquier, because this products inheritance from &lt;strong&gt;BaseProductWithTax&lt;/strong&gt; class.&lt;/p&gt;




&lt;p&gt;I hope this simple example can be usefull, on the next chapters will review other types of pattern matching, See you!&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>programming</category>
      <category>development</category>
    </item>
    <item>
      <title>Conditional Ternary Operator “?” C#, Why (not always) is not “If sentence” replacement</title>
      <dc:creator>Gabriel Castillo</dc:creator>
      <pubDate>Thu, 10 Feb 2022 19:32:26 +0000</pubDate>
      <link>https://dev.to/gabrielcastcl/conditional-ternary-operator-c-why-not-always-is-not-if-sentence-replacement-4bhd</link>
      <guid>https://dev.to/gabrielcastcl/conditional-ternary-operator-c-why-not-always-is-not-if-sentence-replacement-4bhd</guid>
      <description>&lt;p&gt;Maybe you are familiar with the ternary operator “?”, and we can agree that, in a logicall way, can be similar to an “if statement”, but they are not the same, and not always can be interchangeable.&lt;br&gt;
So, in this article, I will try to explain, in a simple way, the difference and similitude between them.&lt;br&gt;
By other hand, if you don’t know anything about the ternary operator, this article can be very useful too. Let’s begin.&lt;/p&gt;


&lt;h2&gt;
  
  
  What is an Operator?
&lt;/h2&gt;

&lt;p&gt;The first topic we need to clarify is “What is an Operator”.&lt;br&gt;
&lt;em&gt;“An Operator is a symbol or character(s) that indicate how to be acting the operands from a expression”&lt;/em&gt;. From the previous sentence we need to rescue the word “&lt;strong&gt;expression&lt;/strong&gt;”, and an expression is like a formule that gives a value result.&lt;/p&gt;

&lt;p&gt;For instance, “&lt;strong&gt;a + b&lt;/strong&gt;” is an expression, “&lt;strong&gt;a&lt;/strong&gt;” and “&lt;strong&gt;b&lt;/strong&gt;” are the operands and “&lt;strong&gt;+&lt;/strong&gt;” is the operator, and “&lt;strong&gt;a+b&lt;/strong&gt;” is the expression (or formule) that give as a result “&lt;strong&gt;a plus b&lt;/strong&gt;”, and this results is assign to a variable or is used to be evaluated.&lt;/p&gt;

&lt;p&gt;Other example about expression is this “&lt;strong&gt;a&amp;gt;=b&lt;/strong&gt;”. In this expression the operator character is “&lt;strong&gt;&amp;gt;=&lt;/strong&gt;” and the result of this expression can be true or false, depending the values of “a” and “b”.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The important word here is related with a expression (keep this word in mind).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;C# has several Operators, and of the diferent types. Exists aritmethics operators (+, -, * , /), comparsion operators (&amp;gt;, &amp;lt;, &amp;lt;=, &amp;gt;=), the iquality, and so on.&lt;/p&gt;
&lt;h2&gt;
  
  
  So, wich mean the conditional ternary operator “?”
&lt;/h2&gt;

&lt;p&gt;Well, the operator “?” is a ternary conditional operator. Is “&lt;strong&gt;&lt;em&gt;ternary&lt;/em&gt;&lt;/strong&gt;” because in his sintax are envolved 3 expressions, and is “&lt;strong&gt;&lt;em&gt;conditional&lt;/em&gt;&lt;/strong&gt;” because dependen of the evaluation of the one expression (true or false), to take the decision of evaluate one of other expression.&lt;/p&gt;

&lt;p&gt;Looks the definition:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;(condition expression) ? (consequent expression) : (alternative expression)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Looks now in action with a simple example code:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In this example “second&amp;gt;0” is the “condition expression”, and its evaluation returns a boolean value, if the result of this evaluation is “true”, then the “consequent expression” is evaluated, in this case “first / second” (division). By other hand, if the result of the “condition expression” is “false”, the “alternative expression” is evaluated (0), and the result of this entire evaluation is, in this case, asign to the “result” variable.&lt;/p&gt;

&lt;p&gt;Talk about some kinds of rules related with this operator.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Before to C# 9.0, the alternative and consequent expression must be the same tipe. One alternative to do this in previous C# version is ralize an explicit conversion of type.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The operator is asociative to the right. If you encadenate ternary operators, this will be grouped from right to left. I mean (condition? (consequent): (alternative) ) ==&amp;gt; ( a&lt;b&gt;b? a : b) ), looks this example.&lt;br&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;/b&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In summary you can view the operator “?” is used to evaluate a condition and &lt;em&gt;&lt;strong&gt;get a value realted with the condition&lt;/strong&gt;&lt;/em&gt;. Looks this example now.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  And with is the difference with If..Else Statement?
&lt;/h2&gt;

&lt;p&gt;The “If…else” is a selection statement, and is used normally to generate a logic fork in our code. The global sintax is like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The “if else” dont give you a result value, I mean, you can’t assign “his result” to a variable (principal difference with a expression).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;So the principal objective of “if else” is take a decision to realize one logic or another in your code.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But, exists cases when you can replace an if sentence with a ternary operator in a natural way, and the more natural case is when you use the “if sentence” to assignate one value or another depending the result of the if condition.&lt;/p&gt;

&lt;p&gt;Looks this example, in the first option we use an if statement&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;You can agree that the second approach, using the ternary operator, is very much simple way, and is easy to read and understand.&lt;/p&gt;




&lt;p&gt;So, in conclusion the ternary operator is an expression, and has similarities with the if statement, but not always can be one replacement by the other. I think the decision to changed is when the result of that change can give you a code more clear and understandable to your team.&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>programming</category>
      <category>dotnetcore</category>
    </item>
  </channel>
</rss>
