2

I was experimenting with making a custom variables but got stuck.

I'm still new to C# so it's only expected for me to not know what's happening, I guess..

struct MyCustomStringVariable
{
    public static implicit operator MyCustomStringVariable(string input)
    {
        return input;
    }
}

class Program
{
    static MyCustomStringVariable myCustomString = "This is a string!";

    static void Main(string[] args)
    {
        Console.WriteLine(myCustomString);
        Console.ReadLine();
    }
}

The following exception is thrown

System.StackOverflowException: 'Exception of type 'System.StackOverflowException' was thrown.'

10
  • 4
    What is your end goal? Defining operators is very rare in C#, are you sure you don't just want a type (a class or struct) with a string field? Commented Jun 21, 2018 at 20:33
  • 1
    @pm100 what are level 500 things? Commented Jun 21, 2018 at 20:37
  • 1
    @JonathonChase I mean covered in the CS5xxx classes not in CS1xxx classes. College classes not c# classes. "CS504 creating custom conversion operators in c#" Commented Jun 21, 2018 at 20:38
  • 1
    My final goal is to visualize the process of making a string variable in my head so that I can better understand the concept behind it. <= I am not sure what a new type as well as an implicit operator how to do with this? Commented Jun 21, 2018 at 20:39
  • 1
    tip for a beginner: avoid messing around with operator at all cost Commented Jun 21, 2018 at 20:47

2 Answers 2

4

This is because the code is stuck in an infinit loop. Your implicit operator will call itself because it returns the original input string which does not throw an exception because of the defined operator.

public static implicit operator MyCustomStringVariable(string input)
{
    return input; // returning string type will call this method again
}

should be

public static implicit operator MyCustomStringVariable(string input)
{
    // and use input somewhere on the returned type
    return new MyCustomStringVariable(); 
}

That said there is probably no reason for you to define a type named MyCustomStringVariable but that is hard to tell because you never share the code for this or how you intend to use it.


My final goal is to visualize the process of making a string variable in my head so that I can better understand the concept behind it.

I am not sure how your custom struct or its implicit operator fit in with this goal. Why not just use the type string?

static string myCustomString  = "This is a string!";
Sign up to request clarification or add additional context in comments.

8 Comments

The big question is: why the compiler compiled this without errors or at least warnings? The retrun type isn't MyCustomStringVariable :)
@CodeNotFound - Because of the operator. This compiles just fine as do other infinit recursive loops, it is a logic error not a syntax error.
@CodeNotFound Sure it is! There's an implicit conversion from the return statement's type to the return type. It just so happens to be implemented by the very same operator implementation.
@Igor & Joe Sewell: I understand the stack overflow exception. What I don't understand why the compiler didn't detect that the same implicit conversion is used to convert the returned value.
@CodeNotFound You've defined a conversion from string to MyCustom. Your conversion returns a string, not a MyCustom. The compiler asks "Is there an implicit conversion from string to MyCustom?" There is, so it compiles along.
|
1

It's because the implicit operator is called recursively. You'll need to implement your structure as so, encapsulating your string variable somehow.

struct MyCustomStringVariable
{
    private string value;

    public MyCustomStringVariable(string input)
    {
        value = input;
    }

    public static implicit operator MyCustomStringVariable(string input)
    {
        return new MyCustomStringVariable(input);
    }

    public string GetValue()
    {
        return value;
    }
}

Then, calling it like

Console.WriteLine(myCustomString.GetValue());

You can refer to the documentation here.

2 Comments

Is there a way I can accept both answers, I think both of them were really helpful?
@Dan - Stack Overflow only allows for a single marked answer per question. Once you have enough reputation you can up-vote each submitted answer once and later on you can also downvote each submitted answer.

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.