C#.Net Software Development Versions 1.0. Overview  Rational for Extension Methods  C# Specification for Extension Methods  Requirements for Extension.

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 17 Templates.
Programming Languages and Paradigms The C Programming Language.
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Generics and the ArrayList Class
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 4: Writing Classes
ISQA 360 – July 8, 2002 Methods Dr. Sergio Davalos.
Classes and Methods Write a class called C1 containing: A static variable SV of type int. A static method SM with one parameter, of type C1, and a void.
Overloading methods review When is the return statement required? What do the following method headers tell us? public static int max (int a, int b)
Principles of Object-Oriented Software Development The language Java.
C# 3.0 Tom Roeder CS fa. Version 3 From PDC 2005 preview compiler available LINQ: language-integrated query High level points: adds native query.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Classes: A Deeper Look Systems 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.
Introduction to C++ Systems Programming.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 14: Overloading and Templates.
Abstract Classes and Interfaces Lecture 2 – 9/6/2012.
Extension Methods Programming in C# Extension Methods CSE Prof. Roger Crawfis.
C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.
C#.Net Development Version 1.0. Overview Nullable Datatype Description ? HasValue Lifted Conversions null coalescing operator ?? Partial Classes Copyright.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
Chapter 4 Writing Classes Part 2. © 2004 Pearson Addison-Wesley. All rights reserved4-2 Classes A class can contain data declarations and method declarations.
Procedural programming in Java Methods, parameters and return values.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Operator Overloading.
Module 10: Inheritance in C#. Overview Deriving Classes Implementing Methods Using Sealed Classes Using Interfaces Using Abstract Classes.
Introduction to Generics
Lambda Expressions Version 1.0
Module 8: Delegates and Events. Overview Delegates Multicast Delegates Events When to Use Delegates, Events, and Interfaces.
ITF11006.NET The Basics. Data Types – Value Types – Reference Types Flow Control – Conditional – Loop Miscellaneous – Enumerations – Namespaces Class.
CMSC 150 CLASSES CS 150: Mon 6 Feb 2012 Images: Library of Congress.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
1 Lecture 3 More about C++. 2 Topic for today More about classMore about class –Init list –Inline functions –Const –Default function parameters –Static.
C# Operator Overloading and Type Conversions C#.NET Software Development Version 1.0.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 10 th Lecture Pavel Ježek
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
CSC 143F 1 CSC 143 Constructors Revisited. CSC 143F 2 Constructors In C++, the constructor is a special function automatically called when a class instance.
Chapter 4 Generic Vector Class. Agenda A systemic problem with Vector of Object – Several approaches at a solution – Generic structures Converting classes.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 11 th Lecture Pavel Ježek
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
CSI 3125, Preliminaries, page 1 Overloading Methods In Java it is possible to define two or more methods within the same class that share the same name,
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Const declares immutable value void Test(char* t) { t++;// OK, address is not const t[0] = 'a'; // OK, char is not const } void Test2(const char* t) {
Version 1.0 C#.Net Software Development. Overview Namespaces using Statement Pre-Processor Directives 2Copyright © by Dennis A. Fairclough all.
Chapter  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.
© 2004 Pearson Addison-Wesley. All rights reserved September 5, 2007 Packages & Random and Math Classes ComS 207: Programming I (in Java) Iowa State University,
Java Generics. Lecture Objectives To understand the objective of generic programming To be able to implement generic classes and methods To know the limitations.
C#.Net Software Development Version 1.0. Overview Inheritance Member Access Constructors Polymorphism (Name Hiding) Multilevel Hierarchy Virtual and VTable.
Lesson 3 Functions. Lesson 3 Functions are declared with function. For example, to calculate the cube of a number function function name (parameters)
Eine By: Avinash Reddy 09/29/2016.
Test 2 Review Outline.
Lambda Expressions Version 1.1
Examples of Classes & Objects
Introduction to C++ Systems Programming.
Namespaces & Preprocessor
Chapter 4: Writing Classes
Chapter 6 Methods: A Deeper Look
Overloading and Overriding
Classes & Objects: Examples
Chapter 4 Writing Classes.
Multidimensional Arrays
CS2011 Introduction to Programming I Methods (II)
16 Strings.
Classes, Objects and Methods
Corresponds with Chapter 5
Presentation transcript:

C#.Net Software Development Versions 1.0

Overview  Rational for Extension Methods  C# Specification for Extension Methods  Requirements for Extension Methods  Invoking Extension Methods  Importing Extension Methods  Generic Extension Methods 2 Copyright © 2008 by Dennis A. Fairclough all rights reserved.

Extension Method Rational  “Extension methods, make it possible to extend existing types and constructed types with additional methods.”  “Extension methods have all the capabilities of regular static methods. In addition, once imported, extension methods can be invoked using instance method syntax.”  Extend sealed and other classes C# 3.0 Specification 3 Copyright © 2008 by Dennis A. Fairclough all rights reserved.

Extension Methods – C# Spec  Extension methods  When the first parameter of a method includes the this modifier, that method is said to be an extension method. Extension methods can only be declared in non-generic, non-nested static classes. The first parameter of an extension method can have no modifiers other than this, and the parameter type cannot be a reference (pointer) type. Copyright © 2008 by Dennis A. Fairclough all rights reserved. 4

Requirements  Must be in a static class  Must be a static method in a static class  First parameter (this, …)  Example static public char LastChar(this string lhs) { return lhs[lhs.Length-1]; } char endchar = “The last string”.LastChar(); 5 Copyright © 2008 by Dennis A. Fairclough all rights reserved.

C# Spec  The following is an example of a static class that declares two extension methods:  public static class Extensions { public static int ToInt32(this string s) { return Int32.Parse(s); }  public static T[] Slice (this T[] source, int index, int count) { if (index < 0 || count < 0 || source.Length – index < count) throw new ArgumentException(); T[] result = new T[count]; Array.Copy(source, index, result, 0, count); return result; } } Copyright © 2008 by Dennis A. Fairclough all rights reserved. 6

C# Spec  An extension method is a regular static method. In addition, where its enclosing static class is in scope, an extension method can be invoked using instance method invocation syntax (§ ), using the receiver expression as the first argument. Copyright © 2008 by Dennis A. Fairclough all rights reserved. 7

C# Spec The following program uses the extension methods declared previously: static class Program { static void Main() { string[] strings = { "1", "22", "333", "4444" }; foreach (string s in strings.Slice(1, 2)) { Console.WriteLine(s.ToInt32()); } } } Copyright © 2008 by Dennis A. Fairclough all rights reserved. 8

Invoking an Extension Method char endchar = “The last string”.LastChar(); same as char endchar = ExtensionClass.LastChar(“The last string”); 9 Copyright © 2008 by Dennis A. Fairclough all rights reserved.

Importing Extension Methods  Import extension method namespaces with the using syntax.  Instance methods take precedence over extension methods. 10 Copyright © 2008 by Dennis A. Fairclough all rights reserved.

Generic Extension Methods  Generic Form  static access_specifier type ident (this, …){ … }  Example  static public T GenExten (this T exid){ … return exid; } 11 Copyright © 2008 by Dennis A. Fairclough all rights reserved.

What did you learn?  ?? 12 Copyright © 2008 by Dennis A. Fairclough all rights reserved.