Store & Retrieve Data using Literal & Variable Values in C#
Char Literal
A literal value, is a hard-coded value. It is known as a constant. This value never changes.
Console.WriteLine('b');
This creates a char literal, as the ‘b’ is surrounded in single quotes. The term char = character.
The above code will produce the following Output in your IDE:
*** Using double quotes around “b”, would create a string data type.
The char literal data type is only used when you have a single alphanumeric character for presentation & not for calculation.
Therefore, the following code would present an error if placed in single quotations.
Console.WriteLine('Jesus is God');
The error message would look similar to this:
That same code would NOT produce an error if placed in double quotations, as it would be producing a string data type, which can display more than one alphanumeric character.
For example:
Console.WriteLine("Jesus is God");
Should produce the following Output on your IDE:
Integer Literals
Int Literals display numeric whole numbers in the output console. It is important these whole numbers are proper integers, and contain NO fractions.
An int literal requires no single or double quotation operators like the char & string data types did.
Console.WriteLine(777);
The above code should produce the following Output in your IDE:
Again, notice how the 777 did not contain single OR double quotation marks. The int literal does not require those operators.
So, what do we do with numbers that contain fractions?
Floating-Point Literals
A floating-point number contains decimals.
The C# language has support for three data types that represent decimal numbers (floating-point numbers):
- (1) float
- (2) double
- (3) decimal
Each of these floating-point data types offer varying degrees of precision. Precision, in this context, “reflects the number of digits past the decimal that are accurate.”
Float Type | Precision | Example |
float | ~6-9 digits past the decimal | Console.WriteLine(0.28F); Console.WriteLine(0.28f); *, ** |
double | ~15-17 digits past the decimal | Console.WriteLine(3.1255); ***, **** |
decimal | 28-29 digits past the decimal | Console.WriteLine(13.39816691m); OR Console.WriteLine(13.39816691M); ***** |
* Either a capitalized F or a lower-case f can be utilized for float.
**Since float is the least precise, it’s best to use it for fixed fractional values to avoid unexpected computation errors.
***A double literal does NOT require a literal suffix, like the float & decimal data types do.
****The compiler defaults to a double literal when a decimal number is entered without a literal suffix.
*****You can use either a lower-case “m” or upper-case “M” as the literal suffix for the decimal data type.
What about a situation that requires a value representing either true or false ?
Boolean Literals
Bool is short for Boolean. The bool datatype is used to print values representing either true or false.
The bool datatype will help add decision logic to applications & expressions to see whether the expression is true or false.
In the .NET Editor web-browser IDE, the input code looks like this:
The bool datatype requires no operators like single or double quotations. The output in your IDE should look similar to this:
Does C# Enforce Data Types?
Yes.
According to Microsoft C# documentation, the language designers of C# believed that data type enforcement helps developers avoid common software bugs in their programs.
C# is a strongly typed language.
EVERY variable & constant has a type, as well as, every expression that evaluates to a value.
Every method declaration specifics (1) a name, (2) the type, and (3) kind [value/reference/output] for each input parameter & for the return value.
The information stored in a type can include:
- The storage space that a variable of the type requires.
- The max & min values that it can represent.
- The members (methods/fields/events/etc) that it contains.
- The base type that it inherits from.
- The interface it implements.
- The operations that are permitted.
The compiler uses this type information to ensure the operations in your code are type-safe. The type information is embedded by the compiler into an executable file as metadata, that is utilized by the common language runtime (CLR) to check at run-time, “when it allocates & reclaims memory.”
Declaring Variables
To recap: hard-coded values (literals) are constant & unchanged throughout the execution of the program.
Variables are containers for storing a type of value, which can change/vary throughout the execution of the program. This value is typically stored in a memory address that the compiler assigns.
Variables can be assigned, read, and changed.
When data will come from a user, across a network, or from files, using a variable may be a good idea.
To declare/create a new variable, you need to declare the data type of that variable & give the variable a name. The data type you declare the variable with, will help the compiler decide what values that container can hold.
For example:
string lastName;
From now on, the variable “lastName” can only store string values.
Leave a Reply