understanding data types and collections n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Understanding Data Types and Collections PowerPoint Presentation
Download Presentation
Understanding Data Types and Collections

Loading in 2 Seconds...

play fullscreen
1 / 32

Understanding Data Types and Collections - PowerPoint PPT Presentation


  • 139 Views
  • Uploaded on

Understanding Data Types and Collections. Lesson 2. Objective Domain Matrix. Data Types in C#. The data type defines the size of memory needed to store the data and the kind of operation that can be performed on the data .

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Understanding Data Types and Collections' - rudolf


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
data types in c
Data Types in C#
  • The data type defines the size of memory needed to store the data and the kind of operation that can be performed on the data.
  • C# is a strongly-typed language. That means the data types for variables, constants , literal values, method return values, and parameters must be known at the compile time.
intrinsic data types
Intrinsic Data Types
  • Intrinsic data types are the primitive data types for which the support is directly built into the programming language.
values types and reference types
Values Types and Reference Types
  • A value type directly stores data within its memory.
  • Reference types store only a reference to a memory location. The actual data is stored at the memory location being referred to.
  • When you copy a reference type variable to another variable of the same type, only the references are copied. As a result, after the copy, both variables will point to the same object.
casting
Casting
  • In C#, you can cast an object to any of its base types.
  • All classes in the .NET Framework inherit either directly or indirectly from the Object class.
  • Assigning a derived class object to a base class object doesn’t require any special syntax:
  • Assigning a base class object to a derived class object must be explicitly cast:
  • At execution time, if the value of o is not compatible with the Rectangle class, the runtime throws a System.InvalidCastException.
boxing and unboxing
Boxing and Unboxing
  • Boxing is the process of converting a value type to the reference type.
  • Unboxing is the process of converting an object type to a value type.
  • For unboxing to succeed, you must cast using the same type that was used for boxing. If you use a different data type, you may might get InvalidCastException.
arrays and collections
Arrays and Collections
  • The collection data types provide data structures to store and manipulate a collection of items. These collection classes are defined as part of the System.Collections or System.Collections.Generic namespace.
  • The arrays are special type of collections declared by using a programming language construct. All arrays are internally derived from the System.Array class.
  • Some common collection data types are arrays, queues, stacks, and linked lists.
demonstration cast object to new type
Demonstration – Cast Object to new type
  • Cast Employee to Person
  • Cast Customer to Person
  • Use the Is keyword to check the type of Person object
arrays
Arrays
  • An array is a collection of items of the same type.
  • The items in an array are stored in contiguous memory locations.
  • Capacity of an array is predefined and fixed.
  • Any array item can be directly accessed by using an index.
  • C# array indexes are zero-based
array common operations
Array – Common Operations
  • Arrays support the following operations:
    • Allocation
    • Access
  • Thefollowing code assigns a value of 10 to the fourth item of the array, and twice that value is then assigned to the variable calc:
queues
Queues
  • A collection of items in which the first item added to the collection is the first one to be removed.
  • First In First Out (FIFO)
  • Queue is a heterogeneous data structure.
  • Capacity of a queue is the number of items the queue can hold.
  • As elements are added to the queue, the capacity can be automatically increased.
queues common operations
Queues – Common Operations
  • Enqueue:Adds an item to the tail end of the queue.
  • Dequeue:Removes the current element at the head of the queue.
  • Peek:Access the current item at the head position without actually removing it from the queue.
  • Contains:Determines whether a particular item exists in the queue.
stacks
Stacks
  • A collection of items in which last item added to the collection is the first one to be removed.
  • Last In First Out (LIFO)
  • Stack is a heterogeneous data structure.
  • Capacity of a stack is the number of items the queue can hold.
  • As elements are added to the stack, the capacity can be automatically increased.
stacks internal representation
Stacks– Internal Representation
  • A stack can be visualized just like the queue, except that the tail is called the top of the stack and the head is called the bottom of the stack.
  • New items are always added to the top of a stack; when this happens, the top of the stack starts pointing to the newly added element.
  • Items are also removed from the top of the stack, and when that happens, the top of the stack is adjusted to point to the next item in the stack.
stack common operations
Stack– Common Operations
  • Push: Adds item to the top of the stack.
  • Pop: Removes the element at the top of the stack.
  • Peek:Access the current item at the top of the stack without actually removing it from the stack.
  • Contains:Determines whether a particular item exists in the stack.
demonstration arrays and collections
Demonstration – Arrays and Collections
  • Create an Array of Customer Names
  • Access the Controls Collection of a PlaceHolder control
linked lists
Linked Lists
  • A linked list is a collection of nodes arranged so that each node contains a link to the next node in the sequence.
  • Each node in a linked list contains of two pieces of information:
    • the data corresponding to the node
    • the link to the next node
linked lists common operations
Linked Lists – Common Operations
  • Add: Adds an item to a linked list.
  • Remove:Removes a given node from the linked list.
  • Find:Finds a node with a given value in the linked list.
linked lists visualizing the add operation
Linked Lists – Visualizing the add operation
  • Adding an item to a linked list is a matter of changing links.
generics
Generics
  • The Generics feature helps you define classes that can be customized for different data types.
  • Generics provide many benefits including reusability, type-safety and performance.
  • The most common use of generics is to create collection classes. 
generics definition and instantiation
Generics – Definition and Instantiation
  • The following code illustrates a simple definition of a generic class:
  • The following code shows how to instantiate a generic class with a concrete type int:
generics constraints
Generics – Constraints
  • Constraints specify restrictions to the kinds of types that client code can use for type arguments when it instantiates your class.
  • This declaration ensures that when an instance of EmployeeRecord is created, the only valid type arguments are of the type Employee or one of classes derived from the Employee class.
generic collections
Generic Collections
  • A generic collection is a collection that stores only the items of the same data type.
  • To ensure type safety, the compiler checks that you can only assign a value of the correct data type from a generic collection.
  • The generic collection types generally perform better than their non-generic equivalent. With generics there is no need to box and unbox each item.
generic collections usage
Generic Collections Usage
  • Generic collections are expressed using a special notation. For example, List<T> is a generic List collection. The T in the notation must be replaced with the data type that you want to store in the collection
common generic collections
Common Generic Collections
  • Some commonly used generic collections:
recap
Recap
  • Data types in the .NET Framework
    • Intrinsic data types
    • Value types and reference types
    • Conversion and casting
    • Boxing and Unboxing
  • Arrays and collections
    • Single dimensional and multi-dimensional arrays
    • Collection classes
  • Generics
    • Generic constraints
    • Generic Collections
lab properties and methods
Lab – Properties and Methods
  • Add Property Procedures to classes
  • Add methods that accept parameters
  • Add methods that return data