|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
overloadingHi
I am 'returning' to the learning of C# after a change in jobs and I remember that I used to struggle with the defintion, purpose and function of 'overloading'. When I was beginning with C# I never fully understood what overloading was good for. Could someone please provide me with some words that define overloading, what it is used for and some really basic examples. Doug "Doug" <queanbe***@hotmail.com> wrote: Overloading refers to two things:> I used to struggle with the defintion, 1) Defining several (i.e >1) methods, constructors, indexers or operators which differ by signature only, where signature refers to the number and types of the parameters (aka formal arguments) to the methods, constructors, indexers or operators. 2) Calling an overloaded method, constructor, indexer or operator and resolving that call to a specific definition by matching the number and types of the actual arguments to the signature of the selected method, constructor, indexer or operator. > purpose and function of 'overloading'. Perhaps this can be best described by example: overloading permits a> When I was beginning with C# I never fully understood what > overloading was good for. method to have a single name (and thus a single semantic meaning), yet have specialized behaviour based on its type. So, imagine that somebody was annoyed that ICollection uses Count as the number of items while strings use Length, and they wanted to standardize their code so they used the same identifier everywhere. One possible way they could do this is with an overloaded static method: ---8<--- using System; using System.Collections; static class Fix { public static int GetCount(ICollection c) { return c.Count; } public static int GetCount(string s) { return s.Length; } } static class App { static void Main(string[] args) { Console.WriteLine(Fix.GetCount(args)); Console.WriteLine(Fix.GetCount(Environment.CommandLine)); } } --->8--- -- Barry Probably the best example is in a constructor method of a class.
Since constructors are generally used to initialize the object's default property values, you could imaging a class that has more than one constructor, one that takes no arguments (and properties would intialize to the class designer's default values) and one that takes some arguments (so certain properties would initialize at the class user's desired values, not the class designer's desired values). Show quoteHide quote "Doug" <queanbe***@hotmail.com> wrote in message news:446f99fc$0$7992$afc38c87@news.optusnet.com.au... > Hi > > I am 'returning' to the learning of C# after a change in jobs and I > remember that I used to struggle with the defintion, purpose and function > of 'overloading'. When I was beginning with C# I never fully understood > what overloading was good for. > > Could someone please provide me with some words that define overloading, > what it is used for and some really basic examples. > > Doug > Doug wrote:
> Hi We can look at it from a basic perspective.> > I am 'returning' to the learning of C# after a change in jobs and I remember > that I used to struggle with the defintion, purpose and function of > 'overloading'. When I was beginning with C# I never fully understood what > overloading was good for. > > Could someone please provide me with some words that define overloading, > what it is used for and some really basic examples. > > Doug Say you needed a function that adds two numbers. int add(int n1, int n2) { return n1 + n2; } Later on you find you need an add function that can add to floating point numbers. You already have a function named add, so what to do? In C# you can overload the function which mean you have two or more functions with the same name. The only difference required is that the functions signature be different. The functions signature is its name and parameters. So in C# you could do something like: int add(int n1, int n2) { return n1 + n2; } double add(double n1, double n2) { return n1 + n2; } Since the parameter types are different, the compiler can figure out which one to use by your function call. int n3 = add(1, 2); // calls the int based func. double n4 = add(1.2, 2.2); // calls the double based func. This was a pretty basic description. Google will give you tons of better examples. Jim -- =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= There's no place like 127.0.0.1 =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= JimD Central FL, USA, Earth, Sol Another excellent use of overloading is the ability to use default values
for parameters, as in: public void foo (int x, int y) { this.SomeField += x; this.SomeotherField -= y; } public void foo() { foo(20, 20); } If x and y are usually 20, you can call the function with no parameters. In exceptional situations, you can pass whatever parameters you need to. -- Show quoteHide quoteHTH, Kevin Spencer Microsoft MVP Professional Numbskull The man who questions opinions is wise. The man who quarrels with facts is a fool. "JimD" <J**@keeliegirl.dyndns.org> wrote in message news:OV_bg.24240$HA2.15756@tornado.tampabay.rr.com... > Doug wrote: >> Hi >> >> I am 'returning' to the learning of C# after a change in jobs and I >> remember >> that I used to struggle with the defintion, purpose and function of >> 'overloading'. When I was beginning with C# I never fully understood >> what >> overloading was good for. >> >> Could someone please provide me with some words that define overloading, >> what it is used for and some really basic examples. >> >> Doug > > We can look at it from a basic perspective. > > Say you needed a function that adds two numbers. > > int add(int n1, int n2) > { > return n1 + n2; > } > > Later on you find you need an add function that can add to floating > point numbers. You already have a function named add, so what to do? > In C# you can overload the function which mean you have two or more > functions with the same name. The only difference required is that the > functions signature be different. The functions signature is its name > and parameters. So in C# you could do something like: > > int add(int n1, int n2) > { > return n1 + n2; > } > double add(double n1, double n2) > { > return n1 + n2; > } > > Since the parameter types are different, the compiler can figure out > which one to use by your function call. > > int n3 = add(1, 2); // calls the int based func. > double n4 = add(1.2, 2.2); // calls the double based func. > > This was a pretty basic description. Google will give you tons of > better examples. > > Jim > -- > =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= > There's no place like 127.0.0.1 > =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= > JimD > Central FL, USA, Earth, Sol
about performace between ++i and i++
Converting Vb.net to C# issues Running Windows Forms App From Network Share Abstract/Sealed method Covariance question control tcp settings Find inherited classes / walk inheritance tree Two diffrent Process tries to open file at the same time..... google calender Encoding characters for HTML |
|||||||||||||||||||||||