<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Code Crumbs</title>
    <description>Blog site for programming knowledge sharing (c#, js, sql)</description>
    <link>https://torszulak.github.io/</link>
    <atom:link href="https://torszulak.github.io/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Fri, 24 May 2024 20:39:46 +0000</pubDate>
    <lastBuildDate>Fri, 24 May 2024 20:39:46 +0000</lastBuildDate>
    <generator>Jekyll v3.9.5</generator><item>
        <title>Non-obvious behaviors in Javascript foreach() loop</title>
        <description>&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/blog-cells@0.7.1/dist/blog-cells.css&quot; /&gt;

&lt;script src=&quot;https://cdn.jsdelivr.net/npm/blog-cells@0.7.1/dist/blog-cells.js&quot;&gt;&lt;/script&gt;

&lt;p class=&quot;intro&quot;&gt;&lt;span class=&quot;dropcap&quot;&gt;I&lt;/span&gt; want to show two inconspicuous-looking behaviors in Javascript foreach() loop that could confuse you while using it.
&lt;/p&gt;

&lt;h4 id=&quot;how-to-stop-foreach-loop-in-javascript&quot;&gt;How to stop foreach loop in Javascript?&lt;/h4&gt;

&lt;p&gt;At first look pretty simple, inconspicuous-looking question, isn’t it? But it is Javascript… so any question could be tricky 😏&lt;/p&gt;

&lt;p&gt;Let’s look at the sample code below (you can edit and run it)&lt;/p&gt;

&lt;script type=&quot;text/notebook-cell&quot;&gt;
const someArray = [0, 1, 2, 3, 4, 5];

someArray.forEach((number) =&gt; {
  if (number &gt; 2) {
    console.log(&quot;Is it stopped? No! The number is:&quot;, number);
    return; //You could expect that this is enough to stop the loop, but it's not
  }
  console.log(&quot;Number:&quot;, number);
});
&lt;/script&gt;

&lt;p&gt;It prints every number from array but &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;return&lt;/code&gt; keyword is ineffective here. As is turns out, this is correct behavior according to &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach&quot; target=&quot;_blank&quot;&gt;documentation&lt;/a&gt;:&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;There is no way to stop or break a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;forEach()&lt;/code&gt; loop other than by throwing an exception.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So, we have only one way to stop this loop:&lt;/p&gt;

&lt;script type=&quot;text/notebook-cell&quot;&gt;
const someArray = [0, 1, 2, 3, 4, 5];

try {
  someArray.forEach((number) =&gt; {
    console.log(&quot;Number:&quot;, number);
    if (number &gt; 2) {
      throw new Error(&quot;Loop stopped.&quot;);
    }
  });
} catch (error) {
  console.log(&quot;Caught an error:&quot;, error.message);
}
&lt;/script&gt;

&lt;p&gt;Now the loop is successfully stopped but… isn’t it looks like using cannon to kill a fly? So if you have to break loop you should consider to use e.g &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;for&lt;/code&gt; loop instead &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;foreach&lt;/code&gt;&lt;/p&gt;

&lt;h4 id=&quot;async-function-in-foreach-loop---will-it-work&quot;&gt;Async function in foreach loop - will it work?&lt;/h4&gt;

&lt;p&gt;The second thing in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;foreach&lt;/code&gt; loop that could be misleading is:&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;foreach()&lt;/code&gt; expects a synchronous function - it does not wait for promises&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Check this code:&lt;/p&gt;

&lt;script type=&quot;text/notebook-cell&quot;&gt;
const array = [0, 1, 2, 3, 4];
let sum = 0;

const sumFunction = async (a, b) =&gt; a + b;

array.forEach(async (number) =&gt; {
  sum = await sumFunction(sum, number);
});

console.log(&quot;Sum:&quot;, sum); // Expected output: 10, actual: 0
&lt;/script&gt;

&lt;p&gt;As you can see, async function is not awaited.&lt;/p&gt;

&lt;p&gt;Maybe for someone this two topics are obvious, but for me both of presented behaviors were unexpected (until today 😉).&lt;/p&gt;
</description><description>I believe that you use foreach loop in Javascript, but do you know about two specific behaviors in it?</description><pubDate>Wed, 07 Feb 2024 00:00:00 +0000</pubDate>
        <link>https://torszulak.github.io/blog/js_foreach_non_obvious_behavior/</link>
        <guid isPermaLink="true">https://torszulak.github.io/blog/js_foreach_non_obvious_behavior/</guid></item><item>
        <title>Few ways to get current directory in .NET</title>
        <description>&lt;p class=&quot;intro&quot;&gt;&lt;span class=&quot;dropcap&quot;&gt;S&lt;/span&gt;ometimes in our application, we need to obtain the current directory path. The purposes for doing so can vary: we might want to find a specific file or even load an external assembly. To achieve this, we have to determine the current location of our running application. .NET provides us with a few ways to do this, but some of them can be tricky in specific cases.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Property AppContext.BaseDirectory&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In .NET this property returns path to directory that contains the entry point assembly (directory of the host executable). You could consider it like replacement for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AppDomain.CurrentDomain.BaseDirectory&lt;/code&gt; from .NET Framework.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Directory.GetCurrentDirectory()&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Returns the path from &lt;strong&gt;where you start&lt;/strong&gt; the application. It won’t always be the same path as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AppContext.BaseDirectory&lt;/code&gt;. For example if you run your .NET console application from Powershell in the directory where .exe file exists it returns:&lt;/p&gt;
&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-powershell&quot; data-lang=&quot;powershell&quot;&gt;&lt;span class=&quot;n&quot;&gt;PS&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;D:\Application\bin\Debug\net7.0&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;\Application.exe&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Directory.GetCurrentDirectory&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;returns:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;D:\Application\bin\Debug\net7.0&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;
&lt;p&gt;but if you run the same app from parent folder it will return:&lt;/p&gt;
&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-powershell&quot; data-lang=&quot;powershell&quot;&gt;&lt;span class=&quot;n&quot;&gt;PS&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;D:\Application&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;\bin\Debug\net7.0\Application.exe&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Directory.GetCurrentDirectory&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;returns:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;D:\Application&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;
&lt;p&gt;In the same way it will be work if you run your application which is hosted as Windows Service - then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GetCurrentDirectory()&lt;/code&gt; method will return path to &lt;em&gt;services.exe&lt;/em&gt; file: Windows\system32. So you should be aware if you use this method 😉&lt;/p&gt;

&lt;p&gt;Property &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Environment.CurrentDirectory&lt;/code&gt; works in exactly the same way as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Directory.GetCurrentDirectory()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Get location by reflection&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can also get current directory using reflection.&lt;br /&gt;
.NET offers two similar method: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Assembly.GetExecutingAssembly().Location&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Assembly.GetEntryAssembly().Location&lt;/code&gt;.&lt;br /&gt;
The difference between them is that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GetExecutingAssembly()&lt;/code&gt; returns assembly that contains the code that is currently executing, while &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GetEntryAssembly()&lt;/code&gt; returns assembly that is the process executable in the default application domain, or the first executable that was executed by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ExecuteAssembly(String)&lt;/code&gt;.&lt;br /&gt;
For example if you run above methods in test project &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GetExecutingAssembly()&lt;/code&gt; will naturally return the test project assembly, but &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GetEntryAssembly()&lt;/code&gt; will return assembly of test runner (e.g. testhost.dll or ReSharperTestRunner.dll).&lt;br /&gt;
The usage of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Assembly.GetExecutingAssembly().Location&lt;/code&gt; could be tricky in some scenarios. Consider a situation where you have main application that, in some way, uses an external assembly (e.g. load some plugin) as follows:&lt;/p&gt;
&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-csharp&quot; data-lang=&quot;csharp&quot;&gt;&lt;span class=&quot;n&quot;&gt;Assembly&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;assembly&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Assembly&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;LoadFrom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;$&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AppContext&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;BaseDirectory&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;\\plugins\\OtherProject.dll&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;
&lt;p&gt;&lt;em&gt;Note that desired dll is located in application subdirectory ‘plugins’&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;If you call method from this assembly that uses &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GetExecutingAssembly()&lt;/code&gt; &lt;strong&gt;it returns the path to ‘plugins’ folder, not to the application directory&lt;/strong&gt;.&lt;/p&gt;

&lt;h3 id=&quot;summary&quot;&gt;Summary&lt;/h3&gt;
&lt;p&gt;I presented you only few - in my opinion most popular - ways to obtain the current dictionary path. More importantly I would like to emphasize that seemingly simple code be confusing when debugging our running application. 😉&lt;/p&gt;

&lt;p&gt;Code with examples is available on my &lt;a href=&quot;https://github.com/torszulak/current-directory-example&quot;&gt;Github&lt;/a&gt;&lt;/p&gt;
</description><description>In .NET current directory path can be get in few ways. In this post I want to describe some of them and show how they work in specific cases.</description><pubDate>Sun, 05 Nov 2023 00:00:00 +0000</pubDate>
        <link>https://torszulak.github.io/blog/dotnet_get_current_directory_examples/</link>
        <guid isPermaLink="true">https://torszulak.github.io/blog/dotnet_get_current_directory_examples/</guid></item></channel>
</rss>
