Posts

Showing posts with the label C# Fundamental

C# Fundamental: Stack vs. Heap

Image
Memory Allocation (Stack and Heap) Operating system and language runtimes such as .NET CLR divides the memory into two chunks called stack ad heap. Stack and heap act differently while allocating memory. When we call a method, the memory required for its parameters ad variable is accommodated by the stack. When a method finishes its job, the memory is automatically released by the garbage collector for the purpose of reuse. Stack Memory Stack is organized like stack of boxes sitting on top of one another. When a method is called, each parameter is put into a box, first box goes first into stack then second on top of it, and so on. As in the following diagram: Stack uses Last In First Out (LIFO) mechanism for allocating memory. Heap Memory Heap memory is an unorganized pile of boxes sitting in a room randomly, not like stack which stored boxes on top of each other in an order. The reference to the object is stored on stack but the actual value is stored on heap....

C# Fundamental: Value and Reference Types

Image
Data Types There are following data types in C#: Value type Reference type Value Type Value types directly contain data Allocated on stack It cannot be  null Data types such as integers, numeric, bytes, Booleans,  char  are called value type. Value types are derived from the  ValueType  class in  System namespace . There are two categories of value types which are built in and user defined. Built in value types  are  int ,  char ,  decimal ,  float ,  long , etc. and User defined  value types are structures ( DateTime  is a value type, a structure) and  enum . For example, when we declare the  int a  compiler allocates 4 bytes of memory (32bit). When we give a value such as 7 to the variable, it copies this value to that block of memory. We can declare a data type by writing the data type name like  int string , etc. then write any name of a variable by using name conve...

C# Fundamental: Data Types and Variable

Image
Data Types In C#, data types are used to store values in the memory. When data type is declared, it allocates a block of memory to hold the value. All the data types are directly or indirectly derived from the  System.object  class. Before moving, we need to understand the variables. Variables A variable is used to store the value (data). It is an address given to a memory location. Example: Take an example of a bag here bag is a variable we can put books into a bag and we can take them out again but we can put books according to bag size, a bigger bag can hold more books. Similarly, we can store and retrieve data from a variable. Here bag is the variable name the size of bag is the data type, an  int  can hold 32 bit value and  long  can hold 64 bit value. Different data types have different sizes we use them according to our requirement for example if we want to store only two values  true  or  false  we...

A detailed introduction to " C# Fundamental "

Introduction Data types are the building block of any programming language. These are like the grammar of any language. I have explained in detail the data types, memory allocation on stack and heap, variables, naming conventions. Topics covered are as follows: Data types Variables Naming convention Value type/reference type Stack/heap memory Note:-- All examples are created by using VS 2015 enterprise edition. Related Topics: Data Types and Variable, Value and Reference Types,   Stack vs. Heap