Chapter 8 Advanced C#.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

EC-241 Object-Oriented Programming
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
C# vs. C++ What's Different & What's New. An example C# public sometype myfn { get; set; } C++ public: sometype myfn { sometype get (); void set (sometype.
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Copyright © 2006 Thomas P. Skinner1 Chapter 5 Indexers, Interfaces, and Enumerators.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, this reading: self-checks: #13-17 exercises:
C#.Net Development Version 1.0. Overview Nullable Datatype Description ? HasValue Lifted Conversions null coalescing operator ?? Partial Classes Copyright.
Introduction to Building Windows 8.1 & Windows Phone Applications.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
ILM Proprietary and Confidential -
E FFECTIVE C# 50 Specific Ways to Improve Your C# Second Edition Bill Wagner محمد حسین سلطانی.
Introduction to C#. Why C#? Develop on the Following Platforms ASP.NET Native Windows Windows 8 / 8.1 Windows Phone WPF Android (Xamarin) iOS (Xamarin)
1 Interfaces and Abstract Classes Chapter Objectives You will be able to: Write Interface definitions and class definitions that implement them.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
Class and Structure. 2 Structure Declare using the keyword struct purpose is to group data Default visibility mode is public A struct doesn't have a constructor.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 10 th Lecture Pavel Ježek
Recitation 8 User Defined Classes Part 2. Class vs. Instance methods Compare the Math and String class methods that we have used: – Math.pow(2,3); – str.charAt(4);
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Properties and Indexers 9. © Aptech Ltd.Building Applications Using C# / Session 92 Objectives  Define properties in C#  Explain properties, fields,
Classes - Intermediate
Module 13: Properties and Indexers. Overview Using Properties Using Indexers.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Unit 2. Constructors It initializes an object when it is created. It has same as its class and syntactically similar to a method. Constructor have no.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Lecture 10 Collections Richard Gesick.
Chapter 10 Thinking in Objects
C# for C++ Programmers 1.
Chapter - 4 OOP with C# S. Nandagopalan.
Chapter 10 Thinking in Objects
Examples of Classes & Objects
Arrays 3/4 By Pius Nyaanga.
BİL527 – Bilgisayar Programlama I
Objects as a programming concept
Ch 10- Advanced Object-Oriented Programming Features
Chapter 10 Thinking in Objects
Methods Attributes Method Modifiers ‘static’
Inheritance & Polymorphism
Advanced Programming in Java
Advanced Programming in Java
OOP’S Concepts in C#.Net
Instance Method Review - CIS 1068 Program Design and Abstraction
Dr Shahriar Bijani Winter 2017
Initializing Arrays char [] cArray3 = {'a', 'b', 'c'};
Functions Used to write code only once Can use parameters.
Pass by Reference, const, readonly, struct
Introduction to Classes
Chapter 9 Thinking in Objects
Overloading the << operator
Classes & Objects: Examples
Chapter 9 Objects and Classes
Building Java Programs
CSCI 3328 Object Oriented Programming in C# Chapter 3: Introduction to Classes and Objects – Exercises UTPA – Fall 2012 This set of slides is revised.
Chapter 9 Thinking in Objects
JAVA Constructors.
class PrintOnetoTen { public static void main(String args[]) {
Building Java Programs
Tonga Institute of Higher Education
CIS 199 Final Review.
Arrays 3/4 June 3, 2019 ICS102: The course.
Chapter 6 Arrays.
Overloading the << operator
Review for Midterm 3.
ITE “A” GROUP 2 ENCAPSULATION.
Chengyu Sun California State University, Los Angeles
Interfaces, Enumerations, Boxing, and Unboxing
Presentation transcript:

Chapter 8 Advanced C#

Chapter Objectives Indexer Overloading Operators Customized Conversion Building a Custom Indexer Overloading Operators +, ==, <, >=, Customized Conversion

Indexers Indexers provide access to an object's members via a subscript operator Indexers permit instances of a class or struct to be indexed in the same way as arrays. Indexers are similar to properties except that their accessors take parameters. Defining an indexer allows you to create classes that act like "virtual arrays." Instances of that class can be accessed using the [ ] array access operator. Defining an indexer in C# is similar to defining operator [ ] in C++, but is considerably more flexible. Use indexers only when the array-like abstraction makes sense.

Declaration get { return items[key]; } set { items[key] = value; } public object this[int key] { get { return items[key]; } set { items[key] = value; } } Indexers may be overloaded and abstract/virtual keywords can also be used public abstract object this[int key] get ; set ;

Example class MyClass { private string[ ] data = new string[5]; public string this [int index] get return data[index]; } set data[index] = value;

Client class MyClient { public static void Main() MyClass mc = new MyClass(); mc[0] = "Scott"; mc[1] = "A3-126"; mc[2] = "35th Street"; mc[3] = "Boston"; mc[4] = "USA"; Console.WriteLine("{0},{1},{2},{3},{4}", mc[0],mc[1],mc[2],mc[3],mc[4]); }

telPhone Class public class telPhone { private int phoneno; private string Name; public telPhone() phoneno = 0; Name = null; } public telPhone(int p, string n) phoneno = p; Name = n; public int Phone get { return phoneno; }

Directory Class using System; using System.Collections; using System.Collections.Specialized; public class Directory { // This class maintains a telephone directory private ListDictionary telDir; public Directory() telDir = new ListDictionary(); }

Directory Class… // Overloaded Indexer - The string indexer. public telPhone this[string name] { get { return (telPhone)telDir[name];} set { telDir.Add(name, value);} } // Overloaded Indexer - The int indexer. public telPhone this[int item] get { return (telPhone)telDir[item];} set { telDir.Add(item, value);}

Client { public static void Main(string[] args) public class TelephoneApp { public static void Main(string[] args) Directory d = new Directory(); d["Scott"] = new telPhone(26613237,"Scott"); d["Allen"] = new telPhone(56789456,"Allen"); telPhone scotttel = d["Scott"]; Console.WriteLine("Telephone No. of Allen is = {0}", scotttel.Phone); Console.WriteLine("Telephone No. of Allen is = {0}", d["Scott"]); }

Refer to OverloadOps folder Operator Overloading Same as C++ public struct Point { public int x, y; …… public static Point operator + (Point p1, Point p2) return new Point(p1.x + p2.x, p1.y + p2.y); } public static bool operator == (Point p1, Point p2) return p1.Equals(p2); } Refer to OverloadOps folder

Custom Type Conversion Square sq; ….. Rectangle rect = (Rectangle) sq;