Class Of<T>
Abstract base class for defining types that alias an existing type.
Implements
Inherited Members
Namespace: Recore
Assembly: Recore.dll
Syntax
[JsonConverter(typeof(OfConverter))]
public abstract class Of<T> : IEquatable<Of<T>>
Type Parameters
Name | Description |
---|---|
T |
Remarks
Use Of<T> to create a strongly-typed "alias" of another type.
You can use a using
directive to create an alias for a type,
but the scope of that alias is limited to that file.
Furthermore, the alias is just that -- an alias -- not a separate type.
So, an alias won't prevent errors like this:
using Name = string;
using Address = string;
class Person
{
public Person(int age, Address address, Name name)
{
}
}
var person = new Person(22, "Alice", "1 Microsoft Way"); // oops!
Note: as of C# 9, you can replace many use cases for Of<T> with record types:
record Address(string Value);
record Name(string Value);
Of<T> is not marked with ObsoleteAttribute because records have some limitations that classes do not have. Also, Of<T> provides easy JSON serialization and implicit conversion to its wrapped type, which records do not provide.
Examples
class Address : Of<string> {}
var address = new Address { Value = "1 Microsoft Way" };
Console.WriteLine(address); // prints "1 Microsoft Way"
var address2 = new Address { Value = "1 Microsoft Way" };
Console.WriteLine(address == address2); // prints "true"
You can add OfJsonAttribute so that the type is serialized
in the same was as the T
type:
using System.Text.Json;
[OfJson(typeof(JsonAddress), typeof(string))]
class JsonAddress : Of<string> {}
var jsonAddress = new JsonAddress { Value = "1 Microsoft Way" };
Console.WriteLine(JsonSerializer.Serialize(address)); // {"value":"1 Microsoft Way"}
Console.WriteLine(JsonSerializer.Serialize(jsonAddress)); // "1 Microsoft Way"
Properties
| Improve this Doc View SourceValue
The underlying instance of the wrapped type.
Declaration
public T Value { get; }
Property Value
Type | Description |
---|---|
T |
Methods
| Improve this Doc View SourceEquals(Of<T>)
Determines whether two instances of the type are equal.
Declaration
public bool Equals(Of<T> other)
Parameters
Type | Name | Description |
---|---|---|
Of<T> | other |
Returns
Type | Description |
---|---|
Boolean |
Remarks
Note that instances of two separate subtypes of Of<T> will compare equal to each other if their values are the same type and are equal.
Equals(Object)
Determines whether this instance is equal to another object.
Declaration
public override bool Equals(object obj)
Parameters
Type | Name | Description |
---|---|---|
Object | obj |
Returns
Type | Description |
---|---|
Boolean |
Overrides
| Improve this Doc View SourceGetHashCode()
Returns the hash code for the underlying object.
Declaration
public override int GetHashCode()
Returns
Type | Description |
---|---|
Int32 |
Overrides
| Improve this Doc View SourceTo<TOf>()
Declaration
public TOf To<TOf>()
where TOf : Of<T>, new()
Returns
Type | Description |
---|---|
TOf |
Type Parameters
Name | Description |
---|---|
TOf |
ToString()
Returns the string representation for the underlying object.
Declaration
public override string ToString()
Returns
Type | Description |
---|---|
String |
Overrides
Operators
| Improve this Doc View SourceEquality(Of<T>, Of<T>)
Determines whether two instances of the type are equal.
Declaration
public static bool operator ==(Of<T> lhs, Of<T> rhs)
Parameters
Type | Name | Description |
---|---|---|
Of<T> | lhs | |
Of<T> | rhs |
Returns
Type | Description |
---|---|
Boolean |
Implicit(Of<T> to T)
Converts an instance of Of<T> to its inner type T
.
Declaration
public static implicit operator T(Of<T> of)
Parameters
Type | Name | Description |
---|---|---|
Of<T> | of |
Returns
Type | Description |
---|---|
T |
Remarks
Of<T> is conceptually (though not in fact) a subtype of T
.
This conversion allows instances of Of<T> to work with methods out of the caller's control.
Inequality(Of<T>, Of<T>)
Determines whether two instances of the type are not equal.
Declaration
public static bool operator !=(Of<T> lhs, Of<T> rhs)
Parameters
Type | Name | Description |
---|---|---|
Of<T> | lhs | |
Of<T> | rhs |
Returns
Type | Description |
---|---|
Boolean |