Conversions, CTS, Value and Reference types in C#

Goud.Kv
Posted by in C# category on for Beginner level | Points: 250 | Views : 2291 red flag

C# is an Object-oriented programming language. C# comes with simplicity, expressiveness and great performance to meet the programmer productivity.

Introduction

So far, we have seen Members, Nested types and public keywords in C# programming. Now in this chapter, let's discuss about conversions, value types, reference types and CTS in C#.

Objective

The main objective of this article is to learn about conversions and types in C# programming.

Conversions

C# has the capability to convert between instances of compatible types. This conversion creates a new value from the old value. 
There are two types of conversions in C#. They are,
  • Implicit - These are the normal conversions which happens automatically.
    Ex:
    int a = 45;  // 'int' is a 32-bit Integer
    long b = a;  // 'long' is a 64-bi Integer. Implicit conversion to 'long'
Conditions - Implicit conversions are allowed, when both of the below conditions are true.
  1. Guarantee of the compiler that, they will always succeed.
  2. There will be no information loss in the process of conversion.
  • Explicit - These types of conversions require cast to convert explicitly.
    Ex:
    int a = 45;          // 'int' is a 32-bit Integer
    short b = (short)a;  // 'short' is a 16-bit Integer. Explicitly converting by using cast.
Conditions - Explicit conversions are needed, when one of the following is true.
  1. No guarantee of the compiler that, they will always succeed.
  2. Might be a loss of information during the process of conversion.
Note: If there is a scenario in which the compiler can determine that conversion will always fail, then both the conversions are prohibited.

Common Type System of C#

There are two important fundamentals in .NET Framework about the type system.
  • Inheritance: supports Inheritance i.e. Types can be derived from other types which are known as base types. methods, properties and other members are being inherited from the base type to the derived type (class).
    There is a capability of inheriting members from more than one base type. All types, including built in such as System.Int32 (C# Keyword is int), derive from a single base type which is System.Object (C# Keyword is Object).
    This process is called as Common Type System (CTS).

  • There are basically two types in C#. They are value types and reference types.

    Relationship between both two types are shown in the below picture,

    Ref: Google Images
Value Types:
Value types are derived from System.ValueType which is derived from System.Object
  • They comprise most built in types (i.e. numeric types, bool types and char types) as well as custom struct and enum types.
  • They have special behavior in the CLR.
  • There is no separate heap (or Garbage collection overhead) allocation for value type variables.
Let's have an example as below,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Man
{
    class Program
    {
        public struct point
        {
            public int A, B;
        }
        static void Main()
        {
            point p1 = new point();
            p1.A = 45;

            point p2 = p1;            // Creating a Copy

            Console.WriteLine(p1.A);  // prints 45
            Console.WriteLine(p2.A);  // prints 45

            p1.A = 100;               // Changing the p1.A value

            Console.WriteLine(p1.A);  // prints 100
            Console.WriteLine(p2.A);  // this will print 45
        }
    }
}
In the above code we are creating a copy of point. If you run this code, it will gives you the following result


Observe the above result in which we are getting the same '45' as output after changing the 'p1.A' value. This means both p1 and p2 have independent storage.

Reference Types:
Reference type is a type which comprises all class, delegate, array and interface
  • It is more complex than a Value type.
  • It has two parts, an object and a reference to that object.
Let's have a same example as with a reference type instead of value type.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Man
{
    class Program
    {
        public class point
        {
            public int A, B;
        }
        static void Main()
        {
            point p1 = new point();
            p1.A = 45;

            point p2 = p1;            // Creating a Copy

            Console.WriteLine(p1.A);  // prints 45
            Console.WriteLine(p2.A);  // prints 45

            p1.A = 100;               // Changing the p1.A value

            Console.WriteLine(p1.A);  // prints 100
            Console.WriteLine(p2.A);  // this will print 100
        }
    }
}
Now if you clearly observe the above code, there is only one minor change from the earlier one i.e. adding class instead of struct (which is a value type).
Run this code and see the change in the result,

Here, both p1 and p2 are pointing to the same object.

Conclusion

In this article, we covered conversions, CTS, value and reference types in C# programming. Hope you understand. 

Thanks for reading.

Regards,
Krishna.
Recommendation
Read Null literal, Numeric types and Numeric literals in C# after this article.
Page copy protected against web site content infringement by Copyscape

About the Author

Goud.Kv
Full Name: Krishna Vamshi Goud
Member Level: Gold
Member Status: Member,MVP
Member Since: 2/12/2014 2:34:09 AM
Country: India
Thanks & Regards, Krishna


Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)