Hamid Noahdi
Hamid Noahdi

Reputation: 1645

How can I have Nullable<T>

    public interface IOperationResult
    {
        bool IsSuccessful { get; set; }
        public string Message { get; set; }
    }

    public interface ICreateCommandOperationResult<T?> : IOperationResult
    {
        public T? InsertedId { get; set; }
    }

When I insert a record in database, I want to return inserted record's Id. But sometimes there are exceptions that in this case I need to send null in InsertedId.

How can I do it?

Upvotes: 0

Views: 95

Answers (1)

Pavel Anikhouski
Pavel Anikhouski

Reputation: 23208

You can apply the where T: struct generic constraint (or class if you are using C# 8 and need a nullable reference types) and replace T? with just T in ICreateCommandOperationResult interface.

public interface ICreateCommandOperationResult<T> : IOperationResult
    where T : struct //where T : class
{
    public T? InsertedId { get; set; }
}

The example of implementation

public class Test : ICreateCommandOperationResult<int>
{
    public bool IsSuccessful { get; set; }
    public string Message { get; set; }
    public int? InsertedId { get; set; }
}

Upvotes: 1

Related Questions