5

Searching Effort:

  1. Google:
  2. Stackoverflow:

And some more I do not remember...


Preconception: Every "built-in type" in c# (such as int, float, charetc.) is a class, and every class in C# inherits from the Object class. Therefore, every "built-in type" inherits from the Object class.

With that preconception in mind, I would assume that to set a double variable, for example, I would need to set some properties using the "normal" syntax. Here is what I mean:

double number = new double();

number.leftSide= 5;
number.rightSide = 23;

Console.Write(number);

// Output:
// 5.23

But C# has a special, equivalent syntax for creating a double variable (in a way that it would do what I tried to do above, not that the code above will actually work):

double number = 5.23;

The compiler understands that the floating point separates the number into two: 5 and 23.

My question is if I can do the same with my own classes. For example, if I have my own Time class (and it is just an example, so please do not suggest to use built-in time classes), I would want to have the option to instantiate it like so:

Time time = 10:25;

And the compiler would understand that the colon separates the number into hours and minutes (which are, I assume, properties I need to create in the Time class).

I have heard about Roslyn CTP but I am looking for a simpler, built-in way, of doing what I have described.

Can I do it?

Community
  • 1
  • 1
Michael Haddad
  • 4,085
  • 7
  • 42
  • 82
  • 1
    general rule is few languages (Groovy) is operator forcing, Your fragment can be presented (integer 10) (operator : ) (integer 25) - but I think ':' is impossible in C# – Jacek Cz May 30 '16 at 11:01
  • 1
    cont .. 5.23 is at lexical level, one level below parser. – Jacek Cz May 30 '16 at 11:02

2 Answers2

8

This is not possible in C# currently. The closest you can do is define an implicit conversion from string to Date. For example

public class Time
{
    public static implicit operator Time(string value)
    {
        // Initialize your object with value
        // Similar to 
        var values = value.Split(':');
        var hour = Convert.ToInt32(values[0]);
        var min = Convert.ToInt32(values[1]);
        . . .
    }
    . . .   // Your fields, properties and methods
}

This would let you

Time time = "10:25";
Shreevardhan
  • 12,233
  • 3
  • 36
  • 50
  • That is a wonderful solution! I would wait for other answers to see if they are more helpful, but if not - I would choose this one. – Michael Haddad May 30 '16 at 11:07
  • 2
    @Sipo This answer provides a really good way to express what you want to ultimatelly achieve. I think other answers, including mine, can just provide more background info. Personally, I'd just consider making `Time` a `struct`, not a `class`. – Ondrej Tucny May 30 '16 at 11:10
  • @OndrejTucny - Thank you vert much! If I would create `Time` as a `struct`, would I be able to do what I try to do, or I will still need @Shreevardhan's solution? – Michael Haddad May 30 '16 at 11:12
  • 2
    @Sipo You can define similar implicit conversion inside a `struct` also. – Shreevardhan May 30 '16 at 11:14
  • @Sipo You need Shreevardhan's solution. – Ondrej Tucny May 30 '16 at 11:25
4

My question is if I can do the same with my own classes.

No, you cannot. C# does not support user-defined syntax constructs. You have to live with the set of language features such as syntax constructs, operators, etc. defined by the language.

Every "built-in type" in c# (such as int, float, charetc.) is a class, and every class in C# inherits from the Object class.

The primitive types (such as int, float, etc.) are not classes. They are primitive types. In order to support object-oriented features such as methods, the language defines structs (such as Int32, Single) that 'kind of' shadow the primitive types.

The fact everything can be an Object is in case of primitive types achieved through boxing—a process that takes the primitive value type and creates an object 'envelope' around it.

Ondrej Tucny
  • 27,626
  • 6
  • 70
  • 90