Some Tricks for C# Developers

In this section we will recall some tricks and techniques in programming with C#, already seen in this book, which can be very useful if you go to an exam for beginner programming.

Inserting Variables in Strings

var text = "some text";
Console.WriteLine("{0}", text);
// This will print on the console "some text"

In this case we are using a placeholder - {x}, where x is a number (larger than or equal to 0), corresponding to the position on which we have placed our variable. Therefore if we insert two variables, we will have one placeholder, which will be {0} and it will keep the value of the first variable and another one - {1}, which will keep the value of на the second variable. For example:

var text = "some text";
var number = 5;
Console.WriteLine("{0} {1} {0}", text, number);
// This will print "some text 5 some text"

In this example we can see that we can insert not only text variables. We can also use a given variable several times and for this we put the number which corresponds with the position of the variable in the placeholder. In this case on position zero is the variable text, and at first position is the variable number. In the beginning the numbering can be confusing, but you need to remember that in programming counting starts from 0.

Formatting with 2 Decimal Places

var number = 5.432432;
Console.WriteLine(Math.Round(number, 2));
// This will print on the console "5.43"

Math.Round(…) takes 2 parameters:

  • the first one is the number we want to round
  • the second is the number which determines to how much decimal places we want to round (this should always be an integer)

If we want to round the number to 2 decimal places and the third digit is lower than 5, as in the example above, the rounding is down, but if the third digit is equal or bigger than 5 - the rounding is up as in the example below:

var number = 5.439;
Console.WriteLine(Math.Round(number, 2));
// This will print on the console "5.44"

Other Rounding Methods

In case we always want to round down instead of Math.Round(…) we can use another method - Math.Floor(…), which always rounds down, but also always rounds to an integer. For example if we have the number 5.99 and we use Math.Floor(5.99), we will get the number 5.

We can also do the exact opposite - to always round up using the method Math.Ceiling(…). Again if we have for example 5.11 and we use Math.Ceiling(5.11), we will get 6. Here are some examples:

var numberToFloor = 5.99;
// This will print on the console 5

var numberToCeiling = 5.11;
// This will print on the console 6

Rounding with a Placeholder

var num = 5.432424;
Console.WriteLine("{0:f2}", num);

In this case after the number we add :f2, which will limit the number to 2 decimal places and will work like Math.Round(…). You should keep in mind that the number after the letter f means to how many decimal places the number is rounded (i.e. it can be f3 or f5).

How to Write a Conditional Statement?

The conditional if construction contains the following elements:

  • Keyword if
  • A boolean expression (condition)
  • Body of the conditional construction
  • Optional: else clause
if (condition)
    // body
else (condition)
    // body

To make it easier we can use a code snippet for an if construction:

  • if + [Tab] + [Tab]

How to Write a 'for' Loop?

For a for loop we need a couple of things:

  • Initializing block, in which the counting variable is declared (var i) and its initial value is set.
  • Condition for repetition (i <= 10).
  • Renewing the counter (i++).
  • Body of the loop.
for (var i = 0; i < 5; i++;)
    // body

To make it easier we can use a code snippet for a for loop:

  • for + [Tab] + [Tab]

results matching ""

    No results matching ""