Syntax for C# Array Property

Started by
16 comments, last by haro 19 years, 11 months ago
What is the correct syntax in C# for declaring a property which acts like an array? I did not find many helpful links googling for C# proprties array. What I want to do is something like:

public Node Child[int offset]
{
	get
	{
          .. blah blah blah
          return localList[offset];
	}
}
I want to access the child of a node in a quadtree via: if( currentNode.child[0] == null) blah blah blah... but the node being accessed isn''t necessarily always going to actually be child[0], so I wanted to use a property as opposed to just making the child list public. I don''t really want to use a getter with an offset as I''ve then just completely missed the point of properties.
Advertisement
I''m guessing what you want is an "indexer" for your class,
from the syntax.

public class Node    {    public Node this[int offset]        {        get { return localList[offset]; }        }    // end    }// end


Node n = new Node();Node child = n[5];  // The 6-th child 


See here for more information, or the MSDN, of course.


Kami no Itte ga ore ni zettai naru!
神はサイコロを振らない!
Thanks, but that''s not quite what I meant. I don''t want to index directly into the class, but rather into a member of the class. Ie- Node.Child[0];
OK, see this thread.


Kami no Itte ga ore ni zettai naru!
神はサイコロを振らない!
You need to expose a property of a type that has an indexer, like an array or an IList.

--
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
[Project site] [Blog] [RSS] [Browse the source] [IRC channel]
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Those solutions really just don''t work so well when I want to create a property of a list of Object X within an Object X since I don''t want to create a public indexer. I don''t like the interface of having the children of a node represented as indices into the parent node.
Arild Fines'' answer does exactly what you''ve asked for in the initial post!

If it''s not good enough, ask a different fucking question.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
EDIT: -clipped- Responding to a flame with a flame is pointless.

[edited by - haro on February 5, 2004 8:29:08 AM]
personaly i use

<access flags> <type>[] Name { get { return array; } }
quote:Original post by haro
I cannot wait for the new forums. You, DrPizza, seem too typical. You think you lots to say, but yet you never say anything useful. The majority of what you say is nothing more than poorly veiled trolling/flaming. You seem distinctly similiar to another GD''er: SaberWolf ( or some spelling of the such ).


Give it a break, DrPizza has lots of useful stuff to say as does SabreMan. You do sometimes too.

This topic is closed to new replies.

Advertisement