• Save
Lesson 2    Understanding Types And Usage In Dot Net
Upcoming SlideShare
Loading in...5
×
 

Lesson 2 Understanding Types And Usage In Dot Net

on

  • 744 views

Understanding Types And Usage In Dot Net

Understanding Types And Usage In Dot Net

Statistics

Views

Total Views
744
Views on SlideShare
741
Embed Views
3

Actions

Likes
0
Downloads
0
Comments
0

2 Embeds 3

http://www.linkedin.com 2
https://www.linkedin.com 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Lesson 2    Understanding Types And Usage In Dot Net Lesson 2 Understanding Types And Usage In Dot Net Document Transcript

    • 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