Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a custom syntax for describing an object in c#?

Tags:

c#

types

object

Searching Effort:

  1. Google:
    • custom c# syntax
    • c# floating point
  2. Stackoverflow:
    • custom c# syntax
    • c# floating point

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?

like image 522
Michael Haddad Avatar asked May 30 '16 10:05

Michael Haddad


People also ask

How to create objects in C #programming?

A typical C# program creates many objects, which as you know, interact by invoking methods. We can Create objects in C# in the following ways: 1) Using the ‘new’ operator: A class is a reference type and at the run time, any object of the reference type is assigned a null value unless it is declared using the new operator.

What is the syntax of defining an object?

As you can see, the syntax of defining an object is simple in a manner. It starts with the name of the class for which we are creating an object, followed by the name of an object which is of user choice. In this example, we have defined the object of class Cellphone with the name as an obj in the main method.

How to declare an object in C++?

Everything in C++ revolves around an Object. To declare an object, it is necessary to define the class of it. Each object will have two types of fields properties and methods.

What is an example of creating an object?

Below is an example of creating an object. Creating an object of a class is very simple. The class is a user-defined datatype, and in our example, it is a Cellphone.


2 Answers

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";
like image 145
Shreevardhan Avatar answered Nov 15 '22 11:11

Shreevardhan


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.

like image 28
Ondrej Tucny Avatar answered Nov 15 '22 11:11

Ondrej Tucny