Notice that defining a floating-point number requires an explicit f letter after the number.
var keyword
C# supports var keyword - which means that you don't always have to explicitly specify a type - you can let the compiler try and understand the type of variable automatically. However, once the type of variable has been determined, it cannot be assigned a different type:
varx=1;vary=2;varsum=x+y;// sum will also be defined as an integer
Exercise
Define three variables:
A string named productName equal to TV
An int named productYear equal to 2012
A float named productPrice equal to 279.99f
Type Conversion
C# types are not the same! In some cases, you have to convert a value's type. There are two methods:
using System;
public class Program
{
public static void Main()
{
// write your code here
// Do not modify code below!
Console.WriteLine("productName: " + productName);
Console.WriteLine("productYear: " + productYear);
Console.WriteLine("productPrice: " + productPrice);
}
}
using System;
public class Program
{
public static void Main()
{
double myDBL = 99.0;
}
}