Basics in Programming #1 (C#): Setup/Basics (Variables & Functions)

Hello and welcome to my set of tutorials for programming in C#. Ideally I’d like to make this a series going over different C# topics from the raw basics to more advanced stuff as we progress. So for this section, it’s assuming you know nothing about programming. We’re gonna go over variables, loops, etc. I will have a focus on games in these tutorials, however a lot of this is general programming knowledge, and you’ll be able to use it outside of games.

You may ask, “Why Cory, are you writing another programming tutorial? There are tons of tutorials out there, by people far more qualified than you.” Well reader, I’m making these tutorials, because I don’t like a lot of the tutorials out there. Also, I just want to write some C# tutorials, which is a good enough reason for me. I also feel like a lot of tutorials out there can be very hand holding, and I want to focus on making you think more, as much as I can anyway.

Also keep in mind going through this tutorial, programming takes a lot of googling and not knowing what you’re doing. If you get confused or stuck, that’s okay and super normal. You just need to keep googling and trying to find the answer, if you’ve already been googling for a while, and you’ve really tried, it’s always okay to ask for help. I’m not going to always go into detail about every possible subject, and that’s on purpose. You should get used to googling how to do something, or what something is. I’m gonna go ahead and bold anything that might be good to google.

So, we’re gonna start with the greatest platform of all for our programming exercises: command prompt. Why command prompt, and not something like Unity or some other engine you may ask. Well I believe trying to learn programming and an engine at the same time is a terrible idea. For one, engines normally have a lot of specific code to use that engine, if you’re learning to program in that engine only, you might be dependent on it. Also, you need to learn to walk before you start to fly, or something like that; you need to understand the basics of coding and architecture before you start trying to make a game.  Do note, while you can use any Integrated Development Environment (IDE), or operating system you want, I’ll be using Visual Studio and Windows 10. If you want to follow along, you’ll need the following:

  • Visual Studio 2015 or 2017 Community Edition (It’s free yo)
  • An internet connection with access to google, which I assume you have. You’re reading this after all.
  • Lots of snacks, chips, cookies, whatever fills your programming appetite.
  • Water, or some other beverage. Gotta stay hydrated.

Alright, lets get started.

Setup

First you’ll want to start a new console (console == command prompt) project, so open up visual studio. You’ll want to go to File > New > Project, on the left side you may see many available options for different languages. You’ll want to find the C# section, and within that choose Console Application, be sure to give it a name and press the okay button.

Capture

Now this should generate some code for you, we’ll touch briefly on what it is, but nothing in depth for now. We’ll go over the details of what it is and how it works later. So at the very top it should have many lines that say using System; among others. These are basically taking code already written for C# and giving it to us to use, it will add functionality to write text to the console screen, read, and do other things. There are many different using statements you can add, which give you different forms of functionality. For example using System.IO adds functionality for saving and loading data.

Next we have namespace YourProject, this is just one way to organize your code. You can put code under namespaces, so when someone wants to access your code they need the namespace. For example, that using System? System is a namespace! Crazy right?

Next we have Class Program, and we’ll get into classes later on, but it’s where your code resides and lays its head down at night. We can put functions and code inside our class, and use it as a container to protect our code.

Following that we have static void Main(string[] args), so this is actually a function. But it’s a very special function, for a console application this is where our code starts and ends. When our program runs, it goes and calls this function. We’ll learn about functions in a second.

Basics

Well, now it’s time to learn.

Variables

First, lets have a chat about variables. What the hell are they? Who knows? Well you will in a second, hopefully. Variables are the data of your program. If you wanted to make a program that counted every time you hit the space bar, that number that you’re tracking and increasing every time you hit space is a variable. If you want to check if something is true or false, you can store that information in a variable. Variables are the noodles to your spaghetti. (Stay with me here)

Now there are different types of variables, tons and tons of them. And you don’t need to know about them all, you’ll learn that through experience and as you need them. But there are some basic types you should know.

  • int – These are basically whole numbers, there are also alternatives called ​short and long which are like ints, except have different amounts of bits, shorts have less while longs have more. (Examples: 1, 2, 5, 1000)
  • float – Also numbers, however these don’t need to be whole. You normally preface these with a f. These also have alternatives as a decimal and a double. (Examples: 0.1f, 0.5f, 1.5f, 0.75f)
  • bool – These are true or false values, not much to explain here.
  • string – This is text! Like what you’re reading now. (Examples: “Hello”, “How’s it going?”)
  • char – This is just a single character. (Examples: ‘a’, ‘B’, ‘c’)
  • Many many many other types you will absolutely never memorize, and you’ll need to google them as you need them. Good luck.

Now, you want to define a variable huh? Think you’re ready for that? Well if you want to make a variable, declare its type, give it a name, and then you can set its value. Do note, names for your variables have rules, for example you cannot start a variable name with a number. You’ll also get errors trying to name a variable the same as another variable.

Example:

int age = 5;
age = 21;

Functions

Now functions, these are pretty cool, and you’ll definitely use them. Well probably, if you’re any good. We can use functions to run code in another location that we give a name and parameters. Let’s say you need to use the Pythagorean theorem, well if you need to use that multiple times you don’t want to write it out manually every single time. That’s a waste of time, and messy to use; instead we can use functions, like so:

float PythagoreanTheorem(float a, float b)
{
    float aSquared = a*a;
    float bSquared = b*b;
    return aSquared + bSquared;
}

Let’s take a look at this. The first part at the beginning, right before we name our function, that’s our return type. Functions don’t just have to call code, they can return code to you for you to use. In this case it would be cSquared. Then of course, we name our function, and inside the parenthesis we have our parameters. These are variables we can pass into the function for it to use. In this case to use in the formula. You’ll notice we can return aSquared + bSquared, that return is what is actually telling the function what to return, note the code will stop here. No code you write after the return is going to run. Note, if you don’t need your function to return anything, just use void as the return type.

Something I was always taught, was that if you have two pieces of code that are the same, then you should make a function for it. You may notice in our function something else that could be put into a function, I’ll let you figure that one out.

Now to actually call our function all you do is:

float cSquared = PythagoreanTheorem(3,3);

Naming Conventions

Now a quick note on the naming of variables and functions. There are tons of standards out there, Microsoft standards, standards individual for each company, you might even set your own standards for how you enjoy to code. There are even different naming conventions based on what programming language you’re using. Most common styles of writing variables and functions is either between PascalCase and camelCase. If you’re a bit unsure about how you want to name stuff, you can refer to Microsoft’s .NET Guidelines.

So… How Do I Use This?

Congrats! You’re all done! This is all you need to know about programming. Ha.

So now you know about some different variable types, and you know how you can change and manipulate these using functions. Isn’t that great? Well now you should do something with them. You could have the user input two numbers, and you could display the result of that Pythagorean Theorem function. You could make a simple text adventure, the options are limitless; in my first month of programming I made a platformer in console.

Now how do you actually write info to console, and read info from the user? I’m not going to outright tell you, that’s for you to figure out. However I will give you some launching points, you’re going to want to google the functions Console.WriteLine() and Console.ReadLine(). You should be able to find the Microsoft Developer Network (MSDN) page on those functions, which also provide examples on how to use them.

Extra Practice

You haven’t had enough yet? Good on you! If you’re looking for a more structured practice than just messing around (though that is still an excellence way to learn), I’ve made this simple word document with a programming challenge inside. It covers some of the basic concepts we went over, and also forces you to google more and learn outside of what I’ve taught. This is completely optional of course, and none of the future tutorials will use anything specific from this. But I do suggest it.

File: CSharpTutorials1

Next Time

We’re going over a lot of general knowledge, about different important keywords! Check it out here.

0 comments on “Basics in Programming #1 (C#): Setup/Basics (Variables & Functions)Add yours →

Leave a Reply

Your email address will not be published. Required fields are marked *