class Person
{
string Name;
int Age;
}
I want to be able to cast a string to Person implicitly like following
var mrFoo = "Foo" as Person;
I know I can do the following by defining implicit casting
Person mrFoo = "Foo";
But I’m specific to use “as” operator
No, you can’t do that. The “as” operator never uses user-defined conversions – only reference conversions and unboxing conversions. Basically, the reference in question already has to be the right type.
Personally I would strongly advise you to stay away from conversion operators (especially implicit ones) for the vast majority of cases. Usually having a conversion method is clearer, e.g.
Person.FromString(...).