What is Type?
- Indicates the type of data stored in variables.
- All data and code is defined within a Type.
- C# program is a collection of the following Types.
- Built-in or pre-defined types
Eg - int, byte, char, string, object, …
- Built-in or pre-defined types
- User defined types
Eg - Classes, structs, enums, interfaces, delegates
- Types can be instantiated and used by calling methods, get and set properties, etc.
C# program is made of programming instructions. Instructions consist of variables. Variables are declared using Types. The variable types in C# include: int, byte, char, string, enum, struct, class, interface, delegate. C# program uses Common Type System (CTS) defined by .NET framework. Due to the object-oriented nature of C#, global variables and global methods are not supported in a program.
Types can be converted from one type to another with restrictions in some cases. Types are organized into namespaces, files, and assemblies.
Types contain data members like fields, constants, arrays, events. They can also contain function members like methods, operators, constructors, destructors, properties, indexers, and other types like classes, structure, enumeration, interfaces, and delegates.
Types
The CTS defines Types into two categories.
- Value type
Value type contains actual data on stack directly. They cannot contain null values. - Primitives Eg int count; float sum;
- Enumerations Eg enum status {yes, no};
- Structure Eg struct point {intx, y;}
- Reference type
- Root Eg- Object
- String Eg- string name;
- Classes Eg - class Line: Shape { . . .}
- Interfaces Eg - interface IDrawable {. . .}
- Arrays Eg - string[] a = new string[10];
- Delegates Eg - delegate void operation();
Value types in CTS are,
Reference type contains references to objects. The reference is stored on stack while the actual object is on managed heap. A Reference type can be null.
Reference types in CTS are,
In .NET applications, reference type variables are allocated on managed heap. Managed heap is controlled by garbage collector which is the component of CLR. It does automatic memory management during the execution of .NET application.
Value type variables are allocated on stack memory. They are not managed by garbage collector. So there is no pressure on garbage collector.
The differences between value type and reference type are mentioned in the following table.
| Value type | Reference type | |
| Variable holds | Actual value | Memory location |
| Allocated on | Stack, member | Heap |
| Nullability | Always has value | Maybe null |
| Default Value | 0 | null |
| Assignment means | Copying actual data | Copying reference |

yerripilli srinivasa raoPosted Mar 16, 2019, 5:08 AM
Very useful information . Thank you..
Vishal PrajapatiPosted Jan 30, 2017, 11:55 PM
Very Nice article..very helpful for beginners..
Nigel FernandesPosted Jan 30, 2017, 5:06 PM
Useful article, thanks .......
Sajjad BhattiPosted Jan 30, 2017, 1:18 PM
Very helpful Information sir
Sibiraj ThattalamPosted Jan 30, 2017, 12:48 PM
Helpful information
Joe WilsonPosted Jan 30, 2017, 11:57 AM
Thank you for sharing it.