Tutorial: C# Statements and DLL Files

So far we have a basic understanding of statements such as return within methods as well as booleans, but there’s lot we haven’t covered. Before we get into creating DLL files and using them in our programs, you should have a basic understanding of the most common statements in programming, especially .NET programming. Let’s take a look at these main few in specific for now:

  • If – Else: These are probably the most common and important statements in any programming language regardless of its nature. They’re used to execute a block of code only if a condition is true. Think of it like, “A store has 15 gallons of milk, if you have 8 dollars, buy two gallons.” It’s easiest to think of if statements as real life situations, as they do exactly what they sound like. 

Your condition is to be wrapped within parentheses, in the sense of something like (a == b) or ((a == b) || (b == c)) If your if statement only has one preceding line of code, you don’t have to wrap it in curly brackets, otherwise the entire code block has to be wrapped in quotes.

For example, either of these two things would work:

If the symbols don’t make sense right now, > means greater than, -= takes the value of the variable before it and then subtracts the variable that comes after it. == checks if two variables have the same value, || means or, letting us execute our if statement if one or another or multiple statements or true, whereas && means and, which lets the statement execute if multiple conditions are true. We’ll talk more about operators in another lesson, but for now you can take a look at a good list of programming operators here (all of which work in C#).

Now that we’ve covered if, let’s talk about else. The easiest way to think about else statements would be to think of the word otherwise. They’re only written after if statements (and else-if), and also follow the one-line rule with curly brackets, but for now or at least for example purposes I’ll demonstrate them with brackets. In the case of our grocery store question, think of it like “If you have 8 dollars, buy 2 gallons. Otherwise buy 1 gallon.”

To write that, let’s say this:

Lastly, you can attach if statements to else’s, also known as an else-if statement. These always should go in-between your starting if statement and your ending else statement, and are used especially for optimizing your program to be as efficient as possible by making a program only check one if block rather than multiple if blocks. With our example again, this would be sort of like: “If you have 8 dollars, buy 3 gallons. Otherwise, if you have 6 dollars, buy 2 gallons. Otherwise buy 1 gallon.”

And of course the code:

        Now let’s move on from if and else statements.

  • Try – Catch: A try-catch allows us to run code that will only fully execute if no error exceptions are thrown. If nothing runs, then you can run whatever is in your catch statement, as well as use it to display the error message. This is helpful especially for debugging programs, allowing you to display the exception rather than crashing the program at run-time. Typically, outside of debugging, they are usually used to run some code that you aren’t quite sure will work 100% of the time. The general syntax goes as follows:

If you want to use it for debugging/error-testing however, you would usually write them as so:

Loops: Using loop statements allows us to execute blocks of code multiple amounts of times, usually based on the length of something such as numbers in a data file, the size of a multidimensional array in which you would use nested loops, or just simply re-executing certain methods or events.

  • For: The first and probably most-used type of loop is everyone’s favorite, the for loop. For loops are set up in a way that uses the least amount of additional lines, whereas something such as a while loop would have to have multiple lines to set up our iteration. The easiest way to set them up is to use an int, something to count up to, and the method of counting. In a written sense, that would look something like (int i = 0; i < 10; i++). To explain, before the first semicolon, we’re creating a new int i that is set to 0, and while it’s less than 10, count up by 1. The reason why we preset i to 0 to count up to 10 is because it works in an index-based sense where it goes up to 10 but not including it. If you wanted to do a more natural counting style, you could also say (int i = 1; i <= 10; i++). You can also change the method of iteration/stepping as you would like, whether it be multiplying, modding, subtracting,dividing, etc. Just change the last part to something such as i+=2, i–, i*=8, i/=3, or i%=2.

        Now, to write that out:

  • While: A while loop allows a block of code to be executed repeatedly based on a given boolean condition, or using pre-constructed variables to be used in a way similar to a for loop.

In most cases, a general while loop is written in either of these two ways:

Or if we were to use numbers:

  • Do: A do-while loop is just a revised type of while loop that executes a block of code at least once, and then repeatedly executes the block, or not, depending on a given boolean condition at the end of the block. In written terms, however, their visual difference is that their conditions are written after their curly brackets, like so:

If you’d like a full list of C# statements, you can check them out here.

Now that you understand how to use statements, let’s make our first DLL utilizing them!

Basic Calculator

To start working on our first DLL, we need to create a Class Library project in Visual Studio.

  1. Start Visual Studio.
  2. On the menu bar, choose File, New, Project.
  3. The New Project dialog box opens.
  4. Expand Installed, expand Templates, expand Visual C#, and then choose Class Library.
  5. In the Name box, specify a name for your project, and then choose the OK button. For me, the project will be named CalculatorDLL.
  6. The new project appears in Solution Explorer.
  7. If Class1.cs isn’t open in the Code Editor, open the shortcut menu for Class1.cs in Solution Explorer, and then choose View Code.

VSdll

If you’re writing your code from scratch, or you’re unsure if you did everything right, your current starting code should look like this:

Now, for our calculator, let’s add 4 basic methods: add, subtract, multiply, and divide, all of which should return a double. They should take two doubles: a and b. Since our class isn’t going to be using private data, these methods can be static or not, but for now let’s just make them not static, and all 4 should be public. Make them return their respective function, and you’re good to go.

To rebrief, your methods should look as follows:

dll code

Now that we have all of our methods written, up at the top click Build, then hit Build CalculatorDLL. After the program is finished building, your newly generated DLL file will be sitting in the debug folder of the solution inside of the bin. (Should be something like “CalculatorDLL\CalculatorDLL\bin\Debug”)

Make up a new console application, call it whatever you’d like, and let’s add the new DLL to our references. To do so, in the Solution Explorer, right click References, click Add Reference, then click Browse, and then hit the Browse… button and navigate to the folder of your new DLL, which should be named CalculatorDLL.dll, then hit Add and OK.

Browse DLL

At the top of your new console application, up by your using statements, type using CalculatorDLL; and you’ve now imported all of our methods!

Once you’ve got everything set up, let’s test things out by presetting two doubles to use. These can be named anything of course, since they’ll be going into our methods to be printed off. Afterwards, we have to create a new class of our DLL, set up just as any other class, in the form Class1 Calc = new Class1(); where Class1 is the name of our main class in the namespace of our DLL. We can now call our methods like any other class method, like Calc.add(num1,num2); or Calc.multiply(num1,num2);.

To test each one of our methods, let’s try something like this:

Console output

Run your program and everything should work great! Since you now understand the basics of C#, you should be ready to start getting into real programs, which means in the next lesson we’ll dive into making Windows Form Programs! Which of course, we’ll implement our new DLL into, and soon some of our newly learned statements such as if statements and loops.