C# Lambda - short spelling

Markus Freitag 3,786 Reputation points
2022-03-24T09:01:31.947+00:00

Hello,

How can I write the below code shorter, how to optimize? Maybe without function name GetDeviceWhiteParameterSet(); Directly.

? means can be null, right?

[XmlAttribute("length")]
public double? Length { get; set; }


private double _Width;
public double WidthRound     // **** not working with ? operator.   
{
 get { return _Width; }
 set { this._Width = Math.Round(value, 2); }
}


public string DeviceWhiteParameterSet => GetDeviceWhiteParameterSet();

public string GetDeviceWhiteParameterSet()
{
 string ret = "";
 if (DeviceWhiteParameterSet1.Length != 0)
 ret += DeviceWhiteParameterSet1;
 if (DeviceWhiteParameterSet2.Length != 0)
 ret += "|" + DeviceWhiteParameterSet2;

 return ret;
}



insertWhite.Parameters.AddWithValue("@Length", length ?? (object)DBNull.Value);

I need this for a database entry. ? operator.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,178 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,263 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,268 questions
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,036 Reputation points
    2022-03-24T10:08:17.003+00:00

    Try this

    private double? _Width;
    public double? WidthRound    
    {
        get => _Width;
        set
        {
            if (value != null) _Width = Math.Round(value.Value, 2);
        }
    }
    

0 additional answers

Sort by: Most helpful