Question

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!

Was it helpful?

Solution

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.

OTHER TIPS

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.

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";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top