Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# check value at design time

public static class PositiveInt32Extensions
{
    public static Boolean IsPositive( this Int32 candidate, out PositiveInt32 value ) => PositiveInt32.TryCreate( candidate, out value );
}

public readonly struct PositiveInt32
{
    public static Boolean TryCreate( Int32 candidate, out PositiveInt32  value )
    {
        if( candidate > 0 )
        {
            value = new PositiveInt32( candidate );
            return true;
        }
        else
        {
            value = default;
            return false;
        }
    }

    private readonly Int32 value;

    public PositiveInt32( Int32 value )
    {
        if( value < 1 ) throw new ArgumentOutOfRangeException( nameof(value), actualValue: value, message: "Value must be positive." );
        this.value = value;
    }

    public static implicit operator Int32( PositiveInt32 self ) => self.value;

    // NOTE: This implicit conversion will fail when `unsignedValue  > UInt32.MaxValue / 2`, but I assume that will never happen.
    public static implicit operator PositiveInt32 ( UInt32 unsignedValue ) => new PositiveInt32( (Int32)unsignedValue );
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: mvc remote validation additional table 
Csharp :: mongodb truncation exception c# 
Csharp :: pause and resume thread C# 
Csharp :: psobject get service name 
Csharp :: autho close in persian time picker 
Csharp :: how to navigate between page in wpf 
Csharp :: unity ar scale 
Csharp :: asp:button onclick not respond 
Csharp :: how to declare variables in c# 
Csharp :: handle multiple threads c# 
Csharp :: aps.net core mvc chek box 
Csharp :: how to make dissapear an object in unity 
Csharp :: vb.net delete line from text file 
Csharp :: .net framework method 
Csharp :: c# loop datatable column names convert to list 
Csharp :: C# check control type 
Csharp :: .net new template 
Csharp :: rename join table in many to many 
Csharp :: how to check that a gameobject touches a colour in unity c# 
Csharp :: mental retardation 
Csharp :: c# catch multiple exceptions at once 
Csharp :: c# md5 hash bouncycastle encypt decrypt with key 
Csharp :: button next for picturebox c# 
Csharp :: c# .net core 3.0 trying Exception The transaction log for database is full due to ACTIVE_TRANSACTION 
Csharp :: C# Create Swiss QR-Bill API 
Csharp :: accord.io read .mat file 
Csharp :: c# how to output array 
Csharp :: get current culture in controller asp.net core 
Csharp :: DrawImage resize to target size c# 
Csharp :: close windows by esc wpf 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =