Say I have generic value wrapper:
class Wrapper<T>
{
public T Value { get; set; }
}
And I'd like to use it as listed below (or something like this):
var wrapper = new Wrapper<Target>();
// ...
Target target = wrapper;
How is it possible using implicit operator, extension method or another approach to provide single-place null checking? As one of benefits I want to keep the following code in the single place:
if (wrapper != null)
{
return wrapper.Value;
}
return default(T);
EDIT:
I tried the operator:
public static implicit operator T(Wrapper<T> wrapper)
{
return wrapper.Value ? default(T) : wrapper.Value;
}
It's perfectly works I the way below:
var wrapper = new Wrapper<long>();
long value = wrapper;
But fails on:
var wrapper = new Wrapper<IEnumerable<long>>();
IEnumerable<long> value = wrapper;
Compiler says it is not possible to convert from Wrapper<IEnumerable<long>> to IEnumerable<long>. Any ideas?
You can use an implicit operator:
public static implicit operator T(Wrapper<T> value)
{
return value == null ? default(T) : value.Value;
}
EDIT:
As #ChrisSinclair has pointed out, this doesn't work when T is interface the only solution a can think of is using an extension method like:
public static T SafeGetValue<T>(this Wrapper<T> value)
{
return value == null ? default(T) : value.Value;
}
And then:
IEnumerable<long> value = wrapper.SafeGetValue();
return wrapper.Value ?? default(T);
The null-checking could be done in the generic wrapper itself as follows.
class Wrapper<T>
{
private T _value;
public T Value
{
get
{
return _value ?? default(T);
}
set
{
_value = value;
};
}
}
Related
How to convert struct to nullable struct if the value is default value. In the code below the EqualityComparer<T>.Default.Equals return false when variable of type int has zero value
public static Nullable<T> ConvertToNullIfDefault<T>(this T src) where T : struct
{
if (EqualityComparer<T>.Default.Equals(src))
{
return null;
}
return (Nullable<T>)src;
}
Test
[Fact]
public void ConvertToNullIfDefault_ReturnsNull_WhenIntegerIsDefault()
{
var val = default(int);
var result = val.ConvertToNullIfDefault();
Assert.Null(result);
}
A better approach is to use the IEquatable<T> constraint:
public static Nullable<T> ConvertToNullIfDefault<T>(this T src)
where T : struct, IEquatable<T>
{
if( src.Equals( default(T) ) )
{
return null;
}
return (Nullable<T>)src;
}
private struct Maybe<T>
{
private readonly T value;
private readonly bool hasValue;
private Maybe(T value)
{
this.value = value;
hasValue = true;
}
public static implicit operator Maybe<T>(T value) =>
value == null ? new Maybe<T>() : new Maybe<T>(value);
}
private static Maybe<byte> OK()
{
return 5;
}
private static Maybe<IEnumerable<byte>> NotOK()
{
var e = new[] { 1, 2, 3 }.Select(x => (byte)x);
Console.WriteLine(e.GetType().Name);
return e;
}
Fiddle (don't use): https://dotnetfiddle.net/NxAw9l
Updated fiddle: https://dotnetfiddle.net/NrARTl
Some generic type is failing for implicit conversion at above code. See the Ok() and NotOk() function calls and return types. A complex generic type is failing and I dont' understand why. I have simplified this from a function of a return type of IEnumerable<IEnumerable<T>>. This IEnumerable<T> still fails. I think If I can understand why this fails, I'd solve the real one too I suppose. Thanks for your help and time.
Here is the error message if you'd like:
Error CS0029 Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<byte>' to 'Maybe<System.Collections.Generic.IEnumerable<byte>>'
Update: Returning Byte[] from the NotOK() can't work because in my real source code I have a LINQ query I have to depend on its lazy-deferred execution (i.e It has to be strictly returning IEnumerable) (see alike answer => https://stackoverflow.com/a/63880804/5917087).
The C# standard currently does not allow implicit conversions from or to interfaces.
This is a well-known problem when implementing a Maybe<T> (or Optional<T>, as it is often called) type in C#. There is an ongoing discussion about this on the C# language github forum:
https://github.com/dotnet/roslyn/issues/14186
As a workaround, you could make the Maybe<T> constructor internal and add a static non-generic helper class:
private static class Maybe
{
public static Maybe<T> From<T>(T value) =>
value == null ? new Maybe<T>() : new Maybe<T>(value);
}
which allows you to use type inference and write Maybe.From(a), which is a bit shorter than new Maybe<IEnumerable<byte>>(a).
I'm going to extend #Heinzi's answer:
You could also use extension methods:
static class MaybeExtensions
{
public static Maybe<T> AsMaybe<T>(this T value)
{
return new Maybe<T>(value);
}
public static Maybe<TResult> AsMaybe<T, TResult>(this T value)
where T : unmanaged
where TResult : unmanaged
{
return new Maybe<TResult>(Unsafe.As<T, TResult>(ref value));
}
}
And in your caller methods, you could use them like:
private static Maybe<IEnumerable<byte>> NotOK()
{
var e = new[] { 1, 2, 3 }.Select(x => (byte)x);
return e.AsMaybe();
}
private static Maybe<byte> OK()
{
return 5.AsMaybe<int, byte>();
}
// Alternatively
private static Maybe<byte> OK()
{
return ((byte)5).AsMaybe();
}
You need the AsMaybe<T, TResult> overload for value types that can convert to each other. For example when you do 5.AsMaybe() it returns Maybe<int>, if your method's return type is Maybe<byte> you will need to convert Maybe<int> to Maybe<byte>, and the overload does that for you.
Now, the type conversion operator in Maybe<T> becomes redundant. And you can use var instead of full type name:
Maybe<int> obj1 = 5; // use operator
var obj2 = 5.AsMaybe(); // use extension method
You can't define a conversion to/from an interface type, If you change your example to use List<T> rather than IEnumerable<T> it will compile - https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/conversions#user-defined-conversions
Change this :
private static Maybe<IEnumerable<byte>> NotOK()
{
IEnumerable<byte> a = new byte[] { 1, 2 };
return a;
}
into this :
private static Maybe<IEnumerable<byte>> NotOK()
{
var a = new byte[] { 1, 2 };
return a;
}
The struct :
private struct Maybe<T>
{
private readonly T value;
private readonly bool hasValue;
private Maybe(T value)
{
this.value = value;
hasValue = true;
}
public static implicit operator Maybe<T>(T value)
{
return value == null ? new Maybe<T>() : new Maybe<T>(value);
}
public bool HasValue(){
return this.hasValue;
}
public T GetValue(){
return this.value;
}
}
private static Maybe<byte> OK()
{
return 5;
}
private static Maybe<IEnumerable<byte>> NotOK()
{
Byte[] a = new byte[] { 1, 2 };
Console.WriteLine(a.GetType().Name);
return a;
}
Usage :
public static void Main(string[] args){
var t1 = OK();
var t2 = NotOK();
Console.WriteLine("t1 type is " + t1.GetType().Name);
Console.WriteLine("t2 type is " + t2.GetType().Name);
if(t2.HasValue())
{
List<byte> search = t2.GetValue().Where(b => b > 0).ToList();
foreach(byte num in search){
Console.WriteLine(num);
}
}
}
The reference IEnumerable<byte> a doesn't change the type, you can continue to var or byte[] and the query with LINQ, after, see in the full example
See the full example :
https://dotnetfiddle.net/V8RHQe
IEnumerable is an interface. Compiler does not know which type to work with. Put ToList() to end of your select like follows :
private static Maybe<IEnumerable<byte>> NotOK()
{
var e = new[] { 1, 2, 3 }.Select(x => (byte)x).ToList();
Console.WriteLine(e.GetType().Name);
return e;
}
To understand what is happening try to create a method like follow in your class and watch compiler to cry :)
public static implicit operator Maybe<IEnumerable<T>>(IEnumerable<T> value)
{
return value == null ? new Maybe<IEnumerable<T>>() : new Maybe<IEnumerable<T>>(value);
}
Given the code:
public class Filter<T>
{
private bool selected = false;
public bool Selected { get { return selected; } }
private T value;
public T Value { get{ return this.value; } set { this.value = value; selected = true; }
}
public class Test
{
public void filter()
{
DateTime a= new DateTime();
Nullable<DateTime> b = new DateTime(); //Work Like a Charm
Filter<DateTime> c = new DateTime(); //Dosent Work
}
}
In Nullable<T> the new DateTime() can be assigned directly into the variable. In my class, it doesn't work. I want to understand what I'm missing.
I think that is something simple. But I couldn't put it on words to find the answer.
You have to implement implicit operators:
public static implicit operator Filter<T>(T value)
{
return new Filter<T>() { Value = value };
}
An implicit operator will allow you to cast the types without explicitly writing Filter<T> filter = (Filter<T>)value; (explicit cast), but rather only Filter<T> filter = value; (implicit cast).
You need to use an implict conversion operator:
See: Implicit cast operator and the equality operator
This allows you to write code to construct your custom type from another type.
Is there a way to implement a generic implicit or explicit converter for anything to an array of anything, something like this:
public static implicit operator T[](T objToConvert)
{
return new T[] { objToConvert };
}
No. The closest I can think of is an extension method:
public static T[] AsArray<T>(this T instance)
{
return new T[]{instance};
}
Use as:
var myArray = myInstnace.AsArray();
Note that you can omit the type name from the array constructor, which means the syntax is fairly clean, even with a long type name:
ReallyLongAndAwkwardTypeName value;
MethodThatTakesArray(new[] {value});
Operator overloading methods have to live inside the class they are overriding operators for (one side or the other). Since "T" is not defined, I don't see how this can be accomplished.
You can do it using normal method:
public static T[] ToArray<T>(T objToConvert) {
return new T[] { objToConvert };
}
I don't think you can define generics operator. Note, anyway, that the compiler is sufficient cleaver to guess the type of the generic param, so you can use:
var aString="";
var aStringArray=ToArray(aString);
aStringArray is defined as a string array even if you don't specify the generic param.
I was trying to think of situations where you might really use an implicit conversion to array. I started to wonder if many of the situations where you would want to do this could be alleviated by use of the params keyword.
The main situation that I could think of was that you had a single item of something and wanted to pass it to a function that takes an array as a parameter:
static void Main(string[] args)
{
string x = "I'm just a poor variable. Nobody loves me.";
Stickler.IOnlyTakeArrays_Rawr111(x); // won't go in! square peg, round hole, etc.
// *sigh* fine.
Stickler.IOnlyTakeArrays_Rawr111(new[] { x });
}
class Stickler
{
public static void IOnlyTakeArrays_Rawr111(string[] yum)
{
// ...
}
}
Hopefully in this situation the author of the method that you want to call has choosen to use the params keyword to allow you to pass your variable without wrapping it in an array:
class DataConcierge
{
public static T Create<T>(int id)
{
// ...
}
public static void Save<T>(params T[] items)
{
// ...
}
}
static void Main(string[] args)
{
var customer = DataConcierge.Create<Customer>(123);
// ...
DataConcierge.Save(customer); // this works!
//----------------------------------------------------
// or
//----------------------------------------------------
var customers = new Customer[]
{
DataConcierge.Create<Customer>(123),
DataConcierge.Create<Customer>(234),
DataConcierge.Create<Customer>(345),
};
// ...
DataConcierge.Save(customers); // this works too!
}
Of course, this doesn't really help you in situations where you need convert a variable to a single item array but not as a parameter to a method or in situations where the author of the method didn't use the params keyword.
But what kind of situation would the former be? Assigning an array to a property? Psh. How often does that happen?
And the latter? If the author didn't use the params keyword when they could have, then send them an email complaining about it. If the author is yourself, feel free to be extra belligerent in the email.
Hopefully you can tell that I'm being facetious. Seriously, though, are there any other common usage situations that you can think of where the params keyword would not be applicable?
** Disclaimer: I don't advocate excessive use of the params keyword. Use it if you think you should, but don't take my post to mean that you should always use the params keyword whenever you can.
In the past I've used the concept of a "Conductor" (my own name for it), which is just a class/struct that provides access to an underlying value.
The concept is useful for abstracting the access to a particular value retrieved from somewhere. For example, if you wanted to abstract access to a particular value in a dictionary, you could create a Conductor object that held a reference to the dictionary and the appropriate key for that value. You can also use this concept to easily implement rollback for serializable classes or for value types, though for that you'd need to add Rollback and Commit methods to the Conductor class/struct.
Below is an example of how you can use implicit conversions from T to Conductor and from Conductor to T[] in order to (sort of) achieve what you want.
static void Main(string[] args)
{
// implicit conversion here from Customer to Conductor<Customer>
Conductor<Customer> conductor = DataConcierge.Create<Customer>(123);
if (conductor.HasValue)
{
Console.WriteLine("I got a customer with Id {0}!", conductor.Value.Id);
// implicit conversion here from Conductor<Customer> to Customer[]
DataConcierge.Save<Customer>(conductor);
}
}
public struct Conductor<T> : IConductor<T>, IEquatable<T>, IEquatable<Conductor<T>>, IEquatable<IConductor<T>>
{
private T _Value;
public Conductor(T value)
{
this._Value = value;
}
public T Value
{
get { return this._Value; }
set { this._Value = value; }
}
public bool HasValue
{
get { return this._Value != null; }
}
public T GetValueOrDefault()
{
if (this.HasValue)
return this.Value;
else
return default(T);
}
public T GetValueOrDefault(T #default)
{
if (this.HasValue)
return this.Value;
else
return #default;
}
public bool TryGetValue(out T value)
{
if (this.HasValue)
{
value = this.Value;
return true;
}
else
{
value = default(T);
return false;
}
}
public T[] AsArray()
{
return new T[] { this._Value };
}
public static implicit operator Conductor<T>(T value)
{
return new Conductor<T>(value);
}
public static implicit operator T(Conductor<T> conductor)
{
return conductor.Value;
}
public static implicit operator T[](Conductor<T> conductor)
{
return conductor.AsArray();
}
public bool Equals(T other)
{
var otherEquatable = other as IEquatable<T>;
if (otherEquatable != null)
return otherEquatable.Equals(this.Value);
else
return object.Equals(this.Value, other);
}
public bool Equals(Conductor<T> other)
{
if (other.HasValue)
return this.Equals(other.Value);
else
return !this.HasValue;
}
public bool Equals(IConductor<T> other)
{
if (other != null && other.HasValue)
return this.Equals(other.Value);
else
return !this.HasValue;
}
public override bool Equals(object obj)
{
if (obj == null)
return !this.HasValue;
var conductor = obj as IConductor<T>;
if (conductor != null)
{
if (conductor.HasValue)
return this.Equals(conductor.Value);
else
return !this.HasValue;
}
return object.Equals(this.Value, obj);
}
public override int GetHashCode()
{
if (this.HasValue)
return this.Value.GetHashCode();
else
return 0;
}
public override string ToString()
{
if (this.HasValue)
return this.Value.ToString();
else
return null;
}
}
So I have the following snippet of code:
private Nullable<decimal> _excessWages;
public decimal ExcessWages
{
get
{
return _excessWages ?? CalculateExcessWages();
}
set
{
if (value != CalculateExcessWages())
_excessWages = value;
else
_excessWages = null;
}
}
So basically the behavior I'm trying to implement is if a field is left blank or is assigned a value equal the calculated one use the calculated value, otherwise store the assigned value.
I have a lot of fields that need to support overriding like this. Is this the best way to accomplish this? If not what would you suggest?
I worked on this a bit based mostly on Vlad's suggestion. Turns out you can use a single generic class to abstract this. Here is the end result:
public class Overridable<T>
{
private Func<T> _calculate;
private readonly Func<T, T, bool> _compare;
protected T _t;
public Overridable(Func<T> calculate, Func<T, T, bool> compare)
{
_calculate = calculate;
_compare = compare;
}
public T Value
{
get { return _compare(_t, default(T)) ? _calculate() : _t; }
set { _t = _compare(value, _calculate()) ? default(T) : value; }
}
}
You need to pass in a compare delegate because the type isn't known until you set it in a subclass. So a simple == isn't going to cut it. I went the easy route and used a Func delegate but this could be replaced with a normal delegate if it had to be adapted for .NET 2.0 for some reason.
You'll notice I'm using default(T) instead of null. This works because the default value for a Nullable<T> is null (or more precisely, undefined but it works out to be the same).
This doesn't prevent you from trying to declare an Overridable<T> for a non-nullable type. What you'd wind up with won't through run time errors but it isn't as useful. Trying to set a Overridable<decimal>.Value to null will get you a compiler error. While setting it to default(decimal) will cause it to revert to calculating the value.
I went this route because the properties from this the class I'm using this in needs to populate a serializable object thats eventually transmitted as xml. The schema for the xml includes numeric fields defined as a mixture of integers, decimals and strings.
You then use the Overriddable class like so:
private Overridable<decimal?> _excessWages =
new Overridable<decimal?>(CalculateExcessWages, (x,y) => x == y);
public virtual decimal? ExcessWages
{
get
{
return _excessWages.Value;
}
set
{
_excessWages.Value = value;
}
}
The only problem I ran into with this was that CalculateExcessWages is a non-static method so it can't be used in a field initializer. Since all the properties in my class are non-static I had to initialize all the backing fields in the constructor.
You can make a class wrapper for this.
class OverridableValue<T>
{
public OverridableValue<T>(Func<T> calculator)
{
_calculator = calculator;
}
private Nullable<T> _t;
private Func<T> _calculator;
public T Get()
{
return return _t ?? _calculator();
}
public void Set(T value)
{
_t = (value != _calculator()) ? value : null;
}
}
It's not so syntactically sweet, but at least saves some keystrokes.
Now you can use it like this:
class Foo
{
OverridableValue<decimal> _excessWages =
new OverridableValue<decimal>(CalculateExcessWages);
public decimal ExcessWages
{
get { return _excessWages.Get(); }
set { _excessWages.Set(value); }
}
...
}
The advantage is that the whole logic is hidden at the class.
You could do this by defining a handy set/get helper method
private static T GetUtil<T>(ref Nullable<T> value, Func<T> calc) {
return value ?? calc();
}
private static void SetUtil<T>(ref Nullable<T> value, T newValue, Func<T> calc) {
if ( newValue != calc() ) {
value = newValue
} else {
value = null;
}
}
private Nullable<decimal> _excessWages;
public decimal ExcessWages
{
get { return GetUtil(ref _excessWages, CalculateExcessWages); }
set { SetUtil(ref _excessWages, value CalculateExcessWages); }
}
That looks reasonable to my eyes. The only change I might make is to cache CalculateExcessWages(), if it is expensive to do, and ok to cache:
private Nullable<decimal> _excessWages;
private Nullable<decimal> _excessWagesCalculated;
public virtual decimal ExcessWages
{
get
{
if (_excessWagesCalculated == null)
_excessWagesCalculated = CalculateExcessWages();
return _excessWages ?? _excessWagesCalculated;
}
set
{
if (_excessWagesCalculated == null)
_excessWagesCalculated = CalculateExcessWages();
if (value != _excessWagesCalculated)
_excessWages = value;
else
_excessWages = null;
}
}
But, this is more code than yours, and I think you are looking to simplify.