question

MarkusFreitag-0088 avatar image
0 Votes"
MarkusFreitag-0088 asked karenpayneoregon edited

C# Lambda - short spelling

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.

dotnet-csharpdotnet-aspnet-core-generaldotnet-aspnet-mvc
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered karenpayneoregon edited

Try this

 private double? _Width;
 public double? WidthRound    
 {
     get => _Width;
     set
     {
         if (value != null) _Width = Math.Round(value.Value, 2);
     }
 }
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks works well.

Second question.

  public string DeviceWhiteParameterSet => GetDeviceWhiteParameterSet();

Need I here a function or can I write it shorter?




0 Votes 0 ·

I'm not seeing room for improvement. I will say that one should always weigh short code against readable code.

0 Votes 0 ·

     public string TestParameterSet2 => { return "Test"; }


I have tried this without function name. I thought maybe must go.
With => I don't need get; set; -- Isn't it?

0 Votes 0 ·
Show more comments