Avatar of John500
John500
Flag for United States of America asked on

How does C# allow for blank assignments? MyVariable = <-------- compiles

Greetings:

I'm reviewing code written by another developer and I've noticed blocks of code that will have no value/object to the right of the variable that needs assignment, like:

if (TSMIAllPasswords.Checked || _searching)
            {
                    bool localro = _password.IsReadOnly;
                    TBDomain.ReadOnly =
                    TBUsername.ReadOnly =
                    TBNotes.ReadOnly =
                    TBPassword.ReadOnly = localro;
                    DTPExpire.Enabled = !localro && _password.Expire;
                    BGenerate.Enabled =
                    CBExpire.Enabled = !localro;
                }
                else
                {
                    TBDomain.ReadOnly =
                    TBUsername.ReadOnly =
                    TBNotes.ReadOnly =
                    TBPassword.ReadOnly = _sectionRO;
                    DTPExpire.Enabled =
                    BGenerate.Enabled =
                    CBBExpire.Enabled = _sectionRO;
                }
            }

How is it that this compiles??  During execution I certainly get an error that
C#

Avatar of undefined
Last Comment
joesthebighmoe

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
mastoo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
silemone

they are not blank assignments...they are saying a = b = c = "ssfds;
silemone

so in essence they all have the same value until you reach the ; (semicolon)...
silemone

equates to:

a = b = c = d = "hello";

shorthand for
a="hello";
b="hello";
.
.
.
d= "hello";
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Arthur_Wood

Just to make it clear, in C#, a line will AUTOMATICALLY be continued until a line termination character (;) is encountered.  Thus

TBDomain.ReadOnly =
TBUsername.ReadOnly =
TBNotes.ReadOnly =
TBPassword.ReadOnly = localro;

is EXACTLY the same as:

TBDomain.ReadOnly = TBUsername.ReadOnly = TBNotes.ReadOnly =        TBPassword.ReadOnly = localro;


the white space is ignored, by the C# compiler.

AW
joesthebighmoe

They are continuations, not blank.  
Oh wait, that is what mastoo already said 20 minutes ago.