There are over ten thousand jobs listed in the Clearance Jobs database for software developers. If you are looking for a career change, each one of those listings is an opportunity to start over. It might seem like an intimidating leap, but here is the thing: It’s a lot easier to learn a programming language than it is to get a security clearance, which means if you are reading this, you’ve done the hard part already.

There are a number of great resources online for learning any programming language. In this four part series, ClearanceJobs will introduce you to software development. We will discuss the tools you need – all of which are free – and get into basic programming concepts. Once you get the software installed and begin experimenting, you can reach any level of ability you desire. It’s just a matter of putting in the hours.

This is part 2 of our light introduction to the C# programming language. This series is written for absolute beginners. Please check out part 1 here before proceeding. It will teach you the basic structure of a C# program and how to compile and run your code. Done? Great – let’s get started.

WHAT ARE VARIABLES?

Before we get started, let’s bring our Hello, World program back into focus.

using System;

public class MyFirstProgram
{
        static public void Main ()
        {
               Console.WriteLine ("hello, world");
        }
}

We will add a few things to it today that give us a slightly better understanding of what we can do with C#. The first thing we will consider are variables. There are quote a few options in C#, but we will stick with four for now: integers, doubles, characters, and strings.

  • Integers are whole numbers, as in: 1, 2, 3, 4, 4573, 9362546 and so on. They are identified in C# with the type int.
  • Doubles—technically, “double-precision floating-points”—are decimals, such as 2.3, 67.9, 3.141592653989, and so on. They are identified in C# with the type double.
  • Characters are just that: letters, numbers, and special characters that are to be treated as such. A, b, c, R, W, and z are characters. So, too, are 3, 5, ^ and +. In the case of the numbers, though: When they are treated as characters, they cannot be added or subtracted. Likewise, the special characters. You cannot use them in any way other than generic text on the screen. (If you are familiar with Excel, you’ve seen this before. Cells designated “text” can’t be used in mathematics. They’re just… text. Characters are identified in C# with the type char.
  • Strings are… a string of characters. As in: “Hello, world!” Strings are a bit more sophisticated than something like int, but we won’t go into that in this introduction series. Strings of characters are identified as string.

Let’s try them out in our sample program.

using System;

public class MySecondProgram
{
        static public void Main ()
        {
               string Foo = "hello, world"; 
               Console.WriteLine ( Foo );
        }
}

Save your code as stringtest.cs and compile it. (In the previous entry in the series, we discussed how to save files and compile your code. Feel free going forward to use any filename.cs you would like.) If you get any errors when compiling your code, go back and make sure you remembered the quotation marks and the semicolon after that new line of code. Note also that the quotation marks are vertical:  (as opposed to the “curly” quotation marks). Once your code compiles cleanly, let’s run the program we just wrote:

csc stringtest.cs

Press <enter>.

mono stringtest.exe

Your terminal should read: hello, world

Just like the first program we wrote! So let’s talk a little about the code.

string Foo = "hello, world"; 

Console.WriteLine (Foo);

Foo is called a variable. We could have called it anything. X. StringTestThing. HiMomHowAreYou. The compiler doesn’t care what you name variables as long as you don’t include any special characters in them. (For example: A1 is a perfectly fine variable name. A+1 is not.)

When we write string Foo in that line, we are creating a variable of the string type. When we follow it with = “hello, world” we are defining the value of Foo.

Let’s try another one.

using System;
public class MyThirdProgram
{
        static public void Main ()
        {
               int a = 5;
               Console.WriteLine ( a );
        }
}

Save your code, compile it, and run it, and you should get an output of…you guessed it: 5

SMOOTH OPERATOR

Obviously there isn’t much value in writing single numbers or phrases to the terminal. So let’s learn about something called operators, which let us do things with our variables.

(Remember: in the code int a, the type is int and a is the variable. int a is creating a variable to hold integers.)

You’ve already seen one operator: =, the “equal” sign. It was performing an operation on a: namely, giving it a numerical value. So let’s look at a few others: +, , *, and /. You should know what each does intuitively: addition, subtraction, multiplication, and division. Keep in mind that when your compiler does math for the equations you type, it follows the same order of operations as you learned in elementary school. You might remember “My Dear Aunt Sally”— multiplication, division, addition, subtraction. In other words, 1+3*6=19 and does not equal 24.

So let’s try some operators in a program.

using System;
public class MyFourthProgram
{
        static public void Main ()
        {
               int a = 5; 
               int b = 10;
               int c = a + b;
               
               Console.WriteLine ( c );
        }
}

This program should print the number 15 on the screen. When you write a + b, you aren’t adding the letters of the alphabet, but the values contained by the variables a and b.

You’ll notice that we wrote:

int c = a + b;

We didn’t have to do it that way. We could have created the integer c and only later given it a value. Like this:

using System;
public class MyFifthProgram
{
        static public void Main ()
        {
               int a = 5; 
               int b = 10;
               int c;

               c = a + b;

               Console.WriteLine ( c );
        }
}

You should get the same output. If you get errors, be sure to check for problems with the semicolons, spelling, and braces. They’ll get you every time. So if you are clear on all we’ve done so far, let’s start combining things. (If you aren’t clear, go back and try again. It’s best to get the basics down before things really get messy.

using System;
public class MySixthProgram
{
        static public void Main ()
        {
               int a = 5; 
               int b = 10;
               int c;

               c = a + b;

               Console.WriteLine ( "Hello, world! We just added two numbers to get " + c );
        }
}

WHOA! You are probably wondering: What in the world is going on in that WriteLine method? Just like in our very first program, we told the computer to write the words that are in quotation marks. So far so good. By appending + c inside of the parenthesis—by feeding the method WriteLine a little more information—we have told the compiler to print that string of characters, and on the same line also print the value of c. You can really get elaborate with that. Check this out, but don’t let it overwhelm you:

using System;
public class MySeventhProgram
{
        static public void Main ()
        {
               int a = 5; 
               int b = 10;
               int c;

               c = a + b;

               Console.WriteLine ( "Hello, world! We just added " + a + "and " + b + "to get " + c );
        }
}

In this case, we told the compiler to print a string of characters, and then print the value of a, and then print more characters, and then b, and then more characters, and then c. The output should read:

Hello, world! We just added 5 and 10 to get 15

Note that depending on the width of your monitor, the line of code that begins “Console…” might have spilled over into the next line. That’s okay! Generally speaking, the compiler isn’t worried about white space in your code. Rather, it’s looking for a semicolon to tell it when a line of code is completed.

 

Congratulations! Today you’ve gotten quite a bit of practice compiling and running your code, you’ve been introduced to variables and operators, and you have even learned how to output slightly sophisticated sentences to the user. In the next installment, we will look at conditional statements and inputs. As always, if you have questions please post them in the comments or email me here. See you next time!

Related News

David Brown is a regular contributor to ClearanceJobs. His most recent book, THE MISSION (Custom House, 2021), is now available in bookstores everywhere in hardcover and paperback. He can be found online at https://www.dwb.io.