6

Is it possible to use indexers with extension methods.

eg. Consider it as an example only.

    public static object SelectedValue(this DataGridView dgv, string ColumnName)
    {            
        return dgv.SelectedRows[0].Cells[ColumnName].Value;
    }

EDIT

  1. usage mygrid.SelectedValue("mycol")

  2. How to use it as an indexer mygrid.SelectedValue["mycol"] rather than above one.

  3. Is it possible to use it like this as well ? mygrid.SelectedValue["mycol"](out somevalue);

What are the syntax of getting this kind of values. Any simple example or link will work.

Shantanu Gupta
  • 20,688
  • 54
  • 182
  • 286

2 Answers2

4

Well, there are two issues here:

  • C# doesn't (by and large) support named indexers1
  • C# doesn't support extension properties, so you can't make SelectedValue a property returning something indexable instead

So no, the syntax you've specified there won't work. You could get this to work:

mygrid.SelectedValue()["mycol"]

but that's a bit ugly. I'd stick with the method form if I were you.


1 C# 4 supports calling named indexers on COM objects.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • @Jon: What kind of indexer i need to create here in that case. http://msdn.microsoft.com/en-us/library/6x16t2tx.aspx. I read this link to create indexer but how will it work within extension class. Is it possible to work out using this within static class as i need to create indexer also static – Shantanu Gupta Jun 25 '10 at 10:51
  • I too would prefer the method specified above in my ques until unless i have named indexer for which i have to start working on C# 4 – Shantanu Gupta Jun 25 '10 at 10:53
  • Why only supporting named indexers on COM? – simendsjo Jun 25 '10 at 10:59
  • @Jon: How it is possible to use named indexers in 2K8 with datatables and datasets and many more. i.e. `(new DataTable).Rows[0]["mycol"]`. Doesn't .net opens this indexers to every one in previous versions itself – Shantanu Gupta Jun 25 '10 at 11:04
  • @Shantanu: That's using the `Rows` property, which returns a `DataRowCollection` which itself has an indexer. See my second bullet point - you can't create extension properties. – Jon Skeet Jun 25 '10 at 11:09
  • @simendsjo: I believe there was a desire to support it more widely, but the cost was seen to be greater than the value provided. The C# team only has limited resources. – Jon Skeet Jun 25 '10 at 11:10
  • @Jon: I read related article on msdn for C# 4. But still i would like to know named indexers are available to Collections in all previous version ( – Shantanu Gupta Jun 25 '10 at 11:14
  • @Shantanu: Named indexers just aren't supported in C# - the idea is that it's generally better to write a property which returns an indexable collection. I'm not sure to what extent I agree with that, but that's the explanation usually given. – Jon Skeet Jun 25 '10 at 12:43
1

Let me try to clarify the usage and intentions of Extension Method.

Consider a Extension Method

public static bool IsNullOrEmpty(this string source)
{
    return source == null || source == string.Empty;
}

Now you extend your string class with this Extension Method

var myString = "Hello World";
Assert.AreEqual(myString.IsNullOrEmpty(), false);

This is what .NET does on compilation:

public static bool IsNullOrEmpty(string source)
{
    return source == null || source == string.Empty;
}

Using our old school

var myString = "Hello World";
Assert.AreEqual(IsNullOrEmpty(myString), false);

Extension method is nothing but a visualization to what we were used to do.

Well, extending indexers could be possible but Microsoft did not think about it.

lesderid
  • 3,388
  • 8
  • 39
  • 65
Abdul Munim
  • 18,869
  • 8
  • 52
  • 61