Docsity

Overloading Members in C#: Understanding Signatures and Rules, Slides of C Sharp Programming

Partial preview of the text

Download Overloading Members in C#: Understanding Signatures and Rules and more Slides C Sharp Programming in PDF only on Docsity!

Programming Pillars

Overloading

Overloading Members

(Introduction)

 Overloading provides the means by which

multiple procedures share the same name

 An overloaded procedure or constructor is

generically referred to as an overloaded

member

Using Overloaded Members

(Example 1)

 The ToString() method of numeric data types is overloaded  One method accepts no arguments and converts the value to a string  A second method accepts one argument – a format string  Examples:

lblToString1.Text = _ System.Int32.MaxValue.ToString() lblToString2.Text = _ System.Int32.MaxValue.ToString("C")

Using Overloaded Members

(Example 2)

 The StreamWriter constructor is overloaded  Note the two different argument lists  If the file exists, it will be overwritten CurrentWriter = new _ StreamWriter(ofdCurrent.FileName)

 Use the second argument to append to a file CurrentWriter = new StreamWriter(ofdCurrent.FileName, True)

Rules for Overloading Members

 By creating multiple members having the same name, those members will be considered overloaded  Constructors can be overloaded  Return data type for overloaded methods must be the same

Overloading Members (Example)

 The declaration for three overloaded members (see IS389OverloadingCSharp.zip for code)

How .NET Calls an

Overloaded Member

 Remember the calling procedure has

arguments

 .NET calls an overloaded procedure based on

the argument count and types

 Widening type coercion is used to match

arguments

 A syntax error will occur if a match cannot be

found

UML Class diagrams

(Overloading)

 Each overloaded member has an entry in the

UML class diagram

 Specify members, parameters, and return data

types as before

 Visio requires that the signature vary for each

overloaded member