Tagvalue types

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
float
int
long
sbyte
short
struct
uint
ulong

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

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.

Addition

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.

 

© 2021 LearnC#WithDarko

Theme by Anders NorenUp ↑