🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)
🔧 AI Nachrichten Major AI platforms go down in unprecedented simultaneous outage(03.09.2026 um 17:34 Uhr)
🔧 AI Nachrichten ChatGPT, Claude, and Grok Down? Users Report Widespread Outages(03.09.2026 um 19:14 Uhr)
🔧 AI Nachrichten OpenAI Launches GPT-6 Astra, Says We May Have Entered the AGI Era(03.09.2026 um 22:08 Uhr)
🔧 AI Nachrichten Claude Comes to CarPlay as Fifth Major AI Chatbot App(05.09.2026 um 05:31 Uhr)
🔧 AI Nachrichten OpenAI’s GPT-6 Astra Is AGI, Says NVIDIA CEO Jensen Huang(07.09.2026 um 06:31 Uhr)
🔧 AI Nachrichten Blame AI companies for Mac mini and Mac Studio shortage(31.08.2026 um 10:32 Uhr)

🔧 Programmierung 🕛 kürzlich 4 Min Lesezeit
0

Exploring Indexers in C#: Array-Like Access for Custom Types

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

Indexers in C# enable you to treat custom objects like arrays, providing an intuitive way to access elements while keeping the internal structure private. In this article, we’ll explore indexers step by step, with clear examples to help you apply the concept in real-world scenarios.









What Are Indexers?



An indexer is a special kind of property that allows you to access elements of a class or struct using array-like syntax. You define it using the this keyword, along with an index type.






Why Use Indexers?





  • Encapsulation: Hide the internal collection structure.


  • Readability: Simplify access to elements without exposing unnecessary details.


  • Flexibility: Customize how elements are retrieved or set.









Step 1: A Simple Read-Only Indexer



Let’s create a class OrderList that holds an array of Order objects. We’ll define an indexer to retrieve orders by their position in the array.






Full Example






CODE
// Define an Order class
public class Order
{
public int Id { get; set; }
public string Description { get; set; }
}

// Define OrderList with an indexer
public class OrderList
{
private Order[] _orders;

// Constructor to initialize orders
public OrderList(Order[] orders)
{
_orders = orders;
}

// Read-only indexer
public Order this[int index]
{
get
{
if (index < 0 || index >= _orders.Length)
throw new IndexOutOfRangeException("Invalid index.");
return _orders[index];
}
}
}

// Program to demonstrate usage
public class Program
{
public static void Main()
{
// Initialize an array of orders
var orders = new Order[]
{
new Order { Id = 1, Description = "Laptop" },
new Order { Id = 2, Description = "Smartphone" },
new Order { Id = 3, Description = "Tablet" }
};

// Create an OrderList
var orderList = new OrderList(orders);

// Access orders using the indexer
Console.WriteLine(orderList[0].Description); // Output: Laptop
Console.WriteLine(orderList[1].Description); // Output: Smartphone
}
}









Explanation





  1. The this Keyword: Used to define an indexer.


  2. Type Safety: The int type ensures only valid indices are used.


  3. Encapsulation: The _orders array is private, so the internal data structure isn’t exposed.


  4. Validation: The indexer validates the index to prevent runtime errors.









Step 2: Adding a Custom Key to the Indexer



What if you want to retrieve an order by its Id instead of its position? We can extend the indexer to accept custom keys like int or GUID.






Full Example






CODE
public class OrderList
{
private Order[] _orders;

public OrderList(Order[] orders)
{
_orders = orders;
}

// Indexer with integer index
public Order this[int index]
{
get
{
if (index < 0 || index >= _orders.Length)
throw new IndexOutOfRangeException("Invalid index.");
return _orders[index];
}
}

// Indexer with GUID key
public Order this[Guid orderId]
{
get
{
var order = _orders.FirstOrDefault(o => o.Id == orderId.GetHashCode());
if (order == null)
throw new KeyNotFoundException("Order not found.");
return order;
}
}
}

// Program to demonstrate usage
public class Program
{
public static void Main()
{
var orders = new Order[]
{
new Order { Id = 1, Description = "Laptop" },
new Order { Id = 2, Description = "Smartphone" },
new Order { Id = 3, Description = "Tablet" }
};

var orderList = new OrderList(orders);

// Access orders using integer index
Console.WriteLine(orderList[1].Description); // Output: Smartphone

// Access orders using GUID
var guid = Guid.NewGuid();
var order = new Order { Id = guid.GetHashCode(), Description = "Monitor" };

Console.WriteLine(orderList[guid].Description); // Throws KeyNotFoundException
}
}









Why Use a Method Instead of an Indexer?



For custom keys, retrieving an element can be expensive, especially with large data sets. Using an indexer might create a false expectation of fast access. Instead, define a method for such lookups:




CODE
public Order FindById(Guid orderId) => _orders.FirstOrDefault(o => o.Id == orderId.GetHashCode());












Step 3: Real-World Example



The Dictionary<TKey, TValue> class in .NET uses indexers to retrieve values by their keys:




CODE
var dictionary = new Dictionary<int, string>
{
[1] = "Laptop",
[2] = "Smartphone"
};

Console.WriteLine(dictionary[1]); // Output: Laptop












Best Practices for Indexers





  1. Use Indexers for Fast Access: Ensure the underlying structure allows efficient retrieval.


  2. Avoid Complex Lookups: Use methods for computationally expensive operations.


  3. Provide Clear Error Messages: Validate indices and keys, throwing appropriate exceptions.

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
3 Quellen
GPT-6 Astra Release Today? OpenAI’s Next Major AI Model Is Almost Here
1 Quelle
Apple accuses OpenAI of destroying evidence as trade-secrets fight intensifies
1 Quelle
Major AI platforms go down in unprecedented simultaneous outage
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Exploring Indexers in C#: Array-Like Access for Custom Types

Thematisch verwandte Begriffe: Exploring, Indexers, ArrayLike, Access · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...