Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is using an extension method for casting a bad idea?

Tags:

c#

casting

I recently started on WPF, and I noticed that you have to do a lot of casting (especially with events). This is an aesthetic issue, but I was wondering how bad it would be if I'd use an extension method to cast, instead of using normal casting.

public static T Cast<T>(this object obj)
{
    return (T)obj;
}

This would mean I could prevent a few nested parantheses, and change:

Console.WriteLine(((DataGridCell)e.OriginalSource).ActualHeight);

to:

Console.WriteLine(e.OriginalSource.Cast<DataGridCell>().ActualHeight);

Are there any clear disadvantages that I might be overlooking? How disgusted will people be when they encounter this in code? :)

like image 801
Dirk Boer Avatar asked May 29 '13 17:05

Dirk Boer


People also ask

Should you use extension methods?

Extension methods are an excellent addition to the C# language. They enable us to write nicer, more readable code. They allow for more functionally styled programming, which is very much needed in an object-oriented language. They also should be used with care.

What is an advantage of using extension methods?

The main advantage of the extension method is to add new methods in the existing class without using inheritance. You can add new methods in the existing class without modifying the source code of the existing class. It can also work with sealed class.

Why do we need static method in extension?

Essentially, an extension method is a special type of a static method and enable you to add functionality to an existing type even if you don't have access to the source code of the type. An extension method is just like another static method but has the “this” reference as its first parameter.


1 Answers

This is similar in intent to Enumerable.Cast, so I wouldn't necessarily say that people will be disgusted.

Are there any clear disadvantages that I might be overlooking?

The main disadvantage is that this will be an extension method available to every single variable in your code, since you're extending System.Object. I typically avoid extension methods on Object for this reason, as it "pollutes" intellisense.

That being said, there are other disadvantages:

If you used this on an existing IEnumerable, you'd get a name collision with Enumerable.Cast<T>. A file having your namespace included but missing a using System.Linq could easily be misunderstood by other developers, as this would have a very different meaning to the expected "Cast<T>" extension method.

If you use this on a value type, you're introducing boxing (pushing the value type into an object), then an unbox and cast, which can actually cause an exception that wouldn't occur with a cast. Your extension method will raise an exception if you do:

int i = 42; 
float f = i.Cast<float>();

This might be unexpected, as float f = (float)i; is perfectly legal. For details, see Eric Lippert's post on Representation and Identity. If you do write this, I would definitely recommend adding a class constraint to your operator.

I, personally, would just use parenthesis. This is a common, language supported feature, and should be understandable to all C# developers. Casting has the advantages of being shorter, understandable, and side effect free (in terms of intellisense, etc).

The other option would be to make this a normal static method, which would allow you to write:

Console.WriteLine(Utilities.Cast<DataGridCell>(e.OriginalSource).ActualHeight);

This eliminates the disadvantage of "polluting" intellisense, and makes it obvious that its a method you wrote, but increases the amount of typing required to use. It also does nothing to prevent the boxing and unbox/cast issue.

like image 189
Reed Copsey Avatar answered Sep 21 '22 01:09

Reed Copsey