You cannot create implicit and explicit operators outside of the desired object.
What you can do instead is :
public static class StringEx
{
public static Guid ToGuid(this string str)
{
Guid guid;
Guid.TryParse(str, out guid);
return guid;
}
}
And later on you can use it like :
string mestring = " ... ";
Guid guid = mestring.ToGuid();
EDIT:
There's another way (of course there is) which is a bit useless but I'll post it here :
Make a class that will wrap string
public class StringWrapper
{
string _string;
public StringWrapper(string str)
{
_string = str;
}
public static implicit StringWrapper operator(string str)
{
return new StringWrapper(str);
}
public static implicit string operator(StringWrapper strWrapper)
{
return strWrapper._string;
}
public static implicit Guid operator(StringWrapper strWrapper)
{
Guid guid;
Guid.TryParse(str, out guid);
return guid;
}
public static implicit StringWrapper operator(Guid guid)
{
return guid.ToString();
}
}
And this useless class let's you do something like this:
string str = "..";
Guid guid = (StringWrapper)str;