we know:
int? number = 10;
Console.WriteLine(number is int); // true
but:
NotNull<string> text = "10"; // NotNull<> is my struct
Console.WriteLine(text is string); // false
I want text is string
return true, how can I do that?
-------------------------------- edit
here is my NotNull:
public class NotNull<T> where T : class
{
public T Value { get; }
private NotNull(T value)
{
this.Value = value;
}
public static implicit operator T(NotNull<T> source)
{
return source.Value;
}
public static implicit explicit NotNull<T>(T value)
{
if (value == null) throw new ArgumentNullException(nameof(value));
return new NotNull<T>(value);
}
}
if a class was declaring like:
public class A
{
public NotNull<string> B { get; set; }
}
I just hope any serializer can serialize and deserialize it same as:
public class A
{
public string B { get; set; }
}
-------------------------------- edit 2
I found this is a impossible question:
NotNull<>
is class, default(NotNull<>)
is null, I do nothing.NotNull<>
is struct, default(NotNull<>).Value
is null.sorry about the question.
Operator Overloading in C++ In C++, we can make operators work for user-defined classes. This means C++ has the ability to provide the operators with a special meaning for a data type, this ability is known as operator overloading.
C does not support overloading of operators or functions. There's no way you can redefine < , <= , > , >= , == , or != to compare struct types directly.
In Python, overloading is achieved by overriding the method which is specifically for that operator, in the user-defined class. For example, __add__(self, x) is a method reserved for overloading + operator, and __eq__(self, x) is for overloading == .
You can redefine or overload the function of most built-in operators in C++. These operators can be overloaded globally or on a class-by-class basis. Overloaded operators are implemented as functions and can be member functions or global functions. An overloaded operator is called an operator function.
On MSDN you have list of overloadable operators: Overloadable Operators (C# Programming Guide)
These operators cannot be overloaded:
=, ., ?:, ??, ->, =>, f(x), as, checked, unchecked, default, delegate, is, new, sizeof, typeof
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With