Some C# Practice
Let’s move beyond our “Hello, World” example from the introduction and explore a simple temperature conversion program. We want our function to convert a Fahrenheit temperature to Celsius.
To get started, fork and clone the csharp-web-dev-examples repository . This repo includes examples you should review as you read for the first 7 or so lessons.
TempConverterproject in your
csharp-web-development-examplessolution in Visual Studio.
Here’s what the file should look like. We will analyze the different statements in a moment.
1 2 3 4 5 6 7 8 9 10 11
double fahrenheit; double celsius; string input; Console.WriteLine("Temperature in F:"); input = Console.ReadLine(); fahrenheit = double.Parse(input); celsius = (fahrenheit - 32) * 5 / 9; Console.WriteLine("The Temperature in C is: " + celsius); Console.ReadLine();
Run the program to verify that it works. Entering a Fahrenheit temperature of
212yields the result,
The temperature in Celsius is: 100.
There are several new concepts introduced in this example. We will look at them in the following order:
- Variable declaration
using statement in C# allows us to access classes, methods, and
data stored in different files other than the one we are currently in.
In C#, you can use any class that is available without having to import the class - subject to two very important conditions:
- The C# compiler must know that the class exists.
- You must use the full name of the class.
Classes that are available to you may be those in the project you are currently working on, or those that come along with the .NET class library, as well as anything you might get from added dependencies.
The class naming system in C# is very hierarchical. The full name of the
class used first on line 13 is really
System.Console. You can think of this name as having
two parts. The first part,
System, is called the namespace, and
the last part is the class. We’ll talk more about the class naming
system a bit later.
One thing to know about the
using statement is that it is not responsible for loading classes into memory.
That task falls on the assembly, which is the unit of compiled code
created by Visual Studio (or the C# compiler, more generally).
using statement tells the compiler that we are going to use a
shortened version of the class’s name. In this example, we are going to
use the class
System.Console, but we can refer to it as just
Console. We could use the
System.Console class without any
problem and without any import statement provided that we always
referred to it by its full name.
Don’t just trust us, try it yourself! Remove the
using statement and
System.Console in the rest of the code. The
program should still compile and run.
In the example above, these lines contain variable declarations:
Specifically, we are saying that
celsius are going
to reference objects that are of type
double. The variable
will contain a string. This means that if we were to try an assignment
fahrenheit = "xyz" the compiler would generate an error because
"xyz" is a string and
fahrenheit is supposed to be a double.
Suppose we forgot the declaration for
celsius and instead
left that line blank. What would happen if we try to run our program?
We get a few errors! The end of the build output looks something like this:
Build FAILED. warning CS8600: Converting null literal or possible null value to non-nullable type. warning CS8604: Possible null reference argument for parameter 's' in 'double double.Parse(string s)'. error CS0103: The name 'celsius' does not exist in the current context error CS0103: The name 'celsius' does not exist in the current context 2 Warning(s) 2 Error(s) Time Elapsed 00:00:00.55 ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== Build: 2 errors, 2 warnings
The compiler detects an error and Visual Studio displays this message. Visual Studio will notify you of the errors detected in a few locations, including the errors pane and the Application Output pane.
You may have expected to receive an error from some red in your program file. When using an IDE such as Visual Studio, your code is typically checked by the IDE’s built-in compiler as you write your code. Thus, errors are usually visually indicated within your code by the IDE as you write your code, saving you the extra step of having to explicitly compile your code before finding compiler errors. Nice, huh?
The general rule in C# is that you must decide what kind of an object your variable is going to reference and then you must declare that variable before you use it. There is much more to say about the static typing of C#, but for now this is enough.
Input / Output and the Console Class
Console input and output is facilitated by the class
We’ll rely heavily on just two methods of this class:
Console.WriteLine can take parameters of various types, including
bool, and others.
Console.WriteLine can only
be provided a single argument. Thus, we’ll need to manually concatenate strings and other
values if we want to print a composite value, converting types if necessary. A newline
character is output after the given message.
int year = 2020; Console.WriteLine("Hello" + "World") Console.WriteLine("The year is " + year.ToString());
Console.ReadLine returns input as a string. To convert it
to a desired type, you can generally use the syntax
[TYPE] replaced by the given type.
Here’s an example:
string userInput = Console.ReadLine(); int year = int.Parse(userInput);
Add Comments to Your Code
As programs get bigger and more complicated, they get more difficult to read. Good programmers try to make their code understandable to others, but it is still tricky to look at a large program and figure out what it is doing and why.
Also, there are times when programmers need to isolate or ignore certain portions of their code as they are testing it. In the “Try It” box above, you were instructed to remove a line of code in order to create compiler errors. However, programmers are usually reluctant to delete lines that they might need to bring back.
Best practice encourages us to add comments to our programs. Comments are notes that clearly explain what the code is doing.
A comment is text within a program intended only for a human reader—–it is
completely ignored by the compiler or interpreter. In C#, the
indicates the start of a comment, and the rest of the line gets ignored. For
comments that stretch over multiple lines, the text falls between the symbols
Comments can be used to temporarily skip a portion of the code when a
program runs. Instead of removing
double celsius; in
could comment out the line. This would create the same compiler errors we
wanted to witness, but it would preserve the original code and allow us to
easily reactivate it by removing the
// token from the line.
In addition to single-line comments and multi-line comments, C# developers can also use XML documentation comments.
XML documentation comments start with
/// and when properly written, can be used to generate documentation for a project.
This comment type is something you may encounter working on enterprise code for a company, but is not something that you would use in smaller, personal projects.
For more information on XML documentation comments, check out the
Check Your Understanding
True or False, a
using statement is required to use a C# class defined outside of your current file.\
What is the name of the method used to convert input strings to different types?