Changing Comparison Programming in C# Changing Comparison CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.

Slides:



Advertisements
Similar presentations
Generics, Lists, Interfaces
Advertisements

CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 12 th -13 th Lecture Pavel Ježek.
Polymorphism. 3 Fundamental Properties of OO 1. Encapsulation 2. Polymorphism 3. Inheritance These are the 3 building blocks of object-oriented design.
Language Fundamentals in brief C# - Introduction.
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Equality Programming in C# Equality CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.
Interfaces Reference: Joe Hummel. 2 UCN Technology: Computer Science 2012 Objectives “Good class design starts with good application design — how many.
1 Role of Interfaces in Large-Scale Software l API:Application Programmer’s Interface: The methods that allow a programmer to manipulate the data elements.
Unit 261 Introduction to Searching and Sorting Comparable Interface Comparator Interface Algorithm Complexity Classes Exercises.
1 Lecture 08(API)Lecture 11 Java API and Interfaces l API:Application Programmer’s Interface: The methods that allow a programmer to manipulate the data.
Collections. 2 Objectives Explore collections in System.Collections namespace –memory management –containment testing –sorting –traversal.
1 Java Object Model Part 2: the Object class. 2 Object class Superclass for all Java classes Any class without explicit extends clause is a direct subclass.
1 Introduction to Searching and Sorting Comparable Interface -Reading p Comparator Interface.
3. Data Types. 2 Microsoft Objectives “.NET is designed around the CTS, or Common Type System. The CTS is what allows assemblies, written in different.
Unit 261 Introduction to Searching and Sorting Comparable Interface Comparator Interface Algorithm Complexity Classes Exercises.
Introduction to Searching and Sorting
5. OOP. 2 Microsoft Objectives “Classes, objects and object-oriented programming (OOP) play a fundamental role in.NET. C# features full support for the.
Comparing Objects in Java. The == operator When you define an object, for instance Person p = new Person("John", 23); we talk about p as if its value.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
 Krzysztof Cwalina Program Manager Microsoft Corporation  Brad Abrams Product Unit Manager.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 7 th & 8 th Lecture Pavel Ježek.
Equality Programming in C# Equality CSE / ECE 668 Prof. Roger Crawfis.
BIM313 – Advanced Programming Techniques Object-Oriented Programming 1.
Data Binding to Controls Programming in C# Data Binding to Controls CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.
Common Type System .NET Types Hierarchy, Cloning, Comparing, Collection Iterators, Value and Reference Types SoftUni Team Technical Trainers Software.
FEN 2012 UCN Technology: Computer Science1 C# - Introduction Language Fundamentals in Brief.
C# Intro Programming languages and programs: Source code and object code Editors and compilers C# fundamentals: Program structure Classes and Objects Variables.
.NET Types Hierarchy, Cloning, Comparing, Value and Reference Types, Parameters Passing Svetlin Nakov Telerik Software Academy academy.telerik.com Technical.
Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]
Delegates Programming in C# Delegates CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.
Data structures and algorithms in the collection framework 1.
Neal Stublen What’s an indexer?  An array has an indexer… int[] myArray = new int[5]; for (int index = 0; index < 5; ++index) {
Processing Sequences of Elements Technical Trainer Telerik Corporation Doncho Minkov.
Some Standard Classes Goals The Object class The String class Wrapper classes The Math class Random Numbers.
Common Type System.NET Types Hierarchy, Cloning, Comparing, Collection Iterators, Value and Reference Types Svetlin Nakov Technical Trainer
Object Oriented Programming Generic Collections and LINQ Dr. Mike Spann
Operator Overloading Week 5.
Neal Stublen Tonight’s Agenda  Indexers  Delegates and events  Operator overloading  Class inheritance  Q&A.
Dictionaries, Hash Tables, Collisions Resolution, Sets Svetlin Nakov Telerik Corporation
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
Arrays and Indexers Programming in C# Arrays and Indexers CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.
C# Interfaces and RTTI CNS 3260 C#.NET Software Development.
1 CSE 331 Comparing objects; Comparable, compareTo, and Comparator slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R.
Introduction to Java Collection. Java Collections What are they? –A number of pre-packaged implementations of common ‘container’ classes, such as LinkedLists,
Run on Windows.NET as system component Run on VM (CLR) Black box compilers Edit in Visual Studio Proprietary Run everywhere Deploy with app Compile.
יסודות מדעי המחשב – תרגול 6
Computing with C# and the .NET Framework
Interfaces and Generics
16: Equals Equals Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Multidimensional Arrays
Simplifying Fractions
CSC 205 Java Programming II
IST311 / 602 Cleveland State University – Prof. Victor Matos
Programming in C# Properties
Collections 24: Collections Programming C# © 2003 DevelopMentor, Inc.
5.1 Being Objects and A Glimpse into Coding
Implementing Polymorphism
Introduction to Searching and Sorting
Comparable and Comparator Interfaces
Programming in C# Comparison (Sorting)
CSC 113: Computer programming II
Generic Classes and Methods
Comparable and Comparator Interfaces
IST311 / 602 Cleveland State University – Prof. Victor Matos
鄭士康 國立台灣大學 電機工程學系/電信工程研究所/ 資訊網路與多媒體研究所
Advanced .NET Programming I 3rd Lecture
Comparable and Comparator Interfaces
5. OOP OOP © 2003 Microsoft.
Presentation transcript:

Changing Comparison Programming in C# Changing Comparison CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis

IComparer IComparer is used to provide pluggable (or interchangable) comparisons. Used with a type, not part of the type. public interface IComparer { int Compare(object x, object y); // -1 if x y } IComparer implementations: Comparer, CaseInsensitiveComparer: for string comparisons

Custom IComparer Creation of table of strings: string[][] Table = { new string[] {"John", "Dow", "programmer"}, new string[] {"Bob", "Smith", "agent"}, new string[] {"Jane", "Dow", "assistant"}, new string[] {"Jack", "Sparrow", "manager"} }; Printing the table: foreach (string[] Row in Table) { Console.WriteLine(String.Join(", ", Row)); }

Custom IComparer Comparer for single table (array) column: class ArrayComparer : IComparer where T : IComparable { private int m_Index; public ArrayComparer(int Index) { m_Index = Index; } public int Compare(T[] x, T[] y) { return x[m_Index].CompareTo(y[m_Index]); } Printing the table: Array.Sort(Employees, new ArrayComparer (2)); foreach (string[] Row in Employees) { Console.WriteLine(String.Join(", ", Row)); } Bob, Smith, agent Jane, Dow, assistant Jack, Sparrow, manager John, Dow, programmer

"BCL v2-friendly" Custom Classes 1/3 In order to cooperate smoothly with other BCL classes in the framework 2.0, custom classes should: override ToString and GetHashCode overload == and != implement ICloneable public interface ICloneable { object Clone(); } class MyClass : ICloneable { public object Clone() { return MemberwiseClone(); } }

"BCL v2-friendly" Custom Classes 2/3 implement IComparable and IComparable public interface IComparable { int CompareTo(object obj); // -1: this obj } public interface IComparable { int CompareTo(T obj); // -1: this obj } class Fraction : IComparable, IComparable { private int n, d; public int CompareTo(object o) { return CompareTo((Fraction) o); } public int CompareTo(Fraction f) { return n*f.d – f.n*d }

"BCL v2-friendly" Custom Classes 3/3 override Equals(object) and implement IEquatable public class Object { public virtual bool Equals(Object obj); … } public interface IEquatable { bool Equals(T other); } class Fraction : IEquatable {// equal to class Fraction : object, IEquatable int n, d; public override bool Equals(object o) { return Equals((Fraction) o); } public bool Equals(Fraction f) { return f.n == n && f.d == d; }