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,
Conditions - Implicit conversions are allowed, when both of the below conditions are true.
- Guarantee of the compiler that, they will always succeed.
- There will be no information loss in the process of conversion.
Conditions - Explicit conversions are needed, when one of the following is true.
- No guarantee of the compiler that, they will always succeed.
- 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.