Does "Object Expression" exist in C# like in F#?
Asked Answered
C

3

8

There is one interesting concept in F# language:

Object Expressions (F#)

http://msdn.microsoft.com/en-us/library/dd233237.aspx

Definition:

An object expression is an expression that creates a new instance of a dynamically created, anonymous object type that is based on an existing base type, interface, or set of interfaces.

I wonder, does something similar exist in C#? Or this stuff is only available in F# programming language?

Chlodwig answered 20/12, 2013 at 11:48 Comment(2)
C# has anonymous types but they are much more limited in scope (basically all you can do is implicitly define properties).Hurds
As others have answered, it's not possible using C# only. But you can use impromptu-interface to achieve something similar. See https://mcmap.net/q/1468960/-implementing-an-interface-at-runtimeClark
N
5

No that does not exist. I think the closest thing is a class that implements those interfaces.

Anonymous types are unable to conform to an interface or derive from anything.

Lambdas and delegates offer functions as variables.

The documentation for Object Expressions state this which makes it sound like it's a language convenience.

You use object expressions when you want to avoid the extra code and overhead that is required to create a new, named type. If you use object expressions to minimize the number of types created in a program, you can reduce the number of lines of code and prevent the unnecessary proliferation of types. Instead of creating many types just to handle specific situations, you can use an object expression that customizes an existing type or provides an appropriate implementation of an interface for the specific case at hand.

You could fake it (although you couldn't constrain it to an interface) with something like this

var foo = new { square = new Func<int, int>(x => x * x) };
Novikoff answered 20/12, 2013 at 11:52 Comment(0)
C
3

A pattern that I use is to create a bunch of "ad-hoc" classes that implement an interface that I instantiate with a delegate (usually a closure):

For instance:

public sealed class AdHocEquatable<T> : IEquatable<T>
{
    readonly Func<T, bool> equals;

    public AdHocEquatable(Func<T, bool> eq)
    {
        equals = eq;
    }

    public bool Equals(T other)
    {
        return equals(other);
    }
}

Then use like so:

var eq = new AdHocEquatable<Person>(p => p.ID == some_person_ref.ID);

In my experience, object expressions are needed for only a handful of interfaces (usually from the the BCL).

See here for more examples.

Crocus answered 2/1, 2014 at 0:21 Comment(0)
O
0

I think the closest you're going to get is a delegate (comments are taken from the description of Object Expression that you gave):

public delegate int MyDelegate(int input);
// ...is based on an existing base type, interface, or set of interfaces

Then somewhere

MyDelegate myDelegate = (i) => i * 2;
// An object expression is an expression that creates a new instance of a dynamically created, anonymous object type

myDelegate(1); // returns 2

But this only really deals with signature. Methods, properties - I can't think of anything that does those at the moment.


Although...

One important thing to remember is that you can have F# and C# in the same solution, so you can always write the bulk of your application in C# and do the fiddly bits which can't be done in C# (like this) with F#.

Ootid answered 20/12, 2013 at 11:54 Comment(1)
As I have read, I think that with object expressions, you can implement an interface on-the-fly without having to create a class. So... It may be too different from what F# is doing... But you have said, that it's "the closest thing", not the similar one, so thanks for the answer! :)Chlodwig

© 2022 - 2024 — McMap. All rights reserved.