+12  A: 

You can manually override the implicit and explicit cast operators for a class. Tutorial here. I'd argue it's poor design most of the time, though. I'd say it's easier to see what's going on if you wrote

string b = a.ToHtml();

But it's certainly possible...

public class A
{
    public string Content { get; set; }

    public static implicit operator string(A obj)
    {
        return string.Concat("<span>", obj.Content, "</span>");
    }
}

To give an example of why I do not recommend this, consider the following:

var myHtml = "<h1>" + myA + "</h1>";

The above will, yield "<h1><span>Hello World!</span></h1>"

Now, some other developer comes along and thinks that the code above looks poor, and re-formats it into the following:

var myHtml = string.Format("<h1>{0}</h1>", myA);

But string.Format internally calls ToString for every argument it receives, so we're no longer dealing with an implicit cast, and as a result, the other developer will have changed the outcome to something like "<h1>myNamespace.A</h1>"

David Hedlund
I agree with you about poor design; though, my example is just an example. What I want to do is much more complicated. Thanks for the answer! It's very helpful.
sfjedi
@sfjedi: The more complicated the scenario, the less appropriate implicit conversions are, IMO. There are *very* few places where it makes the code clearer, in my experience.
Jon Skeet
I'd always go with "ToString()" for converting to a string, think about later on when someone is maintaining code and accidentaly assigns the object to a string instead of to a copy of the object... could take weeks to track down (due to code blindness)
Mauro
@Mauro: `ToString` is an unfortunate name, as it already exists on every object. everyone knows about it and what it's supposed to do. an attentive code-reviewer can see that something is going on when a class is implicitly being cast to a string, but when `ToString` is used, there is *nothing* to imply that it has been overriden and will return something other than the expected `myNamespace.A`-ish value.
David Hedlund
I would at least suggest overriding both `operator string` *and* `ToString()` to return the same result in order to avoid the mentioned confusions ...
MartinStettner
@MartinStettner: that's just one problem that could arise, though. i picked it because it was a striking example of when a developer really can't be expected to realize that a change will alter the result. when you find yourself overriding both the implicit conversion and ToString for the single reason of counter-acting the problems that might arise as a result of the confusion you're introducing, perhaps alarm bells should go off that you're doing it wrong. the core of the problem is that it's not apparent what the code is doing, and your solution doesn't address that.
David Hedlund
OK guys—this is just an example! I have no intention of converting my class to a string. Actually, I want to convert it to a `System.XML.Linq.XElement`, to be more specific and I have a private _xElement in my class that I'm going to return implicity. It might seem bad practice, but I'm not doing this for code clarification. I'm doing it because it's literally the only way I can do what I'm trying to do.
sfjedi
@sfjedi: that may well be! I'm always suspicious when I hear "literally the only way", but hey, you know more about your situation than I do. if this should never ever under any circumstances be done, then it wouldn't be supported by the framework. it's just... "considered harmful". I think the ensuing discussion is very warranted - for those who stumble upon this thread later, if nothing else! if you're aware of all the downsides and still decide to go ahead and do it, then that's totally up to you. after all; I'm from sweden and not likely to ever have to maintain this code ;)
David Hedlund
+3  A: 
public static implicit operator string(A a)
{
    return "foo";
}

HTH,
Kent

Kent Boogaart
Great answer, but I'll be voting @desco's answer because it's more complete. Sorry.
sfjedi
+7  A: 
public class A
{
    public string Content { get; set; }
    public static implicit operator string(A a)
    {
        return string.Format("<span>{0}</span>", a.Content);
    }
}
desco
Absolutely perfect answer! Thank you so much!
sfjedi
+1 for a completely correct answer. However, I *strongly* recommend against doing this in practice, for the reasons mentioned in David's answer.
Christian Hayter
A: 

overriding ToString() would be nice way. Moreover in debug mode you got a tip with ToString() return value.

Arseny
@Arseny: copying my response to BennySkogbergs now deleted reply: I'd argue overriding `ToString` is even more misleading than implicit cast. When using an implicit cast, somebody might be tricked into thinking `a` is a string, but if you watch carefully enough, you'll see that *something* fishy is going on. with this code *everyone* will assume that `b` will contain something like "myNamespace.A" after that operation.
David Hedlund