True or False.
int
is the C# alias for
the .NET Int32
type. They can
be used interchangeably.
All except the last, which requires an explicit cast:
int newInt = (int) myLong;
A uint
is an unsigned
int
and can hold only
positive numbers, but it can hold a positive value twice as big
as an int
(4 million+ rather
than 2 million+).
A float
takes four
bytes and a double
takes
eight bytes, and thus a double
can represent much larger
values with greater precision.
In C, if you wish to use a variable of any type (pass it as a parameter to a method), you must assign it a value.
You refer to the constant like this:
Temperatures.LightJacketWeather
Its value is 33.
Any statement that evaluates to a value.
Write a short program creating and initializing each of
the following types of variables: int
, float
, double
, char
, and then outputting the values
to the console.
namespace fundamentals { class exercise { static void Main( ) { int myInt = 3; float myFloat = 4.25f; double myDouble = 123456789.9867; char myChar = 'A'; System.Console.WriteLine("myInt: {0}, myFloat: {1}, myDouble: {2}, myChar: {3}.", myInt, myFloat, myDouble, myChar); } } }
The output should look like this:
myInt: 3, myFloat: 4.25, myDouble: 123456789.9876, myChar: A.
Modify the program in Exercise 3-1 to change the values of variables and output the values to the console a second time:
namespace fundamentals { class exercise { static void Main( ) { // round one int myInt = 3; float myFloat = 4.25f; double myDouble = 123456789.9867; char myChar = 'A'; System.Console.WriteLine("Round 1: myInt: {0}, myFloat: {1}, myDouble: {2}, myChar: {3}.", myInt, myFloat, myDouble, myChar); // round two myInt = 5; float myFloat = 25.267f; myDouble = 987654321.1234; myChar = 'Z'; System.Console.WriteLine("Round 2: myInt: {0}, myFloat: {1}, myDouble: {2}, myChar: {3}.", myInt, myFloat, myDouble, myChar); } } }
The output should look like this:
Round 1: myInt: 3, myFloat: 4.25, myDouble: 123456789.9876, myChar: A. Round 2: myInt: 5, myFloat: 25.267, myDouble: 987654321.1234, myChar: Z.
Modify the program in Exercise 3-2 to declare a constant
float Pi
equal to 3.14159.
Then assign a new value to pi (3.1) and output its value with
the other variables statement. What happens when you try to
compile this program?
namespace fundamentals { class exercise { static void Main() { const float pi = 3.14159f; int myInt = 3; float myFloat = 4.25f; double myDouble = 123456789.9867; char myChar = 'A'; pi = 3.1f; System.Console.WriteLine("Round 1: myInt: {0}, myFloat: {1}, myDouble: {2}, myChar: {3}. Pi: {4}", myInt, myFloat, myDouble, myChar, pi); } } }
This program won’t compile, because you’re trying to assign a value to a constant. Instead, you receive a compiler error that reads, “The left-hand side of an assignment must be a variable, property or indexer.”