How to use indexes with extension methods by calling parameter and function calls

Is it possible to use indexerswith methods extension.

eg. Consider this as an example only.

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

EDIT

  • Using mygrid.SelectedValue("mycol")

  • How to use it as an indexer mygrid.SelectedValue["mycol"], and not above one.

  • Can I use it the same way? mygrid.SelectedValue["mycol"](out somevalue);

What is the syntax for getting such values. Any simple example or link will work.

+5
source share
2 answers

Well, there are two problems here:

  • C # does not support (by and large) named indexes 1
  • # , SelectedValue , -

, , , . :

mygrid.SelectedValue()["mycol"]

. , .


1 # 4 COM-.

+4

Extension Method.

a Extension Method

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

string Extension Method

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

, .NET :

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

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

- , , .

, , Microsoft .

0

All Articles