TagBegginer

Boxing and unboxing

Boxing and unboxing are processes of converting value types to object type or from object type to a value type. During boxing value type is taken and converted to object type, while during unboxing object type is converted to explicitly defined type, so we can say that Boxing is implicit and unboxing is explicit.

Let’s look at boxing through example:

We have a job to convert two numbers to an object type. Usually we assign numbers to an integer type. Int type can hold numeric values from -2,147,483,648 to 2,147,483,647, but Int64 can hold larger numeric values from -9223372036854775808 to 9223372036854775807. So our job is to convert numbers 2,147,483,000 and 3,147,483,800 to an object. We can do it simply by writing following code:

What we essential did was that we created two variables one int variable to hold number 2147483000 and Int64 variable because number is to large to be stored inside int variable. Then we took those two variables and assigned them to an object type variables.

Unboxing is an opposite process of boxing. First we will take a few object variables with different types of values, like string, int and double and convert them to a desired type by casting. Casting is specifying type in round brackets before objects variable. Let’s take a look:

There are a few things you should always consider while boxing or unboxing.

  • Both boxing and unboxing are expensive operations. While boxing a new object must be created on a managed heap. While unboxing does not usually require creating allocation of a new memory it is still a expensive operation.
  • You can unbox object to a lot of different value types. For example, you can unbox numeric value 5 to a int or a string if you wish:

  • List of objects is also possible

  • Beware of InvalidCastException exceptions that occur when unboxing incorectly

Using keyword

Before we continue to FileInfo and Streams we need to explain using keyword in details.

Using directive

Using directive shortens our syntax so that we do not always have to type entire namespace:

Using directive grants us access to static members having to qualify the access with the type name:

Using directive enables us to create alias directive.

Using statement

Using statement provides us with a easy to use syntax which in return guarantees us with that types that implement IDisposable interface will be disposed of when we finish with them. For now do not stress yourself with questions like why do I need to dispose of something and what use IDisposable interface is to me. Those questions will be covered in a future post.

Example of using statement

Usage of using statement is straight-forward.

First we type using keyword and open parentheses, and inside of those brackets we instantiate class that implements IDisposable interface.  After that we proceed to use class instance inside curly brackets.

Of-course  you can instantiate more than one class while using using directive, just remember to separate them with coma:

I hope you will find using keyword helpful. I certainly use it on a daily basis in both forms

File and FileInfo

While trying to copy, move, rename, create, open, delete and appending to files we use File and FileInfo classes. File and File info classes derive from System.IO namespace. File class provides us static methods to work with files and FileInfo provides us with instance methods. Generally, you will use File class when working with a file and perform one operation on it. Difference between File and FileInfo approaches is that FileInfo does security checking only once (while accessing files).  Because FileInfo is a instance method it does not tack changes to a file so you will have to take a lot of precautions while using that class. File class is on a disadvantage, because you always have to apply path to a file, meaning, it takes more parameters than FileInfo class. Try discovering yourself which approach is faster in your scenario. Also both classes help us while creating FileStream (to be explained in next post).

Common operations working with File class

 

Common operations working with FileInfo  class

 

I hope you had no problem with learning how to use File and FileInfo classes. If you found File and FileInfo classes interesting I recommend that you continue learning about Streams.

String Part 2

Substring

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!

 

HelloWorld!

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 ↑