<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
    <channel>
        <title>نوشته های soheil moonesi</title>
        <link>https://virgool.io/feed/@soheil-moonesi</link>
        <description>C# enthusiast</description>
        <language>fa</language>
        <pubDate>2026-06-16 07:46:14</pubDate>
        <image>
            <url>https://files.virgool.io/upload/users/1632170/avatar/RF94wK.jpg?height=120&amp;width=120</url>
            <title>soheil moonesi</title>
            <link>https://virgool.io/@soheil-moonesi</link>
        </image>

                    <item>
                <title>وب سایت شخصی و ادامه نشر مطالب</title>
                <link>https://virgool.io/@soheil-moonesi/%D9%88%D8%A8-%D8%B3%D8%A7%DB%8C%D8%AA-%D8%B4%D8%AE%D8%B5%DB%8C-%D9%88-%D8%A7%D8%AF%D8%A7%D9%85%D9%87-%D9%86%D8%B4%D8%B1-%D9%85%D8%B7%D8%A7%D9%84%D8%A8-x3cudkawuivg</link>
                <description>سلام به همه دوستان از اینجا به بعد من تمامی نوشته هام رو در سایت شخصی خودم مینویسم ، اونجا تمامی تاپیک ها با عنوان های مربوطشون رو جدا کردم https://soheil-moonesi.github.io/QuartzRepository/</description>
                <category>soheil moonesi</category>
                <author>soheil moonesi</author>
                <pubDate>Fri, 13 Dec 2024 01:19:53 +0330</pubDate>
            </item>
                    <item>
                <title>Unity Audio 2</title>
                <link>https://virgool.io/@soheil-moonesi/unity-audio-2-anjyidzfwyco</link>
                <description>نمدونم چرا پست نمیشهخوب برای این که بتونیم صدای یک چیزی رو توی بازی درست کنیم یه سری موارد رو نیاز داریم:فقط یادمون باشه که audio listener توی main camera هستش خوب حالا مورد بعدی audio source هستش، مثلا میخوایم وقتی که راکتمون داره پرواز میکنه بهش یه صدایی اضافه کنیم خوب این رو باید داخل همون راکت اضافه اش کنیم . چجوری؟ میزنیم روی راکتمون و بعد کامپوننت audio source رو بهش اضافه میکنیم. حالا اونجا یه قسمتی داره به اسم audioClip که میتونیم اون صدای مورد نظرمون رو از اونجا انتخاب کنیم.خوب برای خوده صدا هم میتونیم از این لینک استفاده کنیم فقط قبل این که بتونید دانلودش کنید باید با یه ایمیل عضوش بشید.اگر هم خواستیم خودمون یه sound effect درست کنیم از این برنامه میتونیم استفاده کنیم: audacity که این هم لینکشه . حالا برای فرمتی که میخوایم ذخیره کنیم هم میتونیم از mp3 یا ogg استفاده کنیم ، هر چقدر که فایل کم حجم تر باشه بهتره در کل.خوب دو تا آپشن داریم play on awake که یعنی وقتی که بازی شروع میشه این صدا هم پلی میشه و بعدی هم loop هستش که صدامون هر وقت به انتها رسید لوپ میخوره و دوباره پخش میشه .خوب توی داکیومنت های یونتی هم کلی متد های مختلف براش تعریف شده که اینجا میتونیم ببینیم.خوب حالا میخوایم کد بالا رو بررسی کنیم. اینجا اومدیم برای این که این صدایی که دانلود کردیم و میخوام راکت موقع پرواز کردن پخش کنه چند بار پخش نشه برای همین اومدیم از isPlaying استفاده کردیم. که یعنی در صورتی که در حال پخش نبود بیاد و پخشش کنه با audioSource.Play بعدش اومدیم else رو روی if که وظیفه چک کردن دکمه ی space رو داره گذاشتیم، برای این که این صدا فقط زمانی قراره پخش بشه که داریم دکمه space رو میزنیم و در غیر این صورت باید پخش صدا متوقف بشه برای همین توی else از audioSource.stop استفاده کردیم.freesoundaudacityteam</description>
                <category>soheil moonesi</category>
                <author>soheil moonesi</author>
                <pubDate>Sat, 02 Nov 2024 21:00:24 +0330</pubDate>
            </item>
                    <item>
                <title>C# Dependency Inversion Principle</title>
                <link>https://virgool.io/@soheil-moonesi/c-dependency-inversion-principle-h3qdcdkmnax4</link>
                <description>The Dependency Inversion Principle (DIP) is a fundamental principle of the SOLID design principles that is crucial for building maintainable and extensible software systems in C#. By adhering to the DIP, developers can decouple the high-level components of their applications from the low-level implementation details, resulting in code that is easier to modify and test.The main idea behind DIP is that higher-level modules should not depend on lower-level modules; instead, they should both depend on abstractions.میگه ایده کلی این قانون اینه که ماژول های higher level نباید وابستگی داشته باشن به ماژول های lower level به جاش هر دو اینا باید به abstraction ها وابسته باشن.Dependency Inversion Principle in C# – The BasicsTo implement the Dependency Inversion Principle in C#, you should focus on creating abstractions (interfaces or abstract classes) and making both high-level and low-level modules depend on them. By doing so, you can avoid direct dependencies between high-level and low-level modules.خوب برای اجرای این قانون واجبه که بیشتر بر روی abstraction ها که میشن همون اینترفیس ها یا کلاس های abstract و کاری کنیم که ماژول های high lelev , low level به اون ها وابستگی داشته باشن و از هرگونه وابسته سازی مستیم بین ماژول های high level , low level اجتناب کنیم.Without Dependency Inversion PrincipleImagine you have an email notification system that sends notifications to users. The EmailNotification class has a Send method which directly depends on the SmtpClient class to send the email.خوب اینجا میخوایم مثالی رو بررسی کنیم که این قانون توش رعایت نشده. کلاس emailNotification یه متد send داره که به smtpClient وابسته استImplementing Dependency Inversion PrincipleTo implement the Dependency Inversion Principle, you’ll introduce an abstraction that both the EmailNotification class and the SmtpClient class will depend on. This abstraction will be an interface called IEmailSender.خوب حالا مشکلی که بالاتر گفتیم رو چجوری حل کنیم؟ بیایم از abstraction ها جوری استفاده کنیم که هر دو اینا وابسته به اون abstraction بشن. خوب برای این کار هم میایم یه اینترفیس به اسم IEmailSender درست میکنیم.public interface IEmailSender{  
   void Send(string emailAddress, string message); 
}Refactoring the Example with Dependency Inversion Principleخوب حالا میایم اول smptClient رو وابسته به اینترفیس IEmailSender میکنیمpublic class SmtpClient : IEmailSender{  
   public void Send(string emailAddress, string message)    { 
        // Implementation of email sending using SMTP    }
 }Next, modify the EmailNotification class to depend on the IEmailSender interface instead of the SmtpClient class. You’ll do this by passing an instance of IEmailSender to the constructor of the EmailNotification class.بعدش میایم EmialNotification رو وابسته میکنیم به IEmailSender ، برای این کار هم میایم توی constructor کلاس EmailNotification میایم IEmailSender رو قرار میدیم به عنوان تایپ ورودی.جالا دیگه اینجوری هیچ کدوم از این ها به هم وابسته نیستند و هر دو به یک اینترفیس وابستگی دارن واینطوری اگر بخوایم بخوایم توی مکانیزم ارسال ایمیل هم تغییری به وجود بیاریم فقط کافیه که یه کلاس جدید بسازیم که اون کلاس هم اینترفیس IEmailSender رو پیاده سازی کنه.Dependency Inversion Principle and Interfacesاینترفیس ها نقش بسیار مهمی در این قانون دارند چون باعث میشن ماژول های رده بالا و رده پایین هر دو از طریق اینترفیس ها با هم ارتباط داشته باشن و همین باعث میشه که کد انعطاب بیشتری داشته باشی و کوپل شدگی کمتری داشته باشه.Dependency Inversion Principle and Dependency InjectionDependency Injection (DI) is a technique used to decouple classes by providing their dependencies from external sources. It is often used in conjunction with the Dependency Inversion Principle to achieve even better decoupling and testability.تزریق وابستگی تکنیکیه که برای فراهم آوردن مواردی که یه کلاس میخواد از یه منبع خارجی بدون کوپل شدگی. برای استفاده از این تکنیک بهتره که از DIP استفاده کنیمDependency Injection TechniquesThere are three common techniques for implementing Dependency Injection Constructor Injection: Injecting dependencies through the constructor of a class.Property Injection: Injecting dependencies through public properties of a class.Method Injection: Injecting dependencies as parameters of a method.لینک</description>
                <category>soheil moonesi</category>
                <author>soheil moonesi</author>
                <pubDate>Tue, 02 Apr 2024 15:31:56 +0330</pubDate>
            </item>
                    <item>
                <title>C# Defining the Interface Segregation Principle (ISP)</title>
                <link>https://virgool.io/@soheil-moonesi/c-defining-the-interface-segregation-principle-isp-qcdttrwckomm</link>
                <description>Importance of ISPAdhering to the ISP leads to cleaner, more maintainable code by reducing the coupling between classes and ensuring that interfaces are tailored to specific client requirements. This enables developers to create modular systems that are easy to extend and modify, promoting a more flexible architecture.خوب اهمیت ISP چیه؟ برای این که کد قابل نگهداری و تمیزی داشته باشیم اگر این قوانین رو رعایت کنیم کوپل شدگی بین کلاسهامون کم میشه و میتونیم با توجه به نیازهای client بیایم یه سری اینترفیس رو درست کنیم که به اون نیازها جواب بدیم. Examples of Violating ISPFat InterfacesA fat interface is an interface that contains too many methods, some of which may be unrelated or unnecessary for certain clients. Implementing a fat interface can lead to code bloat and increased coupling, violating the ISP.خوب چیزهایی که این قانون رو نقض میکنن :اینترفیس های شلوغ و چاق باعث میشن که ما بخوایم کلی متد غیر مرتبط و به درد نخور رو پیاده سازی کنیم. اگر از این مدل اینترفیس ها بخوایم استفاده کنیم کوپل شدگی توی کدمون بیشتر میشه.Unrelated MethodsWhen an interface contains methods that are unrelated to its core purpose, it becomes more difficult for clients to implement the interface correctly. Clients are forced to depend on methods they don’t need, leading to a violation of the ISP.اگر توی اینترفیسمون بیایم متدی رو قرار بدیم که با اصل و هدف اصلی اون اینترفیس مغایرت داشته باشه ، پیاده سازیش توی کلاس های مورد استفاده سخت تر میشه و مجبور میشیم که این متد هایی که لازم نداریم رو هم پیاده سازی کنیم که همین باعث نقض قانون میشه.Interface Segregation Principle in C#: Key ConceptsImplementing Interfacesخوب فرض کنید که اومدیم و یه اینترفیس IWorker رو تعریف کردیمpublic interface IWorker{
     void Work();  
   void Eat();  
   void Sleep(); }خوب همونطور که میبینید این جا یه سری متدی که هیچ ربطی به رباط ها ندارن رو باید پیاده سازی کنیم که این قانون ISP رو نقض میکنه. راه درست تعریف اینترفیس به این شکله :حالا اینجا دیگه انسان ها اینترفیس های خودشون رو دارن و رباط ها هم اینترفیس خودشون رو :C# ISP: Best PracticesKeep Interfaces FocusedDesign interfaces with a single, focused purpose to prevent clients from being forced to implement unrelated methods.Use Multiple InterfacesInstead of creating a single, monolithic interface, use multiple, smaller interfaces that can be combined as needed to suit different client requirements.Leverage CompositionUse composition to combine multiple interfaces and create flexible, modular classes that can be easily extended and modified.لینک</description>
                <category>soheil moonesi</category>
                <author>soheil moonesi</author>
                <pubDate>Tue, 02 Apr 2024 13:03:09 +0330</pubDate>
            </item>
                    <item>
                <title>C# Liskov Substitution Principle</title>
                <link>https://virgool.io/@soheil-moonesi/c-liskov-substitution-principle-xqsrkodhn4xq</link>
                <description>Importance of LSPThe LSP is vital because it ensures that the inheritance hierarchy is correctly designed and implemented, promoting code reusability and maintainability. Adhering to LSP leads to a robust and flexible system architecture that can easily accommodate changes in requirements.خوب میرسیم به ال که میشه همون اصل LSP که حالا چرا انقدر مهمه؟ برای این که به ما این اطمینان رو بده که سلسله مراتب ارث بری درست طراحی و پیاده سازی شده و پایداری و نگهداری کد هم راحت تر میشه. پیاده سازی درستش همچنین باعث میشه معماری سیستمی که داریم منعطف باشه.Examples of Violating LSPIncorrect HierarchyAn incorrect class hierarchy violates the LSP when the derived class does not correctly represent the base class. This often happens when the inheritance relationship is based on implementation details rather than a true “is-a” relationship.سلسله مراتب اشتباه: این کار باعث نقض قانون LSP میشه، اگر کلاس مشتق شده یا کلاس فرزند به اشتباه داره کلاس base رو نمایش میده (یعنی نمایش درستی از والد نیست). این اتفاق زمانی میوفته که ما روابط ارث بری رو صرفا فقط برای نوشتن کد و پیاده سازی داریم استفاده میکنیم و این پیاده سازی بر اساس روابط درستی پایه گذاری نشده. حالا این یعنی چی ؟ یعنی به جای این که قبل از این که بخوایم کد رو بنویسیم بیایم کلاس ها رو درست تعریف کنیم و روابط رو درست بنویسیم و طراحی کنیم، اومدیم فقط شروع کردیم به کد نوشتن و طی مسیر هر جایی که برای پیاده سازی کدمون لازم داشتیم رو نوشتیم توی کلاس های مختلف و ارث بری کردیم و ... که همین کار در ادامه باعث سردرگمی و پیچیدگی کد میشه.Violating Method SignaturesWhen a derived class changes the method signature or return type of a base class method, it violates the LSP as the derived class can no longer be substituted for the base class without causing issues.تناقض در مدل متد ها : وقتی که کلاس های مشتق شده بیان و مدل متد یا تایپ اون چیزی رو که قراره return کنه رو در کلاس پایه رو تغییر بدن قانون LSP رو نقض کردن. یعنی اگر در کلاس های مشتق شده بیایم یه متدی رو مدلش رو تغییر بدیم یا تایپی رو که برمیگردونه رو تغییر بدیم قانون LSP رو نقض کردیم.Misuse of InheritanceUsing inheritance to reuse code rather than establish a true “is-a” relationship is another way to violate LSP. When the derived class only inherits the base class for code reuse and not to represent a proper relationship, it can lead to design issues and decreased maintainability.اگر ما داریم فقط از ارث بری برای این استفاده میکنیم که صرفا کدی رو که قبلا نوشتیم رو در یه جا دیگه از کد استفاده کنیم به جای این که ببینیم این واقعا یه ارتباط هستش ، این کار یاعث نقض قانون میشه.LSP in C#: Key ConceptsTo understand the LSP in C#, let’s first explore some key concepts:Base ClassA base class is a general class that acts as a foundation for more specific classes, known as derived classes. The base class defines common properties and methods that the derived classes will inherit.Derived ClassDerived classes are specialized versions of the base class, inheriting its properties and methods. They can also override or extend the functionality provided by the base class.Method OverridingMethod overriding is the process by which a derived class provides a new implementation for a method declared in its base class. This allows the derived class to customize the behavior of the inherited method without changing the base class’s code.خوب مفاهیم اصلی که توی این قانون هستش ایناست:کلاس base: این کلاس میشه پایه و اساس یه سری از کلاس های دیگه که ازش مشتق شده اند. کلاس پایه یه سری از پراپرتی ها و متد هایی که در تمامی این کلاس ها مشترک هستند رو در خودش داره.کلاس مشتق شده: این ها یه سری کلاس هایی هستند که از کلاس base ارث بری کردند و یه سری متد و پراپرتی هایی رو که مخصوص به خودشون هست رو دارن، و میتونن override , extend کنن functionality که توسط کلاس پایه فراهم اومده.متد overriding: توی این پروسه کلاس مشتق میاد یه پیاده سازی جدیدی رو از متدی که در کلاس پایه هست رو انجام میده که با این کار میتونه بدونه این که به کد کلاس پایه دست بزنه و تغییر بده یه مدل جدیدی از اون متد رو که برای کار خودش لازم هست پیاده سازی کنه.خوب ببینید در کد بالا نمونه درستی از پیاده سازی LSP هستش ، همونطوری که میبنید توی کلاس پایه اومده متدی Area رو تعریف کرده و بعد اومده توی مستطیل فرمول و پراپرتی هایی که مرتبط باهاش هست رو تعریف کرده و بعد اومده توی کلاس مربع که از مستطیل ارث بری کرده و متد area رو استفاده کرده ولی اومده  override اش کرده به منظور استفاده مشخص خودش ، این جا ارث بری ها درست انجام شده .C# LSP: Best PracticesDesign by ContractWhen designing classes and their relationships, ensure that derived classes fulfill the contract established by the base class. This includes adhering to method signatures, return types, and any specified behavior.وقتی که میخوایم ارتباطات بین کلاس ها رو تنظیم کنیم حتما این رو در نظر بگیریم که یه سری قرارداد ها بین کلاس پایه و کلاس مشتق شده انجام بشه، که این قضیه شامل مدل متد ها، return type ها و هر رفتار دیگه میشه.Favor Composition over InheritanceInstead of relying heavily on inheritance, consider using composition to achieve code reuse and flexibility. This can help avoid LSP violations and other design pitfalls associated with inheritance.اگر درست متوجه شده باشیم منظورش اینه که به جای این که خیلی از ارث بری استفاده کنیم بیایم از ترکیب کردن استفاده کنیم ، یعنی بیایم با استفاده از چندین تا کلاس بیایم یه کاری رو انجام بدیم و پیاده سازی کنیم.Use Interface-Based ProgrammingImplementing interfaces rather than inheriting from concrete classes can help enforce LSP adherence, as interfaces define contracts that must be fulfilled by any implementing class.وقتی که داریم ارث بری میکنیم بیایم و از اینترفیس ها هم استفاده کنیم.لینک</description>
                <category>soheil moonesi</category>
                <author>soheil moonesi</author>
                <pubDate>Tue, 02 Apr 2024 11:42:44 +0330</pubDate>
            </item>
                    <item>
                <title>C# Open Closed Principle</title>
                <link>https://virgool.io/@soheil-moonesi/c-open-closed-principle-k8meyxh01uli</link>
                <description>میرسیم به اصل open closed Defining the Open/Closed Principle (OCP)The Open/Closed Principle, introduced by Bertrand Meyer, states that software entities (such as classes, modules, and functions) should be open for extension but closed for modification. In other words, developers should be able to add new functionality to a class without changing its existing implementation. This can be achieved through abstraction, inheritance, and polymorphism.خوب این اصل داره میگه که هر entity توی برنامه شامل کلاس ماژول ، فانکشن، هر چی باید برای اضافه کردن و گسترش دادن باز باشه و برای تغییر بسته. برنامه نویس ها باید بتونن به کلاس قابلیت های جدید اضافه کنن بدون این که قسمت های دیگه ای که پیاده سازی شده رو تغییر ندن. این کار با استفاده کردن از abstraction, inheritance , polymorphsim پیاده میشه .Why is the Open/Closed Principle Important?Adhering to the OCP promotes a more maintainable, flexible, and scalable codebase. By ensuring that classes are open for extension and closed for modification, developers can add new functionality without altering existing code, minimizing the risk of introducing bugs or breaking existing features. This principle encourages the use of abstractions and promotes a modular architecture that is easier to understand, test, and refactor.خوب برای این مهمه که برنامه نویس ها بتونن راحت قابلیت های جدید اضافه کنن بدون این که باگ های ناخواسته ایجاد کنن یا بزنن بقیه جاهای برنامه رو بپوکونن. خوب این قانون خیلی تشویق به این میکنه که از abstraction استفاده کنیم طراحی و ساختار ماژولار داشته باشیم که بعدا بتونیم راحت تست بنویسیم و ریفکتور کنیم.AbstractionAbstraction is a technique that allows developers to hide the internal implementation details of a class and expose only its essential features. By using abstraction, developers can create flexible and extensible designs that are less susceptible to change.خوب abstraction چیه داستانش ؟ اینه که یه تکنیکیه که برنامه نویس ها برای مخفی کردن جزییات  و پیاده سازی هایی که توی کلاس شده، و اینطوری فقط مواردی که لازمه رو میان و نشون میدن. با این کار  کد منعطف تره و گسترش دادنش هم راحت تر میشه.Inheritance and PolymorphismInheritance is a mechanism in C# that allows one class to inherit the properties and methods of another class, while polymorphism enables a class to have multiple implementations based on the context. These concepts play a crucial role in achieving the Open/Closed Principle, as they allow developers to extend classes without modifying their existing implementation.ارث بری و polymorphism که به یه کلاس قابلیت این رو میده که از یه کلاس دیگه پراپرتی و متد به ارث ببره و polymorphism به ما قابلیت این رو میده که یه کلاس multiple implementations داشته باشه بر اساس context. این دو تا قضیه خیلی مهمه برای این که برسیم به قضیه open/closed .Open/Closed Principle C# ExampleLet’s consider an example to demonstrate the Open/Closed Principle in C#. Suppose we have a Shape class and a AreaCalculator class that calculates the area of different shapes:خوب بیاید اینجا کلاس shape و کلاس areaCalculator رو در نظر بگیریم که مساحت اشکال رو محاسبه میکنه.In this example, the AreaCalculator class can only calculate the area of rectangles. If we want to add support for other shapes, such as circles and triangles, we would need to modify the existing implementation of the AreaCalculator class, which violates the Open/Closed Principle.خوب توی areaCalculator ما فقط میتونیم مساحت مربع رو محاسبه کنیم، حالا اگر بخوایم مساحت اشکال دیگه رو هم محاسبه کنیم باید بیایم و کلاسمون رو تغییر بدیم که این خلاف اصل باز / بسته بودنه.To adhere to the OCP, we can use abstraction and inheritance to create separate classes for each shape type and provide a consistent method for calculating the area:خوب برای این کار میتونیم کلاس های جدا تعریف کنیم و یه متد هم تعریف کنیم که بیاد و مساحت رو در هر کدوم استفاده کنه.Now, the AreaCalculator class adheres to the Open/Closed Principle, as it can support new shapes without modifying its existing implementation.خوب حالا با این کار هم از اصل باز /بسته پیروی کردیم و هم میتونیم اشکال دیگه رو هم اضافه کنیم بدون این که بخوایم تغییری ایجاد کنیم.Strategies for Implementing the Open/Closed Principle in C#Using Abstract ClassesAbstract classes can be used to define a base class with common functionality and provide a consistent interface for derived classes. By creating abstract methods, developers can enforce that each derived class implements its own version of the method, allowing for extensibility without modifying the base class.خوب حالا چجوری پیاده سازیش کنیم؟ میتونیم از کلاس ها abstract استفاده کنیم و به عنوان کلاس base ازشون استفاده کنیم که باعث میشه بتونیم اینترفیس های یکسان توی کلاس های فرزند و مشتق شده داشته باشیم. با استفاده کردن از متد های abstract کلاس های مشتق شده نسخه ی خودشون از اون متد رو باید پیاده سازی کنن و این باعث میشه که بتونیم کد رو گسترش بدیم بدون این که بخوایم کلاس base رو تغییر بدیم.Applying the Strategy PatternThe Strategy Pattern is a behavioral design pattern that enables selecting an algorithm at runtime. It can be used to implement the Open/Closed Principle by encapsulating different algorithms within separate classes and providing a common interface for them.لینک مطلب : لینک</description>
                <category>soheil moonesi</category>
                <author>soheil moonesi</author>
                <pubDate>Mon, 01 Apr 2024 18:42:25 +0330</pubDate>
            </item>
                    <item>
                <title>C# Foreach Loop</title>
                <link>https://virgool.io/@soheil-moonesi/c-foreach-loop-luig2vrpp2yq</link>
                <description>Foreach loops are essential for software engineers and developers who work with collections and arrays in C#. They allow for the iteration of these data structures with a simple and concise syntax that makes working with them easier and more efficient. One of the key benefits of using foreach loops is that they have less complexity than traditional for loops. Say goodbye to range checking and indexing!خوب foreach ها خیلی مهمن برای این که میتونیم باهاشون روی collection ها و array ها کار کنیم. بهمون قابلیت پیمایش توی data structure ها رو میدن. پیچیدگی های کدمون رو کمتر میکنن به نسبت استفاده از for معمولی.For loops require the use of an index variable, which often requires additional declarations and assignments. On the other hand, foreach loops manage the index variable and iterating logic behind the scenes, reducing the amount of code needed to loop through a collection or array. This simplicity leads to cleaner code that is easier to read and maintain.توی for نیاز به متغییر index داریم ولی توی foreach دگه متغییر index و منطق پیمایش در پشت صحنه انجام میشه و همین باعث میشه که ما کد کمتری بزنیم برای این که بخوایم توی کالکشن ها یا آرایه ها پیمایش کنیم. نگهداری کد ها اینطور بهتر میشه.Another benefit of using foreach loops is easier debugging. Since the iterating logic is abstracted away, coding errors that occur in the iteration process are easier to diagnose. This is particularly relevant when using for loops where the potential exists for an off-by-one error that can be difficult to identify and resolve.خوب یه مزیت دیگه که داره اینه راحت تر میتونیم دیباگ کنیم مشکلات رو چون همه چی دیگه خیلی خلاصه نوشته شده.Foreach loops can also be used in conjunction with the IEnumerable interface, which provides a way to access the values in a collection one at a time. The use of the IEnumerable interface and foreach loops can reduce memory usage and increase performance by allowing developers to obtain values from the collection only when required — but it’s not always that black-and-white. This approach is commonly used when dealing with large datasets that would be impractical to process all at once.خوب باز هم یه مزیتی که foreach داره اینه که از اینترفیس IEnumerable استفاده میکنه که این به این معنی هستش که فقط ما در لحظه به یکی از اون مقادیری که در اون کالکشن هست دسترسی داریم که همین قضیه باعث این میشه که مموری کمتری استفاده کنیم و در نتیجه سرعت و کارایی کدمون بالاتر بره ، البته همیشه قضیه این طوری نیست که به سودمون باشه، زمانی که میخوایم یه dataset بزرگ رو در یک لحظه بخوایم process کنیم استفاده از foreach منطقی نیست.Common Mistakes in Using foreach LoopsModification During IterationOne mistake is attempting to modify the collection being iterated during the loop. Modified collections can cause unintended behavior, such as an infinite loop or skipping over certain items. To avoid this mistake, it is important to create a copy of the collection to work with if modification is necessary, thereby removing the potential for direct modification of the original collection during the iteration process.اشتباهی که معمولا پیش میاد توی استفاده از foreach اینه که بخوایم موقعی که داریم روی کالکشنی پیمایش میکنیم مقادیر رو تغییر بدیم که این کار ممکنه باعث لوپ بینهایت یا skip شدن یه سری از مقادیر بشه، برای این که از این قضیه بخوایم اجتناب کنیم بهتره که یه کپی از کالکشنمون بگیریم و با اون کار کنیم.Null ReferencesAnother common mistake is not checking for null references before attempting to iterate. This mistake can lead to null reference exceptions, which can cause the program to crash and can be difficult to catch and resolve. Checking for null references before starting the iteration process is an effective way to avert this mistake.خوب یه مشکل دیگه که ممکنه پیش بیاد اینه که قبل از این که بخوایم پیمایش کنیم null reference رو چک نکنیم. اگر این کا رو نکنیم باعث null reference exception  میشه که همین قضیه باعث crash شدن برنامه میشه و catch کردن و درست کردن این exception هم دردسر داره.پس قبل از این که بخوایم پیمایش کنیم بیایم و null reference رو چک کنیم.-----------------------------------------------------------------------------------------------------------------------------------Using break And continue Statements in foreach LoopsA break statement will immediately terminate the loop when executed, regardless of whether the collection has finished iterating or not. Alternatively, a continue statement will immediately move on to the next iteration of the loop, skipping over the remaining lines of code in the current iteration. These statements can be used to simplify error-checking and improve code readability.خوب break میاد هر چی دیگه مونده از کد که قراره اجرا بشه رو قطع میکنه فارغ از این که پیمایش تموم شده توی کالکشن یا نه.از اون طرف هم continue وقتی که اجرا میشه دیگه بقیه خط های بعدیش اجرا نمیشه و  میپره توی iteration بعدی یا همون دور بعدی ، خوب ما میتونیم از همین دو تا برای بهتر کردن کدمون استفاده کنیم.Advanced Techniques with foreach LoopsLINQ &amp; Lambdas Within LoopsFilteringParallelizationلینک مطلب : لینک------------------------------------------کجاها نباید از foreach استفاده کنیم: When Modifying Collection: If you need to modify the collection you&#x27;re iterating over (e.g., adding or removing elements), using a foreach loop can lead to runtime exceptions (e.g., InvalidOperationException). In such cases, you should use a for loop instead, which allows more control over the iteration process.// This code will throw an exception
foreach (var item in collection)
{
    collection.Remove(item);
}

// Use a for loop to modify the collection safely
for (int i = collection.Count - 1; i &gt;= 0; i--)
{
    collection.RemoveAt(i);
}When Indexing is Required: If you need to access elements in the collection by index or if you need to manipulate the loop index within the loop body, a for loop is more appropriate. foreach loop abstracts away the index, making it less suitable for such scenarios.// Accessing elements by index
for (int i = 0; i &lt; collection.Length; i++)
{
    Console.WriteLine(collection[i]);
}

// Using the loop index for manipulation
for (int i = 0; i &lt; collection.Length; i++)
{
    if (i % 2 == 0)
    {
        Console.WriteLine(collection[i]);
    }
}When Iterating Over Multiple Collections in Parallel: If you need to iterate over multiple collections in parallel (i.e., iterate over corresponding elements from each collection simultaneously), a foreach loop cannot achieve this directly. In such cases, you might need to use a for loop with the same index for all collections.// Iterate over two arrays in parallel
for (int i = 0; i &lt; array1.Length &amp;&amp; i &lt; array2.Length; i++)
{
    Console.WriteLine($&#039;{array1[i]} - {array2[i]}&#039;);
}</description>
                <category>soheil moonesi</category>
                <author>soheil moonesi</author>
                <pubDate>Mon, 01 Apr 2024 11:03:54 +0330</pubDate>
            </item>
                    <item>
                <title>C# Why Delegates? Why not Call Methods Directly</title>
                <link>https://virgool.io/@soheil-moonesi/c-why-delegates-why-not-call-methods-directly-yl7anacxmrwq</link>
                <description>خوب همونطوری که از عنوان هم معلومه میخوایم راجع به delegate ها بیشتر بدونیم و این که اصلا چرا باید ازشون استفاده کنیم؟خوب سوالی که همیشه موقع استفاده کردن از delegate پیش میاد اینه که خوب اصلا چرا از همون اول نیایم مستقیم اون متدی رو که میخوایم رو call نکنیم به جای این که بخوایم بیایم و از delegate استفاده کنیم؟I’m going to use an example from the video C# Design Patterns with Jon Skeet: Outtakes where Mr. Skeet implements the Decorator Pattern using delegates.خوب فکر کنید شما پیتزا فروشی دارید و میخواید تخفیف بدید روی هر کدوم از سفارش هاتون. خوب برای این کار میایم یه کلاس درست میکنیم که تمام مدل های تخفیف رو داخلش داره.خوب همونطوری که میبینید پیتزا فروشی داره 3 مدل تخفیف میده و ما برای هر کدوم از سفارش هایی که داریم میایم و دونه دونه هر کدوم رو چک میکنیم که ببینیم سفارشی که اومده شامل کدوم تخفیف میشه. خوب نقطه ی ضعفی که این کد داره اینه که اگر بخوایم هر کدوم از این تخفیف ها رو آپدیت کنیم یا یه تخفیف اضافه کنیم یا کم کنیم باید ComputePrice و اون متد رو در PizzaOrderingSystem تغییر بدیم.Open Closed Principle with DelegatesWhat we really want is to be able to extend the PizzaOrderingSystem without modifying it.  We want to make it Open for Extension, Closed for Modification.We can do this with delegates.ما میخوایم که بتونیم pizzaOrderingSystem رو extend کنیم بدون این که بخوایم modify اش کنیم.با استفاده از Delegate میتونیم این کار رو بکنیم.چجوری؟ اینجوری که اول میایم delegate تعریف میکنیم:public delegate decimal DiscountPolicy(PizzaOrder order)با توجه به این داستان، میبینیم که مدل تخفیف ها به مدل delegate مون میخوره.خوب حالا  بت این داستان ها میبینیم که چقدر شبیه به اینترفیس هاست.public interface IDiscountPolicy {    decimal ApplyDiscount(PizzaOrder order);}خوب حالا متوجه میشیم که چقدر کاری که delegate میکنن شبیه به همین اینترفیس هاست .ولی یه فرقی دارن They allow clients of our delegates to ignore all the details of their implementations - even their names!لینک مقاله : لینکخوب کجا ها بهتره که از delegate استفاده کنیم؟callBack mechanism :از delegate ها میشه برای درست کردن callback ها استفاده کر، چون میتونیم  با متد، متد دیگه رو با استفاده delegate صدا کنیم. این قضیه توی event handeling, async programming خیلی به کار میاد.decouplingبا استفاده از delegate میتونیم فرستنده درخواست رو از جایی که داره درخواست handle میشه جدا کنیم که به اصطلاح میگن decoupling کردن. این باعث میشه که نگهداری و scale دادن کد راحت تر بشه چرا که اینطوری وقتی که اعضا به هم وابسته نباشن خیلی راحت تر میشه کارها رو انجام داد.event handling توضیحش رو توی مطلب جداگانه نوشتم.functional programming با استفاده از delegate میتونیم یه سری فانکشن درست کنیم که توی ورودیشون فانکشن میگیرن. این قابلیت توی جاهایی که میخوایم filtering , mapping, reduceing روی کالکشن ها انجام بدیم کارایی داره.Dynamic Method Invocationبا استفاده از delegate میتونیم جاهایی که میخوایم یه سری متد بر اساس شرایطی که ایجاد میشه به صورت dynamic اجرا بشن رو پیاده سازی کنیم.حالا کجاها نباید استفاده بشهSimple Method Callsجاهایی که نمیخوایم از این قابلیت ها استفاده کنیم بهتره که سمت delegate نریمcallback mechanisms, event handling, or dynamic method invocationو بیایم مستقیم اون متد رو صدا کنیم.Performance Critical Applicationsاگر متد که میخوایم توی delegate استفاده کنیم خیلی ازش استفاده میشه ممکنه بار پردازشی غیر ضروری رو ایجاد کنیم پس بهتره که در مواقعی که متدی که میخوایم ازش استفاده کنیم خیلی استفاده میشه بهتره که از Delegate استفاده نکنیم.Overuse in Small-Scale Projects:توی برنامه های کوچیک لازم نیست ازش استفاده کنیم، استفاده کردن ازش یه جورایی overkill کردنه Static Method Invocationجاهایی که متدی که میخوایم call کنیم از نوع static هستش بهتره که از delegate استفاده نکنیم و بیایم مستقیم متد رو call کنیم.When Lambdas or Anonymous Methods Sufficeخوب در زمانی که میتونیم از lamdas یا anonymous method ها استفاده کنیم و کار رو دربیاریم. بهتره که از delegate  استفاده نکنیم.When There&#x27;s a Clear Alternative:همیشه فرض رو بر این بزاریم که تا جایی که میتونیم کد رو ساده بنویسیم و در اینجور مواقع استفاده کردن از delegate اگر باعث پیچیدگی میشه بهتره که از یه چیزه دیگه استفاده کنیم.Avoid Mixing Delegate Types with Different Signatures:// Example: Avoid mixing delegate types with different signaturespublic delegate void MyDelegate(string message);// Incorrect usageMyDelegate delegate1 = (string message) =&gt; { /* Do something */ };MyDelegate delegate2 = (int value) =&gt; { /* Do something else */ }; // Compiler errorدر اینطور مواقع که متد هایی که داریم مدلشون فرق دارن بهتره که از delegate استفاده نکنیم</description>
                <category>soheil moonesi</category>
                <author>soheil moonesi</author>
                <pubDate>Sun, 31 Mar 2024 19:13:16 +0330</pubDate>
            </item>
                    <item>
                <title>Unity Partical System</title>
                <link>https://virgool.io/@soheil-moonesi/unity-partical-system-nqeogjt7b7en</link>
                <description>خوب اینجا میرسیم به مبحث پارتیکل ها خوب کلیتش رو توی این عکس بالا میبینید این دون دون های باحال میشن پارتیکل ها و اون قسمتی که هم که با مربع سیر نشون داده میشه emitter یا جایی که پارتیکل ها ازش به بیرون پرتاب میشن.توی این قسمت فقط یه نکته ی مهم وجود داره که ممکنه وقتی که دارید برای بار اول پیاده سازیشون میکنید بهشون برخورد کنید. خوب ببنید مشکل از این قراره که وقتی که شما میاید و particle ها رو stop میکنید دیگه start نمیشن. حالا این یعنی چی ؟ من میخواستم فقط موقعی که دکمه ی space رو زدیم و موشک داره شتاب میگیره یه سری پارتیکل ازش خارخ بشه و موقعی هم که دستمون رو از Space  برداشتیم پارتیکل ها قطع بشن ولی اتفاقی که می افتاد این بود که وقتی که از systemParticle.stop استفاده میکردم کلا پارتیکل ها کار نمیکردند. توی فروم های یونتی هم این موضوع بیان شده و یه راه حل هایی هم براش نوشتن که من تا اونجایی که تست کردم برای کار خودم هیچ کدوم جواب نداد اسم تاپیک این هستش:Particle system won’t restart emitting before last particle has diedخوب حالا راه حلش چیه ؟راه حلش اینه که بیایم از isPlaying برای این که چک کنیم ببینیم particleSystem در حال اجراست یا خیر و در صورتی که در حال اجرا نبود بیایم اجراش کنیم و  بعد هم برای متوقف کردن پارتیکل ها از particleSystem.stop استفاده میکنیم ، فقط یادتون باشه که به کار بردن اون if باعث میشه که اون باگ که بالاتر گفتم برطرف بشه.مواردی دیگه این هستش که حواستون به play on awake , loop, start life time باشه. </description>
                <category>soheil moonesi</category>
                <author>soheil moonesi</author>
                <pubDate>Sat, 30 Mar 2024 19:34:14 +0330</pubDate>
            </item>
                    <item>
                <title>Unity Editing a Prefab</title>
                <link>https://virgool.io/@soheil-moonesi/unity-editing-a-prefab-oscozeioemne</link>
                <description>خوب حالا چطور شد که میخوایم بریم یه prefab رو تغییرش بدیم و edit اش کنیم؟ از اینجایی که راکتی که درست کردیم رو میخوایم یکم optimize اش کنیم و طراحی شو بهتر کنیم جوری که بیشتر شبیه به یه راکت بشه . خوب برای این کار میایم راکت رو انتخاب میکنیم و بعد میریم توی قسمت inspector و میزنیم روی open prefab و تمام ، حالا وارد محیط ویرایش prefab میشیم.یه راه دیگه هم هست که میبره ما رو توی محیط ایزوله شده برای ویرایش و اون راه اینطوریه که بریم مستقیم بزنیم روی prefab راکت .نکاتی که باید یادمون باشه اینه که اگر یه جسم دیگه توی این محیط درست کنیم میشه فرزند prefab اصلی و تمام ویژگی های والدش روش تاثیر میزاره.خوب راه معمولش اونطوری که استاد میگه اینه که بریم بزنیم رو prefab که داریم اندازه اش رو بکنیم 1,1,1 و بعد بیایم کامپوننت های mesh renderer و box collider رو پاک کنیم و بعد شروع کنیم به تغییرات. دلیلش هم اینه که این کار جلوی خیلی از مشکلاتی که مربوط به rotation هست رو میگیره.نکته ی بعدی این هستش که مختصات و position فرزندان به والد نسبی هستش.خوب حالا رفتیم و داخل محیط ویرایش و تغییرات رو اعمال کردیم، نکته ای که هست اینه که باید حواسمون باشه و بیایم box collider تمام ابجکت های فرزند رو پاک کنیم و بیایم به والد object collider رو اضافه کنیم و از توی قسمتی که توی کامپوننت داره بیایم مرز راکتمون رو برای برخورد با اجسام تعیین کنیم. چرا؟ چون به صورت اتوماتیک هر کدوم از اون ها یه سری مرز بندی دارند، حالا ما میخوایم اینجا اون مرز بندی ها رو پاک کنیم و فقط مرز بندیه آبجکت اصلی یعنی راکتمون بمونه. اینم شد نتیجه اش ، راکت فوق حرفه ای :D</description>
                <category>soheil moonesi</category>
                <author>soheil moonesi</author>
                <pubDate>Sat, 30 Mar 2024 14:43:23 +0330</pubDate>
            </item>
                    <item>
                <title>Unity SceneManager</title>
                <link>https://virgool.io/@soheil-moonesi/unity-scenemanager-lah3fjziltop</link>
                <description>خوب خوب حالا میریم سراغ این که چه جوری صفحه یا level که داریم رو ریست کنیم با استفاده از SceneManager ، حالا این اصلا یعنی چی؟ مثلا میخوایم وقتی که راکتمون خورد به یه جسمی که هیچی تگی براش تنظیم نکردیم بازی دوباره reload  بشه و از اول بیاد.خوب قبل این که بریم کدش رو بررسی کنیم باید با مفهوم Scene آشنا بشیم. خوب هر چیزی که روی صفحه میبینیم و داریم باهاش کار میکنیم میشه scene حالا ما اینجا صفحه که داریم به اسم sandbox اسم گذاریش کردیم و اگر به گوشه سمت راستش دقت کنید میبینید که عدد صفر نوشته شده که این همون index این صفحه است که جلوتر باهاش میخوایم کار کنیمخوب اینجا  اومدیم توی یه switch اجسامی رو که اوکی هستند راکت بهشون بخوره رو تعیین کردیم و توی default اومدیم گفتیم که اگر جسمی که بهش برخورد کردی غیر از این 3 تا بود متد ReloadLevel رو اجرا کن. خوب حالا میرسیم به sceneManager اینجا برای این که از متد loadScence بخوایم استفاده کنیم چند تا راه داره SceneManager.LoadScence(&#x27;SandBox&#x27;)SceneManager.LoadScence(0)یا بیایم توسط getActiveScene و buildIndex بیایم و ایندکس صفحه ای که فعال هستش رو به صورت اتوماتیک بگیریم و ازش استفاده کنیم مثل خط 28 و 29 که این کار رو انجام دادیم.خوب حالا در ادامه میخوایم یه level دیگه رو به بازی اضافه کنیم که بعد از این که مرحله اول رو با موفقیت رد کردیم بره مرحله ی دوم.خوب برای این کار اول لازمه که بریم رو راکت کلیک کنیم و بعد بریم داخل inspector و اونجا بزنیم روی override و همه ی تغییراتی رو که روی راکت انجام دادیم رو apply کنیم.حالا میریم توی قسمت Scene میزنیم روی صفحه که داریم و بعد control + D رو میزنیم و duplicate میکنیم scene رو .خوب حالا که scene جدید درست کردیم میبریم میندازیمش توی build setting که بعدا ازش استفاده کنیم.اینجا نکته ای که هست اینه که حواسمون باشه که sceneManager.sceneCount رو به جای sceneManager.sceneCountInBuildSetting استفاده نکنیم چون این دو با هم فرق دارن.خوب برای توضیح کد بالا باید بگم که اینجا بعد از این که 2 تا صفحه طراحی کردیم اومدیم یه کدی نوشتیم که بعد از این که راکت رو رسوندیم به نقطه finish بره مرحله ی بعد و وقتی که تمام مراحل تموم شدن بازی برگرده به صفحه ی اول.حالا میخوایم وقتی که میخواد مرحله عوض بشه یکم طبیعیش کنیم چون یهو صفحه عوض میشه. برای این کار میایم از invoke استفاده میکنیم. ساختار invoke اینطوریه که وتق که میخوایم ازش استفاده کنیم دو تا پارامتر ورودی میگیره اولی اسم متده و دومی مدت زمانی که میخوایم طول بکشه تا اون متده صدا زده بشه.invoke(&quot;methodName&quot;,delayInSeconds)خوب استفاده کردنش ازش مزیتش اینه که سریعه و کار رو راه میندازه ولی مشکلی که داره اینه که مرجع اون متد به صورت string هستش و داستانی که ایجاد میشه اینه که اگر ما اسم متد اصلی رو تغییر بدیم و یادمون بره که اینجا تغییرات رو ایجاد کنیم invoke کار نمیکنه.not as performant as using coroutine.</description>
                <category>soheil moonesi</category>
                <author>soheil moonesi</author>
                <pubDate>Fri, 29 Mar 2024 19:44:41 +0330</pubDate>
            </item>
                    <item>
                <title>Unity Boost Project</title>
                <link>https://virgool.io/@soheil-moonesi/unity-boost-project-he4rfw9ffmbl</link>
                <description>خوب توی این مطلب میخوام راجع به پروژه ی boost صحبت کنم، این عکسشه ، و در هچنان در فاز طراحی هستش.خوب اینجا قراره که اون مکعب زرده به عنوان یه راکت کار کنه و بتونیم بهش شتاب بدیم و اینور و اونور ببریمش.خوب حالا میرسیم به نکاتی که اینجا من یادگرفتم.خوب، توی خط 30 اومدیم از input.GetKey استفاده کردیم و برای تعیین کردن ورودیش هم از keyCode استفاده کردیم که توی مطالب قبلی گفته شده.برای این که بتونیم یه جسم رو بچرخونیم میایم از transform.Rotate استفاده میکنیم. خوب اونجا میبینید که نوشته Vector3.forward که این خلاصه شده 0,1,0 هستش یعنی x,y,z اون مقادیر رو دارن در forward . حالا نکته بعدی اینه که برای این که جهت rotation رو تغییر بدیم اومدیم از علامت - استفاده کردیم.نکته ی بعدی اینه که توی خط 30 اومدیم از کلید a برای چرخش راکت به سمت چپ استفاده کردیم. همونطوری که توی شکل پایین میبینیم چرخش در جهت پاد ساعتگرد تعریف شده که این به معنی این هستش که اگر بخوایم جهت خلاف پاد ساعتگرد یعنی ساعتگرد چرخش رو انجام بدیم باید مقدار قبلی رو در منفی ضرب کنیم. در کد این قضیه در خط 35 اتفاق افتاده.خوب نکته ی بعدی اینه که چر ما اومدیم اول rotation رو غیر فعال کردیم برای راکت و بعد چرخش رو انجام دادیم و بعد دوباره فعالش کردیم چرا؟خوب دلیلی که استاد داره میگه اینه که قوانین فیزیکی که برای چرخش راکتمون هستش تغییر نکنه، به طور مثال ما میخوایم یه جایی که لبه هستش و داریم نیرو وارد میکنیم میخوایم بره به اون سمتی که ما میخوایم در صورتی که اگر فیزیکش فعال باشه ممکنه با فیزیک  کاری که داریم با راکت میکنیم تداخل به وجود بیاره و بازی رو خراب کنه به همین دلیل هم اومدیم و فیزیک چرخش رو با freezeRotation غیر فعال کردیم بعد با transform.Rotate چرخش راکتمون رو ایجاد کردیم و بعدش اومدیم freezeRotation رو فعال کردیم.خوب نکته ی بعدی اینه که اینجا اومدیم برای تمام کارمون از force داریم استفاده میکنیم که این باعث میشه که کمتر به مشکل و باگ های عجیب و غریب فیزیکی بر بخوریم.</description>
                <category>soheil moonesi</category>
                <author>soheil moonesi</author>
                <pubDate>Fri, 29 Mar 2024 01:04:52 +0330</pubDate>
            </item>
                    <item>
                <title>Unity Mover Project</title>
                <link>https://virgool.io/@soheil-moonesi/unity-mover-project-bvor9yb95s82</link>
                <description>خوب اینجا دوست دارم یه توضیحی در مورد این پروژه بدماین یه عکس از نتیجه نهایی بازی هستش. توی این بازی قراره که اون مکعبی که گوشه سمت راست هستش رو برسونیم به اون مربع بنفشه که گوشه بالایی هستش و توی مسیرمون یه سری مانع طراحی کردیم که هر کدوم یه جوری حرکت میکنن یا ثابت هستند و وقتی که بهشون میخوریم رنگشون به رنگ قرمز تغییر میکنه.حالا بیایم راجع به کد ها صحبت کنیم. میخوایم اسکریپتی بنویسیم که وقتی به یه آبجکتی اضافه اش کردیم بعد از چند ثانیه اون آبجکت از روی آسمون ظاهر بشه و بیوفته روی زمین.اینجا  serialized field معنیش این هستش که میتونیم توی inspector یونیتی مقدارش رو ببینیم و تغییرش بدیم. time to wait همون زمانی که میخوایم اون آبجکت بعد از شروع بازی بگذره و بعد از آسمون بیوفته.خوب توی خط بعدی میایم و cache میکنیم. برای این که بخوایم کامپوننت هایی که میخوایم استفاده کنیم رو یک بار تعریف کنیم و در جاهای مختلف برنامه ازشون استفاده کنیم نیاز به یه ظرف و یه متغییر داریم، اینجا اومدیم برای MeshRenderer و rigidbody دو تا متغییر از نوع خودشون تعریف کردیم.توی خط بعدی اومدم یه متد تعریف کردم که این کد هایی که پایین تر نوشتم رو بزارم داخلش برای این که مرتب تر بشه ولی یادم رفته همینجوری خالی مونده .خوب میرسیم به start اگر یادتون باشه start فقط یک بار در شروع برنامه اجرا میشه و تمام.حالا میایم از اون متغییر هایی که بالاتر تعریفشون کردیم استفاده میکنیم و اون کامپوننت هایی که لازم داریم با GetComponent میگیریم و میریزیم داخل متغییرهاشون که بعدا بتونیم راحت ازشون استفاده کنیم.حالا renderer.enable = false یعنی چی؟ برای این که اون آبجکت هایی که تو آسمون هستند قبل از اون چند ثانیه مخفی باشن اومدیم از این قابلیت استفاده کردیم و مخفیشون کردیمحالا rigidbody.gravity =false یعنی چی؟ خوب میخوایم قبل از اون چند ثانیه این اجسام همونطوری توی آسمون باقی بمونند . طبیعتا برای این کار باید جاذبه رو خاموش کنیم که با این کد همین کار رو کردیم.حالا همونطوری که یادتونه update توی هر فریم اجرا میشه.اینجا اومدیم if گذاشتیم و گفتیم که بعد از Time.time که میشه همون تایمی که داره طی میشه بزرگتر بود از اون تایمی که ما تعیین کردیم برای رها شدن اجسام بیاد اون قسمت از کد رو اجرا کنه.بعد هم اومدیم اون چیزی هایی که در خط های بالا غیر فعالشون کرده بودیم رو فعال میکنیم.ادامه : خوب اینجا یه نکته ی مهم داره دقت کنید وقتی که داریم ورودی horizontal , vertical رو میگیریم چه شکلی میشه :به این شکل در میاد.حالا ممکنه بگید که vertical میشه عمودی ولی اینجا به شکل محور آبی و قرمز که روی زمینه دقت کنید و این vertical  ارتفاع عمودی نیست. اینجا D میشه horizontal  و W میشه  vertical.حالا ممکنه بگید ، چرا توی zValue گذاشتی ؟ برای جوابش وقتی که شما به عکس اول این مطلب نگاه کنید میبینید که ما محور های X و Z رو برای حرکت کردن انتخاب کردیم  و محور Y که اینجا به عنوان ارتفاع هستش رو freeze کردیم. خوب حالا میرسیم به قسمت input خوب یونتی از کجا میفهمه که این کلید هایی که میزنیم هر کدوم روی کدوم یکی از این ها تاثیر میزاره یعنی روی vertical یا horiziontal ؟خوب برای فهمیدنش باید برید توی قسمت edit بعد project manager بعد میرسید به همچین صفحه ای :خوب اینجا خودش از قبل تعریف شده براش و اگر خواستید هم تغییرش بدید از همین جا تغییرش بدید. همونطوری که میبینید برای horizontal از کلید های left , right و a , d  استفاده میشه.خوب از time.deltaTime ها برای همسان سازی زمان اجرا استفاده در سیستم ها استفاده کردیم.حالا میرسیم به objectHit حالا داستانش چیه؟ ما اومدیم به همه اجسامی که توی صفحه درست کردیم این اسکریپت رو اضافه کردیم. خوب حالا این قضیه چی کار میکنه ؟ این باعث میشه که وقتی که player یا همون مکعب نارنجی که خودمون داریم کنترلش میکنیم به هر چیزی که خورد رنگش قرمز رنگ بشه.چطوری؟ اینطوری که اومدیم از متد از قبل تعریف شده unity به اسم onCollistionEnter استفاده کردیم. این متد یه پارامتر میگیره و اون هم اینه که چی بهش برخورد کرده ، اطلاعات اون جسمی که بهش برخورد کرده رو میگیره ، حالا ما هم اومدیم از اون اطلاعات استفاده کردیم و گفتیم که اگر اون چیزی که با تو برخورد کرده تگش Player  بود رنگت قرمز میشه و تمام. حالا خوب ممکنه بگید تگ؟ تگ چیه ؟ خوب برای این که بتونیم اجسام که درست کردیم رو راحت تر بهشون دسترسی پیدا کنیم میایم از تگ استفاده میکنیم ، اینطوری:همونطوری که توی عکس های قبل تر دیدید فولدر بندی کردم و ساختار دادن به پروژه خیلی مهمه پس حواستون بهش باشه.خوب نکته ی دیگه ای که اینجا هست اینه که برای این که یک جسمی رو بتونیم ثابت نگه داریم چه به لحاظ position و چه به لحاظ rotation میتونیم به کامپوننت rigidbody بریم و بعد اونجا توی قسمت constraints بریم و freez اشون کنیم.سعی کردم مواردی رو که یاد گرفتم رو اینجا به اشتراک بزارم ، تمام این ها پروژه های course هستش که دارم میبینم</description>
                <category>soheil moonesi</category>
                <author>soheil moonesi</author>
                <pubDate>Thu, 28 Mar 2024 20:04:55 +0330</pubDate>
            </item>
                    <item>
                <title>Unity Input Manager</title>
                <link>https://virgool.io/@soheil-moonesi/unity-input-manager-axiast26ty46</link>
                <description>خوب توی مطلبی که اینجا اوردیم میخوایم بیایم و بررسی کنیم ببینیم که داستان input ها توی unity چه شکلی میشه، برای همین هم از داکیومنت های خودش استفاده کردیم.Mouse buttons are named mouse 0, mouse 1, mouse 2, and so on.You can also query input for a specific key or button with Input.GetKey and the naming conventions specified above. For example:Input.GetKey(&amp;quota&amp;quot);خوب همونطوری که بالاتر هم دیدیم ما میتونیم بیایم وقتی که یه کلید زده شد اون رو تشخیص بدیم و بعد اگر بخوایم یه کاری رو بعدش انجام بدیم. برای این کار از input.getKey استفاده میکنیم و بعد توی پرانتز هم اون دکمه ای که میخوایم رو حواسمون بهش باشه رو مینویسیم.Using virtual axes in scriptsحالا اگر با محور ها کار داشته باشیم میایم اینجوری ازش استفاده میکنیم:float horizontalInput = Input.GetAxis (&quot;Horizontal&quot;);حالا اینطوری میتونیم به محور افقی دسترسی پیدا کنیم.For axes that describe an event rather than a movement (for example, firing a weapon in a game), use Input.GetButtonDown instead.اگر بخوایم یه event رو شناسی کنیم که با یه کلیدی فعال میشه میتونیم از Input.GetButtonDown استفاده کنیم.یه چند تا نکته ی دیگه ای هم که هست اینه که دو مدل میتونیم از Input.GetKeyاستفاده کنیم.مدل اول:if (Input.GetKey(&quot;up&quot;))        {            print(&quot;up arrow key is held down&quot;);        }مدل دوم:if (Input.GetKey(KeyCode.UpArrow))        {            print(&quot;up arrow key is held down&quot;);        }که توی مدل دوم برای این که ببنید هم کدوم از دکمه ها رو چجوری با استفاده از keyCode بخواید استفاده کنید باید به این لینک که داخل سایت یونیتی هست مراجعه کنید : لینکبرای این مطلب هم از این لینک ها استفاده شده : لینک 1 ، لینک 2</description>
                <category>soheil moonesi</category>
                <author>soheil moonesi</author>
                <pubDate>Wed, 27 Mar 2024 20:31:53 +0330</pubDate>
            </item>
                    <item>
                <title>Unity Engine Objects</title>
                <link>https://virgool.io/@soheil-moonesi/understanding-unity-engine-objects-pohldskr11kp</link>
                <description>UnityEngine.Objectخوب بالاترین قسمت سلسله مراتب آبجکت ها در یونیتی میرسه به؟ به unityEngine.Object name stringint GetInstanceID() methodbunch of equality comparersThe class also provides a static void Destroy(Object obj) method (and some overloads) that destroys a UnityEngine.Object and any of its subclasses.زمانی که object رو از بین میبریم.  قسمت native اون از  مموری آزاد میشه و قسمت مدیریتیش هم بعد از یه مدتی توسط garbage collector جمع آوری میشه و حذف میشه و دیگه هیچ رفرنسی دیگه به اون آبجکت باقی نمیماند. میگه که اگر رفرنسی موجود باشه ما میتونیم با استفاده از unityEngine.Object  به native object که از بین رفته  دسترسی پیدا کنیم ولی unityEngine.Object میاد عملگرهای == و =! رو override میکنه و آبجکت حذف شده رو به صورت null نشون میده. اگر بخوایم از متد ها برای دسترسی به این آبجکت ها استفاده کنیم بهمون nullReferenceException رو میده.GameObjectخوب میگه که GameObject مشتق شده از Object هستش که نشون دهنده ی هر المانی در صفحه هستش.خوب GameObject  میان name و instance ID از پدرشون به ارث میبرن.has a list of Components on it,has a tag string for organizational purposes, andbelongs to a layer.A GameObject’s stateis the product of all of its Components’ state, andwhether an object is active or not.حالا میخوایم عمیق تر موضوع رو بررسی کنیم، خوب همونطوری که میدونید gameObject ها حداقل یک component دارن که transform هستش که نمایانگر مختصات اون GameObject هستش object absolute world position and rotation و همچنین موقعیت اون  به نسبت پدرش رو هم داره .خوب توی editor چون این قضیه خیلی مورد استفاده است transform رو به صورت یه پراپرتی public گذاشتن.حالا برای دسترسی به component هایی دیگه میتونیم از T GetComponent&lt;T&gt;()T[ ]GetComponents&lt;T&gt;()استفاده کنیم. خوب حالا وقتی که این ها رو نوشتیم میاد و میره توی کل component ها سرچ میکنه و اونی که  ما میخوایم رو برامون میاره به همین دلیل میگن که این فرآیند lookup کردنه رو بهتره که برای سرعت بیشتر cache اش کنیم.اگر خودمون بخوایم build/extend کنیم یه gameObject رو  میتونیم از AddComponent&lt;T&gt;()استفاده کنیم.Individual Objects (a Component or ScriptableObject) might refer to other GameObjects in a few ways:آبجکت ها میتونن به gameObject ها به طریق های مختلفی وصل بشن یا اشاره کنن.By reference. By exposing a GameObject serialized field that you then set from the inspector.Using tags. Every Game Object can have a tag string. You can find objects in the scene using that tag through the static functions GameObject.FindGameObjectsWithTag. A GameObject also exposes a public bool CompareTag(string tag) method.اولی با رفرنسه - که میشه همون با استفاده کردن از serialized filed ها میشه راه دوم میشه استفاده کردن از Tag ها هستشThis is a quick-and-dirty way to get the job done, but is still a popular way. A common use of this in the wild is to have a &quot;Player&quot; tag to find the Player. Ideally, these methods should not be called every frame, so if you have to use them, consider caching the result.Using layers. A layer is an int between 0 and 31. Every Game Object is in exactly one layer.استفاده کردن از Layer ها : یک لایه میشه یک int از 0 تا 31 . هر game object یک لایه استخوب از این جا به بعد مقاله رو بعدا ترجمه میکنم و میزارم و تا اون موقع همین رو میزارم ویرگوللینک مطلب</description>
                <category>soheil moonesi</category>
                <author>soheil moonesi</author>
                <pubDate>Tue, 26 Mar 2024 20:25:52 +0330</pubDate>
            </item>
                    <item>
                <title>C# Exception Handling: Techniques and Best Practices</title>
                <link>https://virgool.io/@soheil-moonesi/mastering-c-exception-handling-techniques-and-best-practices-jbfyp80mackb</link>
                <description>خوب اول یه توضیحی راجع به این موضوع داشته باشیم. ببینید توی هر برنامه ای موقعیت های خارج از انتظاری اتفاق میوفته که این ها ممکنه باعث بشن کل برنامه به فنا بره و ما برای این که جلو این اتفاق رو بگیریم و یه جورایی برنامه رو پایدار نگهداریم باید یه سری کارها رو انجام بدیم.  خوب exception handling اصلی یعنی چی؟ یعنی این که یه مکانیزمی رو طراحی کنیم که اگر خطایی رخ داد بتونیم اون رو تشخیص بدیم و بعد بتونیم اون خطا در صورت امکان رفع کنیم.خوب حالا چرا این کار انقدر مهمه؟چون باعث میشه که برنامه مون پایداریش بیشتر بشه ، پیام های خطایی که میده باعث میشه  user experience بهتری داشته باشیم و جلوی crash کردن برنامه رو میتونیم بگیریم.نگهداری و خطایابی توی کدمون راحت تر میشه.Understanding the Exception Class in C#The System.Exception Classتوی سی شارپ exception ها آبجکت هایی هستند که از کلاس system.exception ارث بری کردند.این کلاس چندین تا متد و پراپرتی داره که کمکمون میکنه اطلاعات راجع به exception بدست بیاریم.مثل error message , source , stack trace Commonly Used Exception ClassesSystem.NullReferenceExceptionSystem.ArgumentExceptionSystem.DivideByZeroExceptionSystem.IndexOutOfRangeExceptionSystem.IO.FileNotFoundExceptionImplementing C# Exception HandlingThe Try-Catch Blockخوب این یه  block پایه برای همین بحث error handling هستش .کدی که داخل try اجرا میشه اگر exception براش به وجود بیاد کدی که داخل catch هستش اجرا میشه. try{  
   // Code that might throw an exception
 int result = 10 / 0; } 
catch (DivideByZeroException ex) { 
    // Handle the exception 
   Console.WriteLine(&#039;Error: &#039; + ex.Message); }The Finally Blockخوب قسمت finally آپشناله و هر چی داخلش بنویسیم همیشه اجرا میشه حالا این یعنی این که چه exception اتفاق بیوفته چه نیوفته اون قسمت اجرا میشه.try{   
  // Code that might throw an exception
} 
catch (Exception ex) {  
   // Handle the exception
} 
finally{
     // This code will always execute
    Console.WriteLine(&#039;Finally block executed.&#039;); 
}Nested Try-Catch Blocksخوب این بلوک های try catch رو تو در تو هم میتونیم ازشون استفاده کنیم و زمانی هم به کار میاد که بخوایم توی قسمت های مختلفی از برنامه بخوایم exception handling انجام بدیم.try {    
 // Outer try block
 try    {     
    // Inner try block  
  }   
  catch (ArgumentException ex)     {    
     // Handle ArgumentException  
  } } 
catch (Exception ex) {     
// Handle all other exceptions
}The Throw Statementخوب با استفاده از Throw میتونیم خودمون دستی یه exception رو فعال کنیم. یا وقت هایی که بخوایم یه exception رو برای یه سناریویی که داریم فعال کنیم.try{  
   // Check for an invalid condition 
if (someCondition)     {     
    throw new InvalidOperationException(&#039;Invalid operation.&#039;);     } } catch (InvalidOperationException ex) { 
    // Handle the exception 
   Console.WriteLine(&#039;Error: &#039; + ex.Message); }C# Exception Handling Best PracticesUsing Specific Exceptionsخوب یه راهی که داریم برای exception handling اینه که بیایم ارور ها رو به صورت مشخص شده catch کنیم اینطوری میدونیم دقیقا چه خطایی رخ داده و چه پیامی رو باید براش درنظر بگیریم.try{    
 // Code that might throw an exception
}
 catch (FileNotFoundException ex) {
     // Handle FileNotFoundException
} 
catch (IOException ex) {
     // Handle other IO exceptions
}
 catch (Exception ex) {
     // Handle all other exceptions
}Do Not Catch System.Exception Directlyاز اینکه بخوایم کلاس system.Exception رو مستقیم catch کنیم جدا خودداری بفرمایید. جدی میفرماید؟بله جدی. چرا؟ چون باعث ماسک شدن و یا همون پنهان شدن exception های دیگه میشه. به جاش بیاید و exception ها رو به صورت مشخص catch کنید.Avoid Catching Exceptions You Cannot Handleفقط exception هایی که میتونیم handle یا recover کنیم رو catch کنیم، اون هایی که نمیتونیم handle اش کنیم رو توی اون قسمت از کد ولش کنیم و بزاریم بره داخل call stack و از اونجا یه بلایی سرش میاریم.Use ‘Using’ Statement for Cleanupمیگه جاهایی که دارید با resource هایی که اینترفیس Idisposable دارن استفاده میکنید مثل streams, database connection, network socket  ها بیاید و از using استفاده کنید که بعدش درست clean up  بشه.  using (StreamReader reader = new StreamReader(&#039;file.txt&#039;)) { 
    // Code that uses the StreamReader
} 
// The StreamReader is automatically disposed when the using block exitsLogging Exceptions Effectivelyاز جزییات exception ها log بگیریم . جزییاتی مثل پیام خطا ، مرجع خطا، stack trace برای این که بتونیم بهتر برای دیباگ کردن آنالیز کنیم.میگه طبقه بندی درستی برای این log ها در نظر بگیریم error , warning , info بر اساس درجه اهمیتی که دارن.C# Error Handling Techniquesخوب توی سی شارپ انواع و اقسام تکنیک های  مختلف رو داریم برای error handling کردنC# Error Handlerخوب error handler به صورت مرکزی میاد و هر exception توی برنامه اتفاق بیوفته رو handle میکنه که این قضیه شامل logging , displaying error message یا انجام دادن یه کاری بر اساس یه تایپی از exception ها هستش. ما میتونیم یه کلاس جدا یا یه متد برای handle exception درست کنیم که هر وقت exception اتفاق افتاد اون بیاد و یه کاری رو بر اساس اون exception انجام بده، به طور مثال درست کردن کلاس ErrorHandler برای log گرفتن و نمایش ارور ها به یوزر:public static class ErrorHandler
{  
   public static void HandleException(Exception ex) 
   { // Log the exception
      LogException(ex);     
    // Display a user-friendly error message        
    ShowErrorMessage(ex);     } 
     private static void LogException(Exception ex)    {     
    // Use your preferred logging framework to log the exception details        
    Console.WriteLine(&#039;Error: &#039; + ex.Message);     } 
     private static void ShowErrorMessage(Exception ex)    {   
     // Show a user-friendly error message, e.g., using a message box or a 
     custom UI element   
     Console.WriteLine(&#039;An error occurred: &#039; + ex.Message);
     } }You can then call the HandleException method from your catch blocks:try{   
  // Code that might throw an exception
} 
catch (Exception ex) {     ErrorHandler.HandleException(ex); }C# Exception Error Codeبا assign کردن error کد ها به یک کلاس exception میتونیم اطلاعات بهتری راجع به اون exception بدست بیاریم و trace کردنش هم راحت تر میشه .
 public class CustomException : ApplicationException{ 
    public int ErrorCode { get; }  
    public CustomException(string message, int errorCode) : base(message) {         
 ErrorCode = errorCode;     } }try{     
// Throw a custom exception with an error code throw new CustomException(&#039;Invalid operation.&#039;, 1001); 
} 
catch (CustomException ex) { 
    // Handle the exception based on the error code    Console.WriteLine(&#039;Error Code: &#039; + ex.ErrorCode + &#039;, Message: &#039; + ex.Message); 
}این مطلب از این لینک گرفته شده و ترجمه تحت الفظی شده.</description>
                <category>soheil moonesi</category>
                <author>soheil moonesi</author>
                <pubDate>Mon, 25 Mar 2024 15:14:22 +0330</pubDate>
            </item>
                    <item>
                <title>C# IComparable vs IComparer</title>
                <link>https://virgool.io/@soheil-moonesi/c-icomparable-vs-icomparer-znfnlpphloq8</link>
                <description>خوب اینجا یه مطلب در حد بوندسلیگا نوشته که میخوایم با هم بخونیم : لینکIntroductionWhen doing sorting in C#, you will come across with the interface IComparable and IComparer.Both of the interfaces are frequently used together, and people often got confused because of its similar name, even though they serve different purposes.On this article, I will explain the difference between both of the IComparable and IComparer.خوب وقتی که میخوایم sort کنیم همیشه به این دو تا برمیخوریم IComparable و IComparer حالا با هم بررسیشون میکنیم.First, let us dive into the concept of sorting.Assume that we have a list of integer that we want to sort in an ascending order.Since we are using the data type Int and the built in List, we can use the provided Sort method by Microsoft.حالا بیایم برای این قضیه اول کانسپتشو رو ببینیم چیه. بیاید فرض کنیم که یه آرایه ای از integer داریم و میخوایم به صورت صعودی sort کنیم. خوب طبیعتا چون از list استقاده کردیم برای تایپ پس میتونیم از متد sort هم توش استفاده کنیم.بعد نتیجه کد بالا میشه این:This works because the Sort method knows what it has to sort, which in this case is an Integer number.این اینجا کار میکنه. چرا؟ چون متد sort میدونه دقیقا چطوری باید اعداد رو sort کنه.But, what if I want to sort a list that contains user-defined class?Let&#x27;s create a new class called Student, which will contain a name and age of a student.Note: The age will be a random integer number between 17 to 50.حالا فرض کنید که بخوایم یه list ای که شامل یه کلاس هستش رو sort کنیم و یه نکته ای هم که هست اینه که توی این کلاس ها سن یه عدد تصادفی از 17 تا 50 هستش، حالا چه کنیم؟اینجا میایم و 6 تا نمونه از این کلاس درست میکنیم و میریزیمش توی یک listThen let&#x27;s print out the list and sort it by their Age using the built in Sort function.خوب حالا بیایم با همین متد sort کار رو انجام بدیم و نتایج مرتب شده رو هم نشون بدیم:اینجوری میشه:میتونیم توی کنسول نمایش بدیمشون ولی نمیتونیم sort اش کنیم و به همچین اروری میخوریم:This happens because the sorting algorithm doesn&#x27;t know how to sort the elements. Which in this case is the Name of the student&#x27;s class.خوب این اتفاق برای این داره میوفته که الگوریتم sort نمیدونه چجوری باید این المان ها رو sort کنه.IComparableTo be able to sort it, we will have to implement the IComparable interface into our Student class.So let&#x27;s start by implementing it to our class.خوب حالا برای این که داستان رو رفعش کنیم باید بیایم اینترفیس IComparable رو پیاده سازی کنیم Now because we implemented the interface, we will have to implement its method as well.Here we will be overriding the CompareTo method.The CompareTo method is used to compare one object with another object of the same type. It will then return an integer that indicates the position of the object in the sort order (precedes, follows, occurs)خوب حالا وقتی که از اینترفیس استفاده کردیم باید متد مرتبط باهاش رو هم پیاده سازی کنیم. برای پیاده سازی هم باید متد CompareTo رو override کنیم.این متد برای مقایسه یک آبجکت با یک آبجکت دیگه از همون تایپ استفاده میشه. مقدار خروجیش هم یه int هستش که نمایانگر موقعیت اون آبجکت در مرتب سازی داره.If the current value is bigger than the next value it will return 1, else it will return -1. And if both of the value are the same it will return 0. It works like this:1: Swap0: Keep-1: Don&#x27;t swapاگر مقدار جاری از مقدار بعدی بزرگتر باشه 1 رو برمیگردونه اگر نبود -1 رو ، و اگر با هم برابر بودند 0 رو برمیگردونه.For example, let&#x27;s say that we have a List of [6,2,10,7,3].Then we call the CompareTo method on the list, and it will compare the current item 6 with the next item 2.Since 6 is bigger than 2, it will return the value 1. So both of the element will swap.The list is now [2,6,10,7,3] and it will repeat the process again until it becomes [2,3,6,7,10].خوب برای مثال فرض کنید که یه لیست داریم با مقادیری که در خط بالا نوشته شده.خوب حالا وقتی که متد CompareTo میخواد کارش رو انجام بده این کار رو میکنه اول میاد 6 رو در نظر میگیره و بعد میاد با عدد بعدی که 2 هستش مقایسه میکنه و بعد چون عدد 2 از 6 کوچتر هستش مقدار -1 رو برمیگردونه پس در نتیجه جای این دو تا توی آرایه تغییر میکنه و همین پروسه برای المان های بعدی هم تکرار میشن.حالا بیایم همین کار رو برای همون سوالی که ابتدای این متن اورده شده بود بکنیم:خوب اینم از نتیجه اش:حالا بیایم همین قضیه رو برای اسامی تکرار کنیم یعنی بیایم بر است اسم sort کنیم:این میشه نتیجه اش:IComparerحالا میرسیم به IComparerNow after understanding how IComparable works, we can go into how IComparer works.IComparable allows you to sort on a user-defined class that you have full control in. But, what if you want to apply the sorting in a class you don&#x27;t have control? Which means you can&#x27;t change the implementation of the class.حالا که IComparable رو فهمیدیم میریم سراغ IComparer . خوب Icomparable بهتون آپشن هایی زیادی برای sort کردن یه کلاسی که کاملا تحت کنترلتون هست رو میده. حالا بیاید این رو در نظر بگیریم که کلاسی که میخوایم sort رو انجام بدیم کنترل کاملی روش نداریم و این به معنی این هستش که نمیتونیم داخل اون کلاس چیزی رو پیاده سازی کنیم.Right now, if we call the sort method, it will sort the student by their names.Let&#x27;s say that we want to sort it by their ages. But since we can&#x27;t modify the Student class, we won&#x27;t be able to modify the CompareTo method.This is where IComparer comes in. We will have to create a new class that implements the IComparer interface.بیاید همین کد بالا رو نظر بگیریم، اینجا sort بر اساس name فقط داره انجام میشه و ما نمیتونیم کلاس student رو تغییر بدیم و میخوایم sort بر اساس Age هم انجام بشه ، اینجا دیگه نمیتونیم از متد CompareTo استفاده کنیم و اینجاست که میرسیم به IComparer که کار رو در میاره.خوب اینجا همون اول میایم یه کلاس جدید میسازیم :خوب  حالا وقتی که از این اینترفیس استفاده کردیم باید متد مرتبط باهاش رو هم پیاده سازی کنیم.The Compare method is used to perform a comparison between two objects with the same type. It will then return a value which will indicate if one object is less than, equal to, or greater than the other object.This is what it means by the returned value:Less than zero: x is less than yZero: x equals yGreater than zero: x is greater than yخوب این متد compare برای مقایسه دو تا آبجکت از یک تایپ استفاده میشه و در جواب این مقایسه یه نتیجه ای رو برمیگردونه ، اگر x از y کوچک تر باشه less than zero اگر برابر باشه 0 و اگر بزرگتر باشه greater than zero For example, let&#x27;s say that we are comparing between two object 9 and 3.When we are comparing it based on x - y, we will compare if 9 is bigger than 3, and since 9 is bigger than 3 it will return 1.خوب فرض کنید دو تا آبجکت 9 و 3 رو میخوایم مقایسه کنیم ، اگر بخوایم مقایسه رو بر اساس x-y انجام بدیم چون 9 از 3 بزرگتره مقداری که برمیگردونه 1 هستش.اینطوری هم پیاده سازی میشه:What this is saying is that we will compare the first student&#x27;s age by the second student&#x27;s age.Then we will have to implement our new StudentComparer class into our sorting method by passing it as parameter:خوب حالا بعد از این که یه کلاس جدید ساختیم و متد compare رو توش پیاده سازی کردیم و بعد هم الگوریتیم sort اش رو تعریف کردیم ، حالا نوبت به این میرسه که این کلاس رو به عنوان پارامتر بندازیم توی متد sort برای این کار نهایی بشه.این هم نتیجه اش:نکته : اگر بخوایم به ترتیب نزولی این کار رو بکنیم کافیه که جای x و y رو عوض کنیم و تمام.</description>
                <category>soheil moonesi</category>
                <author>soheil moonesi</author>
                <pubDate>Sun, 24 Mar 2024 08:50:00 +0330</pubDate>
            </item>
                    <item>
                <title>C# Static Constructors</title>
                <link>https://virgool.io/@soheil-moonesi/c-static-constructors-an-essential-guide-r7hmi1elgeij</link>
                <description>خوب امروز در حین کد نوشتن به یه داستان جالبی برخوردم گفتم به اشتراک بزارمش.Introduction to Static Constructors in C#The Basics of Constructors in C#In C#, constructors are special methods that initialize an object when it’s created. They have the same name as the class and don’t return any value. Constructors can be overloaded, allowing you to create multiple constructors with different sets of parameters to initialize objects in various ways.خوب همونطور که میدونید constructor های معمولی توی C# یه سری متد های خاص هستند که موقعی که میخواید یه نمونه بسازید براتون یه آبجکت رو میسازه. اسمشون هم اسمه با اسم اون کلاس و هیچ مقداری رو برنمیگردونه. constructor ها قابلیت overload شدن هم دارن به این معنی که به ازای مقادیر ورودی متفاوت میتونه کارای متفاوتی انجام بده.Why Use Static Constructors?Static constructors are useful when you need to initialize static members of a class. They ensure that the static members are initialized only once, before any instance of the class is created or any static methods are called.حالا میرسیم به static constructor ها . این مدل constructor ها زمانی میتونه استفاده بشه که بخوایم یه سری عضو بخوایم به اون کلاسمون اضافه کنیم، فقط دقت کنید که این constructor ها قبل از اجرای هر متد استاتیکی اجرا میشه و فقط هم همون ابتدای برنامه فقط و فقط یک بار اجرا میشن.Understanding Static ConstructorsDefining a Static ConstructorA static constructor is a constructor that doesn’t require an object instance to execute. It initializes the static members of a class and runs only once during the lifecycle of an application.خوب تعریف static constructor ها اینجوری میشه که اینها یه سری constructor هستن که نیازی ندارن برای این که اجرا اون کلاس ازش نمونه ای ساخته بشه. این constructor میاد یه سری عضو استاتیک به این کلاس اضافه میکنه و فقط هم در طول اجرای برنامه یک بار همون اول اجرا میشه و تمام.Static Constructor SyntaxThe syntax for defining a static constructor is straightforward:class ClassName
{
    static ClassName()
    {
        // Initialization code here
    }
}  این بالا سینتکس یه static constructor هستش.initialization of Static MembersStatic constructors are called automatically by the runtime, initializing static members before the class is used. This ensures that all static members have valid values before they are accessed.این مدل constructor ها به صورت اتوماتیک توسط runtime اجرا میشن و static member ها رو قبل از این که کلاس بخواد استفاده بشه اوکیش میکنن. تمام static member ها با این حرکت قبل از این بخواد بهشون دسترسی اعمال بشه مقادیرشون آماده شده.Execution OrderThe static constructor is called before any static member is accessed or any instance of the class is created. However, the exact order of execution is not guaranteed, so it’s best not to rely on any specific order when working with static constructors.اینجا داره میگه که static constructor ها قبل از این که بخوایم به هر static member دسترسی پیدا کنیم اجرا میشن. البته نوشته که این ترتیب اجرا رو خیلی نمیشه روش حساب کرد، برای همین هم گفته که حواستون باشه و روی این که همه چی قراره به این  ترتیب همیشه اجرا بشه حساب باز نکنید.Exception HandlingIf an exception is thrown in a static constructor, the runtime will not catch it, and the type will become unusable for the lifetime of the application domain. It is crucial to handle exceptions in a static constructor to prevent the application from crashing.آها یه داستان دیگه ای هم که داره اینه که اگر exception یهویی پیش بیاد runtime نمتونه اون ها رو catch منه و میگه که اون تایپ کلا به فنا میره وقتی که این اتفاق میوفته برای همین خیلی مهمه که با یه سری راه های دیگه این exception ها رو handle کنیم که برنامه  crash نکنه و به فنا نره .Static Constructor vs. Instance ConstructorKey DifferencesStatic constructors initialize static members, while instance constructors initialize instance members.Static constructors are called only once, whereas instance constructors are called every time a new object is created.Static constructors do not have access modifiers or parameters.خوب حالا فرق static constructor ها با constructor های معمولی چی هه؟اینه که static constructor  ها میان static member ها رو درست میکنن. ولی constructor های معمولی instance member ها رو میسازن.و این که static constructor ها access modifire ندارن .Practical Examples of Static ConstructorsSingleton Design PatternThe Singleton design pattern ensures that only one instance of a class exists in an application. A static constructor can beused to implement this pattern:اینجا همنطور که میبنید میتونیم از static constructor ها برای پیاده  سازی دیزاین پترن singleton استفاده کنیم. اینجا اومده new که باهاش میان از کلاس یه نمونه میسازن رو توی static constructor تعریف کرده، و این یعنی این که فقط همون شروع برنامه یه نمونه از این کلاس درست میشه و تمام.public class Singleton
{
    private static readonly Singleton _instance;

    static Singleton()
    {
        _instance = new Singleton();
    }

    private Singleton() { }

    public static Singleton Instance
    {
        get { return _instance; }
    }
}Common Pitfalls and Best PracticesBe Mindful of Execution OrderSince the execution order of static constructors is not guaranteed, avoid relying on a specific order when working with multiple static constructors.Avoid Complex Initialization LogicKeep the initialization logic in static constructors simple and straightforward. Complex logic may lead to unforeseen issues and make the code harder to maintain.خوب حالا گفته ممکنه چه خطاهای پیش بیاد.اولیش گفته که زیاد روی ترتیب اجرا این داستان ها حساب باز نکنید دومیش هم گفته که زیاد منطق پیچیده ای رو داخل شون پیاده سازی نکنید چون بعدا ممکنه به یه سری خطاهای عجیب غریب بر بخورید.این هم مقاله ای که ازش استفاده کردم: لینک </description>
                <category>soheil moonesi</category>
                <author>soheil moonesi</author>
                <pubDate>Fri, 22 Mar 2024 11:17:02 +0330</pubDate>
            </item>
                    <item>
                <title>C# LINQ Part 1</title>
                <link>https://virgool.io/@soheil-moonesi/linq-part-1-r4hkfw5ujlwj</link>
                <description>خوب میرسیم به مبحث بسیار بسیار و بازم بسیار مهمه LINQ حالا قبل هر چیز بیایم ببینیم که اصلا معنی LINQ چی هه ؟! اصلا مخففه چیه ؟Language Integrated QueryBefore the introduction of LinQ, querying and manipulating data in C# was an arduous and disjointed task. Developers had to grapple with multiple querying languages and approaches, such as SQL for databases, XPath for XML data, and custom solutions for other data types.خوب اینجا داره تاریخی بررسی میکنه، میگه که قبل از این که linq بیاد برنامه نویس ها برای این که بخوان query  بزنن و تغییراتی بخوان روی دیتا انجام بدن باید از انواع اقسام ابزار ها و زبان ها باید استفاده میکردند تا کار رو دربیارن.These disparate methods lacked integration with C#, which hindered productivity and resulted in overly complex and error-prone code.خوب همین راه هایی که بالاتر هم گفتیم زیاد با C# همخونی چندانی نداشتن و برای همین راه حل ها و کارهایی که برنامه نویس ها انجام میدادن خیلی پیچیده شد و کارایش هم پایین میومد.Recognizing the need for a unified querying language, the creators of C# set out to develop a powerful, flexible, and integrated solution that could cater to a wide range of data sources.خوب برای همین هم نیاز به یک رویکرد جدید و همسان سازی برای C# ایجاد شد و بعد هم سازندگان C# هم اومدن و یه راه حلی رو برای این داستان ساختن.Birth of LinQ: C# 3.0 and its game-changing featuresIn November 2007, Microsoft released C# 3.0 and the .NET Framework 3.5, which introduced multiple groundbreaking features, including extension methods, anonymous types, lambda expressions, and, most importantly, LinQ. These innovations were critical in enabling LinQ to deliver on its promise as a seamless, expressive, and unified querying framework.خوب بعدش اومدن و extention method , anonymous type , lamda expression ها و مهمتر از همه Linq رو درست کردن.قبل از این که linq بیاد برای این که بخوایم دیتا های توی یه کالکشنی رو ببینیم باید میومدیم و foreach استفاده میکردیم.List&lt;int&gt; numbers = new List&lt;int&gt; { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; List&lt;int&gt; evenNumbers = new List&lt;int&gt;();
  foreach (int number in numbers) {  
   if (number % 2 == 0)     {  
       evenNumbers.Add(number);     } }
حالا after و before رو میبینیم، بعد از اومدن linq:List&lt;int&gt; numbers = new List&lt;int&gt; { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };  IEnumerable&lt;int&gt; evenNumbers = from number in numbers                                 where number % 2 == 0 select number;it has opened the door for exciting advancements and enhancements, such as async queries, asynchronous streaming, and parallelism in the LINQ to Objects provider.میگه که درهای جدیدی رو باز کرد این قضیه، مثله : async queries , asynchronous streaming , parallelism و این ها قابلیت های جدیدی بود برای C#حالا entity framework هم اومد دیگه این قابلیت رو کامل تر کرد.LinQ Namespace and AssembliesLinQ is available in various namespaces within the .NET Framework. The primary namespaces you’ll be interacting with are:System.Linq: Contains basic LinQ extension methods for enumerable collectionsSystem.Data.Linq: Offers LinQ-to-SQL componentsSystem.Xml.Linq: Houses LinQ-to-XML functionalitiesخوب حالا میگه که شما برای هر کدوم از کاراهایی که میخواید انجام بدید از این namespace های linq میتونید استفاده کنید مثلا برای sql میشه از system.data.linq استفاده کنیم.Expressions, delegates, and anonymous methods are essential components that underpin LinQ’s functionality.و این که expressions , delegate , anoymous method ها عضو های اصلیه linq هستند.ExpressionsExpressions are C# code blocks that produce a value. A LinQ query often involves filtering or manipulating data using predicate expressions, which are conditions that return either true or false. Due to their concise and expressive nature, lambda expressions (introduced in C# 3.0) are commonly used in these scenarios.خوب expressions ها کد بلاک هایی هستند که نتایج و مقادیر رو درست میکنند. linq query ها معمولا توشون فیلتر کردن یا ایجاد تغییرات روی دیتا ها انجام میشه. predicate expressions ها مثل شرط هستند که جوابشون true یا false هستش، جلوتر مثال گفته:Example of a simple predicate expression:Func&lt;int, bool&gt; isEven = x =&gt; x % 2 == 0;In this example, the expression x =&gt; x % 2 == 0 is a lambda expression that represents a predicate that checks if a given integer is even.اینجا مثلا isEven داره به ما میگه که عدد داده شده زوجه یا فرده DelegatesDelegates are type-safe function pointers that encapsulate references to methods. They are crucial for LinQ because they provide the flexibility needed when working with methods in queries. Methods can be assigned to delegates, which in turn can be used as arguments to other methods, effectively providing a way to “plug in” functionality.This allows LinQ queries to utilize custom methods for operations such as filtering, sorting, or grouping according to different criteria.خوب همونطوری هم که قبلا گفتیم delegate ها مثل یه type safe function pointer عمل میکنن که میان و مرجع متد ها رو توی خودشون نگه میدارن. اتفاقا خیلی هم توی linq مهم و کاربردی هستن برای این که به ما قابلیت های مهم و انعطاف پذیری از متد های مختلف در query ها رو به ما میدن. public delegate bool IsEvenDelegate(int number); 
 public static bool IsEven(int number){ 
    return number % 2 == 0; }  
IsEvenDelegate isEvenDel = IsEven; 
List&lt;int&gt; numbers = new List&lt;int&gt; { 1, 2, 3, 4, 5 }; 
 IEnumerable&lt;int&gt; evenNumbers = numbers.Where(n =&gt; isEvenDel(n));In this example, we created a custom delegate IsEvenDelegate and a method IsEven() that checks if a number is even. We then assigned the method to the delegate and used it within our Where clause to filter out even numbers from a list.خوب برای توضیحات کد بالا به نظرم یکم باید بریم جلو تر.Anonymous MethodsAnonymous methods, primarily expressed as lambda expressions in modern C#, enable more concise and expressive syntax for defining inline functions. These functions are called “anonymous” because they don’t require an explicit method name.This conciseness is particularly important when writing LinQ queries, as it significantly reduces verbosity and enhances code readability.Consider this example that demonstrates the use of an anonymous method through a lambda expression in LinQ:متد های ناشناس در حالت عادی در lambda expression ها مورد استفاده قرار میگیره و یه راه خیلی خوب برای  تعریف یک function هستش. خلاصه نوشتن در نوشتن linq query ها خیلی مهمه چون پیچیدگی کد رو کم میکنه و خوانایی کد رو میبره بالا.Starting Strong: Writing Your First LinQ Queries in C#Basic LinQ Query Syntax and Structureخوب میرسیم به داستان linq ، همونطوری که میدونید با کلمه from میتونیم دیتاستی که قراره باهاش کار کنیم رو انتخاب کنیم بعدش از query operator هایی مثل where ,select, group , order استفاده کنیم برای این که بتونیم دیتا ها رو فیلتر کنیم یا تغییرشون بدیم. // Query syntax example
 var results = from student in students 
               where student.Age &gt; 18
               orderby student.Name ascending 
               select student.Name;Implicitly Typed Local Variables (var) in C# and LinQمیرسیم به این که اینجا میتونیم از var هم استفاده کنیم. چرا؟ چون اینجا دیگه میتونیم فقط تمرکزمون رو بزاریم برای نوشتن کد و query ها و بزاریم کامپایلر بیاد و تایپ ها رو خودش تعیین کنه اینجوری کارمون راحت تر میشه.// Using var with LinQ 
var studentsWithHighScores = from student in students                               
                             where student.Score &gt; 80 
                             select student;From, Select, and Where Keyword Usageحالا داستان هر کدوم از keyword ها چیه؟کلمه from تعیین کننده مرجع و منبع دیتاهامون هستشکلمه select مشخص میکنه که دقیقا شما چه دیتایی رو میخواین واکشی کنین از دیتابیس مثلا شاید بخواین اسم دانشجو ها رو فقط بدست بیارید اینطوری مینویسید : select student.Name کلمه where میاد بر اساس یه سری شرط یه سری فیلتر های رو دیتامون اعمال میکنه.// Using from, select, and where
 var names = from student in students
             where student.Age &gt; 18
             orderby student.Name ascending 
             select student.Name;حالا فرض کنید که بخوایم فقط دانشجو های خانم رو جدا کنیم:// Using from, select, and where with a different condition
 var femaleNames = from student in students
                   where student.Gender == &amp;quotFemale&amp;quot 
                   orderby student.Name ascending 
                   select student.Name;Filter, Projection, and Transformation Operationsخوب حالا بخوایم یه سری تغییرات توی دیتاهمو اعمال کنیم چی کار کنیم؟ خوب قبل از این کار بیایم ببینیم که چه کارهایی رو میتونیم انجام بدیم و یه اصطلاحات مربوط بهش رو یادبگیریم:خوب اصطلاح filtering همونطوری که مشخصه از اسمش برای این که دیتامون رو بخوایم یه سری شرط روش اعمال کنیم میایم ازش استفاده میکنیم ، از where هم برای این کار استفاده میکنی.اصطلاح projection میاد ساختار یا فرمت دیتامون رو تبدیل میکنه برای این کار از select استفاده میکنیم.اصطلاح transformation برای group ,orderby  و join و set oprator و موارد پیشرفته تر استفاده میشه .var highScoreNames = from student in students 
                      where student.Score &gt; 80 
                      orderby student.Age  
                      select student.Name;حالا فرض رو بر این بزارید که بخوایم یه سری از دانشجو ها رو بر اساس کلاسشون گروه بندی کنیم.var studentsByClass = from student in students  
                     group student by student.Class into studentGroup                       
          select new { Class = studentGroup.Key, Students = studentGroup };حالا اینجا همونطوری که میبینید اومده از group برای دسته بندی دیتاهای دانشجو ها انجام داده و گروه بندیشون رو بر اساس کلاس که داخلش هستن انجام داده. بعد اومده از select استفاده کرده و نتیجه این عملیات رو ریخته توی یه anonymous type که حاوی کلاس و یه کالکشن از دانشجو ها هستش. خوب اینجا با استفاده از select اومدیم اصلا ساختار دیتامون رو به یه شکل دیگه تبدیل کردیم.مرجع این مقاله: لینک</description>
                <category>soheil moonesi</category>
                <author>soheil moonesi</author>
                <pubDate>Sun, 17 Mar 2024 13:50:14 +0330</pubDate>
            </item>
                    <item>
                <title>C# Hashset</title>
                <link>https://virgool.io/@soheil-moonesi/c-hashset-tur3swmcxoxv</link>
                <description>The basics: A HashSet is a collection that holds unique elements in no particular order (O(1) complexity for adding, searching or removing). The HashSet&lt;T&gt; is a generic class in the System.Collections.Generic namespace, ideal for managing large data sets and performing set operations.Core aspects: The C# HashSet is a hash-based collection that allows only distinct elements. It supports various operations such as Union, Intersection, Difference, and more.خوب hashset یه کاکشنه که دیتا های منحصر به فرد رو توی خودش بدون هیچ ترتیبی نگهداری میکنه.پیچیدگیه اضافه کردن و سرچ کردن یا پاک کردن توی hasshet ها O1 هستش.و Hashse T یه کلاس generic هستش که که برای مدیریت کردن data set ها و برای Set Operation ها هم خیلی خوبه.و hashset ها کالکشن هایی بر مبنای hash هستند که فقط اجازه ذخیره کردن دیتایی متمایز از هم رو میدن و operation های union intersection , difference و ... رو هم میتونن انجام بدنC# HashSet vs Dictionary, List, and SetHashset vs SetUse HashSet when the order doesn’t matter and you need quicker set operation results, with no required duplicate elements.Use SortedSet when you need a sorted list of unique items but with slower operations due to the tree structure.از hashset ها زمانی که که ترتیب مهم نباشه و فقط مهم این باشه که سریع بتونیم set رو انجام بدیم بدون این که المان های duplicate داشته باشیم.زمانی هم که sort بودن و متمایز (منحصر به فرد بودن) مهم باشه میتونیم از sortedSet استفاده کنیم. ولی یکم سرعتش یکم کمتره// Creating a HashSet
HashSet&lt;int&gt; myHashSet = new HashSet&lt;int&gt; {1, 2, 3, 4, 5}; 
 // Creating a SortedSet
SortedSet&lt;int&gt; mySortedSet = new SortedSet&lt;int&gt; {1, 2, 3, 4, 5};  
// Adding elements
myHashSet.Add(6); 
mySortedSet.Add(6); 
 // Retrieving elements 
foreach (int item in myHashSet) { 
    Console.WriteLine(item); // Output won&#039;t have any specific order}  foreach (int item in mySortedSet) { 
    Console.WriteLine(item);  // Output will be in ascending order}C# HashSet vs Dictionarythe HashSet stores only unique elements, Dictionary stores key-value pairs, with unique keys. Use Dictionary when you require a &#x27;lookup table&#x27; type of structure, and HashSet when you only need a unique collection without any associated values. Let’s understand this with an example:خوب وقتی که میخوایم فقط و فقط المان های منحصر به فرد رو ذخیره کنیم میایم از Hashset ها استفاده میکنیم. dictionary ها میان بر اساس یک کلید دیتاها رو دخیره میکنن. وقتی که یه lookup table بخوایم میتونیم از Dictionary ها استفاده کنیم.// Creating a HashSet
HashSet&lt;string&gt; uniqueNamesHashSet = new HashSet&lt;string&gt; {&#039;Alice&#039;, &#039;Bob&#039;, &#039;Carol&#039;};  
// Creating a Dictionary
Dictionary&lt;string, int&gt; ageDictionary = new Dictionary&lt;string, int&gt; {     {&#039;Alice&#039;, 25},     {&#039;Bob&#039;, 30},     {&#039;Carol&#039;, 28}, }; 
 // Adding values to HashSet
uniqueNamesHashSet.Add(&#039;David&#039;);  
// Adding values to Dictionary
ageDictionary.Add(&#039;David&#039;, 34);  
// Retrieving values from HashSet
 foreach (string name in uniqueNamesHashSet) { 
    Console.WriteLine(name); } 
 // Retrieving values from Dictionary 
foreach (KeyValuePair&lt;string, int&gt; element in ageDictionary) {     Console.WriteLine($&#039;{element.Key}: {element.Value}&#039;); }C# HashSet vs ListList stores elements in sequential order, allowing duplicate elements and supports indexed access. HashSet, on the other hand, emphasizes uniqueness and faster performance. Choose List when the order of elements plays a critical role, and HashSet when you need to optimize for set operations and speed. To illustrate the differences, let’s compare HashSet and List in code:خوب list ها المان ها رو به صورت متوالی و به ترتیب ذخیره میکنه و اجازه این که از یک مقدار چند تا داشته باشیم رو هم میده ، همچنین به ما دسترسی از طریق indexed  رو هم میده.زمانی که توی برنامه ترتیب خیلی مهم باشه از List استفاده کنیم بهتره.// Creating a List
List&lt;int&gt; myList = new List&lt;int&gt; {1, 2, 3, 4, 5};  
// Creating a HashSet
HashSet&lt;int&gt; myHashSet = new HashSet&lt;int&gt; {1, 2, 3, 4, 5}; 
 // Adding elements to List
myList.Add(2); 
// List allows duplicates 
// Adding elements to HashSet
 bool result = myHashSet.Add(2); // HashSet doesn&#039;t allow duplicates, result will be False 
// Indexed access in List
 int firstElement = myList[0]; // firstElement will hold the value 1 // Indexed access is not allowed in HashSet 
// int firstElement = myHashSet[0]; // This line will cause a compilation errorMastering the Art of HashSet Manipulation and Methods With great power comes great responsibility—or, in the case of HashSet, great Methods!میگه که قدرت زیاد مسئولیت زیاد میاره که این مورد برای hashset ها میشه متد های توووپ.// Create a new hash set to hold unique integers
HashSet&lt;int&gt; intHashSet = new HashSet&lt;int&gt;();  
// Add elements using the Add method
intHashSet.Add(42);
 intHashSet.Add(7); 
bool added = intHashSet.Add(42); // Returns false, because 42 is already //present 
Console.WriteLine(&#039;Hash set contains 42: &#039; + intHashSet.Contains(42)); 
// Outputs: &amp;quotHash set contains 42: True&amp;quotUnion, Intersect, and Except: Performing Set Operations with EaseHashSet&lt;int&gt; firstSet = new HashSet&lt;int&gt; { 1, 2, 3 }; 
HashSet&lt;int&gt; secondSet = new HashSet&lt;int&gt; { 2, 3, 4 }; 
 // Perform a union
firstSet.UnionWith(secondSet); 
// firstSet now contains {1, 2, 3, 4} 
// Perform an intersection
firstSet.IntersectWith(secondSet);
 // firstSet now contains {2, 3} 
// Perform a difference(except)
firstSet.ExceptWith(secondSet); 
// firstSet now contains {1}Clear, Remove and Count: Housekeeping Methods for Your HashSet.HashSet&lt;string&gt; myHashSet = new HashSet&lt;string&gt; {&#039;apple&#039;, &#039;orange&#039;, &#039;banana&#039;};  
// Remove an element
myHashSet.Remove(&#039;apple&#039;); 
// myHashSet now contains {&#039;orange&#039;, &#039;banana&#039;} 
// Clear all elements
myHashSet.Clear(); 
// myHashSet is empty 
// Count the number of elements 
int count = myHashSet.Count; 
// count will be 2 before Clear() and 0 after Clear()</description>
                <category>soheil moonesi</category>
                <author>soheil moonesi</author>
                <pubDate>Sat, 16 Mar 2024 13:19:23 +0330</pubDate>
            </item>
            </channel>
</rss>