Ber53rker
Ber53rker

Reputation: 1038

Extending an extension method? Or something?

Sorry I'm not very clear. It's sort of hard to explain what I'm looking to do. I'd like to make extension methods but have them segregated. So for example...

bool b = true;
char c = b.bool_ext.convert_to_YorN();
int i = b.bool_ext.convert_to_1or0();

Is something like that possible? Thanks!

Upvotes: 1

Views: 76

Answers (2)

CodeNaked
CodeNaked

Reputation: 41403

If you want them "segregated", then you'd have to either invent your own type:

public struct MyBool {
    public MyBool(bool value) : this() {
        this.Value = value;
    }

    public bool Value { get; private set; }
}

public static MyBoolExtensions {
    public static char convert_to_YorN(this MyBool value) {
        return value.Value ? 'Y' : 'N';
    }
}

public static BooleanExtensions {
    public static MyBool bool_ext(this bool value) {
        return new MyBool(value);
    }
}

Which can be used like:

bool b = true;
char c = b.bool_ext().convert_to_YorN();

Or just use them as static methods:

public class MyBoolConverters {
    public static char convert_to_YorN(bool value) {
        return value.Value ? 'Y' : 'N';
    }
}

Which can be used like:

bool b = true;
char c = MyBoolConverters.convert_to_YorN(b);

But you cannot categorize them like you show.

Upvotes: 3

Scott Chamberlain
Scott Chamberlain

Reputation: 127603

No that is not possible, The bool_ext would be a extension property of bool, and you can not currently do extension properties, only extension methods.

Upvotes: 5

Related Questions