3

I would look this up on Google/MSDN, but I have no idea what it's called so I'm asking here.

In Java, I seem to remember you can do this really cool thing like:

Class MyClass
{
  int number;

  MyClass() { }

  void setNumber(int number)
  {
    this.number = number;
  }
}

and then do something like:

MyClass myClass = new MyClass()
  {
    override void setNumber(int Number)
    {
      this.number = 2 * number;
    }
  };

...or something. Forgive any mistakes I made above - I haven't actually touched Java in about 6 years.

The point is, I remember you could pseudo-extend a class inline.

Right now, I need to extend a C# WinForms control, but I only need to use it once, and the modifications are very minor. All I need to do is to override the CreateParams property and OnPaint() handler.

My solution is already getting huge with classes all over the place, it seems like a shame to include yet another class which is basically identical to a standard .Net control, just with very slightly different behaviour.

Is it possible to do this inline-extension in C# like you could in Java? If so, how? (and what is it called so I can look it up on MSDN?)

3 Answers 3

5

This (explicit nominative anonymous types) is not possible in C#3/4.

The types must be created explicitly and then constructed. Tasks are sometimes "inverted" in C# with the use of Events and Delegates (class invokes Event which supplies implementation such as "NeedDataSource") -- arguably because of this, although it just makes sense in many cases.

If is possible to create explicit non-nominative types: var x = new { P = 1, }; but only in a local scope. Implicit methods include delegates/lambdas/anonymous functions and do not apply here.

Happy coding.

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

1 Comment

Thanks. That answers my question, but it's not the answer I was hoping for.
1

I understand that you said this cannot be done in C#, but its possible to rewrite this to C# but with not too much complicated method?

new EffectClause("Sleep Clause", SleepEffect.class)
{
    public String getClauseDescription()
    {
        return "Bla bla bla";
    }

    public boolean isEnabledByDefault()
    {
        return true;
    }
};

1 Comment

There is no class-property in C# but in JAVA. Furthermore: is this a question or an answer? And last: how is this concerned to the question?
0

The feature you're looking for in C# is called Extension Methods. They are similar, but instead you make code such as:

public static void setNumber(int Number, MyClass target)
{
    target.number = 2 * number;
}

You usually include this in your own extensions namespace, then bring it into scope when necessary by using MyExtensions; C# is able to figure out that you mean to call this method when you call whatever.setNumber(x);

I'm not 100% sure if this will let you override a method that already exists in a class however. Your other option is to inherit from the target class with a class specifically for the purpose of overriding the target method.

4 Comments

Extension methods are different and do not provide the same functionality. In the above, imagine: T one = new T { public void () { IMPL1(); } }; T two = new T { public void () { IMPL2(); } }; -- that is, Extension methods can decorate a statically-known type but can't allow different anonymous subtypes to be created.
Perhaps his use of "extension" confused you. He's not talking about C# extension methods but extending (in the Java sense) a class. Or in other words, inheriting from a class inline.
Not what the op was asking for. Extension methods do not allow you to override existing methods or properties.
Yes, I see what you are doing in the snippet you provided but I cannot see how it would enable me to override something like OnPaint() in a foreign class. Thanks anyway :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.