SlideShare una empresa de Scribd logo
1 de 4
Understanding Types and usage…                  Microsoft TEG – GE GDC Noida 2


                     Understanding Types and usage in .Net

As a .net programmer, one should have clarity on various TYPES provided by .Net
Framework and their usage. That will help one to write efficient code for better
performance.

In .net framework, the Types are divided into 2 categories: 1) Value Type 2)
Reference Type. Below diagram will give a better picture on the Types and
categories.




Value Type: Value Types are the simplest types in .Net Framework. The most
common value types are Numeric and Boolean types. Value types contain data
directly unlike reference types which contains the address of the data. Instance of
the value types are stored in an area of memory called “Stack”.

Value Types are primarily divided into 3 types as shown in the diagram above.

   1) Built-in Types
   2) User-Defined Types
   3) Enumeration

1) Built-in Types: Built-in types are base types provided with the .NET Framework,
with which other types are built.

Below mentioned is the common list of variables belonging to Built-in Value type
Category and their attributes.

Variable Name      Alias            Byte             Range
System.SByte       SByte            1                -128 t0 127
System.Byte        Byte             1                0 to 255
System.Int16       Short            2                -32768 to 32767
System.Int32       Integer          4                -2147483648 to
                                                     2147483647
System.UInt32      UInteger         4                0 to 4294967295
System.Int64       Long             8                -9223372036854775808 to
                                                     9223372036854775807
System.Single      Single           4                -3.402823E+38 to -
                                                     3.402823E+38
System.Double      Double           8                -1.79769313486232E+308
                                                     to 1.79769313486232E+308
System.Decimal     Decimal          16               -
                                                     79228162514264433759354



TCS Internal                                                                     1
Understanding Types and usage…                    Microsoft TEG – GE GDC Noida 2


                                                      to
                                                      79228162514264433759354
System.Char         Char             2                -
System.Boolean      Boolean          4                -
System.DateTime     Date             8                1/1/0001 to 12/31/9999

Tips: Since 32-bit integer types (int32 and Uint32) are optimized ones, these should
be used for counter and other frequently accessed integral variables. For floating-
point operations, “double” is the most efficient type because those operations are
optimized by hardware.

2) User-Defined Types: User defined types are also called “structures”. The
instances of user-defined types are stored in stack like other value types. Example
of a structure is given below:

   •   In-built structures – system.drawing.point(x,y) where x and y represent the
       coordinates
   •   User-defined structure – you may create you own structure as mentioned
       below

               Vb.Net : Structure Cycle
                            Dim _m,_n as integer
                            Public sub new (min as integer,max as integer)
                                    _m = max
                                    _n = min
                            End sub
                            Public property get MinValue() as integer
                                    Return _n
                            End property
                       End Structure

               C#.Net : struct cycle
                       {
                            Int _m,_n;
                            Public void cycle(int min,int max)
                            {
                                _m = max;
                                _n = min;
                            }
                            Public int MinValue
                            Get { return _n ;}
                       }

Reference Type: Most types in .Net framework are reference types. Reference
Types store the address of their data, known as pointer, on the stack. The actual
data is stored in an area of memory called the heap.

Few built-in reference types are
System.Object,System.String,system.Text.StringBuilder, System.Array etc.




TCS Internal                                                                      2
Understanding Types and usage…                    Microsoft TEG – GE GDC Noida 2


Strings and String Builders:

Before we start discussing on the Strings and String Builders, lets discuss about the
mutable/immutable characteristics. String types of System.Strings are immutable in
nature i.e. any change to a string causes the runtime to create a new string and
abandon the old one.

Let’s take an example here to demonstrate this.
 VB                                          C#
 Dim Mystr as string                         String Mystr;
 Mystr = “Tata”                              Mystr = “Tata”;
 Mystr = Mystr + “Consultancy”               Mystr = Mystr + “Consultancy”;
 Mystr = Mystr + “Services”                  Mystr = Mystr + “Services”;

Only the last string has a reference; the other three will be disposed of during
garbage collection. Every time you add two strings using “+” operator or keep on
reassigning value to a string variable, a new string allocation is reserved in memory
and used for this new string. So let's say, you are adding strings 100 times using +
operator or you are replacing the value stored in a string variable 100 times in a
procedure, there will be 100 new memory allocations created. This operation may be
very resource consuming in lengthy string concatenation operations. In case of
StringBuilder, there will only be one memory allocation.

So avoid using string variables when there is a requirement of concatenating the
variables in a loop or in statements and use StringBuilder instead. For example

Avoid the below statement:

String lstrSQL / Dim lstrSQL as string
lstrSQL = “select * from table_A”
lstrSQL = lstrSQL + “ where table_a.columnA = <somevalue>”
lstrSQL = lstrSQL + “ And table_a.columnB = <somevalue>”

Use string builder instead:

System.Text.StringBuilder lstrSQLBld / Dim lstrSQLBld as System.Text.StringBuilder
lstrSQLBld.Append(“select * from table_A”)
lstrSQLBld.Append( “ where table_a.columnA = <somevalue>”)
lstrSQLBld.Append( “ And table_a.columnB = <somevalue>”)
C# à string lstrSQL = lstrSQLBld.ToString()
VB à Dim lstrSQL as string = lstrSQLBld.ToString()




TCS Internal                                                                       3
Understanding Types and usage…                   Microsoft TEG – GE GDC Noida 2


Summary

   •   Value types directly contain data in memory called “Stack” where as reference
       types contain address of the data in Stack and the actual data is stored in a
       place in memory called “heap”.
   •   Since value types store data directly, they offer excellent performance over
       reference types.
   •   When you copy a value type, a second copy of value is created. When you
       copy a reference type, only the pointer is copied. So if you create copy of a
       reference type and modify the copy, both the copy and original variables are
       changed.
   •   Strings are immutable, hence use stringbuilder class to concatenate strings


Reference
Microsoft Training Kit (Exam 70-536)




TCS Internal                                                                      4

Más contenido relacionado

La actualidad más candente

Lecture 10 user defined functions and modules
Lecture 10  user defined functions and modulesLecture 10  user defined functions and modules
Lecture 10 user defined functions and modules
alvin567
 
CC282 Unsupervised Learning (Clustering) Lecture 7 slides for ...
CC282 Unsupervised Learning (Clustering) Lecture 7 slides for ...CC282 Unsupervised Learning (Clustering) Lecture 7 slides for ...
CC282 Unsupervised Learning (Clustering) Lecture 7 slides for ...
butest
 
CUDA by Example : Thread Cooperation : Notes
CUDA by Example : Thread Cooperation : NotesCUDA by Example : Thread Cooperation : Notes
CUDA by Example : Thread Cooperation : Notes
Subhajit Sahu
 
Lecture 12
Lecture 12Lecture 12
Lecture 12
Rana Ali
 

La actualidad más candente (20)

Lecture 10 user defined functions and modules
Lecture 10  user defined functions and modulesLecture 10  user defined functions and modules
Lecture 10 user defined functions and modules
 
CC282 Unsupervised Learning (Clustering) Lecture 7 slides for ...
CC282 Unsupervised Learning (Clustering) Lecture 7 slides for ...CC282 Unsupervised Learning (Clustering) Lecture 7 slides for ...
CC282 Unsupervised Learning (Clustering) Lecture 7 slides for ...
 
The Ring programming language version 1.2 book - Part 78 of 84
The Ring programming language version 1.2 book - Part 78 of 84The Ring programming language version 1.2 book - Part 78 of 84
The Ring programming language version 1.2 book - Part 78 of 84
 
Hierarchical clustering techniques
Hierarchical clustering techniquesHierarchical clustering techniques
Hierarchical clustering techniques
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
Java String
Java String Java String
Java String
 
Structure in c#
Structure in c#Structure in c#
Structure in c#
 
K means Clustering Algorithm
K means Clustering AlgorithmK means Clustering Algorithm
K means Clustering Algorithm
 
The Ring programming language version 1.3 book - Part 82 of 88
The Ring programming language version 1.3 book - Part 82 of 88The Ring programming language version 1.3 book - Part 82 of 88
The Ring programming language version 1.3 book - Part 82 of 88
 
CUDA by Example : Thread Cooperation : Notes
CUDA by Example : Thread Cooperation : NotesCUDA by Example : Thread Cooperation : Notes
CUDA by Example : Thread Cooperation : Notes
 
NuMachine and NuAlgebra
NuMachine and NuAlgebraNuMachine and NuAlgebra
NuMachine and NuAlgebra
 
K means clustering
K means clusteringK means clustering
K means clustering
 
Lecture 12
Lecture 12Lecture 12
Lecture 12
 
Structures in c++
Structures in c++Structures in c++
Structures in c++
 
Introducing the Concept of Back-Inking as an Efficient Model for Document Ret...
Introducing the Concept of Back-Inking as an Efficient Model for Document Ret...Introducing the Concept of Back-Inking as an Efficient Model for Document Ret...
Introducing the Concept of Back-Inking as an Efficient Model for Document Ret...
 
Fundamental classes in java
Fundamental classes in javaFundamental classes in java
Fundamental classes in java
 
JSpiders - Wrapper classes
JSpiders - Wrapper classesJSpiders - Wrapper classes
JSpiders - Wrapper classes
 
K-Means clustring @jax
K-Means clustring @jaxK-Means clustring @jax
K-Means clustring @jax
 
Struktur data 1
Struktur data 1Struktur data 1
Struktur data 1
 
K-Means, its Variants and its Applications
K-Means, its Variants and its ApplicationsK-Means, its Variants and its Applications
K-Means, its Variants and its Applications
 

Destacado (8)

Lesson 1 Understanding Dot Net Framework
Lesson 1   Understanding Dot Net FrameworkLesson 1   Understanding Dot Net Framework
Lesson 1 Understanding Dot Net Framework
 
PHP Basic & Variables
PHP Basic & VariablesPHP Basic & Variables
PHP Basic & Variables
 
Lesson 3: Variables and Expressions
Lesson 3: Variables and ExpressionsLesson 3: Variables and Expressions
Lesson 3: Variables and Expressions
 
1.Philosophy of .NET
1.Philosophy of .NET1.Philosophy of .NET
1.Philosophy of .NET
 
Dotnet framework
Dotnet frameworkDotnet framework
Dotnet framework
 
Dotnet basics
Dotnet basicsDotnet basics
Dotnet basics
 
Introduction To Dotnet
Introduction To DotnetIntroduction To Dotnet
Introduction To Dotnet
 
Introduction to .net framework
Introduction to .net frameworkIntroduction to .net framework
Introduction to .net framework
 

Similar a Lesson 2 Understanding Types And Usage In Dot Net

A simulation model of ieee 802.15.4 gts mechanism and gts
A simulation model of ieee 802.15.4 gts mechanism and gtsA simulation model of ieee 802.15.4 gts mechanism and gts
A simulation model of ieee 802.15.4 gts mechanism and gts
wissem hammouda
 

Similar a Lesson 2 Understanding Types And Usage In Dot Net (20)

Simple Network Management Protocol by vikas jagtap
Simple Network Management Protocol by vikas jagtapSimple Network Management Protocol by vikas jagtap
Simple Network Management Protocol by vikas jagtap
 
Network Management
Network ManagementNetwork Management
Network Management
 
VCE Unit 01 (1).pptx
VCE Unit 01 (1).pptxVCE Unit 01 (1).pptx
VCE Unit 01 (1).pptx
 
Visual Basic User Interface -IV
Visual Basic User Interface -IVVisual Basic User Interface -IV
Visual Basic User Interface -IV
 
A simulation model of ieee 802.15.4 gts mechanism and gts
A simulation model of ieee 802.15.4 gts mechanism and gtsA simulation model of ieee 802.15.4 gts mechanism and gts
A simulation model of ieee 802.15.4 gts mechanism and gts
 
Network Management System and Protocol
Network Management System and Protocol Network Management System and Protocol
Network Management System and Protocol
 
Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Visula C# Programming Lecture 6
Visula C# Programming Lecture 6
 
Secure Text Transfer Using Diffie-Hellman Key Exchange Based On Cloud
Secure Text Transfer Using Diffie-Hellman Key Exchange Based On CloudSecure Text Transfer Using Diffie-Hellman Key Exchange Based On Cloud
Secure Text Transfer Using Diffie-Hellman Key Exchange Based On Cloud
 
MS SQL SERVER: Neural network and logistic regression
MS SQL SERVER: Neural network and logistic regressionMS SQL SERVER: Neural network and logistic regression
MS SQL SERVER: Neural network and logistic regression
 
MS SQL SERVER:Microsoft neural network and logistic regression
MS SQL SERVER:Microsoft neural network and logistic regressionMS SQL SERVER:Microsoft neural network and logistic regression
MS SQL SERVER:Microsoft neural network and logistic regression
 
Unit 4 qba
Unit 4 qbaUnit 4 qba
Unit 4 qba
 
The static code analysis rules for diagnosing potentially unsafe construction...
The static code analysis rules for diagnosing potentially unsafe construction...The static code analysis rules for diagnosing potentially unsafe construction...
The static code analysis rules for diagnosing potentially unsafe construction...
 
SNMP Project: SNMP-based Network Anomaly Detection Using Clustering
SNMP Project: SNMP-based Network Anomaly Detection Using ClusteringSNMP Project: SNMP-based Network Anomaly Detection Using Clustering
SNMP Project: SNMP-based Network Anomaly Detection Using Clustering
 
B.sc CSIT 2nd semester C++ Unit7
B.sc CSIT  2nd semester C++ Unit7B.sc CSIT  2nd semester C++ Unit7
B.sc CSIT 2nd semester C++ Unit7
 
James Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on PythonJames Jesus Bermas on Crash Course on Python
James Jesus Bermas on Crash Course on Python
 
Introduction to Data Structure
Introduction to Data Structure Introduction to Data Structure
Introduction to Data Structure
 
IRJET - Single Image Super Resolution using Machine Learning
IRJET - Single Image Super Resolution using Machine LearningIRJET - Single Image Super Resolution using Machine Learning
IRJET - Single Image Super Resolution using Machine Learning
 
C Programming - Refresher - Part IV
C Programming - Refresher - Part IVC Programming - Refresher - Part IV
C Programming - Refresher - Part IV
 
12. MODULE 1.pptx
12. MODULE 1.pptx12. MODULE 1.pptx
12. MODULE 1.pptx
 
Objective c slide I
Objective c slide IObjective c slide I
Objective c slide I
 

Lesson 2 Understanding Types And Usage In Dot Net

  • 1. Understanding Types and usage… Microsoft TEG – GE GDC Noida 2 Understanding Types and usage in .Net As a .net programmer, one should have clarity on various TYPES provided by .Net Framework and their usage. That will help one to write efficient code for better performance. In .net framework, the Types are divided into 2 categories: 1) Value Type 2) Reference Type. Below diagram will give a better picture on the Types and categories. Value Type: Value Types are the simplest types in .Net Framework. The most common value types are Numeric and Boolean types. Value types contain data directly unlike reference types which contains the address of the data. Instance of the value types are stored in an area of memory called “Stack”. Value Types are primarily divided into 3 types as shown in the diagram above. 1) Built-in Types 2) User-Defined Types 3) Enumeration 1) Built-in Types: Built-in types are base types provided with the .NET Framework, with which other types are built. Below mentioned is the common list of variables belonging to Built-in Value type Category and their attributes. Variable Name Alias Byte Range System.SByte SByte 1 -128 t0 127 System.Byte Byte 1 0 to 255 System.Int16 Short 2 -32768 to 32767 System.Int32 Integer 4 -2147483648 to 2147483647 System.UInt32 UInteger 4 0 to 4294967295 System.Int64 Long 8 -9223372036854775808 to 9223372036854775807 System.Single Single 4 -3.402823E+38 to - 3.402823E+38 System.Double Double 8 -1.79769313486232E+308 to 1.79769313486232E+308 System.Decimal Decimal 16 - 79228162514264433759354 TCS Internal 1
  • 2. Understanding Types and usage… Microsoft TEG – GE GDC Noida 2 to 79228162514264433759354 System.Char Char 2 - System.Boolean Boolean 4 - System.DateTime Date 8 1/1/0001 to 12/31/9999 Tips: Since 32-bit integer types (int32 and Uint32) are optimized ones, these should be used for counter and other frequently accessed integral variables. For floating- point operations, “double” is the most efficient type because those operations are optimized by hardware. 2) User-Defined Types: User defined types are also called “structures”. The instances of user-defined types are stored in stack like other value types. Example of a structure is given below: • In-built structures – system.drawing.point(x,y) where x and y represent the coordinates • User-defined structure – you may create you own structure as mentioned below Vb.Net : Structure Cycle Dim _m,_n as integer Public sub new (min as integer,max as integer) _m = max _n = min End sub Public property get MinValue() as integer Return _n End property End Structure C#.Net : struct cycle { Int _m,_n; Public void cycle(int min,int max) { _m = max; _n = min; } Public int MinValue Get { return _n ;} } Reference Type: Most types in .Net framework are reference types. Reference Types store the address of their data, known as pointer, on the stack. The actual data is stored in an area of memory called the heap. Few built-in reference types are System.Object,System.String,system.Text.StringBuilder, System.Array etc. TCS Internal 2
  • 3. Understanding Types and usage… Microsoft TEG – GE GDC Noida 2 Strings and String Builders: Before we start discussing on the Strings and String Builders, lets discuss about the mutable/immutable characteristics. String types of System.Strings are immutable in nature i.e. any change to a string causes the runtime to create a new string and abandon the old one. Let’s take an example here to demonstrate this. VB C# Dim Mystr as string String Mystr; Mystr = “Tata” Mystr = “Tata”; Mystr = Mystr + “Consultancy” Mystr = Mystr + “Consultancy”; Mystr = Mystr + “Services” Mystr = Mystr + “Services”; Only the last string has a reference; the other three will be disposed of during garbage collection. Every time you add two strings using “+” operator or keep on reassigning value to a string variable, a new string allocation is reserved in memory and used for this new string. So let's say, you are adding strings 100 times using + operator or you are replacing the value stored in a string variable 100 times in a procedure, there will be 100 new memory allocations created. This operation may be very resource consuming in lengthy string concatenation operations. In case of StringBuilder, there will only be one memory allocation. So avoid using string variables when there is a requirement of concatenating the variables in a loop or in statements and use StringBuilder instead. For example Avoid the below statement: String lstrSQL / Dim lstrSQL as string lstrSQL = “select * from table_A” lstrSQL = lstrSQL + “ where table_a.columnA = <somevalue>” lstrSQL = lstrSQL + “ And table_a.columnB = <somevalue>” Use string builder instead: System.Text.StringBuilder lstrSQLBld / Dim lstrSQLBld as System.Text.StringBuilder lstrSQLBld.Append(“select * from table_A”) lstrSQLBld.Append( “ where table_a.columnA = <somevalue>”) lstrSQLBld.Append( “ And table_a.columnB = <somevalue>”) C# à string lstrSQL = lstrSQLBld.ToString() VB à Dim lstrSQL as string = lstrSQLBld.ToString() TCS Internal 3
  • 4. Understanding Types and usage… Microsoft TEG – GE GDC Noida 2 Summary • Value types directly contain data in memory called “Stack” where as reference types contain address of the data in Stack and the actual data is stored in a place in memory called “heap”. • Since value types store data directly, they offer excellent performance over reference types. • When you copy a value type, a second copy of value is created. When you copy a reference type, only the pointer is copied. So if you create copy of a reference type and modify the copy, both the copy and original variables are changed. • Strings are immutable, hence use stringbuilder class to concatenate strings Reference Microsoft Training Kit (Exam 70-536) TCS Internal 4