Creating and Using Objects, Exceptions, Strings

Slides:



Advertisements
Similar presentations
Road Map Introduction to object oriented programming. Classes
Advertisements

ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Lecture 9 Concepts of Programming Languages
ASP.NET Programming with C# and SQL Server First Edition
Abstract Data Types and Encapsulation Concepts
1.11 Introduction to OOP academy.zariba.com 1. Lecture Content 1.What is OOP and why use it? 2.Classes and objects 3.Static classes 4.Properties, fields.
Review of C++ Programming Part II Sheng-Fang Huang.
Using the Standard.NET Framework Classes Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
More C++ Classes Systems Programming. C++ Classes  Preprocessor Wrapper  Time Class Case Study –Two versions (old and new)  Class Scope and Assessing.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
Using the Standard.NET Framework Classes Svetlin Nakov Telerik Corporation
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Using the Standard.NET Framework Classes Svetlin Nakov Telerik Software Academy academy.telerik.com.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Using the Standard.NET Framework Classes Telerik Software Academy C# Fundamentals – Part 2.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Introduction to Object-Oriented Programming Lesson 2.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Using the Standard.NET Framework Classes Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Topic: Classes and Objects
C# for C++ Programmers 1.
Chapter 13: Overloading and Templates
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Static data members Constructors and Destructors
Abstract Data Types and Encapsulation Concepts
Java Primer 1: Types, Classes and Operators
Review: Two Programming Paradigms
11.1 The Concept of Abstraction
This pointer, Dynamic memory allocation, Constructors and Destructor
Object Oriented Analysis and Design
Lecture 9 Concepts of Programming Languages
Introduction to Classes
Chapter 3 Introduction to Classes, Objects Methods and Strings
Using Classes and Objects
Corresponds with Chapter 7
Abstract Data Types and Encapsulation Concepts
Chapter 9 Classes: A Deeper Look, Part 1
Classes and Objects.
Defining Classes and Methods
Python Primer 1: Types and Operators
Chapter 9 Introduction To Classes
More C++ Classes Systems Programming.
Classes and Objects Object Creation
Programming in C# CHAPTER - 9
Creating and Using Classes
Chapter 7 Objects and Classes
SPL – PS3 C++ Classes.
Lecture 9 Concepts of Programming Languages
CMSC 202 Constructors Version 9/10.
Introduction to Classes and Objects
Presentation transcript:

Creating and Using Objects, Exceptions, Strings C# Language Creating and Using Objects, Exceptions, Strings

Table of Contents Creating and Using Objects Exceptions Handling Strings and Text Processing Collection (same as VB.NET) Attributes

Using Classes and Objects Using the Standard .NET Framework Classes

What is Class? The formal definition of class: * 07/16/96 What is Class? The formal definition of class: Definition by Google Classes act as templates from which an instance of an object is created at run time. Classes define the properties of the object and the methods used to control the object's behavior. (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Classes Classes define: Set of attributes Set of actions (behavior) Represented by fields and properties Hold their state Set of actions (behavior) Represented by methods

(Properties and Fields) Classes – Example Class Name Attributes (Properties and Fields) Account +Owner: Person +Ammount: double Operations (Methods) +Suspend() +Deposit(sum:double) +Withdraw(sum:double)

Objects An object is a concrete instance of a particular class Creating an object from a class is called instantiation Objects have state Set of values associated to their attributes Example: Class: Account Objects: Ivan's account, Peter's account

Objects – Example Object Class Object Object ivanAccount +Owner="Ivan Kolev" +Ammount=5000.0 Account +Owner: Person +Ammount: double Object peterAccount +Owner="Peter Kirov" +Ammount=1825.33 +Suspend() +Deposit(sum:double) +Withdraw(sum:double) Object kirilAccount +Owner="Kiril Kirov" +Ammount=25.0

Classes in C# Basic units that compose programs Implementation is encapsulated (hidden) Classes in C# can contain: Fields (member variables) Properties Methods Constructors Etc. (events, indexers, operators, …)

Classes in C# – Examples Example of classes: System.Console System.String (string in C#) System.Int32 (int in C#) System.Array System.Math System.Random

Declaring Objects An instance of a class or structure can be defined like any other variable: Instances cannot be used if they are not initialized using System; ... // Define two variables of type DateTime DateTime today; DateTime halloween; // Declare and initialize a structure instance DateTime today = DateTime.Now;

Fields Fields are data members of a class Can be variables and constants Accessing a field doesn’t invoke any actions of the object Example: String.Empty (the "" string)

Accessing Fields Constant fields can be only read Variable fields can be read and modified Usually properties are used instead of directly accessing variable fields Examples: // Accessing read-only field String empty = String.Empty; // Accessing constant field int maxInt = Int32.MaxValue;

Properties Properties look like fields (have name and type), but they can contain code, executed when they are accessed Usually used to control access to data fields (wrappers), but can contain more complex logic Can have two components (and at least one of them) called accessors get for reading their value set for changing their value

Properties (2) According to the implemented accessors properties can be: Read-only (get accessor only) Read and write (both get and set accessors) Write-only (set accessor only) Example of read-only property: String.Length

Accessing Properties and Fields – Example using System; ... DateTime christmas = new DateTime(2009, 12, 25); int day = christmas.Day; int month = christmas.Month; int year = christmas.Year; Console.WriteLine( "Christmas day: {0}, month: {1}, year: {2}", day, month, year); "Day of year: {0}", christmas.DayOfYear); Console.WriteLine("Is {0} leap year: {1}", year, DateTime.IsLeapYear(year));

Instance and Static Members Fields, properties and methods can be: Instance (or object members) Static (or class members) Instance members are specific for each object Example: different dogs have different name Static members are common for all instances of a class Example: DateTime.MinValue is shared between all instances of DateTime

Instance and Static Members – Examples Example of instance member String.Length Each string object has different length Example of static member Console.ReadLine() The console is only one (global for the program) Reading from the console does not require to create an instance of it

Methods Methods manipulate the data of the object to which they belong or perform other tasks Examples: Console.WriteLine(…) Console.ReadLine() String.Substring(index, length) Array.GetLength(index)

Instance Methods Instance methods manipulate the data of a specified object or perform any other tasks If a value is returned, it depends on the particular class instance Syntax: The name of the instance, followed by the name of the method, separated by dot <object_name>.<method_name>(<parameters>)

Calling Instance Methods – Examples Calling instance methods of String: Calling instance methods of DateTime: String sampleLower = new String('a', 5); String sampleUpper = sampleLower.ToUpper(); Console.WriteLine(sampleLower); // aaaaa Console.WriteLine(sampleUpper); // AAAAA DateTime now = DateTime.Now; DateTime later = now.AddHours(8); Console.WriteLine("Now: {0}", now); Console.WriteLine("8 hours later: {0}", later);

Static Methods Static methods are common for all instances of a class (shared between all instances) Returned value depends only on the passed parameters No particular class instance is available Syntax: The name of the class, followed by the name of the method, separated by dot <class_name>.<method_name>(<parameters>)

Calling Static Methods – Examples Constant field Static method using System; double radius = 2.9; double area = Math.PI * Math.Pow(radius, 2); Console.WriteLine("Area: {0}", area); // Area: 26,4207942166902 double precise = 8.7654321; double round3 = Math.Round(precise, 3); double round1 = Math.Round(precise, 1); Console.WriteLine( "{0}; {1}; {2}", precise, round3, round1); // 8,7654321; 8,765; 8,8 Static method Static method

* 07/16/96 Constructors Constructors are special methods used to assign initial values of the fields in an object Executed when an object of a given type is being created Have the same name as the class that holds them Do not return a value A class may have several constructors with different set of parameters (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Constructors (2) Constructor is invoked by the new operator Examples: * 07/16/96 Constructors (2) Constructor is invoked by the new operator Examples: <instance_name> = new <class_name>(<parameters>) String s = new String("Hello!"); // s = "Hello!" String s = new String('*', 5); // s = "*****" DateTime dt = new DateTime(2009, 12, 30); DateTime dt = new DateTime(2009, 12, 30, 12, 33, 59); Int32 value = new Int32(1024); (c) 2007 National Academy for Software Development - http://academy.devbg.org. All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

Constructor Overloading Using constructor overloading, any numbers of constructor can be defined for the same class But ensure that each constructor must have different number and type of parameter de

Classified in 5 types 1) Default Constructor 2) Parameterized Constructor 3) Copy Constructor 4) Static Constructor 5) Private Constructor

Default Constructor constructor without any parameters every instance of the class will be initialized to same values not possible to initialize each instance of the class to different values

Parameterized Constructor A constructor with at least one parameter is called as parameterized constructor you can initialize each instance of the class to different values.

Copy Constructor A parameterized constructor that contains a parameter of same class type Main purpose : To initialize new instance to the values of an existing instance.

Static constructor You can create a constructor as static It will be invoked only once for any number of instances of the class When first instance of the class is created Is used to write the code that needs to be executed only once. And to initialize static fields of the class

Private Constructor Can create a constructor as private If at least one private constructor, then it is not possible to create an instance for the class. Private constructor is used to restrict the class from being instantiated. the public constructors can access the private constructors from within the class through constructor chaining.

Some unique points A class can have any number of constructors. A constructor doesn’t have any return type even void. A static constructor can not be a parameterized constructor. Within a class you can create only one static constructor.

Destructors The .NET framework has an in built mechanism called Garbage Collection to de- allocate memory occupied by the un-used objects destructor implements - statements to be executed during the garbage collection process destructor is a function with the same name as the name of the class but starting with the character ~

class Complex {. public Complex(). {. // constructor. }. ~Complex(). { class Complex { public Complex() { // constructor } ~Complex() { // Destructor } }

Unique points A class can only have one destructor. Destructors cannot be inherited or overloaded. Destructors cannot be called. They are invoked automatically. A destructor does not take modifiers or have parameters.

Structures Structures are similar to classes Structures are usually used for storing data structures, without any other functionality Structures can have fields, properties, etc. Using methods is not recommended Structures are value types, and classes are reference types (this will be discussed later) Example of structure System.DateTime – represents a date and time

What is a Namespace? Namespaces are used to organize the source code into more logical and manageable way Namespaces can contain Definitions of classes, structures, interfaces and other types and other namespaces Namespaces can contain other namespaces For example: System namespace contains Data namespace The name of the nested namespace is System.Data

Full Class Names A full name of a class is the name of the class preceded by the name of its namespace Example: Array class, defined in the System namespace The full name of the class is System.Array <namespace_name>.<class_name>

Including Namespaces The using directive in C#: Allows using types in a namespace, without specifying their full name Example: instead of using <namespace_name> using System; DateTime date; System.DateTime date;

Common Type System (CTS) CTS defines all data types supported in .NET Framework Primitive types (e.g. int, float, object) Classes (e.g. String, Console, Array) Structures (e.g. DateTime) Arrays (e.g. int[], string[,]) Etc. Object-oriented by design

CTS and Different Languages CTS is common for all .NET languages C#, VB.NET, J#, JScript.NET, ... CTS type mappings: CTS Type C# Type VB.NET Type System.Int32 int Integer System.Single float Single System.Boolean bool Boolean System.String string String System.Object object Object

Value and Reference Types In CTS there are two categories of types Value types Reference types Placed in different areas of memory Value types live in the execution stack Freed when become out of scope Reference types live in the managed heap (dynamic memory) Freed by the garbage collector

Value and Reference Types – Examples Value types Most of the primitive types Structures Examples: int, float, bool, DateTime Reference types Classes and interfaces Strings Arrays Examples: string, Random, object, int[]