4

I have a weird question about C#.

I have a static class to store methods as extensions. Then, I have the following extension:

public static bool fwHasData(this DataTable table)
{
    return (table == null || table.Rows.Count == 0) ? true : false;
}

My question is: exists some way to avoid use the parentheses when I try to use the extension in my code?

Usage:

bool vHasData = MyDataTable.fwHasData(); // Works fine!

Expected usage:

bool vHasData = MyDataTable.fwHasData; // Removing the parentheses

Thanks a lot!

Servy
  • 202,030
  • 26
  • 332
  • 449
MiBol
  • 1,985
  • 10
  • 37
  • 64

4 Answers4

10

This is not possible in C#. It would require some form of "extension property" syntax, which is not available in C#. It has been suggested in the past, but doesn't exist in the C# language today, nor in the upcoming C# 6 suggestions.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
0

No, this is not possible. Trying to call a method without parentheses doesn't make sense in C#.

You declared an extension method so of course you would need to call it as such (using the parenthesis).

You are accessing it as if it is an extension property which do not yet exist in C#. Regardless, you didn't declare it like a property, so even if they did exist you would still need to call it as a method.

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
  • Many would argue that Extension Properties _do_ make sense, but the cost of development and testing is not worth the value (relative to other features that may make _more_ sense. – D Stanley Jun 12 '14 at 17:44
  • @DStanley, Sorry for the confusion. I wasn't saying that extension properties don't make sense (in fact I agree that they do). I was pointing out that you always have to call a method with parenthesis, and that not doing so doesn't make any sense. I will try to edit and clarify this. – BradleyDotNET Jun 12 '14 at 17:45
0

As suggested by others, the limitation is you can not have an extension property!

but, for the sake of achieving what you want. You can create a class deriving from DataTable (DataTable class is not sealed) and use this derived version in all your code.

Then you can extend DataTable with as many properties as you want!

Example:

public class DataTableEx : DataTable
    {
        public bool fwHasData
        {
            get
            {
                return (Rows.Count == 0) ? true : false;
            }
        }
    }
Manish Basantani
  • 16,931
  • 22
  • 71
  • 103
  • Yes, this is a quick fix, but I will need declare all the DataTable as DataTableEx... I don't want to do this. – MiBol Jun 12 '14 at 18:52
0

Well, I just accidentally did this (I was so surprised, I googled it and that led me here):

Func<IEnumerable<IPlanHeader>> test = header.AsEnumerable;

With this extension method:

public static class ExtendGenericType
{
    public static IEnumerable<T> AsEnumerable<T>(this T entity)
    {
        yield return entity;
    }
}

...and lo-and-behold, no parenthesis! So please try this, and let me know:

Func<bool> vHasData = MyDataTable.fwHasData;

EDIT - Look, ultimately you will still need to convert the Func<bool> to bool using parenthesis at some point:

bool hasData = vHasData();
Riegardt Steyn
  • 5,431
  • 2
  • 34
  • 49