Detailed introduction of methods in C #

Source: Internet
Author: User

1. Let the method return multiple parameters

1.1 results of defining variables stored in methods in Vitro

Copy codeThe Code is as follows: using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;

Namespace Method
{
Class Program
{
Public static int quotient;
Public static int remainder;
Public static void Divide (int x, int y)
{
Quotient = x/y;
Remainder = x % y;
}
Static void Main (string [] args)
{
Program. Divide (6, 9 );
Console. WriteLine (Program. quotient );
Console. WriteLine (Program. remainder );
Console. ReadKey ();

}
}
}

1.2 use output and input parameters
Copy codeThe Code is as follows: using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;

Namespace Method
{
Class Program
{
Public static void Divide (int x, int y, out int quotient, out int remainder)
{
Quotient = x/y;
Remainder = x % y;
}
Static void Main (string [] args)
{
Int quotient, remainder;
Divide (6, 9, out quotient, out remainder );
Console. WriteLine ("{0} {1}", quotient, remainder );
Console. ReadKey ();
}
}
}

2. Method Overloading

Method Overloading is an important extension of object-oriented programming features.

Methods that constitute heavy loads have the following features:

(1) Same method name

(2) The method parameter list is different.

There are three criteria for determining the second point above. If any of the criteria is met, the list of methods parameters may be different:

(1) The number of method parameters is different:

(2) The method has the same number of parameters, but the parameter types are different.

(3) methods have the same number of parameters and parameter types, but the order of parameter types is different,

Note that the type of the return value of a method is not a condition for determining method overloading.

3. Hide Methods

Copy codeThe Code is as follows: namespace method hiding
{
Class Program
{
Static void Main (string [] args)
{
Parent p = new Child ();
P. show ();
Console. ReadKey ();
}
}
Class Parent
{
Public void show ()
{
Console. Write ("parent class method ");
}
}
Class Child: Parent
{
Public new void show ()
{
Console. Write ("subclass method ");
}
}
}

Copy codeThe Code is as follows: namespace method hiding
{
Class Program
{
Static void Main (string [] args)
{
Parent. show ();
Console. ReadKey ();
Child. show (); // parent class Method
}
}
Class Parent
{
Public static void show ()
{
Console. Write ("parent class method ");
}
}
Class Child: Parent
{
Public static new void show ()
{
Console. Write ("subclass method ");
}
}
}

If the storage permission of a Member is not specified, all members are private.
Copy codeThe Code is as follows: namespace method hiding
{
Class Program
{
Static void Main (string [] args)
{
Parent p1 = new Parent ();
Parent p2 = new Child ();
P1.show (); // parent class Method
P2.show (); // parent class Method
(Child) p2). show (); // parent class Method
Console. ReadKey ();
}
}
Class Parent
{
Public void show ()
{
Console. WriteLine ("parent class method ");
}
}
Class Child: Parent
{
New void show ()
{
Console. WriteLine ("subclass method ");
}
}
}

4. Method rewriting and virtual method calling
Copy codeThe Code is as follows: namespace method Rewriting
{
Class Program
{
Static void Main (string [] args)
{
Parent p1 = new Parent ();
Parent p2 = new Child ();
P1.show ();
P2.show ();
(Parent) p2). show (); // subclass Method
Console. ReadKey ();
}
}
Class Parent
{
Public virtual void show ()
{
Console. WriteLine ("parent class method ");
}
}
Class Child: Parent
{
Public override void show ()
{
Console. WriteLine ("subclass method ");
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.