Sahat Yalkabov
Sahat Yalkabov

Reputation: 33694

What does this line of C# code do?

int hour = DateTime.Now.Hour; 
ViewData["greeting"] = (hour < 12 ? "Good morning" : "Good afternoon"); 

Sorry for the noob question, but the ASP.NET MVC book I'm currently reading assumes that I already know C# (but I don't).

I understand the first part - it assigns current date and time to the hour variable. But I am lost on the 2nd line. What are those <, ?, : symbols for? ViewData["greeting"] is this an array of some sorts?

Thanks!

Upvotes: 2

Views: 1840

Answers (10)

Joel Coehoorn
Joel Coehoorn

Reputation: 416131

I understand the first part - it assigns current date and time to the hour variable.

That's not quite right. Let's analyze each part of the right hand side of the expression:

DateTime - This is a type (class) used for storing and operating on values that have a date and time component.

Now - This is a static member of the DateTime type. That means you don't use a particular instance to access it, but rather the name of the type itself. This static member returns a new DateTime instance populated with the current date and time. Note that once created this instance does not change; a few lines of code later it is already out of date.

Hour - This returns a number indicating the Hour portion of a DateTime instance in 24 hour format (0 is midnight, 13 is 1pm).

So, taken together, this returns a number representing the current hour. Combine this with the other answers explaining the conditional and less than operators, and the code should make a little more sense. If the hour is greater than 12, it's after noon.

That still leaves the ViewData[]. ViewData is a special kind of Dictionary type (key/value pairs indexed by key) used for passing data from an MVC controller to an MVC view.

Upvotes: 4

xenonite
xenonite

Reputation: 1671

well im not good in c#, but its pretty clear what this code does:

the first line gets the current hour. meaning: if its 7 o clock, the value of the varialble "hour" will be set to 7:

int hour = DateTime.Now.Hour;

"ViewData" is apparently an array holding some String data. thevalue for index "greeting" will be set to "Good morning", if it is before 12 o clock, to "Good afternoon" elsewhen...

ViewData["greeting"] = (hour < 12 ? "Good morning" : "Good afternoon"); 

to be exact,

ViewData["greeting"] = (hour < 12 ? "Good morning" : "Good afternoon"); 

is equal to

if(hour < 12)
    ViewData["greeting"] = "Good morning"; 
else
    ViewData["greeting"] = "Good afternoon"; 

if this is easier to understand...

Upvotes: 1

kamasheto
kamasheto

Reputation: 1030

The ? is called the conditional operator.

It acts as an if-then-else statement, only difference is that it actually returns something upon evaluation.

Take this simple example:

// ex 1
if (condition)
    name = "Female";
else
    name = "Male";

// ex 2
name = condition ? "Female" : "Male";

These two examples are identical. The conditional operator consists of two clauses (other than the condition), one before and one after the colon : -- the before is evaluated on condition == true, and the after is evaluated on condition == false.

The < is simply an operator that checks whether the left hand side is less than the right hand side. 1 < 2 == true, whereas 3 < 2 == false.

ViewData["greeting"] is just the item with index greeting in the array ViewData.

The whole code simply assigns a greeting message based on the current time.

EDIT: As noted above, int hour = DateTime.Now.Hour; assigns the current hour (0-23) to the integer variable hour

Upvotes: 9

Muad&#39;Dib
Muad&#39;Dib

Reputation: 29256

the < symbol is the good 'ol boy from math, known as "Less Then" the ? together with the : is what we call the "Ternary Operator" or "Conditional Operator" basically, its like an inline if-then-else statment.

if ( 5 > 2 ) 
   dosomething();
else
  dosomethingelse();

could be written as

(5 > 2 ) ? dosomething() : dosomethingelse();

so, what this code is doing is getting the current hour according to the clock, checking to see if it is before 12pm (aka noon), and assigning a greeting accordingly.

Upvotes: 1

spinon
spinon

Reputation: 10857

Just to answer the other part of your question you asked what the symbols were: < = less than (so is hour less than 12) ?: = ternary (http://en.wikipedia.org/wiki/Ternary_operation) basically saying if the if statement (hour < 12) = true then use value after question makr, if not use value after colon.

Upvotes: 1

Mitch Lindgren
Mitch Lindgren

Reputation: 2170

Larsenal's answer is correct. More generally, ? ... : ... is a shorthand notation for if in C based languages. It can detract from readability, but the nice thing about it is that you can use it after an assignment operator, as in your example.

Upvotes: 1

Jesse Jashinsky
Jesse Jashinsky

Reputation: 10683

It's another form of "if else." The statement on the left of the question mark stands as the if statement, and ViewData["greeting"] is assigned the value on the left of the ":" if it's true, and the value to the right of ":" if it's false.

Upvotes: 1

hkon
hkon

Reputation: 1045

it is a short version of an if statement.

if (hour < 12)
    ViewData["Greeting"] = "Good Morning";
else
    ViewData["Greeting"] = "Good afternoon";

Upvotes: 5

Andy Robinson
Andy Robinson

Reputation: 7439

Basically saying if hour < 12 use "Good Morning" otherwise use "Good Afternoon" - like an inline if statement.

Upvotes: 2

Larsenal
Larsenal

Reputation: 51196

If the hour is less than 12, ViewDate["greeting"] is assigned a value of "Good morning". Otherwise, it is assigned a value of "Good afternoon".

The ?: bit is a conditional operator. MSDN gives a good description:

The conditional operator (?:) returns one of two values depending on the value of a Boolean expression.

Upvotes: 18

Related Questions