1

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!

3
  • 1
    @mellamokb I clearly stated it was an example. That being said, I surely hope your code isn't full of irrelevant comments... Commented Jul 21, 2012 at 0:54
  • You could do it like that: b.bool_ext().convert_to_YorN() Commented Jul 21, 2012 at 0:56
  • @ThomasLevesque I think that might suit my needs. Could you provide an example? Thanks! Commented Jul 21, 2012 at 0:58

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.