Instance Method V Class Method...  
Author Message
CJW99





PostPosted: Visual C# Language, Instance Method V Class Method... Top

Hello all,

When we have:

String abc = etc etc;

We can call abc.IndexOf(<some string>)

Whereas when we have an Array we need to use the class method...

string[] bcd = etc etc;

The equivalent is: Array.IndexOf(bcd, <object>);

They both effectively do the same thing (on their respective objects), i'm just curious as to why the difference in syntax in calling them

Thank you

Chris




Visual C#14  
 
 
Ken_L





PostPosted: Visual C# Language, Instance Method V Class Method... Top

Arrays are ordered collections of items of the same data type that are accessed using the array name in conjunction with the offset from the start of the array of the desired item. Whereas a string is a reference type that represents a group (string) of unicode characters. From there it is all in the syntax. Meaning, accessing the data is simple in a string.



 
 
Paul Louth





PostPosted: Visual C# Language, Instance Method V Class Method... Top

All arrays derive from System.Array, just like all strings are System.String's.

In your first example abc.IndexOf(...) the IndexOf method belongs to the System.String which has its own implementation of IndexOf. In your second example Array.IndexOf, you're calling the IndexOf method which belongs to System.Array, which again has it's own implementation.

It's just a matter of inconsistency on Microsoft's part that they didn't make the prototype for each method work in the same way. But they are seperate implementations of a similar concept, so they are free to vary also.

Hope that helps :)


 
 
CJW99





PostPosted: Visual C# Language, Instance Method V Class Method... Top

Thanks guys,

That answers my question really well.

Cheers

Chris



 
 
Paul Louth





PostPosted: Visual C# Language, Instance Method V Class Method... Top

Cool, glad to help! Please mark the relevant posts as helpful :)