"int" and "string" are built-in types. You won't be able to make one just like it that has no discernible fields. However, you can make something that looks and behaves almost exactly like a built-in type.
The best example is Nullable<T>
. This provides a nullable version of all value types and can be assigned just like it's built in:
int? x = 0;
int? y = null;
The way this works is Nullable<T>
overrides the explicit and implicit casts. This way you can assign a built-in type like int to a custom Nullable<int>
and code within Nullable<int>.op_Implicit
handles the conversion transparently.
Here's a complete example:
public struct MyVariable
{
private int _intValue;
private string _stringValue;
public override bool Equals(object obj)
{
if (!(obj is MyVariable))
{
return false;
}
var y = (MyVariable) obj;
return _stringValue == y._stringValue && _intValue == y._intValue;
}
public override int GetHashCode()
{
return (_stringValue ?? _intValue.ToString()).GetHashCode();
}
public override string ToString()
{
return _stringValue ?? _intValue.ToString();
}
public static implicit operator MyVariable(string value)
{
return new MyVariable { _stringValue = value };
}
public static implicit operator MyVariable(int value)
{
return new MyVariable { _intValue = value };
}
public static bool operator==(MyVariable variable, string value)
{
return variable._stringValue == value;
}
public static bool operator ==(MyVariable variable, int value)
{
return variable._intValue == value;
}
public static bool operator !=(MyVariable variable, string value)
{
return variable._stringValue == value;
}
public static bool operator !=(MyVariable variable, int value)
{
return variable._intValue == value;
}
public static void Test()
{
MyVariable a = "This is my own custom variable!";
MyVariable b = 2976;
if (a == "Hello") { }
if (b == 10) { }
Console.WriteLine(a.ToString());
Console.WriteLine(a.ToString());
}
}