<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Elnur on Medium]]></title>
        <description><![CDATA[Stories by Elnur on Medium]]></description>
        <link>https://medium.com/@elnurdev?source=rss-a1fdf17f82c0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*iRLddm1uxgi20HaY.jpg</url>
            <title>Stories by Elnur on Medium</title>
            <link>https://medium.com/@elnurdev?source=rss-a1fdf17f82c0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 09:41:58 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@elnurdev/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Add Methods to Built-in Classes in C#]]></title>
            <link>https://medium.com/star-gazers/extension-methods-in-c-d05a16bc9f37?source=rss-a1fdf17f82c0------2</link>
            <guid isPermaLink="false">https://medium.com/p/d05a16bc9f37</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[methods]]></category>
            <category><![CDATA[extension]]></category>
            <category><![CDATA[learn]]></category>
            <category><![CDATA[c-sharp-programming]]></category>
            <dc:creator><![CDATA[Elnur]]></dc:creator>
            <pubDate>Sun, 19 Sep 2021 18:53:41 GMT</pubDate>
            <atom:updated>2022-04-23T07:12:39.860Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CbzEjkcLkxBnBghzenYZTg.png" /><figcaption>Created in Canva</figcaption></figure><p>Consider, you are writing a program where you work with the arrays, lists. Let’s say you need to check if the list is empty or not. What would you do ?</p><h4>Code Without Extension Method</h4><p>Probably, if you do not know about the extension methods, you would write something like this:</p><pre>List&lt;int&gt; list = new List&lt;int&gt;();</pre><pre>if (list.Count == 0) {<br>   // Do somethings beautiful<br>}</pre><h4>Code After Writing Extension Method</h4><p>Would not it be better if we would write code like this:</p><pre>List&lt;int&gt; list = new List&lt;int&gt;();</pre><pre>if (list.IsEmpty()) {<br>   // Do somethings beautiful<br>}</pre><p>This definitely would be helpful. So, let’s see how we can do it:</p><h4>Writing Extension Method</h4><p>I am going to create a class for only extension methods. This is not required, but it would be much easier to reuse and maintain the code.</p><pre>public static class ListExtension<br>{<br>    public static bool IsEmpty(this List&lt;int&gt; list)<br>    {<br>        return list?.Count == 0;<br>    }<br>}</pre><p>First of all, this class should be static and non-generic.</p><blockquote>Note: Non-Generic means that we cannot write a generic type like this:</blockquote><pre>public static class ListExtension&lt;MyType&gt;<br>{<br>    <br>}</pre><h4>How Does It Work ?</h4><p>Although we make these extensions methods static, they actually will not be static. Instead, they will be used as instance methods. I will give an example in a minute.</p><p>When a List&lt;int&gt; instance calls IsEmpty() method, that instance will be sent as a parameter to this method. this keyword tells the compiler to recognize IsEmpty() method as the built-in List&lt;int&gt; class’ method.</p><p>Then, we have a return list?.Count == 0 which first checks if the list is null or not (with ? operator, you can read more <a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/member-access-operators#null-conditional-operators--and-">here</a>) and return a boolean that indicates if the list’s item count is 0 or not.</p><h4>Example</h4><p>In this example, we have created a list with three items in it. When we call IsEmpty() method, the output is false obviously.</p><pre>public class Program<br>{<br>    public static void Main(string[] args)<br>    {<br>        List&lt;int&gt; list = new List&lt;int&gt;() { 1, 2, 3 };<br>        <br>        Console.WriteLine(list.IsEmpty()); // outputs false</pre><pre>    }<br>}</pre><p>If you are reading this sentence, then it means that you have read the whole article and have a good understanding of extension methods.</p><p>I hope it was helpful for you and see you in the next articles :).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d05a16bc9f37" width="1" height="1" alt=""><hr><p><a href="https://medium.com/star-gazers/extension-methods-in-c-d05a16bc9f37">Add Methods to Built-in Classes in C#</a> was originally published in <a href="https://medium.com/star-gazers">Star Gazers</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Practice Object-Oriented Programming]]></title>
            <link>https://elnurdev.medium.com/how-to-practice-object-oriented-programming-27dc47916fb?source=rss-a1fdf17f82c0------2</link>
            <guid isPermaLink="false">https://medium.com/p/27dc47916fb</guid>
            <category><![CDATA[practice]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[oop]]></category>
            <category><![CDATA[csharp]]></category>
            <dc:creator><![CDATA[Elnur]]></dc:creator>
            <pubDate>Sat, 31 Jul 2021 19:22:16 GMT</pubDate>
            <atom:updated>2021-08-01T05:56:05.194Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DtzP18BVxE4YKNQGh7YH1w.png" /></figure><p>Whenever we want to learn something, we need to practice it. For example, if we learn the sorting algorithms, we go and solve some problems with this algorithm. Practicing algorithms is a little easy since there are lots of great websites for that. But how are we going to practice Object-Oriented Programming (OOP) ?</p><p>OOP is one of the significant topics of programming. It helps us to write more clean, structured and organized code. Experienced programmers would always use OOP concepts to have well-written code.</p><p>Of course, the best way of practicing OOP is starting to use them in your projects, but most of the projects are much bigger, so we need some little projects.</p><p>What we need is “simulating” the real world. It might seem weird, but I am going to explain what I mean.</p><p>As an example, let’s say we are simulating a computer. Our computer has a monitor, case, keyboard and mouse. We are going to print some messages to the console as if it is happening in real life. This is why I am calling it “simulating”.</p><p>We first create our main class, Computer:</p><blockquote>Note: I am using c# as a programming language, but it does not matter what you want to use as long as it is OOP language</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/17d0c53efc74654eaa583a08ab24e6ea/href">https://medium.com/media/17d0c53efc74654eaa583a08ab24e6ea/href</a></iframe><p>We also have four different classes for our monitor, case, keyboard and mouse.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/27a0038a2d5d7ef938b6649d5e38adeb/href">https://medium.com/media/27a0038a2d5d7ef938b6649d5e38adeb/href</a></iframe><p>We will declare instance variables to represent these devices as our computer’s part:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d724c4de6ffc2db859d0015e02827c71/href">https://medium.com/media/d724c4de6ffc2db859d0015e02827c71/href</a></iframe><p>We also need a constructor to set them. Think of it as we plug them in:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/22a7879ad8eba66d4bb8c906f309a3f3/href">https://medium.com/media/22a7879ad8eba66d4bb8c906f309a3f3/href</a></iframe><p>All of our devices should be able to turn on, so we add TurnOn() method to them:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/db513bcfd944443c0700eaeea5f2b15e/href">https://medium.com/media/db513bcfd944443c0700eaeea5f2b15e/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/db396a40260c9b7966592546458fbcd4/href">https://medium.com/media/db396a40260c9b7966592546458fbcd4/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d68f1e561d2e06a37876be6bba993ea2/href">https://medium.com/media/d68f1e561d2e06a37876be6bba993ea2/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c1d7751184e1b61e5ec458282d6794e3/href">https://medium.com/media/c1d7751184e1b61e5ec458282d6794e3/href</a></iframe><p>What do we do when we want to turn on the computer in real life ? We press the power button in our computer</p><p>In our Computer class’ TurnOn() method we call the case’s PressPowerButton() method:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7be1d58347c14c6b65fec8ee4b40fb42/href">https://medium.com/media/7be1d58347c14c6b65fec8ee4b40fb42/href</a></iframe><p>Before running our program, let’s update our Case class’ TurnOn() method to turn on other devices too. This time, it will take other devices as a parameter and will case their TurnOn() method.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/28b659f00d2086bd85699d70d7b8a8d4/href">https://medium.com/media/28b659f00d2086bd85699d70d7b8a8d4/href</a></iframe><p>Now let’s create a ComputerSimulator.cs class to test what we have done till now:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b8bbbd7efbe23cf343a5c207ca437c38/href">https://medium.com/media/b8bbbd7efbe23cf343a5c207ca437c38/href</a></iframe><p>The output is like this:</p><pre>Pressing the power button<br>Monitor is turning on<br>Keyboard is turning on<br>Mouse is turning on</pre><p>OOP is very deep topic and understand it take some time. So, be patient and start to practice them by using this technique.</p><p>Thanks for reading till here. I hope it was helpful to you :).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=27dc47916fb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Algorithms: Linear Search]]></title>
            <link>https://medium.com/star-gazers/quick-algorithms-linear-search-240e760ed54d?source=rss-a1fdf17f82c0------2</link>
            <guid isPermaLink="false">https://medium.com/p/240e760ed54d</guid>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[step-by-step]]></category>
            <category><![CDATA[linear-search]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Elnur]]></dc:creator>
            <pubDate>Mon, 22 Feb 2021 14:25:46 GMT</pubDate>
            <atom:updated>2021-12-05T21:13:48.178Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ub-_mu70PGQpzDwmieYjdw.png" /></figure><p>If you are beginner in programming, you would probably want to practice your skills. When you do, you would see some coding challenges which is not easy to understand.</p><p>In this article, I will go through some of those algorithms and explain them. <br>So, let’s learn our first algorithms:</p><h4>Searching for a Specific Element in the Array (Linear Search)</h4><blockquote>Note: You can also use lists instead of an array, the process is exactly the same</blockquote><p>You will use this algorithm almost every time. It basically, search for the specific element and check if it exists in the array or not.</p><p>To understand how this algorithm work let’s have a look at the image below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6AQEKIqpGm6vnqcORM5wlg.png" /></figure><p>Let’s say we have an array with the size of six. This array has six different numbers in it.</p><p>Now, let’s say we want to write a program that says if a particular number exists in this array or not. To do that, we traverse the array and in each step, we check if our current array element is equal to the target element.</p><p>To understand it better, let’s dive into code:</p><blockquote>Note: In this tutorial, I am using C# programming language, but the logic is the same in all languages</blockquote><h4>Getting Target Number From the User</h4><p>Firstly, I have written some codes to get the number from the user:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e343d4bce0e524c44ec3f532b9b308a4/href">https://medium.com/media/e343d4bce0e524c44ec3f532b9b308a4/href</a></iframe><ul><li>Console.ReadLine() method takes input from the user.</li><li>int.Parse() method convert that input to the integer.</li></ul><blockquote>Note: In C#, we get the input as a string</blockquote><ul><li>I have declared an array with the 6 values</li></ul><p>Then, I have written a method that returns a string value. This value will be either Found or Not Found.</p><h4>Creating a Method Signature</h4><p>So let’s create our method:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0e6d0f6e56bb147bc761831b98255548/href">https://medium.com/media/0e6d0f6e56bb147bc761831b98255548/href</a></iframe><p>This method takes 2 parameters: the first parameter is the array, the second parameter is the target value we are searching for. The method return value will be a string type.</p><h4>Writing LinearSearch Method</h4><p>To search for a target value, we need to loop through the array. We will do that by using a for loop:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/85386dcea6bb182fe68c75cffc462831/href">https://medium.com/media/85386dcea6bb182fe68c75cffc462831/href</a></iframe><p>This loop will start from the 0th index (int i = 0) and will continue till it reaches the end (i &lt; array.Length).</p><p>array.Length gives the count of the elements. If we have six elements, the loop will continue till i &lt; 6, the maximum value of the i will be 5 which is the last index of the array. As we know, arrays have a zero-based index system.</p><h4>Checking For a Target Value in Each Step and Returning a Message Depending on it.</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ecac95fd61e7ffe545e9303f636053f4/href">https://medium.com/media/ecac95fd61e7ffe545e9303f636053f4/href</a></iframe><ul><li>array[i] is the ith element in the arrays.</li></ul><p>In each step, we check if the current element (array[i]) is equal to our target element, if so our method return &quot;Found&quot;.</p><p>If it couldn’t have found a target element after searching the whole array, it continues and returns &quot;Not Found&quot;.</p><h4>Calling The Method</h4><p>As we have written our method, now let’s call it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4ef0feb24c3695ca76036d3b5b7e5008/href">https://medium.com/media/4ef0feb24c3695ca76036d3b5b7e5008/href</a></iframe><p>We first call the LinearSearch method and stores its return value in a string called found.</p><p>Then, we print that string.</p><h4>Testing Program</h4><p>Here is the most interesting part, check if the particular number exists in the array or not:</p><p>I first check for 26:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/976/1*kKaDwP-FvPDP1kOCH60SdQ.png" /></figure><p>It printed &quot;Found&quot;. If we remember the array, we can see that we have 26 in it:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6AQEKIqpGm6vnqcORM5wlg.png" /></figure><p>Now, let’s check for 7:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/976/1*mpICsLvgsIJC61OUN58V2g.png" /></figure><p>It printed &quot;Not Found&quot; as our array doesn’t include the 7.</p><h4>Conclusion</h4><p>The Linear search is one of the important algorithms used in programming. If your reading this part, this means that you have read the article. I hope you have understood it.</p><p>If you have any question about the article, feel free to share in the comment section.</p><p>Thanks for reading my article.</p><h4>My Other Articles:</h4><ul><li><a href="https://medium.com/star-gazers/arrays-vs-lists-in-c-c7cafb268f92">Arrays vs Lists in C#</a></li><li><a href="https://thelightgreen.medium.com/click-logic-behind-click-based-games-55d1d142a64e">Click Logic Behind Click-Based Games</a></li><li><a href="https://thelightgreen.medium.com/linked-list-b69115a0fa78">Linked List</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=240e760ed54d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/star-gazers/quick-algorithms-linear-search-240e760ed54d">Algorithms: Linear Search</a> was originally published in <a href="https://medium.com/star-gazers">Star Gazers</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding Time.deltaTime]]></title>
            <link>https://medium.com/star-gazers/understanding-time-deltatime-6528a8c2b5c8?source=rss-a1fdf17f82c0------2</link>
            <guid isPermaLink="false">https://medium.com/p/6528a8c2b5c8</guid>
            <category><![CDATA[movement]]></category>
            <category><![CDATA[unity]]></category>
            <category><![CDATA[csharp]]></category>
            <category><![CDATA[game-development]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Elnur]]></dc:creator>
            <pubDate>Sun, 24 Jan 2021 13:31:41 GMT</pubDate>
            <atom:updated>2023-03-26T20:25:43.211Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iPjs2LjqepE097b280YVpw.png" /></figure><p>Most of the people (including me) who start with Unity are having a problem with learning Time.deltaTime. To understand how it works, firstly, I am going to explain its definition and secondly, I am going to show a step-by-step example.</p><p>By definition, Time.deltaTime is the completion time in seconds since the last frame. This helps us to make the game frame-independent. That is, regardless of the fps, the game will be executed at the same speed. Let’s understand what this means:</p><p>Suppose, our game runs at 60 fps. It means that the game is updating 60 times per second, in other words, there are 60 frames.</p><blockquote><strong>Note: FPS stands for frames per second</strong></blockquote><p>Now, let’s have a look at to the time between frames:</p><p>Firstly, when the game starts, frame 1 is executing and when it finishes, frame 2 starts executing. When frame 1 ended, the frame 1 starts to be calculated till the end of the frame 2. Let’s see the image below to understand it:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HG8GMAasXqYifGjeHhwfYw.png" /></figure><ul><li>Before the frame 1 is executing, the Time.deltaTime is 0</li><li>Then, the game continues and frame 2 is executing. So, the time between those 2 frames is 0.05 seconds.</li><li>So, the Time.deltaTime is 0.05 seconds. That’s why we call it the time which passed since the last frame.</li></ul><blockquote><strong>Note: Its value is changing continuously, for this example, I just used 0.05</strong></blockquote><p>As we understood what does that mean, let’s see the usage of it and why we are using it:</p><p>Always, when we want to move an object, no doubt we use an Update() method.</p><blockquote>Note: Update() method is called once per frame</blockquote><p>I want to show the difference between using Time.deltaTime and not using it.</p><p>I have a simple player:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-6yyMAiuwTkUPH48zTaedg.png" /></figure><p>And, I attached a Test script to it</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/453/1*3DM0FT-QKefd7Co_6Y54pQ.png" /></figure><p>The script code looks like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/701/1*5alAfecWTkvIaN4D7J1mJw.png" /></figure><ul><li>I declared a float speed variable and assigned 10 to it</li><li>Then in Update() method, I used transform’s Translate method to move the object</li><li>It moves the object forward and multiplies forward vector by speed<br>I didn’t multiply it with Time.deltaTime intentionally</li></ul><blockquote>Note: Space.Self is the parameter which is used to say to move the object local axis, not for world axis (We don’t need to know this, I just explained to the people who are curious about it)</blockquote><h4>Not Using Time.deltaTime</h4><p>Let’s calculate our total movement when we are <strong>not</strong> using Time.deltaTime:</p><p>Suppose, we are testing our game on 2 different computers. In some game, one of them gets 20 fps, and the other one gets 500 fps.</p><h4>Calculating Total Movement for First Computer</h4><p>Firstly, let’s see the total movement for the first computer (20 fps):</p><p>In 1 second, the Update() method will be executed 20 times.</p><p>Our total movement will look like:</p><p>total movement = 20 * Vector3.forward * speed</p><blockquote>Note that Vector3.forward is shorthand for writing new Vector3(0, 0, 1), so it moves by 1 unity.</blockquote><p>If we calculate the total movement, we get:</p><p>total movement = 20 * 1 * 10 = 200;</p><h4>Calculating Total Movement for Second Computer</h4><p>Now, let’s see the total movement for the second computer (500 fps)</p><p>In 1 second, the Update() method will be executed 500 times.</p><p>Our total movement will look like:</p><p>total movement = 500 * Vector3.forward * speed</p><p>Now, if we calculate the total movement, we get:</p><p>total movement = 500 * 1 * 10 = 5000;</p><p>The player who is playing in 20 fps moved 200 unit and the other one who is playing in 500 fps moved 5000 unit. As you can see it is <strong>frame-dependent. </strong>The more fps you have, the more the player moves.</p><h4>Using Time.deltaTime</h4><p>Now, I changed the code so that the movement vector is multiplying by Time.deltaTime too:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/731/1*6-Scj3f-POTMoFEUvP6I4g.png" /></figure><h4>Calculating Total Movement for First Computer</h4><p>In 1 second, the Update() method will be executed 20 times</p><p>Time.deltaTime is approximately equal to 1 divided by fps count. Because, the game runs 20 times per second, so 1 / 20 is the time for each frame</p><p>Time.deltaTime = 1 / 20 = 0.05</p><p>Our total movement will look like:</p><p>total movement = 20 * Vector3.forward * speed * Time.deltaTime</p><p>Now, if we calculate the movement:</p><p>total movement = 20 * 1 * 10 * 0.05 = 10</p><h4>Calculating Total Movement for Second Computer</h4><p>For 500 fps, in 1 second, the Update() method will be executed 500 times.</p><p>So, Time.deltaTime is equal to the following:</p><p>Time.deltaTime = 1 / 500 = 0.002</p><p>Our total movement will look like:</p><p>total movement = 20 * Vector3.forward * speed * Time.deltaTime</p><p>Now, if we calculate the movement:</p><p>total movement = 500 * 1 * 10 * 0.002 = 10</p><p>As you can see, when we use the Time.deltaTime the total movement is equal to 10. So, we see that it is frame-independent. Regardless of how fast the computer is, the game will be played at the same speed.</p><p>That was all for this article. I hope it was clear and you understood Time.deltaTime.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6528a8c2b5c8" width="1" height="1" alt=""><hr><p><a href="https://medium.com/star-gazers/understanding-time-deltatime-6528a8c2b5c8">Understanding Time.deltaTime</a> was originally published in <a href="https://medium.com/star-gazers">Star Gazers</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Complete Classes and Objects Tutorial]]></title>
            <link>https://medium.com/star-gazers/learn-classes-and-objects-8d7293ce607b?source=rss-a1fdf17f82c0------2</link>
            <guid isPermaLink="false">https://medium.com/p/8d7293ce607b</guid>
            <category><![CDATA[class]]></category>
            <category><![CDATA[object-oriented]]></category>
            <category><![CDATA[constructor]]></category>
            <category><![CDATA[objects]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Elnur]]></dc:creator>
            <pubDate>Tue, 22 Dec 2020 09:37:51 GMT</pubDate>
            <atom:updated>2021-07-25T12:05:52.399Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xioo_jcb22JuG7Xqndau8w.png" /></figure><p>All of the programming languages which have Object-Oriented Programming (OOP) feature have classes and objects. We can think of a class as the blueprint for creating objects.</p><p>To understand them, let’s see the real-world example:</p><p>We have an Animal class, and Cat object which is the object of the Animal class. Animal class has some specifications like their color, height, average life expectancy and so on. If we create an object of it with the name of Cat, then it will have all of those specifications.</p><p>Similarly, if we look at the <strong>Object-Oriented Programming</strong>, we can see that a class defines some fields, properties, methods etc. These are properties which the objects of this class will have.</p><p>By definition, a<strong> class </strong>is the user-defined data type which is created by grouping built-in data types, methods, constructors etc.</p><p>As we understood the basic meaning of classes, let’s dive into the programming to learn how are they used.</p><blockquote>Note: In this tutorial, I am using C# language, but it does not matter which language you know. The idea behind this blog is understanding classes so that you can implement in whatever language you want.</blockquote><p>Firstly, we need to declare classes outside of Main method (In some languages like Python, you do not have Main method, so you simply need to declare it):</p><p>To declare a class we use the following syntax:</p><p>[access modifier] class [unique name] {}</p><p>For example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/438/1*CrbRnLQXz60nVS4JCQaiWw.png" /></figure><blockquote>Note: In this case, we did not specify the access modifier so that it will be private automatically. So, it will not be accessed outside of Program class.</blockquote><p>We can declare variables and methods in that class:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/558/1*MeSq5258bhjp1bd4vM64Qw.png" /></figure><p>As you see, we declared an int variable age, a string variable name, and an Eat() method. So, now the Animal class have properties. When we create objects, they will be able to use those variables and methods.</p><blockquote>Note: It is not good practice to make variables public, because it gives direct access which can cause problems. For now, to make the example simpler, I marked them as public.</blockquote><p>If we review what we have done so far, first we declared a class, then declared its variables and methods. Now, let’s see how we create an object of the class:</p><p>To create an object of a class, we first declare it then we initialize it:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/558/1*BV65XCfiSG-yXbYQl59DJg.jpeg" /></figure><p>When we initialize an object, it automatically calls a constructor. To understand how these all work, let’s first learn constructors.</p><h4>What are Constructors? How do we use it?</h4><p>As the name implies, the constructors are used to construct a class. We can have so many constructors. Similar to methods, they may have parameters.</p><p>Their syntax is like this:</p><p>[access modifier] [class name] () {}</p><p>A constructor should be public to create an object by using that constructor.</p><p>Constructors are similar to methods, but they are entirely different things. They do not have any return type. They have different versions, but in our case, we only need to know <strong>default</strong> and <strong>parameterized constructors</strong>. Because we need to learn them to understand how objects are created.</p><p>So, let’s first have a look at the Default Constructor:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/553/1*S3EOuj1cmNEP2F9uN-k19A.jpeg" /></figure><p>In this code, We created a default constructor which assign 5 to the age and “Cat” to the name. It does not matter how many objects we create, if we create that objects using this constructor, then its age will be equal to 5, and name will be equal to “Cat”.</p><h4>Parameterized Constructors</h4><p>When we create an object, we can pass some parameters like an age to the constructor, and assign those parameters to the object’s variables. These types of constructors are called <strong>parameterized constructors</strong> To do that, we need to create a parameterized constructor:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/518/1*wtdFDazz6BJZEAFviwatJw.jpeg" /></figure><p>Let’s understand those codes, one-by-one:</p><blockquote>Note: In this case, we are not using default constructor, so its existence will not matter for us. I just did not delete it, because I want to show two or more constructor can be used in the class.</blockquote><p>Firstly, we created our second constructor which takes two parameters:</p><ul><li>an integer age</li><li>a string name</li></ul><p>Then, it assigns those values to the object’s values in this part:</p><pre>this.age = age;<br>this.name = name;</pre><p>By using this keyword we can differentiate the class’s age or name variables and the ones which are passed to the constructor.</p><p>To review what we have done here:</p><ul><li>We first initialize an object with two parameters 10 and Dog corresponding to age and name.</li><li>Then we assign those values to the object’s age and name values</li></ul><p>In this case, we can use that object to access its variables and methods:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/360/1*P-9APGFFNuAG6_oRR3hegw.png" /></figure><p>As you can see, we created an object with the name of dog and accessed its properties:</p><ul><li>We printed its age</li><li>We printed its name</li><li>We called its Eat() method which prints “Food has been eaten”;</li></ul><p>After all of these statements, the result will look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/993/1*HkGXM81Az-h9yvXBsFvy9A.png" /></figure><h4>Creating an object from a class in which we did not create a constructor</h4><p>Till now, we have learnt that when we want to create an object of any class, we have to use constructors.</p><p>But a question arises, how can an object be created if we did not have a constructor in a class.</p><p>The answer is simple. If we do not create any constructor, it automatically does it behind the scene. For example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/558/1*MeSq5258bhjp1bd4vM64Qw.png" /></figure><p>In this code piece, we did not declare a constructor, but we want to create an object of that class. As you can see we write:</p><pre>Animal animal = new Animal();</pre><p>In this case, the Animal class will say to the animal objects that:</p><p><em>Class: Hey, I do not have any declared constructor, so I will initialize you with my default constructor.</em></p><p>Then the object will automatically be initialized. We do not see that constructor, but we know it is there and works behind the scene. This constructor is called <strong>default constructor</strong>.</p><h4>Creating More Than One Object</h4><p>In the previous examples, we created only one dog object. Now, let’s see how we are creating multiple objects:</p><p>Our Animal Class:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/664/1*STeg-YcpdlrH_ZOPRPdwDw.png" /></figure><p>In the Eat() method I did some changes so that when it is called it will print in the format &quot;Animal ate the food”. The rest of the class is the same.</p><p>Our Main method which creates 3 objects:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/650/1*p_HISwAY2jijohaUYKNkqg.png" /></figure><p>In this case, as you can see we are creating 3 objects, printing their age, name, and calling their Eat() method. At the end, the result will look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/988/1*BY2o-WSSYBXX_aeJYbrSjA.png" /></figure><p>Firstly, it prints dog’s information, then cat’s, and at the end, it prints bird’s information.</p><h4>Conclusion</h4><p>To summarize, Classes and Objects are the most necessary topics in Object-Oriented Programming. Without learning them, we cannot continue to <strong>OOP</strong>. <br>In this article, I have explained classes and how are they used.</p><p>I hope it was helpful and you liked it.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8d7293ce607b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/star-gazers/learn-classes-and-objects-8d7293ce607b">Complete Classes and Objects Tutorial</a> was originally published in <a href="https://medium.com/star-gazers">Star Gazers</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[7 Useful Unity Attributes That Make the Inspector Easier To Use]]></title>
            <link>https://medium.com/star-gazers/4-useful-unity-attributes-that-make-the-inspector-easier-to-use-part-1-d42b5b0b7543?source=rss-a1fdf17f82c0------2</link>
            <guid isPermaLink="false">https://medium.com/p/d42b5b0b7543</guid>
            <category><![CDATA[games]]></category>
            <category><![CDATA[programmming]]></category>
            <category><![CDATA[unity]]></category>
            <category><![CDATA[attributes]]></category>
            <category><![CDATA[unity-game-development]]></category>
            <dc:creator><![CDATA[Elnur]]></dc:creator>
            <pubDate>Wed, 16 Dec 2020 18:49:27 GMT</pubDate>
            <atom:updated>2021-01-06T10:56:50.008Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C4uGvLm28sMLt24a62DWOw.png" /></figure><p>When I am working with Unity, I have learnt some very useful attributes. For the first time, when I learnt them, I got surprised about how helpful they are. They help us in using the inspector easier, saving your time, and so on. For example, I have learnt theHeader attribute that helps us to organize our variables in the inspector. Imagine, you have lots of variables and do not use Header, how messy they are going to seem! Fortunately, we have these kinds of stuff in Unity. In this article, I am going to explain some of them to help you when you are working in Unity.</p><p>If you want to learn these great attributes and improve your working quality, let’s get started.</p><h4>SerializeField Attribute</h4><p>If you want to write better code, of course, one of the main points is <a href="https://en.wikipedia.org/wiki/Encapsulation_(computer_programming)">Encapsulation</a>. For example, if you have a variable, which you do not want to be accessed by another class, you mark it as private. In this case, it will not show up in the inspector. Because, Unity only serialize public variables, so that they are being visible in the inspector and their values can be changed. To display a private field in the inspector, we can use SerializeField attribute.</p><blockquote><strong>Note: Using public instead of </strong><strong>SerializeField attribute is not good practice. Whenever you want something to show up in Inspector, use </strong><strong>SerializeField attribute.</strong></blockquote><p>Let’s see a quick example here:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/425/1*0R6Ip2BgPI_LYJSiUmMiJQ.png" /></figure><p>In the script, we have 3 different variables. Two of them are private, and the other one is public. We have marked the first private field with [SerializeField, we have not marked the second private field with [SerializeField, and the public field will automatically be displayed in the inspector. So, let’s look at how they are showing up in the inspector:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/454/1*I4N4xJETF78-oCLhLwBLCA.png" /></figure><p>As you can see score and myFloat variables are displayed in the editor, and we can change their value, but speed value didn’t show up there. That’s because we didn’t mark it with [SerializeField] and it is private.</p><h4>Header Attribute</h4><p>Imagine you have lots of fields in the inspector, they all will be display mixed. You will end up with spending redundant time for searching some fields. In these situations, Header attribute comes handy. Let’s have a look at to the code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/509/1*L0qy0loQI5xbwGA9YI2kZA.png" /></figure><p>Firstly, I want to show you how fields are displayed before using Header attribute:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/452/1*HSzB07gMte7ZKXLkR3xZFg.png" /></figure><p>After implementing Header attribute:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/449/1*7txZW7k8KhF5tfMBK9fnJg.png" /></figure><p>Look how they are organized, you do not need to be confused between them!</p><h4>Range Attribute</h4><p>When we make a field to show up in the inspector, we can assign any value to them. If we want to restrict it, we can use Range attribute. With this attribute is used, it will do both: restricting assignment between two number and making it to show up as a slider. Let’s have a look at the example code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/456/1*XpRtAyVqMPyRNgaDyV9aaA.png" /></figure><p>After that code, our variables will look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/456/1*EDgZZXiqQvyKtwWqXZuceQ.png" /></figure><h4>Tooltip Attribute</h4><p>Sometimes we want to have a description of the variables in the Inspector. In this case, Tooltip attribute can be helpful. For example, let’s say we have a damage value which should only be between 0 and 150. If we use this attribute when we hover over the damage variable we will see a description. Let’s see the example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/607/1*DWj_nnL7-GTRZAI4S-vRxQ.png" /></figure><p>In the Inspector, it will look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/441/1*wdnQG0twjTlUN0M4DFukbg.png" /></figure><h4>RequireComponent Attribute</h4><p>Imagine we are working with the Rigidbody component and we have to attach the script to the five different objects. Instead of adding Rigidbody component one-by-one, we can use this handy feature. This attribute makes sure that whenever we want to attach this script to some object, it is added together with the component which we have declared. Let’s see the example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/462/1*6-IFkR3UAqLa9VSJJpef5Q.png" /></figure><p>In the script, we added RequireComponent for the type Rigidbody. When we added this script to the game object, Rigidbody is also added.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/454/1*cpgYwKcdcgoRT5FPYLRzVQ.png" /></figure><p>Also, if we want to remove the Rigidbody component, we will not be able to it. It will say:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/379/1*R9NY-9Te28bO3M2iss8Jyg.png" /></figure><p>It will prevent us from accidentally removing it and wasting time.</p><h4>TextArea Attribute</h4><p>Normally, when we declare a string variable which will be set in the Inspector, we get only 1 line. When we add long string, it continues to the only right which is not readable. It is displayed like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/453/1*qobDLQhqSsCE5uNjbtMflQ.png" /></figure><p>Now, let’s see it with the TextArea Attribute:</p><p>Firstly, we add this attribute before the string variable:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/456/1*QEtJmprRDX2aZj9zdef-Jw.png" /></figure><p>As you can see, there are 2 parameters: minLines and maxLines.</p><ul><li>minLines define at least how many lines have to be used</li><li>maxLines define how many lines have to appear at most. If there is more line than the maxLines, we can still add a new line. But instead of expanding, it will be controlled by scrolling (I will show it in a minute).</li></ul><p>In the Inspector, it will be displayed like this without text:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/455/1*DWzyynWQ66C9d9njdMPqLQ.png" /></figure><p>Our minLines property is 2, so it has 2 lines by default.</p><p>Our maxLines property is 4. If we add 6 lines, for example, we will see a scroll button:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/451/1*hJ6JbsH5YicjrAgL7oOwqQ.png" /></figure><h4>SelectionBase Attribute</h4><p>Let’s say we have an empty parent object and a cube object which is a child of it. Like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/329/1*comHZu21-rdT-zr1yLPLmw.png" /></figure><p>In the scene view:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0K4FZF5-Z8c9O6z0GWFeEA.png" /></figure><p>As a default, if we click to the object, it will choose Child one:</p><p>Now, let’s add TestScript to the Parent object and mark it as SelectionBase.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/455/1*nN8N12G8v46euTs3y9lC3w.png" /></figure><blockquote>Note: We added TestScript to the only Parent object.</blockquote><p>In the code, we mark the class as SelectionBase:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/415/1*ApoLyuL6GCb0_dNfZ1v4-g.png" /></figure><p>Now, whenever we click to them, it will choose the Parent object, even though it is an empty object:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KBPxpSWBlsxX0HbxBiIMrw.png" /></figure><h4>Conclusion</h4><p>To sum up, in this article, I explained 7 helpful Unity attribute which helps us when we are working. I hope you liked it.</p><blockquote>If you want to read more get this link:</blockquote><p><a href="https://docs.unity3d.com/ScriptReference/index.html">Unity - Scripting API:</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d42b5b0b7543" width="1" height="1" alt=""><hr><p><a href="https://medium.com/star-gazers/4-useful-unity-attributes-that-make-the-inspector-easier-to-use-part-1-d42b5b0b7543">7 Useful Unity Attributes That Make the Inspector Easier To Use</a> was originally published in <a href="https://medium.com/star-gazers">Star Gazers</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Override Methods in C#]]></title>
            <link>https://medium.com/swlh/how-to-override-methods-in-c-abeada7e62a?source=rss-a1fdf17f82c0------2</link>
            <guid isPermaLink="false">https://medium.com/p/abeada7e62a</guid>
            <category><![CDATA[code]]></category>
            <category><![CDATA[methods]]></category>
            <category><![CDATA[learning]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[function]]></category>
            <dc:creator><![CDATA[Elnur]]></dc:creator>
            <pubDate>Tue, 08 Dec 2020 08:40:53 GMT</pubDate>
            <atom:updated>2021-01-23T16:15:14.069Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Sj76gx1iy30Kq2mTEIL-jg.jpeg" /></figure><p>Method Overriding, in Object-Oriented Programming, is a language feature that allows a child class to implement a method that’s is already is in the parent class. Implementation in the subclass overrides the implementation in base class by creating a method that has the same name.</p><p><strong>To understand overriding, let’s look at the real-world example:</strong></p><p>Let’s say we have an Animal class and we have three different animals:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZLV5YrMYFw6qsEHFCC-FOw.png" /><figcaption><strong>Illustration</strong></figcaption></figure><p>As you can see from the image, those three class; Cat, Dog, and Bird inherit from Animal class. They all have a bunch of different parameters and methods.</p><p>So, let’s take a look at them one by one:</p><p>Animal class have two methods called Eat() and Move(). Of course, those three different animals have various type of eating and moving.</p><p>Firstly, our Cat class overrides Eat() and Move() method to implement its behavior. Also, it adds its own Meow() method for its speaking.</p><p>Secondly, our Dog class also overrides Eat() and Move() method. Because, the dog has a different type of eating and moving too. It adds its Bark() method to bark.</p><p>Thirdly, our Bird class overrides Eat() and Move() again and a Fly() method. As everyone knows, only the bird can fly.</p><p>As we grasped overriding methods with real-world examples, let’s take a look at actual code.</p><h4>Overriding Methods in Code</h4><p>Initially, we create an Animal class:</p><pre>class Animal<br>{<br>    public int age;<br>    public <strong>virtual </strong>void Eat()<br>    {<br>        // Eat food<br>    }<br>    public <strong>virtual </strong>void Move()<br>    {<br>        // Move around<br>    }<br>}</pre><p>Then, we create three animal class that inherits from Animal class</p><pre>class Cat : Animal<br>{<br>    public new int age;<br><br>    public <strong>override </strong>void Eat()<br>    {<br>        // Cat eats food<br>    }<br>    public <strong>override </strong>void Move()<br>    {<br>        // Cat moves around<br>    }<br>    public void Meow()<br>    {<br>       // Only cats can meow<br>    }<br>}</pre><pre>class Dog : Animal<br>{<br>    public new int age;<br><br>    public <strong>override </strong>void Eat()<br>    {<br>       // Dog eats food<br>    }<br>    public <strong>override </strong>void Move()<br>    {<br>       // Dog moves around<br>    }<br>    public void Bark()<br>    {<br>       // Only dogs can bark<br>    }<br>}</pre><pre>class Bird : Animal<br>{<br>    public new int age;<br><br>    public <strong>override </strong>void Eat()<br>    {<br>       // Bird eats food<br>    }<br>    public <strong>override </strong>void Move()<br>    {<br>      // Bird moves around<br>    }<br>    public void Fly()<br>    {<br>       // Only birds can fly<br>    }<br>}</pre><p>As you can see there are two different keywords there: <strong>virtual </strong>and <strong>override</strong>. Let’s understand why are they used?</p><h4>Virtual Keyword</h4><p>To override any method, you need to mark the base class’ method as virtual as we did with the Animal class’ method.</p><h4>Override Keyword</h4><p>Once we marked our base class’ method as virtual, we can override it in the child class. To do that, we need to mark our method with override keyword.</p><p>We have learnt the main part, the only thing that left is <strong>calling the method we are overriding.</strong></p><h4>Calling the base class’ method from our child class</h4><p>Let’s say we have a virtual method that prints “Hello, World!”:</p><pre>class MainClass<br>{<br>   public virtual void MainMethod()<br>   {<br>      Console.WriteLine(&quot;Hello, World!&quot;);<br>   }<br>}</pre><p>To override this method, let’s create our child class’ method.</p><p>Once you write “public override” to the editor, IntelliSense will show you the methods that can be overridden. When you click to the method that you want to override, it will automatically write the method code:</p><pre>class ChildClass : MainClass<br>{<br>    public override void MainMethod()<br>    {<br>        base.MainMethod();<br>    }<br>}</pre><p>Wait a minute, there is a strange code here, base.MainMethod().</p><p>Basically, what this method does is calling base the base method that we have overridden. If we now run the program, the output will be like this:</p><pre>Hello, World!</pre><p>Also, we can add code before or after base.MainMethod(). Adding code before or after only effect to the calling order. So, let’s print another message that says Hello, C#!.</p><pre>class ChildClass : MainClass<br>{<br>    public override void MainMethod()<br>    {<br>        base.MainMethod();<br>        <strong>Console.WriteLine(&quot;Hello, C#!&quot;);</strong><br>    }<br>}</pre><p>Now, the output will look like as follow:</p><pre>Hello, World!<br>Hello, C#!</pre><p>Finally, I would mention that overriding method feature is a handy method in Object-Oriented Programming. As we can saw from the examples, we can do so many things by using them. So, I hope you have understood this topic and enjoyed it.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=abeada7e62a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/how-to-override-methods-in-c-abeada7e62a">How to Override Methods in C#</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Arrays vs Lists in C#]]></title>
            <link>https://medium.com/star-gazers/arrays-vs-lists-in-c-c7cafb268f92?source=rss-a1fdf17f82c0------2</link>
            <guid isPermaLink="false">https://medium.com/p/c7cafb268f92</guid>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[lists]]></category>
            <category><![CDATA[c-sharp-programming]]></category>
            <category><![CDATA[versus]]></category>
            <category><![CDATA[arrays]]></category>
            <dc:creator><![CDATA[Elnur]]></dc:creator>
            <pubDate>Sat, 24 Oct 2020 16:18:59 GMT</pubDate>
            <atom:updated>2023-04-30T13:12:17.200Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*_IZLYRhH18J9EsWscs0EuA.png" /></figure><h4>Introduction</h4><p>Arrays and lists are the most used data structures in C# programming language. Both of them store data like integer, string, objects, and so on. They seem very similar at first glance, however they have crucial differences, which affect their usages. Depends on the situation we can prefer to arrays than lists or vice versa. In this article, I am going to explain, which one is better based on what we want to do.</p><h4>Arrays</h4><p>Initially, let’s look at the array’s definition:</p><p>An array is the data structure that stores a fixed number of literal values of the same type. If we want an array with the different types in it, you can define “object” as its type (object type is not preferable, because they are complex).</p><h4>Array Declaration</h4><p>Arrays can be declared by specifying their type and size. Assume, we want an array of integers with the size of 5. We can declare this kind of an array as follows:</p><pre>int[] myArray;</pre><p>To create an array we need to initialize it. We do it using <strong>new</strong> keyword:</p><pre>int [] myArray = new int[5];</pre><p>Additionally, you can initialize array elements when you declare it:</p><pre>int[] myArray = {2,15,36};</pre><p>Then, we can initialize its items as follows:</p><pre>myArray[0] = 2;  // Assign 2 to the myArray[0]</pre><pre>myArray[1] = 15; // Assign 15 to the myArray[1]</pre><pre>myArray[2] = 36; // Assign 36 to the myArray[2]</pre><h4>Advantages and Disadvantages</h4><p>So, as we grasped some basics of the arrays, let’s look at their advantages and disadvantages:</p><blockquote>Advantages</blockquote><ul><li>Arrays are strongly typed, meaning you can only have one type an element in an array (except object array). This feature gives us 2 advantages:</li></ul><p><em>1.Firstly, the performance will be better, because </em><a href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/boxing-and-unboxing"><em>boxing and unboxing</em></a><em> will not occur.</em></p><p><em>2.Secondly, runtime errors caused by type mismatching will not happen. These kinds of errors are commonly seen while using Lists, Stacks, Queues, etc. For example:</em></p><pre>using System;<br>using System.Collections;<br><br>namespace Test<br>{<br>    class Program<br>    {<br>        static void Main(string[] args)<br>        {<br>            Stack stack = new Stack();<br><br>            stack.Push(5);<br>            stack.Push(&quot;Typing&quot;);<br><br>            foreach (<strong>int</strong> item in stack)<br>            {<br>                Console.WriteLine(item);<br>            }<br>        }<br>    }<br>}</pre><p><em>In this code, we declared a stack, then we pushed “5” and “Typing” to the stack. In the </em><em>foreach loop when we wanted to print </em><em>item which is int type, we will get </em><em>InvalidCastException error because of type mismacthing.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/621/1*Jsnksm3cXTjLBIORqjA-Jg.png" /></figure><p>But if we do the same things on the array, it will not compile:</p><pre>using System;<br>using System.Collections;<br><br>namespace Test<br>{<br>    class Program<br>    {<br>        static void Main(string[] args)<br>        {<br>            int[] array = new int[5];<br><br>            array[0] = 5;<br>            array[1] = &quot;Typing&quot;;<br>        }<br>    }<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/645/1*wfx8oNN5flO22wDCgOzWMw.png" /></figure><ul><li>Its elements can be accessed by using their indexes.</li><li>Iterating the arrays using their index is faster compared to any other methods like Linked List etc.</li></ul><blockquote>Disadvantages</blockquote><ul><li>The size of the array should be known before we initialize its elements. Therefore, if we want to add elements more than its size, it will not compile.</li><li>Insertion and deletion are quite difficult in an array because they are stored in consecutive memory locations. To insert and delete an item would need to shifting operation which is costly.</li></ul><h3>Lists</h3><p>Firstly, let’s see its definition:</p><p>The List&lt;T&gt; is the collection of objects that can be accessed by index and have special methods for adding, removing, searching, and sorting elements.</p><h4>List Declaration</h4><p>The List&lt;T&gt; is dynamic data structures, meaning we do not need to know the number of elements when we declare it. Basically, the list of integers can be declared like this:</p><pre>List&lt;int&gt; myList;</pre><p>To use this list, we need to instantiate it:</p><pre>myList = new List&lt;int&gt;();</pre><blockquote><strong>Note</strong>: We used int variable type, but do not forget that we can use other variable types too</blockquote><p>Unlike the arrays, List&lt;T&gt; has its own methods which are very useful.</p><blockquote>Add() Method:</blockquote><p>You can add new elements to the list using the Add() method:</p><pre>myList.Add(2); </pre><pre>myList.Add(15);</pre><pre>myList.Add(36);</pre><p>If we want to print its elements, we would use the foreach loop:</p><pre>foreach(int item in myList) <br>{<br>   Console.WriteLine(item);<br>}</pre><p>Output:</p><pre>2<br>15<br>36</pre><blockquote>Remove() Method</blockquote><p>You can remove items in the list using Remove() method:</p><pre>myList.Remove(15);</pre><blockquote>Contains() Method</blockquote><p>This method helps us to search for a particular item in an array. It returns true if that item exists:</p><pre>if(myList.Contains(36))<br>{<br>   Console.WriteLine(&quot;36 exists in the List&quot;);<br>}</pre><h4>Advantages and Disadvantages</h4><blockquote>Advantages</blockquote><ul><li>List size can be scaled up after declaration.</li><li>Like an array, you can access its elements by indexes.</li><li>It has useful methods like Add() and Remove().</li></ul><blockquote>Disadvantages</blockquote><ul><li>Lists occupy more memory than arrays.</li></ul><h4>Conclusion</h4><p>In conclusion, If you have a fixed size collection, then it would be much better to use arrays. For example, you have a string array called names with the names of 5 people. On the other hand, if your collection has to be dynamic, then use lists. For example, you create a list but you will fill it later. In this case, lists are more useful. So, we can say that both arrays and lists are being really useful when they are used correctly.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c7cafb268f92" width="1" height="1" alt=""><hr><p><a href="https://medium.com/star-gazers/arrays-vs-lists-in-c-c7cafb268f92">Arrays vs Lists in C#</a> was originally published in <a href="https://medium.com/star-gazers">Star Gazers</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>