<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Guidelines - TheCodeBuzz</title>
	<atom:link href="https://thecodebuzz.com/category/guidelines/feed/" rel="self" type="application/rss+xml" />
	<link>https://thecodebuzz.com</link>
	<description>Best Practices for Software Development</description>
	<lastBuildDate>Sat, 09 Mar 2024 22:19:24 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://thecodebuzz.com/wp-content/uploads/2022/11/cropped-android-chrome-512x512-1-1-51x51.jpg</url>
	<title>Guidelines - TheCodeBuzz</title>
	<link>https://thecodebuzz.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>MongoDB Date query to get records between two dates</title>
		<link>https://thecodebuzz.com/mongodb-date-range-query-greater-than-less-than/</link>
					<comments>https://thecodebuzz.com/mongodb-date-range-query-greater-than-less-than/#comments</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Tue, 11 Jul 2023 02:58:00 +0000</pubDate>
				<category><![CDATA[Guidelines]]></category>
		<category><![CDATA[epoch to date]]></category>
		<category><![CDATA[find-objects-between-two-dates-mongodb]]></category>
		<category><![CDATA[mongodb date]]></category>
		<category><![CDATA[mongodb date between]]></category>
		<category><![CDATA[mongodb date greater than]]></category>
		<category><![CDATA[mongodb date query]]></category>
		<category><![CDATA[mongodb filter by date]]></category>
		<category><![CDATA[mongodb greater than date]]></category>
		<category><![CDATA[mongodb gte]]></category>
		<category><![CDATA[mongodb gte date]]></category>
		<category><![CDATA[mongodb isodate]]></category>
		<category><![CDATA[mongodb query date]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=13542</guid>

					<description><![CDATA[<p>MongoDB query Date</p>
<p>The post <a href="https://thecodebuzz.com/mongodb-date-range-query-greater-than-less-than/">MongoDB Date query to get records between two dates</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading">MongoDB Date query to get records between two dates</h1>



<figure class="wp-block-image size-large is-resized"><img fetchpriority="high" decoding="async" width="1024" height="292" src="https://www.thecodebuzz.com/wp-content/uploads/2020/12/MongoDB-Date-Range-query-greater-than-or-lesss-than-Date-C-query-1024x292.jpg" alt="MongoDB Date query to get records between two dates" class="wp-image-13556" srcset="https://thecodebuzz.com/wp-content/uploads/2020/12/MongoDB-Date-Range-query-greater-than-or-lesss-than-Date-C-query-1024x292.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2020/12/MongoDB-Date-Range-query-greater-than-or-lesss-than-Date-C-query-300x86.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2020/12/MongoDB-Date-Range-query-greater-than-or-lesss-than-Date-C-query-768x219.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2020/12/MongoDB-Date-Range-query-greater-than-or-lesss-than-Date-C-query-1536x438.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2020/12/MongoDB-Date-Range-query-greater-than-or-lesss-than-Date-C-query-785x224.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2020/12/MongoDB-Date-Range-query-greater-than-or-lesss-than-Date-C-query.jpg 1633w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>Today, in this article will see how to write a MongoDB Date query to get records based on <strong>timestamp </strong>or <strong>date </strong>range. </p>



<p></p>



<p>We can easily find the records between the two given dates. These queries are effective means to perform efficient searches.</p>



<p></p>



<p>We will cover the below aspects in today&#8217;s article,</p>



<div class="wp-block-aioseo-table-of-contents"><ul><li><a href="#aioseo-getting-started">Getting started</a></li><li><a href="#aioseo-mongo-date-query-using-cli-or-compass-ui">MongoDB Find objects between Two dates</a><ul><li><a href="#aioseo-date-greater-than-query">Mongo Date greater than query (&#x27;gt&#x27;) &#8211; ISO Date</a><ul><li><a href="#aioseo-command">Command</a></li><li><a href="#aioseo-example">Example</a></li></ul></li><li><a href="#aioseo-mongodb-query-date-without-timestamp">MongoDB query date without timestamp</a><ul><li><a href="#aioseo-example">Example</a></li><li><a href="#aioseo-mongoshell-query">MongoShell query</a></li></ul></li><li><a href="#aioseo-date-less-than-query">Mongo Date Less than query (&#x27;$lt)</a><ul><li><a href="#aioseo-command">Command</a></li><li><a href="#aioseo-example">Example</a></li></ul></li><li><a href="#aioseo-date-greater-than-and-less-than-query">MongoDB query &#8211; For records between greater than ($gte) the date and less than ($lte) dates</a><ul><li><a href="#aioseo-command">Command</a></li><li><a href="#aioseo-examples">Examples</a></li><li><a href="#aioseo-mongo-atlas-ui">Mongo Atlas UI</a></li><li><a href="#aioseo-mongoshell">Mongo shell</a></li></ul></li></ul></li><li><a href="#aioseo-what-if-there-is-no-date-field-in-mongo-schema">What if there is no Date field in Mongo Schema?</a></li><li><a href="#aioseo-what-is-the-difference-between-gt-and-gte-or-lt-and-lte-in-mongodb">What is the difference between $gt and $gte or $lt and $lte in MongoDB?</a></li></ul></div>



<p></p>



<p>We already looked at a simple way of adding or updating a new field to the document in our previous <strong><a href="https://www.thecodebuzz.com/mongodb-add-new-field-every-document-in-mongodb-collection/" target="_blank" rel="noreferrer noopener">MongoDB sample series</a>. </strong></p>



<p></p>



<p></p>



<p>We shall see examples for the MongoDB Date Range filter including a date greater than or less than time or date assuming we already have a date-specific field in the database. </p>



<p></p>



<p>If you don&#8217;t have a date or any timestamp field in the document, you can still use another approach using _Id an <strong>ObjectID </strong>field to get the records between two dates. </p>



<p></p>



<p>Please visit the below article for the same, </p>



<p></p>



<ul class="wp-block-list">
<li><a href="https://www.thecodebuzz.com/query-mongodb-objectid-by-date-mongoshell-compass/" target="_blank" rel="noopener" title="Query MongoDB using ObjectId by Date -MongoShell">Query MongoDB using ObjectId by Date -MongoShell</a></li>
</ul>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-getting-started">Getting started</h2>



<p></p>



<p>Here below is a sample schema or document we shall use for the date range query,</p>



<p></p>



<figure class="wp-block-image size-large is-resized"><img decoding="async" width="700" height="878" src="https://www.thecodebuzz.com/wp-content/uploads/2020/09/Add-the-new-field-to-document-in-a-MongoDB-Collection-using-_Id.jpg" alt="MongoDB query Date " class="wp-image-12234" srcset="https://thecodebuzz.com/wp-content/uploads/2020/09/Add-the-new-field-to-document-in-a-MongoDB-Collection-using-_Id.jpg 700w, https://thecodebuzz.com/wp-content/uploads/2020/09/Add-the-new-field-to-document-in-a-MongoDB-Collection-using-_Id-239x300.jpg 239w, https://thecodebuzz.com/wp-content/uploads/2020/09/Add-the-new-field-to-document-in-a-MongoDB-Collection-using-_Id-415x520.jpg 415w" sizes="(max-width: 700px) 100vw, 700px" /></figure>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-mongo-date-query-using-cli-or-compass-ui"><strong>MongoDB Find objects between Two dates </strong></h2>



<p></p>



<p>Here we will be using the below query to get the documents between two dates in MongoDB Collection.</p>



<p></p>



<h3 class="wp-block-heading" id="aioseo-date-greater-than-query"><strong>Mongo Date greater than query (&#8216;gt&#8217;) &#8211; ISO Date</strong></h3>



<p></p>



<p>The below query gives us all the records matching the Date greater than the query</p>



<p></p>



<h4 class="wp-block-heading" id="aioseo-command">Command </h4>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
{ &lt;Field Name&gt;: { $gt:ISODate(&#039;Date here&#039;) } }

 
</pre></div>


<h4 class="wp-block-heading" id="aioseo-example">Example </h4>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
db.collection.find({ &quot;DateAdded&quot;: { $gt: ISODate(&quot;2023-05-01T00:00:00Z&quot;) } })


</pre></div>


<p></p>



<p></p>



<p></p>



<h3 class="wp-block-heading" id="aioseo-mongodb-query-date-without-timestamp">MongoDB query date without timestamp</h3>



<p></p>



<p>The below query gives us all the records using greater than the query with date only </p>



<h4 class="wp-block-heading" id="aioseo-example"><br>Example </h4>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
db.collection.find({ &quot;DateAdded&quot;: { $gt: ISODate(&quot;2023-05-01&quot;) } })


</pre></div>


<p></p>



<p></p>



<h4 class="wp-block-heading" id="aioseo-mongoshell-query">MongoShell query </h4>



<p></p>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="374" src="https://www.thecodebuzz.com/wp-content/uploads/2021/01/MongoDB-Date-Range-query-greater-than-or-less-thanl-mongo-shell-1024x374.jpg" alt="mongodb date query " class="wp-image-14674" srcset="https://thecodebuzz.com/wp-content/uploads/2021/01/MongoDB-Date-Range-query-greater-than-or-less-thanl-mongo-shell-1024x374.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2021/01/MongoDB-Date-Range-query-greater-than-or-less-thanl-mongo-shell-300x110.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2021/01/MongoDB-Date-Range-query-greater-than-or-less-thanl-mongo-shell-768x280.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2021/01/MongoDB-Date-Range-query-greater-than-or-less-thanl-mongo-shell-1536x561.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2021/01/MongoDB-Date-Range-query-greater-than-or-less-thanl-mongo-shell-2048x748.jpg 2048w, https://thecodebuzz.com/wp-content/uploads/2021/01/MongoDB-Date-Range-query-greater-than-or-less-thanl-mongo-shell-785x287.jpg 785w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<h3 class="wp-block-heading" id="aioseo-date-less-than-query"><strong>Mongo Date Less than query (&#8216;$lt) </strong></h3>



<p></p>



<p>The below query gives us all the records using field value where it matches with the less than the given date.</p>



<p></p>



<h4 class="wp-block-heading" id="aioseo-command"> Command  </h4>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
{ &lt;Field Name&gt;: { $lt:ISODate(&#039;Date here&#039;) } }

</pre></div>


<p></p>



<h4 class="wp-block-heading" id="aioseo-example">Example </h4>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
db.collection.find({ &quot;DateAdded&quot;: { $lt: ISODate(&quot;2023-05-01T00:00:00Z&quot;) } })


</pre></div>


<p></p>



<h3 class="wp-block-heading" id="aioseo-date-greater-than-and-less-than-query"><strong>MongoDB query &#8211; For records between greater than (<code>$gte</code>) the date and less than (<code>$lte</code>) dates</strong></h3>



<p></p>



<p>The below query gives us all the records using field value where it matches with the less than and greater than the given date.</p>



<p></p>



<h4 class="wp-block-heading" id="aioseo-command"> Command   </h4>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
{  &lt;Field name&gt; : { $gt:ISODate(&#039;Date Here&#039;), $lt:ISODate(&#039;Date here&#039;)  }  }


</pre></div>


<p></p>



<h4 class="wp-block-heading" id="aioseo-examples">Examples</h4>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
db.collection.find({ DateAdded : { $gt:ISODate(&#039;2019-09-18T21:07:42.313+00:00&#039;)}, { $lt:ISODate(&#039;2020-09-18T21:07:42.313+00:00&#039;)}})
</pre></div>


<p></p>



<h4 class="wp-block-heading" id="aioseo-mongo-atlas-ui"><strong>Mongo Atlas UI</strong></h4>



<p></p>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" width="1024" height="433" src="https://www.thecodebuzz.com/wp-content/uploads/2020/12/MongoDB-Date-Range-query-examples-CLI-or-compass-1024x433.jpg" alt="MongoDB Date Time query greater or less than examples" class="wp-image-13546" srcset="https://thecodebuzz.com/wp-content/uploads/2020/12/MongoDB-Date-Range-query-examples-CLI-or-compass-1024x433.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2020/12/MongoDB-Date-Range-query-examples-CLI-or-compass-300x127.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2020/12/MongoDB-Date-Range-query-examples-CLI-or-compass-768x325.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2020/12/MongoDB-Date-Range-query-examples-CLI-or-compass-1536x650.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2020/12/MongoDB-Date-Range-query-examples-CLI-or-compass-785x332.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2020/12/MongoDB-Date-Range-query-examples-CLI-or-compass.jpg 1921w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<p></p>



<h4 class="wp-block-heading" id="aioseo-mongoshell"><strong>Mongo shell </strong></h4>



<p> </p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="151" src="https://www.thecodebuzz.com/wp-content/uploads/2021/01/MongoDB-Date-Range-query-greater-than-or-less-than-gt-lt-1024x151.jpg" alt="mongodb-query between two dates,
mongodb query greater than and less than date,
mongodb query date greater than,
mongodb query greater than and less than date in java,
<a href=" class="wp-image-14675" srcset="https://thecodebuzz.com/wp-content/uploads/2021/01/MongoDB-Date-Range-query-greater-than-or-less-than-gt-lt-1024x151.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2021/01/MongoDB-Date-Range-query-greater-than-or-less-than-gt-lt-300x44.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2021/01/MongoDB-Date-Range-query-greater-than-or-less-than-gt-lt-768x113.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2021/01/MongoDB-Date-Range-query-greater-than-or-less-than-gt-lt-1536x226.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2021/01/MongoDB-Date-Range-query-greater-than-or-less-than-gt-lt-2048x301.jpg 2048w, https://thecodebuzz.com/wp-content/uploads/2021/01/MongoDB-Date-Range-query-greater-than-or-less-than-gt-lt-785x116.jpg 785w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-what-if-there-is-no-date-field-in-mongo-schema">What if there is no Date field in Mongo Schema?</h2>



<p></p>



<p>You may be wondering if there is a way to perform a search query based on a date even if there is no Date field in Mongo Schema. </p>



<p></p>



<p>It&#8217;s possible but with a few limitations.</p>



<p></p>



<p>Please visit the article for more details&#8230;</p>



<p></p>



<ul class="wp-block-list">
<li class="has-medium-font-size"><a href="https://www.thecodebuzz.com/query-mongodb-objectid-by-date-mongoshell-compass/" target="_blank" rel="noopener" title="Query MongoDB using ObjectId by Date -MongoShell">How to find records in MongoDB without date fields? </a></li>
</ul>



<p></p>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-what-is-the-difference-between-gt-and-gte-or-lt-and-lte-in-mongodb">What is the difference between $gt and $gte or $lt and $lte in MongoDB?</h2>



<p></p>



<p>The above commands are very simple and easy to understand. There are small differences between these commands.</p>



<p></p>



<p><strong>$gte </strong></p>



<p></p>



<pre class="wp-block-verse">$<strong>gte  = greater than or equal to i.e &gt; =</strong>
</pre>



<p><strong>$gt</strong></p>



<p></p>



<pre class="wp-block-verse">$<strong>gt  = greater than i.e &gt; </strong>

</pre>



<p><strong>$lte</strong></p>



<p></p>



<pre class="wp-block-verse"><strong>lte  = less than or equal to i.e &lt; =</strong>

</pre>



<p><strong>$lt</strong></p>



<p></p>



<pre class="wp-block-verse"><strong>lt  = less than i.e &lt; </strong>
</pre>



<p></p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p><strong>TIP  </strong>&#8211; IF you need to use Data range query often then is recommanded to <a href="https://www.thecodebuzz.com/mongodb-create-indexing-compound-index-mongo-shell-cli/" target="_blank" rel="noopener" title="MongoDB Create Index using MongoShell">indexed </a>the dates fields.</p>
</blockquote>



<p></p>



<p><strong>Other <em>References</em></strong></p>



<p></p>



<ul class="wp-block-list">
<li><a href="https://www.thecodebuzz.com/query-mongodb-objectid-by-date-mongoshell-compass/" target="_blank" rel="noopener" title="Query MongoDB using ObjectId by Date -MongoShell">How to find records in MongoDB without date fields? </a></li>
</ul>



<p></p>



<ul class="wp-block-list">
<li><a href="https://www.thecodebuzz.com/mongodb-repository-implementation-unit-testing-net-core-example/" target="_blank" rel="noreferrer noopener">MongoDB Repository implementation in .NET Core with example</a></li>
</ul>



<p></p>



<ul class="wp-block-list">
<li><a href="https://www.thecodebuzz.com/mongo-db-naming-conventions-standards-guidelines/" target="_blank" rel="noopener" title="MongoDB Naming Standards and Guidelines">MongoDB naming standards and convention </a></li>
</ul>



<p></p>



<p></p>



<p></p>



<p style="font-size:18px">Do you have any <strong>comments or ideas or any better </strong>suggestions to share?</p>



<p class="has-small-font-size"></p>



<p style="font-size:18px">Please sound off your comments below.</p>



<p class="has-medium-font-size"></p>



<p class="has-medium-font-size"><strong>Happy Coding </strong>!!</p>



<p></p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p><p>The post <a href="https://thecodebuzz.com/mongodb-date-range-query-greater-than-less-than/">MongoDB Date query to get records between two dates</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/mongodb-date-range-query-greater-than-less-than/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>Async Await in C#.NET &#8211; Guidelines and Best Practices</title>
		<link>https://thecodebuzz.com/async-await-csharp-net-guidelines-and-best-practices/</link>
					<comments>https://thecodebuzz.com/async-await-csharp-net-guidelines-and-best-practices/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Fri, 16 Jun 2023 04:30:18 +0000</pubDate>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Guidelines]]></category>
		<category><![CDATA[Async Await in C#.NET]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=27563</guid>

					<description><![CDATA[<p>Async Await in C#.NET &#8211; Guidelines and Best Practices Today in this article we will learn best practices for Async Await in C#.NET. When working with asynchronous programming in .NET using the async and await keywords, it&#8217;s important to follow best practices to ensure efficient and maintainable code. Here are some best practices for working [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/async-await-csharp-net-guidelines-and-best-practices/">Async Await in C#.NET – Guidelines and Best Practices</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading"><strong>Async Await in C#.NET &#8211; Guidelines and Best Practices</strong></h1>



<p>Today in this article we will learn best practices for <strong>Async Await in C#.NET. </strong></p>



<p></p>



<p>When working with asynchronous programming in .NET using the <strong><code>async</code> </strong>and <strong><code>await</code> </strong>keywords, it&#8217;s important to follow best practices to ensure efficient and maintainable code. </p>



<p></p>



<p>Here are some best practices for working with <code>async</code> and <code>await</code> in .NET.</p>



<p></p>



<div class="wp-block-aioseo-table-of-contents"><ul><li><a href="#aioseo-use-async-and-await-throughout-the-code-workflow">Use async and await throughout the code workflow</a></li><li><a href="#aioseo-avoid-async-void">Avoid Async Void</a></li><li><a href="#aioseo-prefer-task-return-types">Prefer Task Return Types</a></li><li><a href="#aioseo-configure-awaits-correctly">Configure Awaits Correctly</a></li><li><a href="#aioseo-use-task-whenall-or-task-whenany">Use Task.WhenAll or Task.WhenAny</a></li><li><a href="#aioseo-handle-exceptions-appropriately">Handle Exceptions Appropriately</a></li><li><a href="#aioseo-utilize-cancellationtoken">Utilize CancellationToken</a></li><li><a href="#aioseo-do-not-mix-blocking-and-non-blocking-code">Do not mix Blocking and Non-Blocking Code</a></li><li><a href="#aioseo-avoid-cpu-bound-operations-in-async-methods">Avoid CPU-Bound Operations in Async Methods</a></li><li><a href="#aioseo-understand-synchronization-context">Understand Synchronization Context</a></li><li><a href="#aioseo-summary">Summary</a></li></ul></div>



<p></p>



<h2 class="wp-block-heading" id="aioseo-use-async-and-await-throughout-the-code-workflow">Use async and await throughout the code workflow</h2>



<p></p>



<p>To get the best out of your code workflow this is an important aspect.</p>



<p></p>



<p>Use must use <strong><code>async</code> </strong>and <strong><code>await</code> </strong>throughout your codebase consistently and without missing any place to get full execution of the <a href="https://www.thecodebuzz.com/moq-to-mock-asynchronous-method-net-core-unit-tests/">asynchronous nature of method</a> execution.</p>



<p></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="720" src="https://www.thecodebuzz.com/wp-content/uploads/2023/06/async-wait-1024x720.jpg" alt="" class="wp-image-27602" srcset="https://thecodebuzz.com/wp-content/uploads/2023/06/async-wait-1024x720.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2023/06/async-wait-300x211.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2023/06/async-wait-768x540.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2023/06/async-wait-740x520.jpg 740w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<p><strong>Any miss of calling </strong>method/services will make the method a blocking method causing <strong>synchronous </strong>code execution of code.</p>



<p></p>



<p>Apply <strong><code>async</code> </strong>to methods that perform long-running or I/O-bound operations, and use <strong><code>await</code> </strong>to asynchronously wait for the results of those operations. </p>



<p></p>



<p>This allows for non-blocking execution and better utilization of system resources.</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-avoid-async-void">Avoid Async Void</h2>



<p></p>



<p>Avoid using <code><strong>async void</strong></code> methods except for event handlers, as they can lead to unhandled exceptions that are difficult to catch and handle properly. </p>



<p>Instead, prefer using <code><strong>async </strong></code><strong><code>Task</code> </strong>or <code><strong>async Task</strong>&lt;T&gt;</code> for methods that can be awaited.</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-prefer-task-return-types">Prefer Task Return Types</h2>



<p></p>



<p>Use <code>Task</code> or <a href="https://www.thecodebuzz.com/returning-bool-task-from-task-true-false-async-sync/" target="_blank" rel="noopener"> from a Task&#8221;&gt;<code><strong>Task&lt;T&gt;</strong></code> </a>as the return type for <code>async</code> methods whenever possible. </p>



<p></p>



<p>This allows callers to await the method and handle exceptions or perform additional operations asynchronously.</p>



<p></p>



<p><strong>Example </strong>:</p>



<p></p>



<pre class="wp-block-code"><code>    &#91;HttpPost]
    public async Task&lt;IActionResult&gt; SendSms1(NotifyRequest request)
    {
        return Ok(await A());
    }
    private async Task&lt;bool&gt; A()
    {
        return await B();
    }</code></pre>



<p></p>



<h2 class="wp-block-heading" id="aioseo-configure-awaits-correctly">Configure Awaits Correctly</h2>



<p></p>



<p>Use <strong>ConfigureAwait(false) </strong>when awaiting tasks within library code that don&#8217;t require synchronization context. </p>



<p></p>



<p>This helps avoid unnecessary overhead by not capturing the current synchronization context.</p>



<p></p>



<p><strong>Example</strong>:</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="239" src="https://www.thecodebuzz.com/wp-content/uploads/2023/06/async-wait-Configure-Awaits-Correctly-1024x239.jpg" alt="Async Await in C#.NET - Guidelines and Best Practices" class="wp-image-27604" srcset="https://thecodebuzz.com/wp-content/uploads/2023/06/async-wait-Configure-Awaits-Correctly-1024x239.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2023/06/async-wait-Configure-Awaits-Correctly-300x70.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2023/06/async-wait-Configure-Awaits-Correctly-768x179.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2023/06/async-wait-Configure-Awaits-Correctly-1536x358.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2023/06/async-wait-Configure-Awaits-Correctly-785x183.jpg 785w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<p>Note: ASP.NET Core doesn&#8217;t have any SynchronizationContext so it is preferable to use  <strong>ConfigureAwait(false)</strong></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-use-task-whenall-or-task-whenany">Use <a href="http://www.thecodebuzz.com/asynchronous-programming-invoke-multiple-async-await-api-combine-results/" target="_blank" rel="noopener" title="Multithreading – Invoke Async methods and Combine Results using WhenAll">Task.WhenAll </a>or Task.WhenAny</h2>



<p></p>



<p>When you need to await multiple tasks concurrently, use <code><strong>Task.WhenAll</strong></code> to wait for all tasks to be completed, or <code><strong>Task.WhenAny</strong></code> to wait for the first completed task. </p>



<p></p>



<p>When used method asynchronously awaits multiple asynchronous operations, until all operations are completed.</p>



<p></p>



<p>It’s a very good means of handling a few <em><strong>collections of the Tasks </strong></em>and getting their results together as completion criteria.</p>



<p></p>



<p>Let’s use WhenAll to await the completion of all the running Tasks.</p>



<p></p>



<pre class="wp-block-preformatted">EmployeeDetails[] employeeList = awaitTask.WhenAll(completedTask);
</pre>



<p></p>



<p>This allows for better parallelism and can improve overall performance.</p>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-handle-exceptions-appropriately">Handle Exceptions Appropriately</h2>



<p></p>



<p>Use <code>try-catch</code> blocks to handle exceptions that may occur within <code>async</code> methods.</p>



<p></p>



<p>Use the <code>await</code> operator inside the <code>try</code> block and handle any exceptions in the <code>catch</code> block. </p>



<p></p>



<p>Properly log and handle exceptions to prevent application crashes and maintain code robustness.</p>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-utilize-cancellationtoken">Utilize CancellationToken</h2>



<p></p>



<p>Pass a <strong><code>CancellationToken</code> </strong>to <code>async</code> methods when possible to support cancellation. </p>



<p></p>



<p>This allows for better responsiveness and the ability to cancel long-running operations when needed.</p>



<p></p>



<pre class="wp-block-preformatted">static async Task&lt;bool&gt; ProcessAsync(HttpClient client, CancellationToken token)
    {
        HttpResponseMessage response = await client.GetAsync(url, token);
        byte[] content = await response.Content.ReadAsByteArrayAsync(token);
       //do something 
        return true;
    }</pre>



<p></p>



<h2 class="wp-block-heading" id="aioseo-do-not-mix-blocking-and-non-blocking-code">Do not mix Blocking and Non-Blocking Code</h2>



<p></p>



<p>Avoid blocking on asynchronous code by using blocking calls like <code>Task.Wait</code> or <code>Task.Result</code>. </p>



<p></p>



<p>Mixing blocking and non-blocking code can lead to deadlocks and reduce the benefits of asynchronous programming.</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-avoid-cpu-bound-operations-in-async-methods">Avoid CPU-Bound Operations in Async Methods</h2>



<p></p>



<p>Avoid performing CPU-bound operations directly in <code>async</code> methods. </p>



<p></p>



<p>Instead, offload such operations to dedicated worker threads or use parallel processing techniques like <code>Parallel.ForEach</code> to maximize performance.</p>



<p><strong>Example </strong>:</p>



<pre class="wp-block-preformatted">private static IList&lt;int&gt; GetAllPrimeNumbers(IList&lt;int&gt; numbers)
        {

            Parallel.ForEach(numbers, number =&gt;
            {
                if (IsPrime(number))
                {
                    primeNumbers.Add(number);
                }
            });

            return primeNumbers.ToList();
        }</pre>



<p></p>



<h2 class="wp-block-heading" id="aioseo-understand-synchronization-context">Understand Synchronization Context</h2>



<p></p>



<p>Be aware of the synchronization context in which your <code>async</code> code is executing. Understand the implications of different contexts, such as UI thread synchronization context in GUI applications.</p>



<p></p>



<p><strong>Example</strong>: ASP.NET Core doesn&#8217;t need to worry about synchronization as it doesn&#8217;t have any SynchronizationContext.</p>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-summary">Summary</h2>



<p></p>



<p>Today in this article we discussed the best practice of using async and await keywords using which we can develop clean, efficient, and maintainable asynchronous code using async and await in .NET.</p>



<p></p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p>



<p></p><p>The post <a href="https://thecodebuzz.com/async-await-csharp-net-guidelines-and-best-practices/">Async Await in C#.NET – Guidelines and Best Practices</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/async-await-csharp-net-guidelines-and-best-practices/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>HTTPS Vs HTTP- Top 10 Benefits and Differences</title>
		<link>https://thecodebuzz.com/https-vs-http-top-10-benefits-differences-ssl-tls-seo/</link>
					<comments>https://thecodebuzz.com/https-vs-http-top-10-benefits-differences-ssl-tls-seo/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Sat, 10 Jun 2023 14:56:24 +0000</pubDate>
				<category><![CDATA[API Best Practices]]></category>
		<category><![CDATA[Guidelines]]></category>
		<category><![CDATA[HTTPS Vs HTTP]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=27493</guid>

					<description><![CDATA[<p>HTTPS Vs HTTP- Top 10 Benefits and Differences Today in this article, we will cover HTTPS Vs HTTP and their top 10 benefits and differences. Implementing HTTPS (HTTP over SSL/TLS) in your API offers several benefits for security, privacy, and trustworthiness. Here are some key advantages of using HTTPS over HTTP, Security HTTP sends data [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/https-vs-http-top-10-benefits-differences-ssl-tls-seo/">HTTPS Vs HTTP- Top 10 Benefits and Differences</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading">HTTPS Vs HTTP- Top 10 Benefits and Differences</h1>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="686" height="492" src="https://www.thecodebuzz.com/wp-content/uploads/2023/06/Https-vs-http.jpg" alt=" HTTPS Vs HTTP" class="wp-image-27498" srcset="https://thecodebuzz.com/wp-content/uploads/2023/06/Https-vs-http.jpg 686w, https://thecodebuzz.com/wp-content/uploads/2023/06/Https-vs-http-300x215.jpg 300w" sizes="auto, (max-width: 686px) 100vw, 686px" /></figure>



<p>Today in this article, we will cover HTTPS Vs HTTP and their top 10 benefits and differences. Implementing <strong>HTTPS </strong>(HTTP over SSL/TLS) in your API offers several benefits for security, privacy, and trustworthiness. </p>



<p></p>



<p>Here are some key advantages of using HTTPS over HTTP,</p>



<p></p>



<div class="wp-block-aioseo-table-of-contents"><ul><li><a href="#aioseo-security">Security</a></li><li><a href="#aioseo-data-encryption">Data Encryption between client-server</a></li><li><a href="#aioseo-secure-authentication">Secure Authentication</a></li><li><a href="#aioseo-data-integrity-while-data-in-transit">Data Integrity &#8211; Data in Transit</a></li><li><a href="#aioseo-trust-and-credibility">Trust and Credibility</a></li><li><a href="#aioseo-protection-against-mitm-man-in-the-middle-attacks">Protection against MITM (Man-in-the-Middle Attacks)</a></li><li><a href="#aioseo-compliance-with-security-standards">Compliance with Security Standards</a></li><li><a href="#aioseo-protection-against-information-leakage">Protection against Information Leakage</a></li><li><a href="#aioseo-online-visibility-and-traffic">Online Visibility and Traffic</a><ul><li><a href="#aioseo-seo-benefit">SEO Benefit</a></li><li><a href="#aioseo-antivirus-flagging">Antivirus Flagging</a></li></ul></li><li><a href="#aioseo-when-to-use-http-over-https">When to use HTTP Over HTTPS</a><ul><li><a href="#aioseo-development-and-testing">Development and Testing</a></li><li><a href="#aioseo-internal-networks">Internal Networks</a></li><li><a href="#aioseo-non-sensitive-information">Non-sensitive Information</a></li><li><a href="#aioseo-performance-optimization">Performance Optimization</a></li></ul></li><li><a href="#aioseo-summary">Summary</a></li></ul></div>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-security">Security</h2>



<p></p>



<p><strong>HTTP</strong> sends data in plain text, making it vulnerable to eavesdropping, tampering, and data interception.</p>



<p></p>



<p><strong>HTTPS </strong>provides data encryption and authentication. <strong>It </strong>encrypts the data exchanged between the client and the server, ensuring that it remains confidential and cannot be easily intercepted or read by unauthorized parties. </p>



<p></p>



<p>It also uses digital certificates to authenticate the identity of the server, protecting against man-in-the-middle attacks. </p>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-data-encryption">Data Encryption between client-server</h2>



<p></p>



<p><strong>HTTPS </strong>encrypts the data transmitted between the client and the API server. </p>



<p></p>



<p>It ensures that the information exchanged, including sensitive data such as authentication credentials, remains confidential and cannot be easily intercepted or read by unauthorized parties. </p>



<p></p>



<p>Encryption protects against eavesdropping and data tampering.</p>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-secure-authentication">Secure Authentication</h2>



<p></p>



<p><strong>HTTPS </strong>enhances the security of authentication processes. </p>



<p></p>



<p>It ensures that login credentials, tokens, or session cookies are transmitted securely, reducing the risk of interception or impersonation. </p>



<p></p>



<p>This is crucial in preventing unauthorized access to APIs and protecting user accounts from being compromised.</p>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-data-integrity-while-data-in-transit">Data Integrity &#8211; Data in Transit </h2>



<p></p>



<figure class="wp-block-image size-full is-resized"><a href="https://www.thecodebuzz.com/http-strict-transport-security-hsts-security-headers/" target="_blank" rel="noreferrer noopener"><img loading="lazy" decoding="async" width="945" height="497" src="https://www.thecodebuzz.com/wp-content/uploads/2023/06/Https-vs-http-Data-encryption-while-in-motion-or-transit.jpg" alt="Top 10 Benefits of HTTPS Vs HTTP" class="wp-image-27499" style="width:612px;height:321px" srcset="https://thecodebuzz.com/wp-content/uploads/2023/06/Https-vs-http-Data-encryption-while-in-motion-or-transit.jpg 945w, https://thecodebuzz.com/wp-content/uploads/2023/06/Https-vs-http-Data-encryption-while-in-motion-or-transit-300x158.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2023/06/Https-vs-http-Data-encryption-while-in-motion-or-transit-768x404.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2023/06/Https-vs-http-Data-encryption-while-in-motion-or-transit-785x413.jpg 785w" sizes="auto, (max-width: 945px) 100vw, 945px" /></a></figure>



<p></p>



<p>By combining <strong><em>encryption </em></strong>and <strong><em>integrity </em></strong>checks, <strong>HTTPS </strong>provides a secure channel for transmitting data over the Internet while ensuring that the data remains unaltered and trustworthy. </p>



<p></p>



<p>HTTPS employs <strong>encryption </strong>algorithms to secure the data exchanged between the client and the server. This encryption process ensures that the data is protected and remains confidential during transit. </p>



<p>Encryption prevents unauthorized parties from eavesdropping on the communication and accessing the data.</p>



<p></p>



<p>HTTPS performs <strong><em>integrity </em></strong>checks. </p>



<p></p>



<p>After the client sends a request or the server sends a response, the message digest of the transmitted data is recalculated at the receiving end.  This recalculation is based on the received data. </p>



<p></p>



<p>The client or server then compares the recalculated message digest with the one sent along with the message.</p>



<p></p>



<p>The cryptographic measures employed by HTTPS protect against unauthorized modifications, tampering, and interception of data during transit, thereby maintaining the integrity of the transmitted information.</p>



<p></p>



<p>In addition to data integrity, HTTPS ensures that the encryption of the data remains intact. The client and server verify the integrity of the encryption by confirming that the encryption keys have not been tampered with or compromised during the SSL/TLS handshake.</p>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-trust-and-credibility">Trust and Credibility</h2>



<p></p>



<p>Using <strong>HTTPS </strong>and displaying a valid<strong> <a href="https://www.ssl.com/faqs/what-is-https/" target="_blank" rel="noopener" title="">SSL</a>/TLS c</strong>ertificate demonstrates your commitment to security and builds trust with API consumers.</p>



<p></p>



<p>It assures them that their data is transmitted securely, and their interactions with your API are protected. </p>



<p></p>



<p><strong>HTTPS </strong>also helps establish the authenticity of your API server, as the certificate validates its identity.</p>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-protection-against-mitm-man-in-the-middle-attacks">Protection against MITM (Man-in-the-Middle Attacks)</h2>



<p></p>



<p><strong>HTTPS</strong> (HTTP over SSL/TLS)<strong> </strong>helps protect against man-in-the-middle (MITM) attacks, which are a common threat to unsecured connections</p>



<p></p>



<p><strong>HTTPS </strong>protects against man-in-the-middle (MITM) attacks where an attacker intercepts and alters the communication between the client and the server. </p>



<p></p>



<p>Employing the below <strong>HTTPS </strong>ensures the integrity and privacy of the data being transmitted.</p>



<p></p>



<ul class="wp-block-list">
<li>Encryption, </li>



<li>Certificate validation</li>



<li>Secure handshake protocols</li>
</ul>



<p></p>



<h2 class="wp-block-heading" id="aioseo-compliance-with-security-standards">Compliance with Security Standards</h2>



<p></p>



<p><strong>HTTPS </strong>has become the industry standard for secure communication on the web. </p>



<p></p>



<p>Using <strong>HTTPS </strong>in your API aligns with the best practices and expectations of API consumers, establishing your API as a secure and trustworthy service.</p>



<p></p>



<p>Many security standards and regulations, such as <strong>PCI DSS </strong>(Payment Card Industry Data Security Standard) and <strong>GDPR </strong>(General Data Protection Regulation), mandate the use of encryption and secure communication protocols like HTTPS when handling sensitive data.</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-protection-against-information-leakage">Protection against Information Leakage</h2>



<p></p>



<p>HTTPS prevents information leakage and protects against unauthorized access to sensitive data. </p>



<p></p>



<p>It ensures that Application requests and responses are encrypted, reducing the risk of exposure to sensitive information in transit.</p>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-online-visibility-and-traffic">Online Visibility and Traffic</h2>



<p></p>



<h4 class="wp-block-heading" id="aioseo-seo-benefit">SEO Benefit</h4>



<p></p>



<p>Many search algorithms used by search engines like Google, and Bing uses HTTPS as a ranking signal.</p>



<p></p>



<p>Using HTTPS in your application can positively impact your search engine rankings, resulting in improved visibility and traffic</p>



<p></p>



<h4 class="wp-block-heading" id="aioseo-antivirus-flagging">Antivirus Flagging</h4>



<figure class="wp-block-image size-full is-resized"><img loading="lazy" decoding="async" width="639" height="346" src="https://www.thecodebuzz.com/wp-content/uploads/2023/06/Https-vs-http-Data-encryption-antivirus-warnings.jpg" alt="" class="wp-image-27500" style="width:397px;height:215px" srcset="https://thecodebuzz.com/wp-content/uploads/2023/06/Https-vs-http-Data-encryption-antivirus-warnings.jpg 639w, https://thecodebuzz.com/wp-content/uploads/2023/06/Https-vs-http-Data-encryption-antivirus-warnings-300x162.jpg 300w" sizes="auto, (max-width: 639px) 100vw, 639px" /></figure>



<p>HTTPS-supported applications are recommended by many antivirus software.</p>



<p></p>



<p>Antivirus software generally does not flag HTTPS connections as malicious or suspicious based on the fact that they use HTTPS vs HTTPS. </p>



<p></p>



<p>However flagging also depends on other factors like malware, viruses, or malicious activities, rather than flagging HTTPS connections themselves.</p>



<p></p>



<p>However, any warning signs informing users not to click on such websites result in reduced online visibility and traffic on the website or application.</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-when-to-use-http-over-https">When to use HTTP Over HTTPS</h2>



<p></p>



<p><strong>HTTP </strong>(Hypertext Transfer Protocol) is typically used when security and encryption are not a primary concern, and when there is no sensitive information being transmitted. </p>



<p></p>



<p>Here are a few scenarios where using HTTP might be appropriate,</p>



<p></p>



<h4 class="wp-block-heading" id="aioseo-development-and-testing">Development and Testing</h4>



<p></p>



<p>During the early stages of development or testing, you may use HTTP to simplify the setup and configuration. </p>



<p></p>



<p>It can be easier to debug and monitor HTTP traffic compared to HTTPS, as encryption can add complexity.</p>



<p></p>



<h4 class="wp-block-heading" id="aioseo-internal-networks">Internal Networks</h4>



<p></p>



<p>In closed or private internal networks where there is no exposure to the public internet or external threats, using HTTP can be acceptable. </p>



<p></p>



<p>Example: LAN network or communication over private endpoints</p>



<p></p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p></p>



<p>This assumes that the network is adequately secured and isolated from unauthorized access.</p>
</blockquote>



<p></p>



<h4 class="wp-block-heading" id="aioseo-non-sensitive-information">Non-sensitive Information</h4>



<p></p>



<p>If the information being transmitted is not sensitive and does not require confidentiality, integrity, or authentication, such as publicly available data or non-sensitive public APIs, using HTTP may be sufficient.</p>



<p></p>



<h4 class="wp-block-heading" id="aioseo-performance-optimization">Performance Optimization </h4>



<p></p>



<p>In certain cases, where the overhead of encryption and decryption poses a significant performance impact, and the data being transmitted is not sensitive, HTTP may be preferred over HTTPS to optimize performance.</p>



<p></p>



<p>However, it&#8217;s important to note that the use of HTTP should be carefully considered, as it exposes data to interception, tampering, and unauthorized access. </p>



<p></p>



<p>In general, it is recommended to use HTTPS for most scenarios to ensure the confidentiality, integrity, and security of data transmitted over the Internet, especially when dealing with sensitive information or user interactions.</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-summary">Summary </h2>



<p></p>



<p>Overall, implementing HTTPS in your API provides essential security measures to protect data, authenticate users securely, and establish trust with Application consumers. It helps safeguard sensitive information, enhances the integrity of data transmission, and ensures compliance with security standards and regulations.</p>



<p></p>



<p>That&#8217;s all! Happy coding!</p>



<p></p>



<p>Does this help you fix your issue? </p>



<p></p>



<p>Do you have any better solutions or suggestions? Please sound off your comments below.</p>



<p></p>



<p></p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p><p>The post <a href="https://thecodebuzz.com/https-vs-http-top-10-benefits-differences-ssl-tls-seo/">HTTPS Vs HTTP- Top 10 Benefits and Differences</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/https-vs-http-top-10-benefits-differences-ssl-tls-seo/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>MongoDB Query for field is null or not set</title>
		<link>https://thecodebuzz.com/mongodb-query-field-is-null-or-not-set-mongo-shell-compass/</link>
					<comments>https://thecodebuzz.com/mongodb-query-field-is-null-or-not-set-mongo-shell-compass/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Thu, 13 Oct 2022 18:59:00 +0000</pubDate>
				<category><![CDATA[Guidelines]]></category>
		<category><![CDATA[how to set null value in mongodb]]></category>
		<category><![CDATA[ifnull mongodb]]></category>
		<category><![CDATA[mongodb aggregation match not null]]></category>
		<category><![CDATA[mongodb find not null or empty string]]></category>
		<category><![CDATA[mongodb ignore null values]]></category>
		<category><![CDATA[mongodb or]]></category>
		<category><![CDATA[mongodb query null or empty]]></category>
		<category><![CDATA[not in mongodb]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=14252</guid>

					<description><![CDATA[<p>MongoDB Query for records where the field is null or not set Today in the article we shall learn how to MongoDB Query for field is null or not set i.e doesn&#8217;t exist in the database. We shall verify queries using MongoShell or UI tools like a Compass UI. Today in this article, we will [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/mongodb-query-field-is-null-or-not-set-mongo-shell-compass/">MongoDB Query for field is null or not set</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading">MongoDB Query for records where the field is null or not set </h1>



<p>Today in the article we shall learn how to MongoDB Query for field is null or not set i.e doesn&#8217;t exist in the database. </p>



<p></p>



<p>We shall verify queries using <em><strong>MongoShell </strong></em>or UI tools like a<strong><em> Compass UI</em></strong>.</p>



<p></p>



<p>Today in this article, we will cover below aspects,</p>



<div class="wp-block-aioseo-table-of-contents"><ul><li><a href="#aioseo-getting-started">Getting started</a></li><li><a href="#aioseo-the-field-is-not-set-in-mongodb">The field is not set</a></li><li><a href="#aioseo-the-field-is-null-in-mongodb">The field is null</a></li><li><a href="#aioseo-field-is-null-or-not-set-in-mongodb">The field is null or not set</a></li></ul></div>



<p>We shall also be running the query using the .NET C# Mongo driver. </p>



<p></p>



<p>If interested to know how to run queries using <strong><em><strong>.NET C#</strong></em> mongo driver</strong> please check <strong><em><a href="https://www.thecodebuzz.com/mongodb-query-for-records-where-field-is-null-or-not-set/" target="_blank" rel="noreferrer noopener" title="this">this</a> </em></strong>article,</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-getting-started">Getting started </h2>



<p></p>



<p>I have a sample MongoDB document as below in one of the collections. Here we shall be trying to search all the documents where the Author field is <strong><em>NULL</em></strong> or not set in documents.</p>



<p></p>



<figure class="wp-block-image size-large is-resized"><img decoding="async" src="https://www.thecodebuzz.com/wp-content/uploads/2021/01/mongodb-query-the-field-is-null-compass-and-mongo-shell-1024x841.jpg" alt="MongoDB Query for field is null " class="wp-image-14272" style="width:395px;height:325px"/></figure>



<p></p>



<h2 class="wp-block-heading" id="aioseo-the-field-is-not-set-in-mongodb">The field is not set</h2>



<p></p>



<p>The below <strong><em>query</em></strong> matches documents that do not contain the <strong><em>Author</em></strong>&nbsp;field.</p>



<p></p>



<p>So far above Mongo documents, we shall get results for only &#8220;_id&#8221;:&#8221;<strong><em>5db5a4476997188b2722c820</em></strong>&#8220;</p>



<p></p>



<p><strong><em>Query Pattern</em></strong></p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size">{  Field Name:  {$exists:false}  }
</pre>



<p><strong><em>Example Query </em></strong></p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size">{ Author: {$exists:false} }</pre>



<p></p>



<p><strong><em>Results</em></strong>:</p>



<p></p>



<p><strong><em>Using Mongo shell</em></strong></p>



<p></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="112" src="https://www.thecodebuzz.com/wp-content/uploads/2021/01/mongodb-query-the-field-is-null-or-not-set-1024x112.jpg" alt="mongodb query the field is null or not set" class="wp-image-14255" srcset="https://thecodebuzz.com/wp-content/uploads/2021/01/mongodb-query-the-field-is-null-or-not-set-1024x112.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2021/01/mongodb-query-the-field-is-null-or-not-set-300x33.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2021/01/mongodb-query-the-field-is-null-or-not-set-768x84.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2021/01/mongodb-query-the-field-is-null-or-not-set-1536x167.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2021/01/mongodb-query-the-field-is-null-or-not-set-2048x223.jpg 2048w, https://thecodebuzz.com/wp-content/uploads/2021/01/mongodb-query-the-field-is-null-or-not-set-785x86.jpg 785w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p><strong><em>Using Compass </em> UI</strong></p>



<p></p>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" width="763" height="630" src="https://www.thecodebuzz.com/wp-content/uploads/2021/01/mongodb-query-the-field-is-null-or-not-set-compass.jpg" alt="mongodb query field is null or not set" class="wp-image-14256" style="width:298px;height:245px" srcset="https://thecodebuzz.com/wp-content/uploads/2021/01/mongodb-query-the-field-is-null-or-not-set-compass.jpg 763w, https://thecodebuzz.com/wp-content/uploads/2021/01/mongodb-query-the-field-is-null-or-not-set-compass-300x248.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2021/01/mongodb-query-the-field-is-null-or-not-set-compass-630x520.jpg 630w" sizes="auto, (max-width: 763px) 100vw, 763px" /></figure>



<p></p>



<p><strong><em>Note:</em></strong></p>



<p></p>



<p>If <strong><em>$<code>exists</code></em></strong> is <strong><em>false</em></strong>, the query returns only the documents that do not contain the field.</p>



<p></p>



<p>In the above query, we shall get the result as only 1 document.</p>



<p></p>



<p>&nbsp;<strong><em><code>$exists</code>&nbsp;</em></strong>when set as <strong><em>true</em></strong>, it matches the documents that contain the field, including documents where the field value is&nbsp;<code><strong><em>null</em></strong></code>.</p>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-the-field-is-null-in-mongodb">The field is null</h2>



<p></p>



<p>The below <strong><em>query</em></strong> matches documents that contain the <strong><em>Author</em></strong>&nbsp;field which is set as null.</p>



<p>Here we are assuming the fields already exist and don&#8217;t want to consider the records where records where fields do not exist.</p>



<p></p>



<p>So far above Mongo documents, we shall get results for only id = <strong><em>5ff50353a29ce9564c2724e2</em></strong></p>



<p></p>



<p><strong><em>Query Pattern</em></strong></p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size">{$and:[{Field Name:{$type:'null'}}]}
</pre>



<p></p>



<p><strong><em>Example Query </em></strong></p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size">{$and:[{Author:{$type:'null'}}]}</pre>



<p></p>



<p><strong><em>Results</em></strong>:</p>



<p></p>



<p><strong><em>Using Mongo shell</em></strong></p>



<p></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="130" src="https://www.thecodebuzz.com/wp-content/uploads/2021/01/mongodb-query-the-field-is-null-1024x130.jpg" alt="" class="wp-image-14259" srcset="https://thecodebuzz.com/wp-content/uploads/2021/01/mongodb-query-the-field-is-null-1024x130.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2021/01/mongodb-query-the-field-is-null-300x38.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2021/01/mongodb-query-the-field-is-null-768x97.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2021/01/mongodb-query-the-field-is-null-1536x195.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2021/01/mongodb-query-the-field-is-null-2048x260.jpg 2048w, https://thecodebuzz.com/wp-content/uploads/2021/01/mongodb-query-the-field-is-null-785x100.jpg 785w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /><figcaption class="wp-element-caption">MongoDB query the field is null</figcaption></figure>



<p></p>



<p><strong><em>Using Compass </em></strong></p>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" width="919" height="598" src="https://www.thecodebuzz.com/wp-content/uploads/2021/01/mongodb-query-the-field-is-null-compass.jpg" alt="MongoDb $exists example" class="wp-image-14260" style="width:339px;height:220px" srcset="https://thecodebuzz.com/wp-content/uploads/2021/01/mongodb-query-the-field-is-null-compass.jpg 919w, https://thecodebuzz.com/wp-content/uploads/2021/01/mongodb-query-the-field-is-null-compass-300x195.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2021/01/mongodb-query-the-field-is-null-compass-768x500.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2021/01/mongodb-query-the-field-is-null-compass-785x511.jpg 785w" sizes="auto, (max-width: 919px) 100vw, 919px" /><figcaption class="wp-element-caption">mongodb query the field is null compass</figcaption></figure>



<p></p>



<h2 class="wp-block-heading" id="aioseo-field-is-null-or-not-set-in-mongodb">The field is null or not set </h2>



<p></p>



<p>Let&#8217;s now see if you have requirements to find the records where Field is not set or does not exist in MongoDB.</p>



<p></p>



<p><strong><em>Query Pattern</em></strong></p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size">{$and:[{Field Name:null }]}</pre>



<p></p>



<p><strong>Query Example</strong></p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size">{$and:[{Author:null }]}</pre>



<p></p>



<p>The above query does work for the below 2 conditions,</p>



<p></p>



<ul class="wp-block-list">
<li>matches &#8220;<strong><em>Author</em></strong>&#8221; value is <strong><em>NULL</em></strong></li>



<li>and matches &#8220;<strong><em>Author</em></strong>&#8221; fields <strong><em>don&#8217;t exist</em></strong></li>
</ul>



<p></p>



<p><strong><em>Results:</em></strong></p>



<p></p>



<p><strong><em>Mongo Shell</em></strong></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="158" src="https://www.thecodebuzz.com/wp-content/uploads/2021/01/Query-for-records-where-the-field-is-null-or-not-set-mongo-shell-1024x158.jpg" alt="MongoDB Query for field is not set" class="wp-image-14261" srcset="https://thecodebuzz.com/wp-content/uploads/2021/01/Query-for-records-where-the-field-is-null-or-not-set-mongo-shell-1024x158.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2021/01/Query-for-records-where-the-field-is-null-or-not-set-mongo-shell-300x46.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2021/01/Query-for-records-where-the-field-is-null-or-not-set-mongo-shell-768x119.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2021/01/Query-for-records-where-the-field-is-null-or-not-set-mongo-shell-1536x237.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2021/01/Query-for-records-where-the-field-is-null-or-not-set-mongo-shell-2048x316.jpg 2048w, https://thecodebuzz.com/wp-content/uploads/2021/01/Query-for-records-where-the-field-is-null-or-not-set-mongo-shell-785x121.jpg 785w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<p><strong><em>Compass UI</em></strong></p>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" width="940" height="823" src="https://www.thecodebuzz.com/wp-content/uploads/2021/01/Query-for-records-where-the-field-is-null-or-not-set-compass-UI.jpg" alt="" class="wp-image-14262" style="width:305px;height:266px" srcset="https://thecodebuzz.com/wp-content/uploads/2021/01/Query-for-records-where-the-field-is-null-or-not-set-compass-UI.jpg 940w, https://thecodebuzz.com/wp-content/uploads/2021/01/Query-for-records-where-the-field-is-null-or-not-set-compass-UI-300x263.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2021/01/Query-for-records-where-the-field-is-null-or-not-set-compass-UI-768x672.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2021/01/Query-for-records-where-the-field-is-null-or-not-set-compass-UI-594x520.jpg 594w" sizes="auto, (max-width: 940px) 100vw, 940px" /></figure>



<p></p>



<p>That&#8217;s all, So today we learned the simple Mongo query for identifying the records where the field is null or not set. It&#8217;s pretty easy to prepare the query for such needs.</p>



<p></p>



<p><strong><em>References</em></strong>:</p>



<p></p>



<ul class="has-medium-font-size wp-block-list">
<li><a href="https://www.thecodebuzz.com/mongo-db-naming-conventions-standards-guidelines/" target="_blank" rel="noreferrer noopener" title="MongoDB Naming Conventions and Standards"><strong><em>MongoDB Naming Conventions and Standards</em></strong></a></li>
</ul>



<ul class="has-medium-font-size wp-block-list">
<li><a href="https://www.thecodebuzz.com/mongodb-query-field-is-null-or-not-set-mongo-shell-compass/" target="_blank" rel="noreferrer noopener" title="MongoDB Query - Fields not set or Field is null"><strong><em>MongoDB Query &#8211; Fields not set or Field is null</em></strong></a></li>
</ul>



<p></p>



<p></p>



<p style="font-size:18px">Do you have any <strong>comments or ideas or any better </strong>suggestions to share?</p>



<p class="has-small-font-size"></p>



<p style="font-size:18px">Please sound off your comments below.</p>



<p class="has-medium-font-size"></p>



<p class="has-medium-font-size"><strong>Happy Coding </strong>!!</p>



<p></p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p>



<p></p><p>The post <a href="https://thecodebuzz.com/mongodb-query-field-is-null-or-not-set-mongo-shell-compass/">MongoDB Query for field is null or not set</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/mongodb-query-field-is-null-or-not-set-mongo-shell-compass/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to use Files- Coding Best practices</title>
		<link>https://thecodebuzz.com/how-to-use-file-in-best-practices-csharp-dotnet-python/</link>
					<comments>https://thecodebuzz.com/how-to-use-file-in-best-practices-csharp-dotnet-python/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Thu, 21 Apr 2022 01:26:00 +0000</pubDate>
				<category><![CDATA[File Best Practices]]></category>
		<category><![CDATA[Guidelines]]></category>
		<category><![CDATA[best coding practices websites]]></category>
		<category><![CDATA[code organization best practices]]></category>
		<category><![CDATA[coding best practices java]]></category>
		<category><![CDATA[File coding best practices]]></category>
		<category><![CDATA[file structure best practices]]></category>
		<category><![CDATA[folder structure best practices programming]]></category>
		<category><![CDATA[good coding practices in software engineering]]></category>
		<category><![CDATA[good programming practices pdf]]></category>
		<category><![CDATA[python coding best practices]]></category>
		<guid isPermaLink="false">https://www.thecodebuzz.com/?p=22480</guid>

					<description><![CDATA[<p>How to use Files- Coding Best practices Today in this article, we will learn How to use Files- Coding Best practices to be followed. Handling files properly (being an unmanaged resource) is important and since all file operation uses OS-level operation behind the picture, it&#8217;s important to take care of the file handles including read [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/how-to-use-file-in-best-practices-csharp-dotnet-python/">How to use Files- Coding Best practices</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading">How to use Files- Coding Best practices</h1>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="765" height="420" src="https://www.thecodebuzz.com/wp-content/uploads/2022/06/How-to-use-File-best-practices-.NET_.jpg" alt="How to use File - Code Best practices" class="wp-image-22482" srcset="https://thecodebuzz.com/wp-content/uploads/2022/06/How-to-use-File-best-practices-.NET_.jpg 765w, https://thecodebuzz.com/wp-content/uploads/2022/06/How-to-use-File-best-practices-.NET_-300x165.jpg 300w" sizes="auto, (max-width: 765px) 100vw, 765px" /></figure>



<p>Today in this article, we will learn How to use Files- Coding Best practices to be followed.</p>



<p></p>



<p>Handling files properly (being an <strong>unmanaged </strong>resource) is important and since all file operation uses OS-level operation behind the picture, it&#8217;s important to take care of the file handles including read operation or file create or delete operation.</p>



<p></p>



<p></p>



<p>Today in this article, we will cover below aspects,</p>



<p></p>



<div class="wp-block-aioseo-table-of-contents"><ul><li><a href="#aioseo-understand-file-enumeration---file-best-practices">Understand File Enumeration &#8211; File Best practices</a></li><li><a href="#aioseo-implements-idisposable-for-files">Implements IDisposable for Files</a></li><li><a href="#aioseo-implement-a-retry-pattern-for-file-processing">Implement a Retry pattern for File processing</a></li><li><a href="#aioseo-implement-thread-synchronization-for-file--lock-and-unlock">Implement Thread Synchronization for File -Lock and Unlock</a></li><li><a href="#aioseo-watch-if-the-file-is-in-use-by-another-process">Watch if the File is in use by another process</a></li></ul></div>



<p></p>



<p>We will use generic guidelines and will see a few examples using C# code. However, you can use the discussed approaches for any coding language.</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-understand-file-enumeration---file-best-practices">Understand File Enumeration &#8211; File Best practices</h2>



<p></p>



<p>While using <strong><em>FileStream </em></strong>class, you get the ability to specify the <em><strong>FileShare </strong></em>access type another process that can have while dealing with File Processing.</p>



<p></p>



<p>Example of <strong><em>FileShare </em></strong>Enumeration,</p>



<p></p>



<p></p>



<p><strong>Example </strong></p>



<pre class="wp-block-preformatted has-medium-font-size">     using (FileStream stream = File.Open(filePath,FileMode.OpenOrCreate,               FileAccess.ReadWrite, <span class="has-inline-color has-vivid-red-color">FileShare.ReadWrite</span>))
            {


                /// You logic here////
            }</pre>



<p>There are <strong><em>5 enumerations</em></strong> that can be used which define the file access level.</p>



<p></p>



<p><strong><span class="has-inline-color has-vivid-red-color">FileShare</span>.None </strong></p>



<p></p>



<ul class="wp-block-list">
<li>Allows subsequent opening of the file for reading. If this flag is not specified,</li>



<li>Any request to open the file for reading (by this process or another process) will fail until the file is closed. However, even if this flag is specified,</li>



<li>Additional permissions might still be needed to access the file.</li>
</ul>



<p></p>



<p><strong><br><span class="has-inline-color has-vivid-red-color">FileShare</span>.Read</strong></p>



<p></p>



<ul class="wp-block-list">
<li>Allows subsequent opening of the file for Reading. If this flag is not specified.</li>
</ul>



<ul class="wp-block-list">
<li>Any request to open the file for writing (by this process or another process will fail until the file is closed. However, even if this flag is specified</li>
</ul>



<ul class="wp-block-list">
<li>Additional permissions might still be needed to access the file.</li>
</ul>



<p></p>



<p><br><strong><strong><span class="has-inline-color has-vivid-red-color">FileShare</span></strong> .Write</strong></p>



<p></p>



<ul class="wp-block-list">
<li>Allows subsequent opening of the file for reading or writing. </li>
</ul>



<ul class="wp-block-list">
<li>If this flag is not specified, any request to open the file for reading or writing (by this processor or another process) will fail until the file is closed. </li>
</ul>



<ul class="wp-block-list">
<li>However, even if this flag is specified, additional permissions might still be needed to access the file</li>
</ul>



<p></p>



<p><strong><strong><span class="has-inline-color has-vivid-red-color">FileShare</span></strong> .</strong>Delete</p>



<p></p>



<p>Allows subsequent deleting of a file. </p>



<p></p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>Proper use of <strong><em>FileShare </em></strong>enumeration will help you resolve the most common issues while dealing with files.</p>
</blockquote>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-implements-idisposable-for-files">Implements IDisposable for Files</h2>



<p></p>



<p>Dealing with File instances or handle is important. </p>



<p></p>



<p>Most file methods are actually core OS-level methods and their associated behavior is determined in the run time.</p>



<p></p>



<p></p>



<p>These file-level classes come under <strong>unmanaged </strong>resources and hence implement an <em><strong>IDisposable </strong></em>interface in C# language. </p>



<p></p>



<p>Other languages will also have their own way of managing file objects</p>



<p></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="190" src="https://www.thecodebuzz.com/wp-content/uploads/2022/06/How-to-File-best-practices-File.Create-1024x190.jpg" alt="How to use File in C# .NET - Best practices,How to use Files- Coding Best practices" class="wp-image-22404" srcset="https://thecodebuzz.com/wp-content/uploads/2022/06/How-to-File-best-practices-File.Create-1024x190.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2022/06/How-to-File-best-practices-File.Create-300x56.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2022/06/How-to-File-best-practices-File.Create-768x142.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2022/06/How-to-File-best-practices-File.Create-1536x285.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2022/06/How-to-File-best-practices-File.Create-2048x380.jpg 2048w, https://thecodebuzz.com/wp-content/uploads/2022/06/How-to-File-best-practices-File.Create-785x145.jpg 785w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<p>It&#8217;s always safe to use the &#8220;<em><strong>using&#8221; statement while dealing with files instead of creating and managing the instance ourselves</strong></em>.</p>



<p></p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>Wrap all your code inside &#8220;<em><strong>using</strong></em>&#8221; statements. it opens the file for reading and writes and closes the file handle once the code scope goes out of using statement.</p>
</blockquote>



<p></p>



<figure class="wp-block-image size-large is-resized"><a href="https://www.thecodebuzz.com/file-lock-unlock-read-write-file-c-sharp-thread-safe-net-core/" target="_blank" rel="noopener"><img loading="lazy" decoding="async" src="https://www.thecodebuzz.com/wp-content/uploads/2022/06/How-to-File-best-practices-File.Create-always-use-using-1024x300.jpg" alt="File in C# .NET - Best practices" class="wp-image-22405" width="730" height="214" srcset="https://thecodebuzz.com/wp-content/uploads/2022/06/How-to-File-best-practices-File.Create-always-use-using-1024x300.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2022/06/How-to-File-best-practices-File.Create-always-use-using-300x88.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2022/06/How-to-File-best-practices-File.Create-always-use-using-768x225.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2022/06/How-to-File-best-practices-File.Create-always-use-using-1536x450.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2022/06/How-to-File-best-practices-File.Create-always-use-using-785x230.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2022/06/How-to-File-best-practices-File.Create-always-use-using.jpg 1863w" sizes="auto, (max-width: 730px) 100vw, 730px" /></a></figure>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-implement-a-retry-pattern-for-file-processing">Implement a Retry pattern for File processing</h2>



<p></p>



<p>This is not mandatory but always come in handy while dealing with file exceptions or temporary glitch accessing the file.</p>



<p></p>



<p>The retry mechanism is a very basic and common pattern for I/O operations.</p>



<p></p>



<p><strong>Example </strong>:</p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size">Retry(() =&gt;PerformFileProcessing(filePath), 3);

</pre>



<p>Here method  <strong><em>PerformFileProcessing </em></strong>performs all file read or write operations.</p>



<p><strong>Example </strong>&#8211;<a href="https://www.thecodebuzz.com/implement-retry-pattern-in-csharp-dotnet-resiliency/" target="_blank" rel="noopener" title="Implement Simple Retry pattern in C# – Resiliency"> Simple retry pattern</a></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-implement-thread-synchronization-for-file--lock-and-unlock">Implement Thread Synchronization for File -Lock and Unlock</h2>



<p><a href="https://stackoverflow.com/posts/3457783/timeline"></a></p>



<p>The file can be accessed by multiple threads within the application or by external process threads. </p>



<p></p>



<p>If you have multiple threads attempting to access the same file, consider using a  Synchronization mechanism using Lock or another threading synchronization mechanism.</p>



<p></p>



<p><strong>Example</strong></p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; title: ; notranslate">
 public class WiteToFileUsingLock
    {
        public string Filepath { get; set; }
        private static object lockerFile = new Object();

        public void WriteToFile(StringBuilder text)
        {
            lock (lockerFile)
            {
                using (FileStream file = new FileStream(Filepath, FileMode.Append, FileAccess.Write, FileShare.Read))
                using (StreamWriter writer = new StreamWriter(file, Encoding.Unicode))
                {
                    writer.Write(text.ToString());
                }
            }

        }
    }
</pre></div>


<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-watch-if-the-file-is-in-use-by-another-process"><span style="font-size: revert; color: initial;">Watch if the </span>File is in use by another process</h2>



<p></p>



<p>Please make sure to verify the file is not opened by another process. If Yes make sure to handle the scenario gracefully. You could retry or exit the operation based on the need.</p>



<p></p>



<p>This could be any specific process on the system or program opening the file at the time when your program or application is already using it. Closing or stopping the other process may resolve the access issue.</p>



<p></p>



<p>There are multiple ways to deal with File<em><strong> Open, Creating</strong></em>, Reading, or writing operations using C# code. Its important to how different methods are being used in the code.</p>



<p></p>



<p>A few examples of File related methods but not limited are as below,</p>



<p></p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>File.Create</p>



<p>File.Open</p>



<p><code>File.ReadAllText()</code></p>



<p><code>File.WriteAllText()</code></p>



<p><code>File.ReadAllLines()</code></p>



<p><code>File.WriteAllLines()</code> </p>
</blockquote>



<p></p>



<p>The above method allows performing Read or writing conveniently.</p>



<p></p>



<p><strong><em>Example </em></strong></p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; highlight: [2]; title: ; notranslate">
          
            // Create the file, or overwrite if the file exists.

            using (FileStream fs = File.Create(path))
            {
                byte&#x5B;] info = new UTF8Encoding(true).GetBytes(&quot;This is test file.&quot;);

                // Add details to the file.
                fs.Write(info, 0, info.Length);
            }

          
</pre></div>


<p></p>



<p><strong>Note</strong>: When using <strong>File.Create</strong>, please note <strong>FileShare&nbsp;</strong>value of&nbsp;<strong>None </strong>is used as default behavior i.e no other process or code can access the file until the original file handle is closed.</p>



<p></p>



<p></p>



<p style="font-size:18px">Do you have any <strong>comments or ideas or any better </strong>suggestions to share?</p>



<p class="has-small-font-size"></p>



<p style="font-size:18px">Please sound off your comments below.</p>



<p class="has-medium-font-size"></p>



<p class="has-medium-font-size"><strong>Happy Coding </strong>!!</p>



<p></p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p>



<p></p>



<p></p><p>The post <a href="https://thecodebuzz.com/how-to-use-file-in-best-practices-csharp-dotnet-python/">How to use Files- Coding Best practices</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/how-to-use-file-in-best-practices-csharp-dotnet-python/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
