1

Here is my snippet:

var country = BLLocations.Instance.GetCountries();
ddlCountry.DataSource = 
ddlCountry.DataTextField = "Country";
ddlCountry.DataValueField = "CountryCode";
ddlCountry.DataBind();

See the second line:

ddlCountry.DataSource = 

And it compiled successfully and published to cloud also. Strange!

gdoron
  • 147,333
  • 58
  • 291
  • 367
Moons
  • 3,833
  • 4
  • 49
  • 82

3 Answers3

10

It's simply this:

ddlCountry.DataSource =  ddlCountry.DataTextField = "Country";

The line break doesn't effect, Which is a valid code.

Just like:

var x = 2;
var y = 3;
x = y = 1000;

(Note this is really not good practice at all! it's confusing and hard to disgust)

Every code in C# returns a value(though the value can be void) which lets you do this lazy loading:

return x ?? x = new ExpensiveObject();//

What it does:

  1. If x is not null returns x.
  2. If x is null assigns x new ExpensiveObject() an returns the assignment value - x.

Helpful feature but be careful with it.

gdoron
  • 147,333
  • 58
  • 291
  • 367
2

It is acceptable, just like when you can have:

a = b = c;

I am suprised there isn't a warning though due to types. ddlCountry.DataSource and ddlCountry.DataTextField would have to be the same type I think.

The compiler ignores white space in this instance as it is syntactically insignificant. More specifically this is done by the pre processor.

ddoor
  • 5,819
  • 9
  • 34
  • 41
0

In C#, like with many other languages, statements are separated by ; characters, not by new line characters. Thus writing :

var someString =


"SomeValue";

Is perfectly fine.

Your snippet does the same as

ddlCountry.DataSource = "Country";
ddlCountry.DataTextField = "Country";
Yanick Rochon
  • 51,409
  • 25
  • 133
  • 214