<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: hasan kılıç</title>
    <description>The latest articles on DEV Community by hasan kılıç (@hasan_kl_6197f0ae57e14).</description>
    <link>https://dev.to/hasan_kl_6197f0ae57e14</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3290651%2Fb0f3eaad-5c0f-43bd-9adf-4fcd535888a1.png</url>
      <title>DEV Community: hasan kılıç</title>
      <link>https://dev.to/hasan_kl_6197f0ae57e14</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hasan_kl_6197f0ae57e14"/>
    <language>en</language>
    <item>
      <title>Reflection in C# : What It Is, How It Works, and Why It Matters for Unity Developers</title>
      <dc:creator>hasan kılıç</dc:creator>
      <pubDate>Sun, 06 Jul 2025 20:24:56 +0000</pubDate>
      <link>https://dev.to/hasan_kl_6197f0ae57e14/reflection-in-c-what-it-is-how-it-works-and-why-it-matters-for-unity-developers-ljb</link>
      <guid>https://dev.to/hasan_kl_6197f0ae57e14/reflection-in-c-what-it-is-how-it-works-and-why-it-matters-for-unity-developers-ljb</guid>
      <description>&lt;h2&gt;
  
  
  What is Reflection?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;C# program compiles into an &lt;strong&gt;assembly&lt;/strong&gt; that includes metadata, compiled code and resources. Inpsecting the &lt;strong&gt;metadata&lt;/strong&gt; and compiled code at runtime is called &lt;strong&gt;&lt;em&gt;reflection&lt;/em&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;blockquote&gt;
&lt;p&gt;To understand reflection properly , you need to understand what metadata is first.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  What is metadata?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;In C# , metadata refers to &lt;strong&gt;information about built-in or custom types, members and other constructs defined in an assembly&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;In other words, metadata is not the data itself — &lt;strong&gt;&lt;em&gt;it’s data about data&lt;/em&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;It tells the runtime things like : What classes and interfaces exist, Which field, properties and methods they have, what attributes decorate them and more.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  How It Works?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Let’s take a look at how to obtain a &lt;strong&gt;Type&lt;/strong&gt; — the backbone of the Reflection API — inspect its &lt;strong&gt;metadata&lt;/strong&gt;, and use it to interact with your code at &lt;strong&gt;runtime&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;In C#, every object and type has metadata attached to it. The System.Type class represents this information. You can get a Type object in a few ways:&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Using &lt;strong&gt;typeof(T)&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;Type type1 = typeof(Player)&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Using .GetType() on an instance&lt;br&gt;
&lt;code&gt;Type type2 = (new Player()).GetType();&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Once you have a Type , you can:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Inspect its fields, properties and methods&lt;/li&gt;
&lt;li&gt;Check its attributes&lt;/li&gt;
&lt;li&gt;Invoke methods or create instances dynamically&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How to use it?
&lt;/h2&gt;

&lt;p&gt;Okay that's enough of theory let's code a simple player class and inspect it's properties , methods and attributes at runtime.&lt;/p&gt;

&lt;h3&gt;
  
  
  Player class
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[Serializable]
public class Player
{
    public string Name { get; private set; }
    public int Level { get; private set; }

    public Player(string name, int level)
    {
        Name = name;
        Level = level;
    }

    // parameterless constructor
    public Player() { }

    public void LevelUp()
    {
        Level++;
        Debug.Log("Player leveled up");
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Reflector class
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private void Start()
    {
        Player player = new Player("Dude", 1);

        // Get the Type metadata of Player
        Type playerType = player.GetType();

        // Get property value using Reflection
        var level = playerType.GetProperty("Level").GetValue(player);
        Debug.Log($"Level: {level}"); // Output: Level: 1

        // Inspect all properties
        PropertyInfo[] properties = playerType.GetProperties(
            BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

        foreach (var property in properties)
        {
            Debug.Log($"Property Name: {property.Name}"); // Output: Name, Level
        }

        // Inspect and invoke methods with no parameters
        MethodInfo[] methods = playerType.GetMethods(
            BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

        foreach (var methodInfo in methods)
        {
            if (methodInfo.GetParameters().Length == 0)
            {
                Debug.Log($"Invoking: {methodInfo.Name}");
                methodInfo.Invoke(player, null); 
                // Will call LevelUp and show "Player leveled up"
            }
        }

        // Inspect class-level attributes
        IEnumerable&amp;lt;Attribute&amp;gt; attributes = playerType.GetCustomAttributes();
        foreach (var attribute in attributes)
        {
            Debug.Log($"Player class has an attribute named: {attribute.GetType().Name}"); 
            // Output: Player class has an attribute named: SerializableAttribute
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✨ And that’s it — now you’ve seen how Reflection works in C# and how you can use it in Unity to peek inside your own code!&lt;/p&gt;

&lt;p&gt;It’s a handy tool for building smarter workflows, editor tools, or just satisfying your curiosity at runtime, but be careful about performance costs of it.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Stay tuned — in upcoming articles, we’ll build a simple yet extendable Developer Console using Reflection and Attributes. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;✨ Thanks for reading!&lt;br&gt;
&lt;a href="https://www.patreon.com/hasankilic/membership?view_as=patron" rel="noopener noreferrer"&gt;buy me a coffee&lt;/a&gt;&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>programming</category>
      <category>gamedev</category>
      <category>unity3d</category>
    </item>
    <item>
      <title>How to Use Operator Overloading in C# to Write Cleaner Unity Code</title>
      <dc:creator>hasan kılıç</dc:creator>
      <pubDate>Thu, 03 Jul 2025 16:14:27 +0000</pubDate>
      <link>https://dev.to/hasan_kl_6197f0ae57e14/how-to-use-operator-overloading-in-c-to-write-cleaner-unity-code-4o14</link>
      <guid>https://dev.to/hasan_kl_6197f0ae57e14/how-to-use-operator-overloading-in-c-to-write-cleaner-unity-code-4o14</guid>
      <description>&lt;h2&gt;
  
  
  What is operator overloading?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Opetor overloading is use to make math operations like &lt;strong&gt;+, -, ==, != ...&lt;/strong&gt; on your own custom types.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Why use operator overloading?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;It’s perfect for custom numeric types, like health, damage, stats, currencies, or game grid positions. Your math operations become short, readable, and less error-prone..&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Without operator overloading, you’d often write repetitive Add(), Subtract(), CombineWith() methods. Overloading removes that boilerplate and keeps your API small.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  How to use operator overloading?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;To overload an operator in C#, you define a &lt;strong&gt;public static&lt;/strong&gt; method inside your class or struct. The method must return a value and use the special &lt;strong&gt;operator&lt;/strong&gt; keyword.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You tell C# which operator you’re overloading by writing it after the operator keyword — like operator &lt;strong&gt;+&lt;/strong&gt; for addition, operator &lt;strong&gt;-&lt;/strong&gt; for subtraction, or operator &lt;strong&gt;==&lt;/strong&gt; for equality.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The operator method must take the same number of operands the operator normally uses. For example, + and - are binary operators, so they take two parameters.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Example use case
&lt;/h2&gt;

&lt;p&gt;Let's see it in action. I’ll write a simple GridPosition struct for demonstration — I think this is one of the most common ways you’d use operator overloading in Unity, especially for tilemaps, grid-based movement, or strategy games.&lt;/p&gt;

&lt;p&gt;But you can use the exact same approach for your stat system, prize system, or any other numeric game logic where clean, natural math syntax makes your code easier to read.&lt;/p&gt;

&lt;h3&gt;
  
  
  CustomGrid struct
&lt;/h3&gt;

&lt;p&gt;The CustomGrid struct is simple — it holds the X and Y coordinates of a grid position. You can use this to represent tile positions, map nodes, or any grid-based system in your game..&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    public struct CustomGrid
    {
        public int X { get; }
        public int Y { get; }    

        public CustomGrid(int x, int y)
        {
            X = x;
            Y = y;
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Adding operator overloading
&lt;/h3&gt;

&lt;p&gt;Let's see how we can overload "+" operator.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        // + operator requires two parameters
        public static CustomGrid operator + (CustomGrid grid1, CustomGrid grid2)
        {
            return new CustomGrid(grid1.X + grid2.X, grid1.Y + grid2.Y);
        }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;The method must be public static&lt;br&gt;
It must return a CustomGrid&lt;br&gt;
It takes two CustomGrid parameters — one for each side of the + operation.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Adding other operations
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; public static CustomGrid operator -(CustomGrid grid1, CustomGrid grid2)
 {
     return new CustomGrid(grid1.X - grid2.X, grid1.Y - grid2.Y);
 }

 public static CustomGrid operator *(CustomGrid grid1, CustomGrid grid2)
 {
     return new CustomGrid(grid1.X * grid2.X, grid1.Y * grid2.Y);
 }

 // == operator must return a bool 
 public static bool operator ==(CustomGrid grid1, CustomGrid grid2)
 {
     return grid1.X == grid2.X &amp;amp;&amp;amp; grid1.Y == grid2.Y;
 }

 // != operator must return a bool
 public static bool operator !=(CustomGrid grid1, CustomGrid grid2)
 {
     return grid1.X != grid2.X || grid1.Y != grid2.Y;
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;⚠️ When you overload == and != in C#, you should also override the Equals and GetHashCode methods to ensure your type works correctly in collections and comparisons.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Testing
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void Start()
{
  CustomGrid grid1 = new CustomGrid(4, 1);
  CustomGrid grid2 = new CustomGrid(2, 6);

  var addition = grid1 + grid2; // add
  var subtraction = grid1 - grid2; // subtract
  var multiplication = grid1 * grid2; // multiply element-wise

  // override ToString() methods to get expected output
  Debug.Log($"Addition: {addition} , subtraction: {subtraction} , multiplication: {multiplication}"); 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;output: Addition: (6.00, 7.00) , subtraction: (2.00, -5.00) , multiplication: (8.00, 6.00)&lt;/code&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Operator overloading is a powerful feature in C# that lets you make your custom types behave like built-in ones — using natural and readable syntax.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In Unity development, this means cleaner and more intuitive code for things like grid positions, stats, currencies, and other numeric systems. Instead of writing verbose helper methods, you get to write expressive math operations that improve readability and reduce errors.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Give it a try in your next Unity project, and enjoy the benefits of clearer, cleaner, and more maintainable code!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Thanks for reading&lt;br&gt;
&lt;a href="https://www.patreon.com/hasankilic/membership?view_as=patron" rel="noopener noreferrer"&gt;buy me a coffee&lt;/a&gt;&lt;/p&gt;

</description>
      <category>unity3d</category>
      <category>csharp</category>
      <category>gamedev</category>
      <category>programming</category>
    </item>
    <item>
      <title>Unity C# Tip: Sort Your Data Like a Pro with IComparable &amp; IComparer</title>
      <dc:creator>hasan kılıç</dc:creator>
      <pubDate>Thu, 26 Jun 2025 17:13:47 +0000</pubDate>
      <link>https://dev.to/hasan_kl_6197f0ae57e14/unity-c-tip-sort-your-data-like-a-pro-with-icomparable-icomparer-3k4d</link>
      <guid>https://dev.to/hasan_kl_6197f0ae57e14/unity-c-tip-sort-your-data-like-a-pro-with-icomparable-icomparer-3k4d</guid>
      <description>&lt;p&gt;__Have you ever needed to sort your inventory items by rarity, or rank your enemies based on distance to the player? Writing custom sorting logic in Unity can quickly become messy if you don’t know about two powerful C# interfaces: &lt;strong&gt;IComparable&lt;/strong&gt; and &lt;strong&gt;IComparer&lt;/strong&gt;. In this post, we'll look at how you can use these interfaces to cleanly organize and sort your data in Unity — with real-world examples like an inventory system.Since the today's discussion is all about &lt;strong&gt;Sorting&lt;/strong&gt; things , our inventory system will be pretty small , but you can scale it however you want (i also suggest to using Scriptable Objects).&lt;/p&gt;

&lt;h2&gt;
  
  
  What is IComparable interface?
&lt;/h2&gt;

&lt;p&gt;The IComparable interface in C# is used to define a natural (default) sorting order for objects of a class.&lt;/p&gt;

&lt;p&gt;You implement this interface when your objects should be able to compare themselves to other objects of the same type.&lt;/p&gt;

&lt;p&gt;This allows you to use methods like List.Sort() directly without needing an external comparer.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Use this interface to implement a &lt;strong&gt;default&lt;/strong&gt; comparison.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  What is IComparer interface?
&lt;/h2&gt;

&lt;p&gt;The IComparer interface is used to define custom or external sorting logic for objects.&lt;/p&gt;

&lt;p&gt;You use this interface when you want to define multiple ways to compare objects, or when you can't modify the original class.&lt;/p&gt;

&lt;p&gt;You pass the comparer as an argument to sorting methods like List.Sort(IComparer).&lt;/p&gt;

&lt;h2&gt;
  
  
  Item class
&lt;/h2&gt;

&lt;p&gt;Item class is pretty simple , it holds the item data , implements IComparable interface for default comparison&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Item : IComparable&amp;lt;Item&amp;gt;    
{
    public int Id { get; }
    public int Power { get; }
    public Rarity Rarity { get; }

    public Item(int id, int power, Rarity rarity)
    {
        Id = id;
        Power = power;
        Rarity = rarity;
    }

    // Comes from IComparable interface , it helps to define 'default' sorting logic.
    public int CompareTo(Item other)
    {
        // let's compare ids as default
        return this.Id.CompareTo(other.Id);
    }
}

public enum Rarity
{
    Common,
    Rare,
    Legendary
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Comparer classes
&lt;/h2&gt;

&lt;p&gt;Comparer classes implement &lt;strong&gt;IComparer&lt;/strong&gt; interface, which allows us to define custom comparing logic.Since we implemented &lt;strong&gt;default&lt;/strong&gt; comparison logic by using &lt;strong&gt;IComparable&lt;/strong&gt; interface in &lt;strong&gt;Item&lt;/strong&gt; class, let's create our own Comparer classes.I will start with &lt;strong&gt;Power&lt;/strong&gt; comparer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    /// &amp;lt;summary&amp;gt;
    /// Use this class to sort items based on their power
    /// &amp;lt;/summary&amp;gt;
    public class ItemPowerComparer : IComparer&amp;lt;Item&amp;gt;
    {
        // Compares by power
        public int Compare(Item x, Item y)
        {
            return x.Power.CompareTo(y.Power);
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;And now — let’s sort our items based on &lt;strong&gt;Rarity&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;💡 Remember: enums in C# are backed by integers by default. So comparing enum values is just like comparing integers — and the C# compiler is smart enough to handle that elegantly under the hood.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
    /// &amp;lt;summary&amp;gt;
    /// Use this class to sort items based on their rarity
    /// &amp;lt;/summary&amp;gt;
    public class ItemRarityComparer : IComparer&amp;lt;Item&amp;gt;    
    {
        // Compares by Rarity
        public int Compare(Item x, Item y)
        {
            return x.Rarity.CompareTo(y.Rarity);
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Inventory class
&lt;/h2&gt;

&lt;p&gt;I think we all get it.Let's use it in action.It's time for our big guy &lt;strong&gt;Inventory&lt;/strong&gt; class.I will make it simple for the sake of this tutorial but you can extend it however you want.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Inventory : MonoBehaviour
{
    // Item list
    private readonly List&amp;lt;Item&amp;gt; items = new List&amp;lt;Item&amp;gt;()
    {
        new Item(id: 0 , power:10 , rarity:Rarity.Rare),
        new Item(id: 1 , power:6 , rarity:Rarity.Common),
        new Item(id: 2 , power:7 , rarity:Rarity.Legendary)
    };

    private void Update()
    {
        // default sorting
        if (Input.GetKeyDown(KeyCode.Space))
        {
            Sort();
            // output : 0,1,2 -&amp;gt; when sorted by id
        }

        //Power sorting
        if (Input.GetKeyDown(KeyCode.P))
        {
            Sort(new ItemPowerComparer());
            // output : 1,2,0 -&amp;gt; when sorted by power
        }

        //Rarity sorting
        if (Input.GetKeyDown(KeyCode.R))
        {
            Sort(new ItemRarityComparer());
            // output : 1,0,2 -&amp;gt; when sorted by Rarity
        }
    }

    public void Sort(IComparer&amp;lt;Item&amp;gt; comparer = null)
    {
        items.Sort(comparer); // sorts by ascending 
        foreach (Item item in items)
        {
            Debug.Log($"\n {item.Id}");
        }
    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  &lt;strong&gt;Conclusion&lt;/strong&gt;
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Sorting is one of those things that sounds simple — until your game needs to rank, order, or organize complex data. Thankfully, with IComparable and IComparer, you can keep your logic clean, modular, and easy to extend.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Whether you're building a full inventory system, a quest tracker, or a leaderboard, these interfaces will save you from messy code and hard-to-maintain conditionals_&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Thanks for reading&lt;br&gt;
&lt;a href="https://www.patreon.com/hasankilic/membership?view_as=patron" rel="noopener noreferrer"&gt;buy me a coffee&lt;/a&gt;&lt;/p&gt;

</description>
      <category>unity3d</category>
      <category>csharp</category>
      <category>gamedev</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
