Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Overloading Index Operator

Overloading Index Operator

Scheduled Pinned Locked Moved C#
csharpdatabasedata-structuresquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    budidharma
    wrote on last edited by
    #1

    Is it possible in C#? I have a class, Hand. It contains an Array of cards. I have defined a property in the array which returns the array of cards that it contains, but it makes for convoluted syntax: Player.Cards.Card[1]; Where Player.Cards refers to the "hand" a player holds. If my Hand class looks something like: public class Hand private Card[] m_Hand; public Hand(params Card[] cards) { m_Hand = cards; } public Card[] Cards { get { return m_Hand; } } } Can I simply overload the index operator for my Hand class and get rid of the Cards property? So I could do something like: Player.Cards[0]; Would refer to: Player.Cards.m_Hand[0]; That make sense? As always, thanks!

    H 1 Reply Last reply
    0
    • B budidharma

      Is it possible in C#? I have a class, Hand. It contains an Array of cards. I have defined a property in the array which returns the array of cards that it contains, but it makes for convoluted syntax: Player.Cards.Card[1]; Where Player.Cards refers to the "hand" a player holds. If my Hand class looks something like: public class Hand private Card[] m_Hand; public Hand(params Card[] cards) { m_Hand = cards; } public Card[] Cards { get { return m_Hand; } } } Can I simply overload the index operator for my Hand class and get rid of the Cards property? So I could do something like: Player.Cards[0]; Would refer to: Player.Cards.m_Hand[0]; That make sense? As always, thanks!

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      Yes you can, using this as a property name and optionally using the IndexerNameAttribute to get it a name (for some languages like VB.NET have to refer to the indexer while C# doesn't) other than "Item":

      public class Hand
      {
      // ...
      [System.Runtime.CompilerServices.IndexerName("Card")] // Optional
      public Card this[int index]
      {
      get { return m_Hand[index]; }
      }
      // ...
      }

      This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Customer Product-lifecycle Experience Microsoft [My Articles] [My Blog]

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups