<?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: Hassan BOLAJRAF</title>
    <description>The latest articles on DEV Community by Hassan BOLAJRAF (@hbolajraf).</description>
    <link>https://dev.to/hbolajraf</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%2F1003677%2F7636e2c5-7a6f-4c50-abb4-56b879dbbdcf.jpg</url>
      <title>DEV Community: Hassan BOLAJRAF</title>
      <link>https://dev.to/hbolajraf</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hbolajraf"/>
    <language>en</language>
    <item>
      <title>SQL | Interview Questions and Answers</title>
      <dc:creator>Hassan BOLAJRAF</dc:creator>
      <pubDate>Mon, 19 Aug 2024 10:43:31 +0000</pubDate>
      <link>https://dev.to/hbolajraf/sql-interview-questions-and-answers-2173</link>
      <guid>https://dev.to/hbolajraf/sql-interview-questions-and-answers-2173</guid>
      <description>&lt;blockquote&gt;
&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt; &lt;strong&gt;Note&lt;/strong&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;You can check other posts on my personal website: &lt;a href="https://hbolajraf.net" rel="noopener noreferrer"&gt;https://hbolajraf.net&lt;/a&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  1. What is SQL?
&lt;/h2&gt;

&lt;p&gt;SQL (Structured Query Language) is a programming language designed for managing and manipulating relational databases. It is used for tasks such as querying data, updating data, and defining the structure of a database.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. What is the difference between SQL and NoSQL databases?
&lt;/h2&gt;

&lt;p&gt;SQL databases are relational databases that use structured query language for defining and manipulating the data. NoSQL databases, on the other hand, are non-relational databases that can handle unstructured or semi-structured data and do not require a fixed schema.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. What is a primary key?
&lt;/h2&gt;

&lt;p&gt;A primary key is a unique identifier for a record in a database table. It ensures that each record in a table can be uniquely identified.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Explain the difference between INNER JOIN and LEFT JOIN.
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;INNER JOIN returns only the matching rows from both tables.&lt;/li&gt;
&lt;li&gt;LEFT JOIN returns all rows from the left table and the matching rows from the right table. If there is no match, NULL values are returned for columns from the right table.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  5. What is normalization?
&lt;/h2&gt;

&lt;p&gt;Normalization is the process of organizing data in a database to eliminate redundancy and dependency. It involves dividing large tables into smaller tables and defining relationships between them.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Write a SQL query to retrieve all employees from the "employees" table.
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  7. How do you add a new column to an existing table in SQL?
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="k"&gt;table_name&lt;/span&gt;
&lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;COLUMN&lt;/span&gt; &lt;span class="n"&gt;new_column_name&lt;/span&gt; &lt;span class="n"&gt;data_type&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  8. Explain the difference between UNION and UNION ALL.
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;UNION combines the result sets of two or more SELECT statements and removes duplicate rows.&lt;/li&gt;
&lt;li&gt;UNION ALL also combines result sets but includes all rows, including duplicates.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  9. What is a foreign key?
&lt;/h2&gt;

&lt;p&gt;A foreign key is a column or a set of columns in a table that refers to the primary key of another table. It establishes a link between the two tables.&lt;/p&gt;

&lt;h2&gt;
  
  
  10. Write a SQL query to calculate the average salary from the "salaries" table.
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;AVG&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;salary&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;salaries&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  11. How do you delete a record from a table in SQL?
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;DELETE&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="k"&gt;table_name&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  12. Explain the ACID properties in the context of database transactions.
&lt;/h2&gt;

&lt;p&gt;ACID stands for Atomicity, Consistency, Isolation, and Durability. These properties ensure that database transactions are processed reliably.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Atomicity: Ensures that a transaction is treated as a single, indivisible unit.&lt;/li&gt;
&lt;li&gt;Consistency: Ensures that a transaction brings the database from one valid state to another.&lt;/li&gt;
&lt;li&gt;Isolation: Ensures that the concurrent execution of transactions does not result in data inconsistencies.&lt;/li&gt;
&lt;li&gt;Durability: Ensures that once a transaction is committed, its changes are permanent.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  13. What is the purpose of the GROUP BY clause?
&lt;/h2&gt;

&lt;p&gt;The GROUP BY clause is used to group rows that have the same values in specified columns into summary rows. It is often used with aggregate functions like COUNT, SUM, AVG, etc.&lt;/p&gt;

&lt;h2&gt;
  
  
  14. Write a SQL query to find the second-highest salary from the "employees" table.
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;MAX&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;salary&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;MAX&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;salary&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  15. How do you create an index in SQL?
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="n"&gt;index_name&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="k"&gt;table_name&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;column1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;column2&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;h2&gt;
  
  
  16. Explain the difference between a clustered index and a non-clustered index.
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A clustered index determines the physical order of data in a table. There can be only one clustered index per table.&lt;/li&gt;
&lt;li&gt;A non-clustered index does not affect the physical order of the table. Tables can have multiple non-clustered indexes.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  17. What is a stored procedure?
&lt;/h2&gt;

&lt;p&gt;A stored procedure is a precompiled collection of one or more SQL statements that can be executed as a single unit. It is stored in the database and can be called from applications or other stored procedures.&lt;/p&gt;

&lt;h2&gt;
  
  
  18. Write a SQL query to find the total number of rows in a table.
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;COUNT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="k"&gt;table_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  19. Explain the difference between a view and a table.
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A table is a physical storage structure that holds data.&lt;/li&gt;
&lt;li&gt;A view is a virtual table based on the result of a SELECT query. It does not store data itself but provides a way to represent the result of a query.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  20. How do you update data in a table using SQL?
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="k"&gt;table_name&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;column1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;column2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  21. What is the purpose of the HAVING clause?
&lt;/h2&gt;

&lt;p&gt;The HAVING clause is used in conjunction with the GROUP BY clause to filter the results of a grouped query based on a specified condition.&lt;/p&gt;

&lt;h2&gt;
  
  
  22. Write a SQL query to find the top N records from a table.
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="k"&gt;table_name&lt;/span&gt;
&lt;span class="k"&gt;LIMIT&lt;/span&gt; &lt;span class="n"&gt;N&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  23. How do you perform a self-join in SQL?
&lt;/h2&gt;

&lt;p&gt;A self-join is a regular join, but the table is joined with itself. It is typically used when a table has a foreign key that references its own primary key.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;t1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;column1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;column2&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="k"&gt;table_name&lt;/span&gt; &lt;span class="n"&gt;t1&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="k"&gt;table_name&lt;/span&gt; &lt;span class="n"&gt;t2&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;t1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;common_column&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;common_column&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  24. Explain the purpose of the CASE statement in SQL.
&lt;/h2&gt;

&lt;p&gt;The CASE statement is used to perform conditional logic within a SQL query. It allows you to perform different actions based on different conditions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; 
  &lt;span class="n"&gt;column1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="k"&gt;CASE&lt;/span&gt; 
    &lt;span class="k"&gt;WHEN&lt;/span&gt; &lt;span class="n"&gt;condition1&lt;/span&gt; &lt;span class="k"&gt;THEN&lt;/span&gt; &lt;span class="n"&gt;result1&lt;/span&gt;
    &lt;span class="k"&gt;WHEN&lt;/span&gt; &lt;span class="n"&gt;condition2&lt;/span&gt; &lt;span class="k"&gt;THEN&lt;/span&gt; &lt;span class="n"&gt;result2&lt;/span&gt;
    &lt;span class="k"&gt;ELSE&lt;/span&gt; &lt;span class="n"&gt;result3&lt;/span&gt;
  &lt;span class="k"&gt;END&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;new_column&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="k"&gt;table_name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  25. What is the difference between CHAR and VARCHAR data types?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;CHAR is a fixed-length character data type.&lt;/li&gt;
&lt;li&gt;VARCHAR is a variable-length character data type.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  26. Write a SQL query to find the third-highest salary from the "employees" table.
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;MAX&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;salary&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;MAX&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;salary&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;MAX&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;salary&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  27. What is a trigger in SQL?
&lt;/h2&gt;

&lt;p&gt;A trigger is a set of instructions that are automatically executed (or "triggered") in response to specific events on a particular table or view. These events can include INSERT, UPDATE, DELETE operations.&lt;/p&gt;

&lt;h2&gt;
  
  
  28. Explain the difference between a database and a schema.
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A database is a container for tables and related objects.&lt;/li&gt;
&lt;li&gt;A schema is a collection of database objects (tables, views, procedures) that are logically grouped together.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  29. How do you find duplicate rows in a table?
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;column1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;column2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;COUNT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="k"&gt;table_name&lt;/span&gt;
&lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;column1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;column2&lt;/span&gt;
&lt;span class="k"&gt;HAVING&lt;/span&gt; &lt;span class="k"&gt;COUNT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  30. What is a subquery?
&lt;/h2&gt;

&lt;p&gt;A subquery is a query nested inside another query. It can be used to retrieve data that will be used in the main query as a condition to further restrict the data to be retrieved.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;column1&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt;

 &lt;span class="k"&gt;table_name&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;column2&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;column2&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;another_table&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This concludes the list of SQL interview questions and answers. Feel free to use these questions as a study guide or reference for your SQL interviews.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Next?
&lt;/h2&gt;

&lt;p&gt;These are just a few examples of SQL interview questions. Depending on the level of expertise required, interview questions may vary from basic to advanced topics. Good luck with your interviews!&lt;/p&gt;

</description>
      <category>sql</category>
      <category>help</category>
      <category>interview</category>
    </item>
    <item>
      <title>C# | Interview Questions and Answers</title>
      <dc:creator>Hassan BOLAJRAF</dc:creator>
      <pubDate>Mon, 19 Aug 2024 10:40:43 +0000</pubDate>
      <link>https://dev.to/hbolajraf/c-interview-questions-and-answers-4481</link>
      <guid>https://dev.to/hbolajraf/c-interview-questions-and-answers-4481</guid>
      <description>&lt;blockquote&gt;
&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt; &lt;strong&gt;Note&lt;/strong&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;You can check other posts on my personal website: &lt;a href="https://hbolajraf.net" rel="noopener noreferrer"&gt;https://hbolajraf.net&lt;/a&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  1. What is C#?
&lt;/h2&gt;

&lt;p&gt;C# (pronounced C-sharp) is a modern, object-oriented programming language developed by Microsoft as part of the .NET platform.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Differentiate between &lt;code&gt;String&lt;/code&gt; and &lt;code&gt;StringBuilder&lt;/code&gt; in C#.
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;String&lt;/code&gt; is immutable, meaning its value cannot be changed after it's created. &lt;code&gt;StringBuilder&lt;/code&gt; is mutable and allows for efficient manipulation of strings.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  3. Explain the difference between &lt;code&gt;const&lt;/code&gt; and &lt;code&gt;readonly&lt;/code&gt; in C#.
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;const&lt;/code&gt; is compile-time constant, and its value must be assigned at the time of declaration. &lt;code&gt;readonly&lt;/code&gt; allows the value to be assigned either at the time of declaration or within a constructor.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  4. What is the purpose of the &lt;code&gt;using&lt;/code&gt; statement in C#?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;using&lt;/code&gt; statement is used to include a namespace in the program. It also helps in resource management by automatically disposing of objects when they are no longer needed.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  5. Describe the concept of boxing and unboxing in C#.
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Boxing is the process of converting a value type to a reference type, and unboxing is the reverse process. It involves converting a reference type back to a value type.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  6. What is the significance of the &lt;code&gt;var&lt;/code&gt; keyword in C#?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;var&lt;/code&gt; keyword is used for implicitly declaring variables, letting the compiler infer the type based on the assigned value.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  7. Explain the purpose of the &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;await&lt;/code&gt; keywords.
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;async&lt;/code&gt; is used to declare an asynchronous method, and &lt;code&gt;await&lt;/code&gt; is used to asynchronously wait for a task to complete, allowing other tasks to run in the meantime.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  8. Differentiate between &lt;code&gt;==&lt;/code&gt; and &lt;code&gt;Equals&lt;/code&gt; method in C#.
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;==&lt;/code&gt; is used for comparing the values of two variables, while the &lt;code&gt;Equals&lt;/code&gt; method is used for comparing the contents of objects.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  9. What is a delegate in C#?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A delegate is a type that represents references to methods with a specific signature. It is similar to a function pointer in C or C++.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  10. Explain the use of LINQ in C#.
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;LINQ (Language Integrated Query) is a set of features that extends C# by the addition of query expressions, allowing the manipulation of data from different sources.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  11. How does garbage collection work in C#?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Garbage collection is an automatic memory management process in C# that identifies and collects objects that are no longer needed, freeing up memory.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  12. Describe the Singleton design pattern.
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The Singleton pattern ensures that a class has only one instance and provides a global point of access to that instance. It involves a private constructor and a static method to retrieve the instance.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  13. What is the difference between &lt;code&gt;throw&lt;/code&gt; and &lt;code&gt;throw ex&lt;/code&gt; in C#?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;throw&lt;/code&gt; is used to rethrow the current exception, preserving the original stack trace, while &lt;code&gt;throw ex&lt;/code&gt; discards the original stack trace and starts a new one.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  14. How does the &lt;code&gt;try&lt;/code&gt;, &lt;code&gt;catch&lt;/code&gt;, and &lt;code&gt;finally&lt;/code&gt; blocks work in C#?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;try&lt;/code&gt; is used to enclose the code that might throw an exception. &lt;code&gt;catch&lt;/code&gt; is used to handle the exception, and &lt;code&gt;finally&lt;/code&gt; is used to specify a block of code that will be executed, regardless of whether an exception is thrown or not.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  15. What is the purpose of the &lt;code&gt;sealed&lt;/code&gt; keyword in C#?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;sealed&lt;/code&gt; keyword is used to prevent a class from being inherited. It ensures that the class cannot be a base class for other classes.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  16. Explain the concept of an interface in C#.
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;An interface is a contract that defines a set of methods without providing the implementation. Classes that implement an interface must provide concrete implementations for all its methods.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  17. How does method overloading work in C#?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Method overloading allows a class to have multiple methods with the same name but different parameters, enabling the flexibility of using different argument lists.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  18. Describe the use of the &lt;code&gt;out&lt;/code&gt; keyword in C#.
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;out&lt;/code&gt; keyword is used to pass a parameter by reference, allowing the called method to modify the value of the parameter.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  19. What is a property in C#?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  20. How does the &lt;code&gt;IEnumerable&lt;/code&gt; interface work in C#?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;IEnumerable&lt;/code&gt; is an interface that provides an enumerator, allowing a collection to be iterated using a foreach loop.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  21. Explain the concept of indexers in C#.
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Indexers allow objects to be indexed like arrays, providing a convenient way to access elements within a class.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  22. What is the purpose of the &lt;code&gt;using&lt;/code&gt; directive in C#?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;using&lt;/code&gt; directive is used to include namespaces in a program, allowing the use of types defined in those namespaces without fully qualifying the type names.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  23. Describe the concept of events in C#.
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Events are a mechanism for communication between objects. They allow a class to notify other classes or objects when an action or state change occurs.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  24. How does the &lt;code&gt;lock&lt;/code&gt; statement work in C#?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;lock&lt;/code&gt; statement is used to synchronize access to a shared resource by acquiring a mutual exclusion lock.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  25. What is the role of the &lt;code&gt;params&lt;/code&gt; keyword in C#?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;params&lt;/code&gt; keyword allows a method to accept a variable number of parameters, making it more flexible.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  26. Explain the concept of nullable types in C#.
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Nullable types allow variables to be assigned a value or &lt;code&gt;null&lt;/code&gt;. They are useful when dealing with databases or other situations where values may be missing.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  27. What is the purpose of the &lt;code&gt;ref&lt;/code&gt; keyword in C#?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;ref&lt;/code&gt; keyword is used to pass a parameter by reference, allowing the called method to modify the value of the parameter.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  28. Differentiate between &lt;code&gt;interface&lt;/code&gt; and &lt;code&gt;abstract class&lt;/code&gt; in C#.
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;An interface cannot contain any implementation, while an abstract class can have both abstract and concrete methods. A class can implement multiple interfaces but can inherit from only one abstract class.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  29. How does the &lt;code&gt;yield&lt;/code&gt; keyword work in C#?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;yield&lt;/code&gt; keyword is used in iterator methods to simplify the implementation of custom iterators.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  30. Explain the concept of boxing and unboxing in C#.
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Boxing is the process of converting a value type to a reference type, and unboxing is the reverse process. It involves converting a reference type back to a value type.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  31. What are the different access modifiers in C#?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The main access modifiers in C# are &lt;code&gt;public&lt;/code&gt;, &lt;code&gt;private&lt;/code&gt;, &lt;code&gt;protected&lt;/code&gt;, &lt;code&gt;internal&lt;/code&gt;, &lt;code&gt;protected internal&lt;/code&gt;, and &lt;code&gt;private protected&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  32. Describe the use of the &lt;code&gt;base&lt;/code&gt; keyword in C#.
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;base&lt;/code&gt; keyword is used to access members of the base class from within a derived class.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  33. What is the purpose of the &lt;code&gt;this&lt;/code&gt; keyword in C#?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;this&lt;/code&gt; keyword is used to refer to the&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;current instance of the class. It is often used to differentiate between instance variables and parameters with the same name.&lt;/p&gt;

&lt;h2&gt;
  
  
  34. Explain the concept of polymorphism in C#.
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Polymorphism allows objects of different types to be treated as objects of a common base type. It includes method overloading and method overriding.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  35. How does the &lt;code&gt;String.Split&lt;/code&gt; method work in C#?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;String.Split&lt;/code&gt; method is used to split a string into an array of substrings based on a specified delimiter.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  36. What is the purpose of the &lt;code&gt;Dispose&lt;/code&gt; method in C#?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;Dispose&lt;/code&gt; method is used to release unmanaged resources held by an object. It is often implemented by classes that use resources such as files or network connections.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  37. Describe the concept of generics in C#.
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Generics allow the creation of classes, interfaces, and methods with placeholder types, providing type safety and code reusability.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  38. How does the &lt;code&gt;Nullable&amp;lt;T&amp;gt;&lt;/code&gt; struct work in C#?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;Nullable&amp;lt;T&amp;gt;&lt;/code&gt; struct allows value types to have a &lt;code&gt;null&lt;/code&gt; value, providing a way to represent the absence of a value.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  39. What is the purpose of the &lt;code&gt;volatile&lt;/code&gt; keyword in C#?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;volatile&lt;/code&gt; keyword is used to indicate that a field might be modified by multiple threads, preventing compiler optimizations that could cause unexpected behavior in a multi-threaded environment.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  40. How does the &lt;code&gt;as&lt;/code&gt; keyword work in C#?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;as&lt;/code&gt; keyword is used for safe type casting. It attempts to cast an object to a specified type and returns &lt;code&gt;null&lt;/code&gt; if the cast fails instead of throwing an exception.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  41. Explain the role of the &lt;code&gt;static&lt;/code&gt; keyword in C#.
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;static&lt;/code&gt; keyword is used to declare a member that belongs to the type itself rather than to a specific instance of the type.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  42. What is the difference between a value type and a reference type in C#?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Value types store the actual data, while reference types store a reference to the memory location where the data is stored.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  43. How does the &lt;code&gt;using&lt;/code&gt; statement work in C# for implementing IDisposable?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;using&lt;/code&gt; statement ensures that the &lt;code&gt;Dispose&lt;/code&gt; method of an object that implements &lt;code&gt;IDisposable&lt;/code&gt; is called, providing a convenient way to manage resources.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  44. Describe the concept of inversion of control (IoC) in C#.
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Inversion of Control is a design principle where the control of the flow of a program is inverted, typically achieved through dependency injection.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  45. Explain the concept of extension methods in C#.
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Extension methods allow adding new methods to existing types without modifying them, enhancing the functionality of types from external libraries.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  46. How does the &lt;code&gt;try&lt;/code&gt; pattern work in C# 7.0 and later?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;try&lt;/code&gt; pattern in C# 7.0 and later allows for pattern matching within the &lt;code&gt;catch&lt;/code&gt; block, enabling more expressive and concise exception handling.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  47. What is the purpose of the &lt;code&gt;nameof&lt;/code&gt; operator in C#?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;nameof&lt;/code&gt; operator is used to obtain the simple (unqualified) string name of a variable, type, or member.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  48. How does the &lt;code&gt;Expression&lt;/code&gt; class work in C#?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;Expression&lt;/code&gt; class in C# allows representing code as data, enabling operations like compiling and manipulating expressions at runtime.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  49. Explain the concept of the &lt;code&gt;async/await&lt;/code&gt; pattern in C#.
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;async/await&lt;/code&gt; pattern is used for asynchronous programming in C#, making it easier to write asynchronous code without using callbacks or blocking threads.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  50. Describe the role of the &lt;code&gt;Thread&lt;/code&gt; class in C#.
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;Thread&lt;/code&gt; class in C# is used to create and control threads, providing methods for starting, pausing, and terminating threads.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What Next?
&lt;/h2&gt;

&lt;p&gt;These are just a few examples of C# interview questions. Depending on the level of expertise required, interview questions may vary from basic to advanced topics. Good luck with your C# interviews!&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>interview</category>
      <category>microsoft</category>
      <category>help</category>
    </item>
    <item>
      <title>Azure | Creating an Azure Storage Account and Uploading Files to a Container</title>
      <dc:creator>Hassan BOLAJRAF</dc:creator>
      <pubDate>Wed, 07 Aug 2024 09:20:26 +0000</pubDate>
      <link>https://dev.to/hbolajraf/azure-creating-an-azure-storage-account-and-uploading-files-to-a-container-1l14</link>
      <guid>https://dev.to/hbolajraf/azure-creating-an-azure-storage-account-and-uploading-files-to-a-container-1l14</guid>
      <description>&lt;blockquote&gt;
&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt; &lt;strong&gt;Note&lt;/strong&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;You can check other posts on my personal website: &lt;a href="https://hbolajraf.net" rel="noopener noreferrer"&gt;https://hbolajraf.net&lt;/a&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjghfiryp8d3x917ik3so.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjghfiryp8d3x917ik3so.png" alt="Image description" width="800" height="268"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this tutorial, we'll walk through creating an Azure Storage account, setting up a container, and uploading a file to it.&lt;br&gt;
Also it is a part of the following &lt;a href="https://hbolajraf.net/posts/Creating-Azure-Logic-Apps-to-integrate-workflows-for-sending-notifications-about-changes-within-a-Blob-Storage-container/" rel="noopener noreferrer"&gt;post&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt; &lt;strong&gt; Note&lt;/strong&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;I highlighted screenshots with a yellow color to focus on some important informations.&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Step 1: Create an Azure Storage Account
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Sign in to the Azure Portal&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Click on &lt;strong&gt;Create a resource&lt;/strong&gt; in the upper left corner.&lt;/li&gt;
&lt;li&gt;Search for &lt;strong&gt;Storage account - blob, file, table, queue&lt;/strong&gt; and click on it.&lt;/li&gt;
&lt;li&gt;Fill in the details:

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Subscription&lt;/strong&gt;: Choose your subscription.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Resource group&lt;/strong&gt;: Create a new one or use an existing one.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Storage account name&lt;/strong&gt;: in my case &lt;code&gt;hbolajraf00strorage&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Location&lt;/strong&gt;: Choose a location.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Performance&lt;/strong&gt;: Standard (recommended).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Account kind&lt;/strong&gt;: StorageV2 (general-purpose v2).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Replication&lt;/strong&gt;: Choose your replication preference.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Access tier&lt;/strong&gt;: Hot (recommended for frequently accessed data).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Review + create&lt;/strong&gt; and then &lt;strong&gt;Create&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmmnr8nj6j9er3n9mmuhd.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmmnr8nj6j9er3n9mmuhd.PNG" alt="Image description" width="800" height="676"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Create a Blob Container
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;After the storage account is created, navigate to it in the Azure Portal.&lt;/li&gt;
&lt;li&gt;In the storage account menu, under &lt;strong&gt;Blob service&lt;/strong&gt;, click on &lt;strong&gt;Containers&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;+ Container&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Name the container as &lt;code&gt;lab-container&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Set the &lt;strong&gt;Public access level&lt;/strong&gt; to &lt;strong&gt;Blob (anonymous read access for blobs only)&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Create&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj82fmvg3p997yj1ukeff.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj82fmvg3p997yj1ukeff.PNG" alt="Image description" width="800" height="731"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Upload a File to the Container
&lt;/h2&gt;

&lt;p&gt;Now let's upload a file to the newly created container.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;In the container view, click &lt;strong&gt;Upload&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Select the file &lt;code&gt;1.PNG&lt;/code&gt; from your local machine.&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Upload&lt;/strong&gt; to add the file to the container.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1nr5qs4ut9m4oorkk2dz.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1nr5qs4ut9m4oorkk2dz.PNG" alt="Image description" width="800" height="356"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What next ?
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;You have successfully created an Azure Storage account named &lt;code&gt;hbolajraf00strorage&lt;/code&gt;, set up a container named &lt;code&gt;lab-container&lt;/code&gt;, and uploaded a file &lt;code&gt;1.PNG&lt;/code&gt; to it. Azure Storage provides a scalable and cost-effective solution for storing and managing your data in the cloud.&lt;br&gt;
Feel free to explore more features of Azure Storage and integrate it with your applications for reliable data storage and access.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>microsoft</category>
      <category>azure</category>
      <category>csharp</category>
      <category>azureaccountstorage</category>
    </item>
    <item>
      <title>Azure | Ways to Connect to Microsoft Azure</title>
      <dc:creator>Hassan BOLAJRAF</dc:creator>
      <pubDate>Mon, 29 Jul 2024 09:35:59 +0000</pubDate>
      <link>https://dev.to/hbolajraf/azure-ways-to-connect-to-microsoft-azure-2ngj</link>
      <guid>https://dev.to/hbolajraf/azure-ways-to-connect-to-microsoft-azure-2ngj</guid>
      <description>&lt;blockquote&gt;
&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt; &lt;strong&gt;Note&lt;/strong&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;You can check other posts on my personal website: &lt;a href="https://hbolajraf.net" rel="noopener noreferrer"&gt;https://hbolajraf.net&lt;/a&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;/blockquote&gt;

&lt;p&gt;Microsoft Azure provides several ways to interact with its services, including the command-line interface (CLI), PowerShell, and other tools. Let's explore each of these options:&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Azure Portal
&lt;/h2&gt;

&lt;p&gt;Azure Portal is a web-based interface for managing Azure resources. It provides a graphical user interface (GUI) for performing various tasks.&lt;/p&gt;

&lt;h3&gt;
  
  
  Connection
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Open a web browser and navigate to the &lt;a href="https://portal.azure.com/" rel="noopener noreferrer"&gt;Azure Portal&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Sign in with your Azure account credentials.&lt;/li&gt;
&lt;li&gt;Once logged in, you can navigate through the Azure Portal to access and manage your resources.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt; &lt;strong&gt; Note&lt;/strong&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;We can also use Azure PowerShell or Azure CLI within the Azure Portal as shown below. Additionally, we can use them locally as will be described in the next sections.&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl8qm3g4u8uf4k56wnbjn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl8qm3g4u8uf4k56wnbjn.png" alt="Image description" width="800" height="375"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Azure CLI
&lt;/h2&gt;

&lt;p&gt;Azure CLI is a command-line tool that provides a set of commands to manage Azure resources. It's available on Windows, macOS, and Linux, and it offers a consistent interface across platforms.&lt;/p&gt;

&lt;h3&gt;
  
  
  Installation
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Windows&lt;/strong&gt;: You can install Azure CLI using the MSI installer available from the &lt;a href="https://docs.microsoft.com/en-us/cli/azure/install-azure-cli-windows" rel="noopener noreferrer"&gt;Azure CLI documentation&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;macOS&lt;/strong&gt;: You can install Azure CLI using Homebrew by running &lt;code&gt;brew install azure-cli&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Linux&lt;/strong&gt;: Installation instructions vary depending on the Linux distribution. You can find them in the &lt;a href="https://docs.microsoft.com/en-us/cli/azure/install-azure-cli-linux" rel="noopener noreferrer"&gt;Azure CLI documentation&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Connection
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;After installation, open a terminal or command prompt.&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;az login&lt;/code&gt; command. This will open a browser window for authentication.&lt;/li&gt;
&lt;li&gt;Once authenticated, you will be connected to your Azure subscription.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  3. Azure PowerShell
&lt;/h2&gt;

&lt;p&gt;Azure PowerShell is a set of cmdlets for managing Azure resources. It's built on top of the PowerShell scripting language and offers deep integration with Windows environments.&lt;/p&gt;

&lt;h3&gt;
  
  
  Installation
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Windows&lt;/strong&gt;: Azure PowerShell is pre-installed on Windows systems starting with Windows 10. However, you can also install the Az module by running &lt;code&gt;Install-Module -Name Az&lt;/code&gt; in PowerShell.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;macOS and Linux&lt;/strong&gt;: PowerShell Core is available for non-Windows platforms. After installing PowerShell Core, you can install the Az module similarly to Windows.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Connection
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Open PowerShell.&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;Connect-AzAccount&lt;/code&gt; to sign in to Azure.&lt;/li&gt;
&lt;li&gt;Follow the authentication prompts to complete the sign-in process.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  4. Azure SDK for .NET
&lt;/h2&gt;

&lt;p&gt;Azure SDKs are available for various programming languages, including .NET, Python, Java, Node.js, and others. As a .Net developper we will take the Azure SDK for .NET as example, so it allows you to interact with Azure services using .NET programming languages like C#. It provides libraries and APIs to integrate Azure functionality into your applications.&lt;/p&gt;

&lt;h3&gt;
  
  
  Installation
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;You can install the Azure SDK for .NET via NuGet Package Manager or directly from Visual Studio.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Connection
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Add the required NuGet packages to your project. For example, for Azure Storage, you would use the &lt;code&gt;Azure.Storage.Blobs&lt;/code&gt; package.&lt;/li&gt;
&lt;li&gt;Authenticate with Azure using Azure Active Directory (AD) credentials or a connection string.&lt;/li&gt;
&lt;li&gt;Use the SDK's classes and methods to interact with Azure services programmatically.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What next ?
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Microsoft Azure offers multiple ways to connect and manage your resources, catering to different preferences and use cases. Whether you prefer command-line interfaces, scripting, graphical user interfaces, or programmatic access, Azure provides tools to suit your needs. Choose the method that best fits your workflow and start managing your Azure resources efficiently.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>microsoft</category>
      <category>azure</category>
      <category>cli</category>
      <category>azureportal</category>
    </item>
    <item>
      <title>Git | Connecting to GitHub and Pushing Changes Using SSH on Windows</title>
      <dc:creator>Hassan BOLAJRAF</dc:creator>
      <pubDate>Mon, 29 Jul 2024 09:31:24 +0000</pubDate>
      <link>https://dev.to/hbolajraf/git-connecting-to-github-and-pushing-changes-using-ssh-on-windows-2f5</link>
      <guid>https://dev.to/hbolajraf/git-connecting-to-github-and-pushing-changes-using-ssh-on-windows-2f5</guid>
      <description>&lt;blockquote&gt;
&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt; &lt;strong&gt;Note&lt;/strong&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;You can check other posts on my personal website: &lt;a href="https://hbolajraf.net" rel="noopener noreferrer"&gt;https://hbolajraf.net&lt;/a&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;/blockquote&gt;

&lt;p&gt;Connecting to GitHub and pushing changes using SSH on a Windows machine involves several steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Generate SSH Key Pair&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Add SSH Key to GitHub Account&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Configure SSH for GitHub&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Clone Repository or Add Remote URL&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Commit and Push Changes&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Step-by-Step Instructions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Generate SSH Key Pair
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Open Git Bash on your Windows machine.&lt;/li&gt;
&lt;li&gt;Generate a new SSH key pair by running:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   ssh-keygen &lt;span class="nt"&gt;-t&lt;/span&gt; rsa &lt;span class="nt"&gt;-b&lt;/span&gt; 4096 &lt;span class="nt"&gt;-C&lt;/span&gt; &lt;span class="s2"&gt;"hassan.bolajraf@gmail.com"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace &lt;code&gt;hassan.bolajraf@gmail.com&lt;/code&gt; with your GitHub email address. When prompted, save the key to the default location (&lt;code&gt;/c/Users/your_username/.ssh/id_rsa&lt;/code&gt;) and optionally set a passphrase.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Add SSH Key to GitHub Account
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Copy the SSH key to your clipboard. You can use the following command to do this:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   &lt;span class="nb"&gt;cat&lt;/span&gt; ~/.ssh/id_rsa.pub | clip
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Log in to your GitHub account.&lt;/li&gt;
&lt;li&gt;Go to &lt;strong&gt;Settings&lt;/strong&gt; &amp;gt; &lt;strong&gt;SSH and GPG keys&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;New SSH key&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Paste the copied SSH key into the key field and give it a descriptive title.&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Add SSH key&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5rlnrpwyehx331va1lgh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5rlnrpwyehx331va1lgh.png" alt="Image description" width="800" height="274"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Configure SSH for GitHub
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Ensure your SSH agent is running. Start the SSH agent by running:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   &lt;span class="nb"&gt;eval&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;ssh-agent &lt;span class="nt"&gt;-s&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Add your SSH private key to the SSH agent:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   ssh-add ~/.ssh/id_rsa
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. Clone Repository or Add Remote URL
&lt;/h3&gt;

&lt;p&gt;If you haven't cloned your repository yet, you can do so using SSH:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Go to your repository on GitHub.&lt;/li&gt;
&lt;li&gt;Click on the &lt;strong&gt;Code&lt;/strong&gt; button and copy the SSH URL (it looks like &lt;code&gt;git@github.com:username/repository.git&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Clone the repository:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   git clone git@github.com:username/repository.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you already have a repository cloned using HTTPS and want to switch to SSH, you can change the remote URL:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Navigate to your repository directory in Git Bash.&lt;/li&gt;
&lt;li&gt;Change the remote URL:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   git remote set-url origin git@github.com:username/repository.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  5. Commit and Push Changes
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Make your changes and stage them for commit:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   git add &lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Commit your changes:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   git commit &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s2"&gt;"Your commit message"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Push your changes to GitHub:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   git push origin main
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace &lt;code&gt;main&lt;/code&gt; with the name of your branch if you're working on a different branch.&lt;/p&gt;

&lt;h2&gt;
  
  
  Additional Tips
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Verify SSH Connection&lt;/strong&gt;: You can test your SSH connection to GitHub by running:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  ssh &lt;span class="nt"&gt;-T&lt;/span&gt; git@github.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This should return a success message like &lt;code&gt;Hi username! You've successfully authenticated&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;SSH Key Management&lt;/strong&gt;: If you manage multiple SSH keys, you can create a &lt;code&gt;~/.ssh/config&lt;/code&gt; file to specify which key to use for GitHub:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What next ?
&lt;/h2&gt;

&lt;p&gt;By following these steps, you should be able to connect to GitHub and push changes using SSH on your Windows machine.&lt;/p&gt;

</description>
      <category>git</category>
      <category>github</category>
      <category>sourcecontrol</category>
      <category>ssh</category>
    </item>
    <item>
      <title>C# | Implementing Event Driven Microservices Architecture</title>
      <dc:creator>Hassan BOLAJRAF</dc:creator>
      <pubDate>Mon, 29 Jul 2024 09:23:57 +0000</pubDate>
      <link>https://dev.to/hbolajraf/implementing-event-driven-microservices-architecture-33o9</link>
      <guid>https://dev.to/hbolajraf/implementing-event-driven-microservices-architecture-33o9</guid>
      <description>&lt;blockquote&gt;
&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt; &lt;strong&gt;Note&lt;/strong&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;You can check other posts on my personal website: &lt;a href="https://hbolajraf.net" rel="noopener noreferrer"&gt;https://hbolajraf.net&lt;/a&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this guide, we'll explore how to implement an event-driven microservices architecture using .NET technologies, specifically focusing on C# examples. Event-driven architecture (EDA) offers numerous benefits such as scalability, loose coupling, and resilience, making it a popular choice for building modern distributed systems. We'll cover the key concepts and demonstrate how to design and implement event-driven microservices in .NET.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction to Event-Driven Microservices
&lt;/h2&gt;

&lt;p&gt;Event-driven microservices architecture is a distributed approach where services communicate through events asynchronously. This means services produce and consume events without direct coupling, allowing for better scalability, flexibility, and fault isolation. Key components of event-driven architecture include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Events&lt;/strong&gt;: Immutable messages representing a change or an occurrence in the system.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Event producers&lt;/strong&gt;: Services that generate and publish events.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Event consumers&lt;/strong&gt;: Services that subscribe to events and react accordingly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Message brokers&lt;/strong&gt;: Middleware responsible for routing and delivering events between producers and consumers.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Setting Up the Development Environment
&lt;/h2&gt;

&lt;p&gt;Before diving into coding, ensure you have the following prerequisites installed:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://dotnet.microsoft.com/download" rel="noopener noreferrer"&gt;.NET SDK&lt;/a&gt; for your platform&lt;/li&gt;
&lt;li&gt;IDE such as Visual Studio or Visual Studio Code&lt;/li&gt;
&lt;li&gt;Messaging middleware (e.g., RabbitMQ, Kafka)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Defining Events and Messages
&lt;/h2&gt;

&lt;p&gt;Events in an event-driven architecture represent meaningful occurrences within the system. Define clear event schemas using a language-neutral format such as JSON or Protobuf. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"eventId"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"guid"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"eventType"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"OrderPlaced"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"timestamp"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2024-02-18T12:00:00Z"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"payload"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"orderId"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"12345"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"customerId"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"67890"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"totalAmount"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mf"&gt;100.00&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ensure that events are immutable once published to maintain consistency and reliability.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementing Microservices with C#
&lt;/h2&gt;

&lt;p&gt;When implementing microservices in .NET, follow these best practices:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use lightweight frameworks such as ASP.NET Core for building microservices.&lt;/li&gt;
&lt;li&gt;Design services around business domains to achieve proper encapsulation and separation of concerns.&lt;/li&gt;
&lt;li&gt;Implement each microservice as an independent deployable unit, focusing on a single responsibility.&lt;/li&gt;
&lt;li&gt;Utilize C# features like async/await for asynchronous communication and handling.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Example of a simple order service&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OrderService&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;readonly&lt;/span&gt; &lt;span class="n"&gt;IMessageBus&lt;/span&gt; &lt;span class="n"&gt;_messageBus&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;OrderService&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IMessageBus&lt;/span&gt; &lt;span class="n"&gt;messageBus&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_messageBus&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;messageBus&lt;/span&gt;&lt;span class="p"&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;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt; &lt;span class="nf"&gt;PlaceOrderAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Order&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Process order logic...&lt;/span&gt;

        &lt;span class="c1"&gt;// Publish order placed event&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;orderPlacedEvent&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;OrderPlacedEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CustomerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;TotalAmount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;_messageBus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;PublishAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"OrderPlaced"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;orderPlacedEvent&lt;/span&gt;&lt;span class="p"&gt;);&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;h2&gt;
  
  
  Using Messaging Middleware
&lt;/h2&gt;

&lt;p&gt;Choose a messaging middleware that suits your requirements. Popular options include RabbitMQ, Kafka, and Azure Service Bus. Configure the middleware to ensure reliable event delivery and fault tolerance.&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="c1"&gt;// Example of RabbitMQ setup in .NET&lt;/span&gt;
&lt;span class="n"&gt;services&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;AddMassTransit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;AddBus&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;provider&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Bus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Factory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;CreateUsingRabbitMq&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cfg&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;cfg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Host&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Uri&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"rabbitmq://localhost/"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Username&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hbolajraf"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Password&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"azerty:)"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;});&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;h2&gt;
  
  
  Handling Event Consumption and Processing
&lt;/h2&gt;

&lt;p&gt;Consuming events involves subscribing to event topics and executing appropriate business logic. Use message handler patterns to process events efficiently.&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="c1"&gt;// Example of event consumer in .NET&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OrderPlacedConsumer&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;IConsumer&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;OrderPlacedEvent&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;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt; &lt;span class="nf"&gt;Consume&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ConsumeContext&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;OrderPlacedEvent&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;orderPlacedEvent&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Message&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="c1"&gt;// Process order placed event...&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;h2&gt;
  
  
  Testing and Deployment
&lt;/h2&gt;

&lt;p&gt;Test each microservice in isolation using unit tests, integration tests, and contract tests. Deploy microservices independently using containerization (e.g., Docker) and orchestration tools (e.g., Kubernetes).&lt;/p&gt;

&lt;h2&gt;
  
  
  What Next?
&lt;/h2&gt;

&lt;p&gt;Event-driven microservices architecture offers a scalable and flexible approach to building distributed systems. By leveraging .NET and C#, you can design robust microservices that communicate asynchronously through events, enabling better decoupling and resilience. With the right tools and practices, you can develop and deploy event-driven microservices efficiently in .NET ecosystem.&lt;/p&gt;

</description>
      <category>microsoft</category>
      <category>csharp</category>
      <category>architecture</category>
      <category>eventdriven</category>
    </item>
    <item>
      <title>C# | Types of Classes</title>
      <dc:creator>Hassan BOLAJRAF</dc:creator>
      <pubDate>Thu, 25 Jul 2024 10:08:46 +0000</pubDate>
      <link>https://dev.to/hbolajraf/c-types-of-classes-477c</link>
      <guid>https://dev.to/hbolajraf/c-types-of-classes-477c</guid>
      <description>&lt;blockquote&gt;
&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt; &lt;strong&gt;Note&lt;/strong&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;You can check other posts on my personal website: &lt;a href="https://hbolajraf.net" rel="noopener noreferrer"&gt;https://hbolajraf.net&lt;/a&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;/blockquote&gt;

&lt;p&gt;In C#, classes are the building blocks of object-oriented programming. They encapsulate data for the object and define methods to manipulate that data. There are various types of classes in C#, each serving different purposes. Let's explore them with detailed examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Regular Classes
&lt;/h2&gt;

&lt;p&gt;Regular classes are the most common type of classes in C#. They are used to define objects and their behavior.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example:
&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;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;Age&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&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;PrintDetails&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="s"&gt;$"Name: &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="s"&gt;, Age: &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;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&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;In this example, we define a &lt;code&gt;Person&lt;/code&gt; class with properties &lt;code&gt;Name&lt;/code&gt; and &lt;code&gt;Age&lt;/code&gt;, and a method &lt;code&gt;PrintDetails()&lt;/code&gt; to print the person's details.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Abstract Classes
&lt;/h2&gt;

&lt;p&gt;Abstract classes are used as base classes and cannot be instantiated directly. They can contain abstract methods that must be implemented by derived classes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example:
&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;public&lt;/span&gt; &lt;span class="k"&gt;abstract&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Shape&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;abstract&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;Area&lt;/span&gt;&lt;span class="p"&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;class&lt;/span&gt; &lt;span class="nc"&gt;Rectangle&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Shape&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;Length&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;Width&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&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;override&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;Area&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;Length&lt;/span&gt; &lt;span class="p"&gt;*&lt;/span&gt; &lt;span class="n"&gt;Width&lt;/span&gt;&lt;span class="p"&gt;;&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;In this example, &lt;code&gt;Shape&lt;/code&gt; is an abstract class with an abstract method &lt;code&gt;Area()&lt;/code&gt;. &lt;code&gt;Rectangle&lt;/code&gt; is a concrete class that derives from &lt;code&gt;Shape&lt;/code&gt; and implements the &lt;code&gt;Area()&lt;/code&gt; method.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Static Classes
&lt;/h2&gt;

&lt;p&gt;Static classes cannot be instantiated and can only contain static members. They are often used to group related utility methods together.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example:
&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;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MathUtils&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;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&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;static&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;Multiply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="p"&gt;*&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&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;In this example, &lt;code&gt;MathUtils&lt;/code&gt; is a static class containing static methods for addition and multiplication.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Sealed Classes
&lt;/h2&gt;

&lt;p&gt;Sealed classes cannot be inherited. They are often used to prevent further derivation or to increase security.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example:
&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;public&lt;/span&gt; &lt;span class="k"&gt;sealed&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;FinalClass&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;Method&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="s"&gt;"Method in FinalClass"&lt;/span&gt;&lt;span class="p"&gt;);&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;In this example, &lt;code&gt;FinalClass&lt;/code&gt; is a sealed class that cannot be inherited by other classes.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Partial Classes
&lt;/h2&gt;

&lt;p&gt;Partial classes allow a class's members to be defined in multiple files. They are often used in large projects to organize code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;

&lt;p&gt;File 1 (&lt;code&gt;Person.cs&lt;/code&gt;):&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;public&lt;/span&gt; &lt;span class="k"&gt;partial&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;Name&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&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;File 2 (&lt;code&gt;PersonAdditional.cs&lt;/code&gt;):&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;public&lt;/span&gt; &lt;span class="k"&gt;partial&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;Age&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;get&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;set&lt;/span&gt;&lt;span class="p"&gt;;&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;In this example, &lt;code&gt;Person&lt;/code&gt; is defined across two files, each containing a partial definition of the class.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Next?
&lt;/h2&gt;

&lt;p&gt;Understanding the different types of classes in C# is crucial for building robust and maintainable applications. Whether it's regular classes for defining objects, abstract classes for defining base behavior, static classes for utility methods, sealed classes for security, or partial classes for organizing code, each type has its own purpose and use cases. Choose the appropriate type based on your requirements and design principles.&lt;/p&gt;

</description>
      <category>microsoft</category>
      <category>csharp</category>
      <category>development</category>
    </item>
    <item>
      <title>C# | Difference Between Array and ArrayList</title>
      <dc:creator>Hassan BOLAJRAF</dc:creator>
      <pubDate>Thu, 25 Jul 2024 10:06:59 +0000</pubDate>
      <link>https://dev.to/hbolajraf/c-difference-between-array-and-arraylist-1aej</link>
      <guid>https://dev.to/hbolajraf/c-difference-between-array-and-arraylist-1aej</guid>
      <description>&lt;blockquote&gt;
&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt; &lt;strong&gt;Note&lt;/strong&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;You can check other posts on my personal website: &lt;a href="https://hbolajraf.net" rel="noopener noreferrer"&gt;https://hbolajraf.net&lt;/a&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;/blockquote&gt;

&lt;p&gt;In C#, both arrays and ArrayLists are used to store collections of elements, but they have different characteristics and behaviors. Let's explore the differences between them with detailed examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  Arrays
&lt;/h2&gt;

&lt;p&gt;Arrays are fixed-size data structures that store elements of the same type in contiguous memory locations. Once an array is created, its size cannot be changed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example of Array in C
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Declaring an array of integers with a fixed size of 5&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;5&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="c1"&gt;// Initializing array elements&lt;/span&gt;
&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;20&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;30&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;40&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;50&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Accessing array elements&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;numbers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; &lt;span class="c1"&gt;// Output: 30&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Characteristics of Arrays
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Fixed size: The size of an array is determined at compile time and cannot be changed at runtime.&lt;/li&gt;
&lt;li&gt;Type-safe: Arrays can only store elements of the same type.&lt;/li&gt;
&lt;li&gt;Better performance: Arrays offer better performance in terms of accessing elements because they store elements in contiguous memory locations.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  ArrayLists
&lt;/h2&gt;

&lt;p&gt;ArrayLists are dynamic collections that can grow or shrink in size dynamically. They can store elements of different types and automatically resize themselves as needed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example of ArrayList in C
&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;using&lt;/span&gt; &lt;span class="nn"&gt;System&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System.Collections&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Program&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Creating an ArrayList&lt;/span&gt;
        &lt;span class="n"&gt;ArrayList&lt;/span&gt; &lt;span class="n"&gt;list&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;ArrayList&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="c1"&gt;// Adding elements to the ArrayList&lt;/span&gt;
        &lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;3.14&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// Accessing elements in the ArrayList&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;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; &lt;span class="c1"&gt;// Output: Hello&lt;/span&gt;

        &lt;span class="c1"&gt;// Iterating through the ArrayList&lt;/span&gt;
        &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;item&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;list&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;item&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&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;h3&gt;
  
  
  Characteristics of ArrayLists
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Dynamic size: ArrayLists can grow or shrink in size dynamically as elements are added or removed.&lt;/li&gt;
&lt;li&gt;Heterogeneous elements: ArrayLists can store elements of different types.&lt;/li&gt;
&lt;li&gt;Slower performance: ArrayLists may have slower performance compared to arrays because they involve additional overhead for resizing and boxing/unboxing operations.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What Next?
&lt;/h2&gt;

&lt;p&gt;Both arrays and ArrayLists have their own advantages and use cases. Arrays are suitable for situations where the size of the collection is known and fixed, and when performance is critical. ArrayLists, on the other hand, are more flexible and convenient for scenarios where the size of the collection may change dynamically or when storing elements of different types. Choose the appropriate data structure based on the specific requirements of your application.&lt;/p&gt;

</description>
      <category>microsoft</category>
      <category>csharp</category>
      <category>development</category>
    </item>
    <item>
      <title>C# | Unit Tests with xUnit and Complex Inline Data Object</title>
      <dc:creator>Hassan BOLAJRAF</dc:creator>
      <pubDate>Thu, 25 Jul 2024 10:04:51 +0000</pubDate>
      <link>https://dev.to/hbolajraf/c-unit-tests-with-xunit-and-complex-inline-data-object-312h</link>
      <guid>https://dev.to/hbolajraf/c-unit-tests-with-xunit-and-complex-inline-data-object-312h</guid>
      <description>&lt;blockquote&gt;
&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt; &lt;strong&gt;Note&lt;/strong&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;You can check other posts on my personal website: &lt;a href="https://hbolajraf.net" rel="noopener noreferrer"&gt;https://hbolajraf.net&lt;/a&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this post, we will explore how to create unit tests using xUnit, a popular unit testing framework for .NET, with complex inline data object theory methods examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  Introduction to xUnit
&lt;/h2&gt;

&lt;p&gt;xUnit is an open-source unit testing tool for the .NET Framework. It is highly extensible and provides various features to simplify the process of writing and executing unit tests.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up xUnit
&lt;/h2&gt;

&lt;p&gt;Before writing unit tests, make sure you have installed the xUnit framework in your project. You can do this using NuGet Package Manager or .NET CLI.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dotnet add package xunit
dotnet add package xunit.runner.visualstudio
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Writing Unit Tests with Complex Inline Data Object
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Example Scenario
&lt;/h3&gt;

&lt;p&gt;Let's assume we have a class &lt;code&gt;Calculator&lt;/code&gt; with a method &lt;code&gt;Add&lt;/code&gt;, which takes two integers as input and returns their sum. We want to test this method with different sets of input data, including complex inline data objects.&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;using&lt;/span&gt; &lt;span class="nn"&gt;Xunit&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;class&lt;/span&gt; &lt;span class="nc"&gt;Calculator&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&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;h3&gt;
  
  
  Writing Unit Tests
&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;using&lt;/span&gt; &lt;span class="nn"&gt;Xunit&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;class&lt;/span&gt; &lt;span class="nc"&gt;CalculatorTests&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;readonly&lt;/span&gt; &lt;span class="n"&gt;Calculator&lt;/span&gt; &lt;span class="n"&gt;_calculator&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;CalculatorTests&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_calculator&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Calculator&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Theory&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;InlineData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="c1"&gt;// Simple inline data&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;InlineData&lt;/span&gt;&lt;span class="p"&gt;(-&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="m"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="c1"&gt;// Simple inline data with negative numbers&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;InlineData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="c1"&gt;// Simple inline data with zeros&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;InlineData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;500&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1500&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="c1"&gt;// Simple inline data with large numbers&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;InlineData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="c1"&gt;// Simple inline data with mixed positive and negative numbers&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;InlineData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;2147483647&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="m"&gt;2147483648&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="c1"&gt;// Simple inline data with maximum integer value&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;InlineData&lt;/span&gt;&lt;span class="p"&gt;(-&lt;/span&gt;&lt;span class="m"&gt;2147483648&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;2147483647&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="c1"&gt;// Simple inline data with minimum integer value&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;InlineData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MaxValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MaxValue&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="c1"&gt;// Simple inline data with maximum integer value and zero&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;InlineData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MinValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MinValue&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="c1"&gt;// Simple inline data with minimum integer value and zero&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;InlineData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;2147483647&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="m"&gt;2147483648&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="c1"&gt;// Simple inline data with maximum and minimum integer values&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;InlineData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MaxValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MaxValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="c1"&gt;// Simple inline data with two maximum integer values&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;InlineData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MinValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MinValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="c1"&gt;// Simple inline data with two minimum integer values&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;InlineData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;2147483647&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;2147483647&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="c1"&gt;// Simple inline data with zero and maximum integer value&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;InlineData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MinValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MinValue&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="c1"&gt;// Simple inline data with zero and minimum integer value&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;InlineData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MinValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MaxValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt; &lt;span class="c1"&gt;// Simple inline data with maximum and minimum integer values&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;Add_WithInlineData_ReturnsExpectedResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;expected&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Arrange&lt;/span&gt;

        &lt;span class="c1"&gt;// Act&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_calculator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// Assert&lt;/span&gt;
        &lt;span class="n"&gt;Assert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Equal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;expected&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Theory&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;ClassData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ComplexInlineDataGenerator&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;Add_WithComplexInlineData_ReturnsExpectedResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;expected&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Arrange&lt;/span&gt;

        &lt;span class="c1"&gt;// Act&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_calculator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// Assert&lt;/span&gt;
        &lt;span class="n"&gt;Assert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Equal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;expected&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&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;class&lt;/span&gt; &lt;span class="nc"&gt;ComplexInlineDataGenerator&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;IEnumerable&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;object&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;]&amp;gt;&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;readonly&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;object&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;]&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;_data&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;object&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;]&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;object&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;5&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;object&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="m"&gt;5&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;object&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;object&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="m"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;500&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1500&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;object&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;object&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="m"&gt;2147483647&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="m"&gt;2147483648&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;object&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="m"&gt;2147483648&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;2147483647&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;object&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MaxValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MaxValue&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;object&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MinValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MinValue&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;object&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="m"&gt;2147483647&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="m"&gt;2147483648&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;object&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MaxValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MaxValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;object&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MinValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MinValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;object&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;2147483647&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;2147483647&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;object&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MinValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MinValue&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;object&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MinValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MaxValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;IEnumerator&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;object&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;]&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;GetEnumerator&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;_data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetEnumerator&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="n"&gt;IEnumerator&lt;/span&gt; &lt;span class="n"&gt;IEnumerable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetEnumerator&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;GetEnumerator&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;In this example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We have a &lt;code&gt;CalculatorTests&lt;/code&gt; class containing tests for the &lt;code&gt;Add&lt;/code&gt; method of the &lt;code&gt;Calculator&lt;/code&gt; class.&lt;/li&gt;
&lt;li&gt;We use &lt;code&gt;[Theory]&lt;/code&gt; attribute to denote parameterized tests.&lt;/li&gt;
&lt;li&gt;We use &lt;code&gt;[InlineData]&lt;/code&gt; attribute to provide inline data for simple test cases.&lt;/li&gt;
&lt;li&gt;We define a custom data generator class &lt;code&gt;ComplexInlineDataGenerator&lt;/code&gt; implementing &lt;code&gt;IEnumerable&amp;lt;object[]&amp;gt;&lt;/code&gt; to generate complex inline data.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;ComplexInlineDataGenerator&lt;/code&gt; class provides a collection of test cases with complex inline data.&lt;/li&gt;
&lt;li&gt;Each test case consists of three integers: &lt;code&gt;a&lt;/code&gt;, &lt;code&gt;b&lt;/code&gt;, and the expected result &lt;code&gt;expected&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;We use &lt;code&gt;Assert.Equal&lt;/code&gt; to verify that the actual result matches the expected result.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What Next?
&lt;/h2&gt;

&lt;p&gt;By using xUnit with complex inline data object theory methods examples, we can effectively test methods with various input scenarios, ensuring the correctness and robustness of our code.&lt;/p&gt;

</description>
      <category>microsoft</category>
      <category>csharp</category>
      <category>tdd</category>
      <category>unittest</category>
    </item>
    <item>
      <title>C# | Using NSubstitute NuGet Package for C# Unit Tests</title>
      <dc:creator>Hassan BOLAJRAF</dc:creator>
      <pubDate>Thu, 25 Jul 2024 10:02:55 +0000</pubDate>
      <link>https://dev.to/hbolajraf/using-nsubstitute-nuget-package-for-c-unit-tests-591b</link>
      <guid>https://dev.to/hbolajraf/using-nsubstitute-nuget-package-for-c-unit-tests-591b</guid>
      <description>&lt;blockquote&gt;
&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt; &lt;strong&gt;Note&lt;/strong&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;You can check other posts on my personal website: &lt;a href="https://hbolajraf.net" rel="noopener noreferrer"&gt;https://hbolajraf.net&lt;/a&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;/blockquote&gt;

&lt;p&gt;NSubstitute is a powerful mocking library for C# unit tests, allowing developers to easily create substitute objects (mocks) for dependencies. These substitutes can be configured to behave in specific ways, making them ideal for isolating the unit under test and verifying its interactions with dependencies. In this guide, we'll explore how to use NSubstitute in your C# unit tests with detailed examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installation
&lt;/h2&gt;

&lt;p&gt;Before you can start using NSubstitute, you need to install the NSubstitute NuGet package in your C# project. You can do this via the NuGet Package Manager Console or through the Visual Studio UI:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Install-Package NSubstitute
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alternatively, you can search for "NSubstitute" in the NuGet Package Manager UI and install it from there.&lt;/p&gt;

&lt;h2&gt;
  
  
  Basic Usage
&lt;/h2&gt;

&lt;p&gt;Let's start with a basic example of using NSubstitute to mock an interface. Suppose we have the following interface representing a data repository:&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;public&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;IDataRepository&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nf"&gt;GetData&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;Now, let's create a unit test for a class (&lt;code&gt;DataProcessor&lt;/code&gt;) that depends on this interface. We'll use NSubstitute to mock the &lt;code&gt;IDataRepository&lt;/code&gt; interface:&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;using&lt;/span&gt; &lt;span class="nn"&gt;NSubstitute&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Xunit&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;class&lt;/span&gt; &lt;span class="nc"&gt;DataProcessorTests&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;Fact&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;ProcessData_WhenDataIsAvailable_ReturnsProcessedData&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Arrange&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;mockRepository&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Substitute&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;For&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;IDataRepository&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;
        &lt;span class="n"&gt;mockRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetData&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;Returns&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Mocked Data"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;dataProcessor&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;DataProcessor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mockRepository&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// Act&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;dataProcessor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ProcessData&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="c1"&gt;// Assert&lt;/span&gt;
        &lt;span class="n"&gt;Assert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Equal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Processed Mocked Data"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&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;In this example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We create a substitute for the &lt;code&gt;IDataRepository&lt;/code&gt; interface using &lt;code&gt;Substitute.For&amp;lt;IDataRepository&amp;gt;()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;We configure the substitute's behavior using the &lt;code&gt;Returns&lt;/code&gt; method to return "Mocked Data" when the &lt;code&gt;GetData&lt;/code&gt; method is called.&lt;/li&gt;
&lt;li&gt;We then create an instance of &lt;code&gt;DataProcessor&lt;/code&gt;, passing the mock repository as a dependency.&lt;/li&gt;
&lt;li&gt;Finally, we invoke the method under test (&lt;code&gt;ProcessData&lt;/code&gt;) and verify its behavior.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Advanced Usage
&lt;/h2&gt;

&lt;p&gt;NSubstitute provides various advanced features, such as argument matching, configuring return values based on arguments, and verifying method calls. Let's look at some examples:&lt;/p&gt;

&lt;h3&gt;
  
  
  Argument Matching
&lt;/h3&gt;

&lt;p&gt;You can use argument matching to specify different behaviors based on method arguments. 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="n"&gt;mockRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Arg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Any&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;()).&lt;/span&gt;&lt;span class="nf"&gt;Returns&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Mocked Data"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This configuration will return "Mocked Data" regardless of the integer argument passed to &lt;code&gt;GetData&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Verifying Method Calls
&lt;/h3&gt;

&lt;p&gt;You can verify that specific methods were called on the substitute and optionally specify the number of times they were called. 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="n"&gt;mockRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Received&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;GetData&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;mockRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Received&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;GetData&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Verifies that GetData was called exactly 3 times.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Configuring Callbacks
&lt;/h3&gt;

&lt;p&gt;You can configure substitutes to perform custom actions when methods are called. 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="n"&gt;mockRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetData&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;Returns&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"Mocked Data"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This configuration uses a lambda expression to return "Mocked Data" based on the arguments passed to &lt;code&gt;GetData&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Next?
&lt;/h2&gt;

&lt;p&gt;NSubstitute is a powerful mocking library that simplifies unit testing in C#. By allowing developers to create substitutes for dependencies with specific behaviors, NSubstitute enables more effective isolation of units under test and verification of their interactions. With its intuitive syntax and advanced features, NSubstitute is a valuable tool for writing robust and maintainable unit tests in C#.&lt;/p&gt;

&lt;p&gt;For more information on NSubstitute, refer to the &lt;a href="https://nsubstitute.github.io/help/getting-started/" rel="noopener noreferrer"&gt;official documentation&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>microsoft</category>
      <category>csharp</category>
      <category>tdd</category>
      <category>unittest</category>
    </item>
    <item>
      <title>C# | Understanding the Observer Pattern</title>
      <dc:creator>Hassan BOLAJRAF</dc:creator>
      <pubDate>Tue, 23 Jul 2024 10:59:11 +0000</pubDate>
      <link>https://dev.to/hbolajraf/c-understanding-the-observer-pattern-1nm6</link>
      <guid>https://dev.to/hbolajraf/c-understanding-the-observer-pattern-1nm6</guid>
      <description>&lt;blockquote&gt;
&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt; &lt;strong&gt;Note&lt;/strong&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;You can check other posts on my personal website: &lt;a href="https://hbolajraf.net" rel="noopener noreferrer"&gt;https://hbolajraf.net&lt;/a&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;/blockquote&gt;

&lt;p&gt;The Observer Pattern is a behavioral design pattern where an object, known as the subject, maintains a list of its dependents, called observers, and notifies them of any state changes, usually by calling one of their methods. This pattern promotes loose coupling between objects, as observers are only aware of the subject and not each other. In C#, this pattern is commonly used in event-driven programming.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementation
&lt;/h2&gt;

&lt;p&gt;Let's understand the Observer Pattern through a detailed example in C#.&lt;/p&gt;

&lt;h3&gt;
  
  
  Subject Interface
&lt;/h3&gt;

&lt;p&gt;First, we define an interface for the subject. This interface will contain methods for registering, unregistering, and notifying observers.&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;public&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;ISubject&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;RegisterObserver&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IObserver&lt;/span&gt; &lt;span class="n"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;UnregisterObserver&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IObserver&lt;/span&gt; &lt;span class="n"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;NotifyObservers&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;h3&gt;
  
  
  Observer Interface
&lt;/h3&gt;

&lt;p&gt;Next, we define an interface for the observer. This interface will contain a method that the subject will call when it needs to notify observers.&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;public&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;IObserver&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Update&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;h3&gt;
  
  
  Concrete Subject
&lt;/h3&gt;

&lt;p&gt;Now, let's implement a concrete subject class that implements the &lt;code&gt;ISubject&lt;/code&gt; interface.&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;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ConcreteSubject&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ISubject&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;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;IObserver&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;observers&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;IObserver&lt;/span&gt;&lt;span class="p"&gt;&amp;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;RegisterObserver&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IObserver&lt;/span&gt; &lt;span class="n"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;observers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;observer&lt;/span&gt;&lt;span class="p"&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;UnregisterObserver&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IObserver&lt;/span&gt; &lt;span class="n"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;observers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Remove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;observer&lt;/span&gt;&lt;span class="p"&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;NotifyObservers&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;observer&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;observers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Update&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="p"&gt;}&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;h3&gt;
  
  
  Concrete Observer
&lt;/h3&gt;

&lt;p&gt;Next, let's implement a concrete observer class that implements the &lt;code&gt;IObserver&lt;/code&gt; interface.&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;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ConcreteObserver&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;IObserver&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;Update&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="s"&gt;"Observer notified of state change."&lt;/span&gt;&lt;span class="p"&gt;);&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;h3&gt;
  
  
  Example Usage
&lt;/h3&gt;

&lt;p&gt;Now, let's see how we can use these classes together.&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;class&lt;/span&gt; &lt;span class="nc"&gt;Program&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;ConcreteSubject&lt;/span&gt; &lt;span class="n"&gt;subject&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;ConcreteSubject&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;ConcreteObserver&lt;/span&gt; &lt;span class="n"&gt;observer1&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;ConcreteObserver&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;ConcreteObserver&lt;/span&gt; &lt;span class="n"&gt;observer2&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;ConcreteObserver&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="n"&gt;subject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;RegisterObserver&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;observer1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;subject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;RegisterObserver&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;observer2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="n"&gt;subject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;NotifyObservers&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="n"&gt;subject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;UnregisterObserver&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;observer1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="n"&gt;subject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;NotifyObservers&lt;/span&gt;&lt;span class="p"&gt;();&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;In this example, &lt;code&gt;ConcreteSubject&lt;/code&gt; is the subject, and &lt;code&gt;ConcreteObserver&lt;/code&gt; is the observer. When &lt;code&gt;NotifyObservers()&lt;/code&gt; is called, both observers are notified of the state change. After unregistering one observer, only the remaining observer is notified.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Next?
&lt;/h2&gt;

&lt;p&gt;The Observer Pattern is a powerful way to implement communication between objects in C#. It promotes loose coupling and can be particularly useful in event-driven architectures. By understanding and implementing this pattern, you can write more maintainable and scalable code.&lt;/p&gt;

</description>
      <category>microsoft</category>
      <category>csharp</category>
      <category>designpatterns</category>
    </item>
    <item>
      <title>C# | Avoid Multiple Nested If-Else Statements Using Guard Clause</title>
      <dc:creator>Hassan BOLAJRAF</dc:creator>
      <pubDate>Tue, 23 Jul 2024 10:51:27 +0000</pubDate>
      <link>https://dev.to/hbolajraf/c-avoid-multiple-nested-if-else-statements-using-guard-clause-2ng4</link>
      <guid>https://dev.to/hbolajraf/c-avoid-multiple-nested-if-else-statements-using-guard-clause-2ng4</guid>
      <description>&lt;blockquote&gt;
&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt; &lt;strong&gt;Note&lt;/strong&gt;
&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;You can check other posts on my personal website: &lt;a href="https://hbolajraf.net" rel="noopener noreferrer"&gt;https://hbolajraf.net&lt;/a&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;/blockquote&gt;

&lt;p&gt;In C#, multiple nested if-else statements can often lead to code that is hard to read, understand, and maintain. Guard Clauses offer a technique to refactor such code and make it more readable and maintainable. In this guide, we'll explore how to avoid multiple nested if-else statements using Guard Clauses in C# with detailed examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are Guard Clauses?
&lt;/h2&gt;

&lt;p&gt;Guard Clauses, also known as Early Returns, are conditional statements placed at the beginning of a method to handle exceptional cases or conditions. Instead of nesting multiple if-else blocks, Guard Clauses provide a clear exit strategy if certain conditions are met, reducing the complexity of the code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example without Guard Clauses
&lt;/h2&gt;

&lt;p&gt;Consider the following C# method that calculates the total price of a product with different discounts based on the customer type:&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;public&lt;/span&gt; &lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="nf"&gt;CalculateTotalPrice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Product&lt;/span&gt; &lt;span class="n"&gt;product&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;CustomerType&lt;/span&gt; &lt;span class="n"&gt;customerType&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="n"&gt;totalPrice&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&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;product&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="kt"&gt;decimal&lt;/span&gt; &lt;span class="n"&gt;basePrice&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;BasePrice&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;customerType&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="n"&gt;CustomerType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Regular&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;totalPrice&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;basePrice&lt;/span&gt; &lt;span class="p"&gt;*&lt;/span&gt; &lt;span class="m"&gt;0.95m&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 5% discount for regular customers&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;else&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;customerType&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="n"&gt;CustomerType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Premium&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;totalPrice&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;basePrice&lt;/span&gt; &lt;span class="p"&gt;*&lt;/span&gt; &lt;span class="m"&gt;0.90m&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 10% discount for premium customers&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;else&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;customerType&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="n"&gt;CustomerType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;VIP&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;totalPrice&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;basePrice&lt;/span&gt; &lt;span class="p"&gt;*&lt;/span&gt; &lt;span class="m"&gt;0.85m&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 15% discount for VIP customers&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;totalPrice&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;This code contains multiple nested if-else statements, which can make it difficult to understand and maintain as more conditions are added.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example with Guard Clauses
&lt;/h2&gt;

&lt;p&gt;Now, let's refactor the above method using Guard Clauses:&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;public&lt;/span&gt; &lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="nf"&gt;CalculateTotalPrice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Product&lt;/span&gt; &lt;span class="n"&gt;product&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;CustomerType&lt;/span&gt; &lt;span class="n"&gt;customerType&lt;/span&gt;&lt;span class="p"&gt;)&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;product&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="k"&gt;return&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// No product, return 0&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="n"&gt;basePrice&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;product&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;BasePrice&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;customerType&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="n"&gt;CustomerType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Regular&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;basePrice&lt;/span&gt; &lt;span class="p"&gt;*&lt;/span&gt; &lt;span class="m"&gt;0.95m&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 5% discount for regular customers&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;customerType&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="n"&gt;CustomerType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Premium&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;basePrice&lt;/span&gt; &lt;span class="p"&gt;*&lt;/span&gt; &lt;span class="m"&gt;0.90m&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 10% discount for premium customers&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;customerType&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="n"&gt;CustomerType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;VIP&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;basePrice&lt;/span&gt; &lt;span class="p"&gt;*&lt;/span&gt; &lt;span class="m"&gt;0.85m&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 15% discount for VIP customers&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;basePrice&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Default price if no specific discount applies&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this refactored version, each special condition is checked at the beginning of the method, and if it's met, the method returns immediately. This approach reduces nesting and makes the code more readable and maintainable.&lt;/p&gt;

&lt;h2&gt;
  
  
  Benefits of Using Guard Clauses
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Readability&lt;/strong&gt;: Guard Clauses make the code easier to understand by removing unnecessary nesting.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Maintainability&lt;/strong&gt;: It's easier to maintain and extend the code as new conditions can be added without adding more nesting.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Debugging&lt;/strong&gt;: Guard Clauses provide clear exit points, making it easier to debug and trace the flow of execution.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What Next?
&lt;/h2&gt;

&lt;p&gt;Guard Clauses offer a cleaner and more maintainable way to handle special conditions in C# methods, especially when dealing with multiple nested if-else statements. By using Guard Clauses, you can improve the readability and maintainability of your code, making it easier to understand and debug.&lt;/p&gt;

</description>
      <category>microsoft</category>
      <category>bestpractices</category>
      <category>csharp</category>
      <category>development</category>
    </item>
  </channel>
</rss>
