Chengyu Sun California State University, Los Angeles

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 9 Delegates and Events.
Advertisements

Road Map Introduction to object oriented programming. Classes
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++
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
OOP Languages: Java vs C++
4.1 JavaScript Introduction
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Extension Methods Programming in C# Extension Methods CSE Prof. Roger Crawfis.
Putting it all together: LINQ as an Example. The Problem: SQL in Code Programs often connect to database servers. Database servers only “speak” SQL. Programs.
CS320 Web and Internet Programming Java Beans and Expression Language (EL) Chengyu Sun California State University, Los Angeles.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler.
ILM Proprietary and Confidential -
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 10 th Lecture Pavel Ježek
From C++ to C# Part 5. Enums Similar to C++ Similar to C++ Read up section 1.10 of Spec. Read up section 1.10 of Spec.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Andrew(amwallis) Classes!
C# for C++ Programmers 1.
Creating Your Own Classes
INF230 Basics in C# Programming
Chapter 13: Overloading and Templates
CS5220 Advanced Topics in Web Programming JavaScript and jQuery
Variables, Environments and Closures
Chapter Eleven Handling Events.
JavaScript Syntax and Semantics
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
Generics, Lambdas, Reflections
Functional Programming with Java
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
Lecture 9 Concepts of Programming Languages
User-Defined Classes and ADTs
.NET and .NET Core 9. Towards Higher Order Pan Wuming 2017.
6 Delegate and Lambda Expressions
Variables, Environments and Closures
Chapter 9 Objects and Classes
Closure Closure binds a first-class function and a lexical environment together This is a complex topic, so we will build up our understanding of it we.
Lesson 7. Events, Delegates, Generics.
CS5220 Advanced Topics in Web Programming JavaScript Basics
CS2011 Introduction to Programming I Objects and Classes
User-Defined Classes and ADTs
CS2011 Introduction to Programming I Methods (II)
Functional interface.
CS5220 Advanced Topics in Web Programming Node.js Basics
CS3220 Web and Internet Programming JavaScript Basics
CS3220 Web and Internet Programming Expression Language (EL)
CS5220 Advanced Topics in Web Programming Angular – TypeScript
CS360 Client/Server Programming Using Java
Chengyu Sun California State University, Los Angeles
CIS 199 Final Review.
CS3220 Web and Internet Programming Expression Language (EL)
Chengyu Sun California State University, Los Angeles
CS5220 Advanced Topics in Web Programming Angular – TypeScript
Chengyu Sun California State University, Los Angeles
CIS 136 Building Mobile Apps
CS3220 Web and Internet Programming JavaScript Basics
CS5220 Advanced Topics in Web Programming More Node.js
Creating and Using Classes
CS4540 Special Topics in Web Development LINQ to Objects
Chengyu Sun California State University, Los Angeles
Generics, Lambdas and Reflection
Lecture 9 Concepts of Programming Languages
Chengyu Sun California State University, Los Angeles
Presentation transcript:

Chengyu Sun California State University, Los Angeles CS4540 Special Topics in Web Development C# for Java Programmers: Advanced Language Features Chengyu Sun California State University, Los Angeles

Overview Indexers Extension methods Delegates Anonymous methods Lambda expressions event

Access List Elements By Index List<string> translations = …. for( int i=0 ; i < translations.Count ; ++i ) Console.WriteLine("{0}. {1}", i+1, transactions[i]); List elements can be accessed like array elements Where does it say this in the List API documentation??

Indexer Syntax public T this[int index] { get; set; } A type parameter for a generic collection, or a regular type for a non-generic one

About Indexer It's a property backed by a getter and/or a setter The type of index doesn't have to be int Internally the index property has a name which can be used by other languages Default name Item Can be customized with an IndexerName attribute Some classes that have indexers: List, Dictionary, String

Add Methods To A Class Example: add a method Substring(string a, string b) to String which returns the substring that starts with a and ends with b, e.g. "Amazon".Substring("a","z")  "Amaz" How can we do this when String is a sealed class (like a final class in Java) which can not be in inherited

An Extension Method public static class MyExtensions { public static string Substring( this string s, string begin, string end) { … } } var word = "Amazon"; Console.WriteLine( word.Substring("a", "z") );

About Extension Methods Static methods invoked using instance method syntax Must be defined in a non-generic, static class Use with caution – static, utility methods are not replacements for proper OO design & implementation

The Need for Function (or Method) References GUI programming E.g. button click Asynchronous programming E.g. Ajax request Some object has a field/property that is of function type The field/property is assigned a function (a.k.a. event handler or callback function) The function will be called when something happens

Function References in Some Language C and C++ have function pointers In JavaScript, functions are objects (i.e. no special treatment needed) What's about Java? No function references Use objects that implement certain interfaces (e.g. ActionListener)  event handler/callback function must have a certain name (in addition to parameters and return types)

A C# Delegate // Define a delegate type that represents references to // methods with a particular parameter list and return type public delegate int BinaryOp(int op1, int op2); // A method static int Add(int a, int b) => a + b; // Create a delegate object that references the method BinaryOp bop = new BinaryOp(Program.Add); // Call the method int sum = bop(10, 20);

Multicast C# delegates support multicast, i.e. references multiple methods and invoke them together int Add2(int x, int y) => x + y; bop += (new Program()).Add2; Overloaded += operator that adds a method to the list of methods referenced by the delegate

Simplify Use of Delegates Generic delegates for methods take up to 16 arguments Action<> for methods that return void Func<> for methods that return a value Method group conversion allows providing a method in place of a delegate

Anonymous Method Can access variables outside the method int c = 100; BinaryOp bop = delegate(int a, int b) { return a+b+c; }; Must have a ; after the method body

Lambda Expression (parameter list) => { statements; } The simpler and preferred way to write anonymous methods, e.g. int c = 100; BinaryOp bop = (int a, int b) => { return a+b+c; };

Syntactical Variations of Lambda Expression Action<> line = () => { Console.WriteLine(); }; () => Console.WriteLine(); Func<int,int> square = (int x) => { return x*x; }; (int x) => x*x; (x) => x*x; x => x*x;

Closure Example in JavaScript function foo(){ let value=10; return () => {console.log(value++);} } var bar1 = foo(); var bar2 = foo(); bar1(); // ?? bar2(); // ??

Closure in C# Like functions in JavaScript, anonymous methods and lambda expression in C# form closures, i.e. the combination of a function and the lexical environment within which that function was declared Example??

Delegate Example: List.FindAll() Use List.FindAll() to find all the people whose last name is Doe

Event Handling Example Register two event handlers for the text entered event of MockTextField 1. Using regular delegate The event handlers object must be private to prevent client code messing it up 2. Using event The compiler automatically provides registration and unregistration methods and guard against undesirable actions

Readings Pro C# 7: Chapter 10, 11