MonthJanuary 2016


Methods are used to for plenty of reasons:

  • Cleaner code
  • Code re-usability
  • Easier maintenance
  • Code readability


How does method actually look like?

Method consists of method signature and method body.


Method signature signature always look like this:

  • Access modifier
  • Return type or void
  • Method name
  • Parameters



And After method signature we have

  • Opening curly brackets
  • Method body/our logic
  • Closing curly brackets



Let’s dissect this method:


Our method could be void, meaning that method will not return any value.


Of course, our method can also be parameter-less, contain multiple parameters or with different access modifiers. We could also have multiple methods with same name but will have to have different parameters or at-least different order of same parameters.



So far we have covered few basic operators and now would be good time to expand our knowledge of them. All of the operators will be tested using if statements:

Certainly there are some more operators, but we will explain them as we go!


Classes are your blueprints. Inside classes you place all your data types and logic. Class can be recognized by class keyword. Multiple classes can exist inside one namespace. So far you have probably heard of an object, object is an instance of an class, and many objects can be created from one class. As mentioned in previous post we know that class is an reference type.

How do we create class?


We said that „Inside classes you place all your data types and logic“. Ok… so how can we use it? First, we start by creating our custom class which will have an method that will print full name in console window, then we will call an instance of that class from our second class and supply method from there with parameters.

I hope you learned something from this article and that you will visit tomorrow when we will continue with operators.

C# Reference types & Value types

In C# universe we know of two kind of types: value types and reference types. When dealing with value types, one variable points to one value, two variables can have the same value but changing value of one variable will not change value of second value. Two or more variables of reference types can contain same value, but when you change value of one variable all other variables are changed too. Values of value types are stored on stack (size of heap is 1Mb), and reference types values are stored on heap (size limitation is 2GB on 32bit system, and pretty-much unlimited on 64bit systems). Stack is designed to be fast and revolves around first in last out principle, we store value types inside stack for fast access and when we do not expect object to live long. Heap, looking at it’s size, is designed to hold bigger variables and variables inside heap can survive longer, meaning we can access it for longer periods of time.
So…how will I know which type is value or reference type? It’s really simple actually. By looking at the type’s name.

Value types Reference types
bool class
byte interface
char delegate
decimal object
double string
enum class

In next post first we will cover classes (our second reference type) and then cover life-cycle of both value types and reference types.

String Part 2


A substring is any sequence of characters that is contained in a string.  We use Substring method when we want to create a new string that is based on original string. Replace method is used when we want to change parts of original string and create new one. IndexOf is used when we want to locate first occurrence of certain substring in string.


How to create new string using Multiple existing strings?

That process is called concatenation and all it basically does is append one string at the end of another string. In next example we will create 3 string and concatenate them. For this example Gangsta Lorem Ipsum is used.

As you can see new string is created using simple addition. Notice on third operation ” “, we have created a new string with only one blank space. Why? Because when we create new string we have to add it ourselves. Look at the end of first string, you can see that there is empty space at the end, so we did not have to add between firstString and secondString .


That’s all for now regarding string in future we will talk some more about them.

Tomorrow we will talk about reference types so be sure co check in again!


String Part 1

Strings are one of the most useful types in .Net, and since you’ll be using it almost every day, let’s see what we can learn about String.

MSDN: A string is a sequential collection of Unicode characters that is used to represent text.

Strings are a reference type but in some ways it behaves like a value type. For instance, String is immutable, meaning you cannot change content of a string.

Strings can be null.

You can compare two strings.

Maximum size of a string is 2Gb.

Common Operations with Strings

Strings are hard-coded with literals. There are two types of literals: “regular literal” and “verbatim literal”.

If you try running a program, the output will be the same. So how do we know which type of literal to use? When you have a simple text that can be written in one line and does not contain escape characters, you will typically use regular expressions.  Verbatim literals are for example useful when you write SQL code.

Let’s try out some common operations with strings using the following well-known sentence:  The quick brown fox jumps over the lazy dog.

\n –  newline

\” – double quotes

\’ – single quote

\\ – backslash


Trim text

A very common operation, and all it does is basically remove empty space at the beginning, end or both. Remember, because strings are immutable, String itself will not be changed.


How to Check if String Is Empty

All the scenarios above bring about the same result. But there are a few things to be aware of. Length method is the fastest one, both Length and Empty methods create the same code in IL, but in my opinion, the last approach is the easiest one to read and it also checks the strings for null.


Thank you for following me so far and be sure to check in tomorrow because in the next post we will continue covering Strings.


Value Types Part 2 – Enumeration

Through this article we will talk about what are enum (enumeration) , how and where we can use it. The enum is a keyword which we use when we want to associate some constant values with a name. Enumeration are strongly typed and appear inside IntelliSense and therefore save a lot of time 🙂  By default enumeration are zero based so first value is equal to 0 and each following is incremented by one. You can change starting and all other values to your liking, but remember that all subsequent values will also be incremented starting of last custom value. Enough of theory, lets jump to examples.

Example 1

Lets say we want to print to Console numerical day of the week.

There is nothing wrong with this example. But, what if workweek starts on some other day? You will have to change all values. How can we avoid this happening to us? We can use enums!

We declared enum with enum keyword, then we gave it a meaningful name, then inside curly brackets we wrote days of the week. To display numeric value I have explicitly changed value to int with (int). Also order of days is slightly change and Sunday is now on the first place so you should get changed output when you run the program.

Example 2

What if I want to give custom value to my enums? It’s easily accomplished. We will us our previous example.

As you can see Sunday has a value 1 and next three days have incremented by 1, but when we got to Thursday its value changed to 8, Friday got incremented to 9.  In Saturday case we set its value to Thursday + Friday and got output of 17 just to show you that option as well.


That covers enums. Stay tuned for more!




Value Types Part 1

In this post we will cover some basic value types that we use in our everyday work, such as int, double,and bool.
Before we begin with data types you should be familiar with variables. Variables are nothing more than name given to a memory that our programs can manipulate. Value types store values inside variables that are stored in side stack. Stack and heap will be covered in post where we will compare Value and Reference types. For now, all you need to know is that every value type variable has its value stored in its own place inside memory, and when you assign value from one value type to another you create copy of that variable.
By now you must have heard that you must Assign variable, Initialize variable and declare variable. What do these terms stand for?

First of, variable can be initialized

Secondly, variable can be declared

Lastly, variable can be assigned value

Basic mathematical operations with value types

(Addition, subtraction, multiplication, and division)

Be warned that it is recommended to use same data types  when performing these kind of operations, soon you will find why.


Lets start by adding two integers together.

Try running this code and result will be “3”.

Now try changing secondNumber value to 2.2, you will get compiler warning “Cannot implicitly convert type ‘double’ to ‘int’.” Following actions can be performed depending on your desired outcome:

I want my result to be an integer:

Difference between these two approaches is that first one will always throw Exception but second one will throw exception only if value is greater than Int32.MaxValue (2,147,483,647) or less than Int32.MinValue (-2,147,483,648).

I want my result to be an double

Subtraction, multiplication, and division

Same as addition, but instead of “+” operator we use “-“, “*” and “/” operators.

Thank you for following me this far! Our next lesson will be Eumerations.



HelloWorld is typically  first application that young programmer writes so we will write one as well.


Lets start analysis our code bottoms up.

Console – we use Console class that resides in System namespace to access method WriteLine

WriteLine – is a method which we use to display desired output to our console application

(“Hello World!”) – is an input parameter of Write line method. If you take look at  this link you will see every possible overload type that WriteLine can take. In this specific example we are overloading WriteLine method with string.  Strings will be covered in their own post.

public – public keyword is an access modifier.

static – we will discuss static keyword in latter post.

void – void is a return type. Every method has to have one. Void keyword is used to specify that this method doesn’t return a value. Return types will be covered in later post when we will discuss Methods.

Main() – represents Method that takes no input parameters.

Class is an important subject and will be covered in future post along with namespaces.

Comments can be found all around any code an is used to remind user (programmer) what some line of code/method/class/etc means or how to use it. Comments can be created by putting two upper slashes (//) and will comment only one line of code. Alternative way of creating a comment is by using following syntax:

For now all you need to know is that this line of code is used to access Console class and that using keyword in this context is called using directive and will be covered in future post.

Try running your code. Depending how fast your computer you will briefly see black console (command prompt). Why doesn’t it stay opened? You could run the program with Ctrl+F5 to run it without the debugger attached but that does not solve our problem. Put breakpoint on line “Console.WriteLine(“Hello World!”); ” and run program. Now we can see our console application with HelloWorld message. Jump into your Visual Studio and press F10 key twice. And you will see that our program has continued to curly brackets. What can we do to allow our user to decide freely when our program will stop executing? Well we could add following line beneath “Console.WriteLine(“Hello World!”); “

Run again program and you will see “HelloWorld!” printed inside Console application, and application will exit when we press any key.

Hooray, we wrote our first application using C#!

© 2021 LearnC#WithDarko

Theme by Anders NorenUp ↑