Home > front end >  ternary conditional operator If Else C#
ternary conditional operator If Else C#

Time:12-06

i wrote this code:

        if (B_21_GDV_Variant.RowCount > 0)
        {
            metroContextMenuStrip1.Enabled = true;
            B_21_GDV_Variant.AllowDeleting = false;
            B_21_GDV_Variant.Columns["HB"].Format = "{0:N}";
            B_21_GDV_Variant.Columns["HB"].CellStyle.HorizontalAlignment = HorizontalAlignment.Left;
        }
        else
        {
            metroContextMenuStrip1.Enabled = false;
            B_21_GDV_Variant.AllowDeleting = true;
        }

I just want to know, how to write it using ternary conditional operator ?

CodePudding user response:

In general, it may not be recommended in this context, but it's still possible. Not only ternary conditional operator, but also using direct boolean values to set. This is:

metroContextMenuStrip1.Enabled = (B_21_GDV_Variant.RowCount > 0);
B_21_GDV_Variant.AllowDeleting = !(B_21_GDV_Variant.RowCount > 0);
B_21_GDV_Variant.Columns["HB"].Format = (B_21_GDV_Variant.RowCount > 0) ? "{0:N}" : B_21_GDV_Variant.Columns["HB"].Format;
B_21_GDV_Variant.Columns["HB"].CellStyle.HorizontalAlignment = (B_21_GDV_Variant.RowCount > 0) ? HorizontalAlignment.Left : B_21_GDV_Variant.Columns["HB"].CellStyle.HorizontalAlignment;

I would not recommend using the ternary conditional operator in this case since it does not help with readability and in general using it in this case will not generate any performance improvement.

CodePudding user response:

If you wanted to force the use of ternary and you wanted some semblance of readability, then I'd suggest this:

var data = 
    B_21_GDV_Variant.RowCount > 0
    ? (
        Enabled: true,
        AllowDeleting: false,
        Format: "{0:N}",
        HorizontalAlignment: HorizontalAlignment.Left
    )
    : (
        Enabled: false,
        AllowDeleting: true,
        Format: B_21_GDV_Variant.Columns["HB"].Format,
        HorizontalAlignment: B_21_GDV_Variant.Columns["HB"].CellStyle.HorizontalAlignment
    );

metroContextMenuStrip1.Enabled = data.Enabled;
B_21_GDV_Variant.AllowDeleting = data.AllowDeleting;
B_21_GDV_Variant.Columns["HB"].Format = data.Format;
B_21_GDV_Variant.Columns["HB"].CellStyle.HorizontalAlignment = data.HorizontalAlignment;

CodePudding user response:

The closest form would be this:

_ = (B_21_GDV_Variant.RowCount > 0)
    ? new Func<bool>(() => 
    {
        metroContextMenuStrip1.Enabled = true;
        B_21_GDV_Variant.AllowDeleting = false;
        B_21_GDV_Variant.Columns["HB"].Format = "{0:N}";
        B_21_GDV_Variant.Columns["HB"].CellStyle.HorizontalAlignment = HorizontalAlignment.Left;
        return true;
    })()
    : new Func<bool>(() => 
    {
        metroContextMenuStrip1.Enabled = false;
        B_21_GDV_Variant.AllowDeleting = true;
        return false;
    })();

That is, the ternary operator must return a value, which is here discarded (see the underscore).

The solution to wrap more operations is to enclose them in a delegate (Func in this case), then immediately executed (see the parens pair at the very end of each block).

Technically feasible, but I honestly don't see any convenience in doing that.

  • Related