Detailed introduction to Common methods in C #

Source: Internet
Author: User
This article describes a detailed description of the methods in C #. Need a friend to reference under

1. Let the method return multiple parameters

1.1 Defining variables to save results in the method body

The 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 Pide (int x, int y)         {             quotient = x/y;             remainder = x% y;         }         static void Main (string[] args)         {             program.pide (6,9);             Console.WriteLine (program.quotient);             Console.WriteLine (Program.remainder);             Console.readkey ();         }     } }

1.2 Using output and input parameters

The code is as follows:

Using system;using system.collections.generic;using system.linq;using system.text;namespace Method{    class Program    {public       static void pide (int x, int. y, out int quotient, out int remainder)        {            quotient = x/y;
  remainder = x% y;        }        static void Main (string[] args)        {            int quotient, remainder;            Pide (6,9,out quotient,out remainder);            Console.WriteLine ("{0} {1}", Quotient,remainder);            Console.readkey ();}}}    

2. Overloading of methods

Method overloading is an important extension of object-oriented to structured programming features

The methods that make up the overloads have the following characteristics:

(1) Same method name

(2) different method parameter list

Judging the above 2nd standard has three points, satisfies any point can determine the method parameter list is different:

(1) The number of method parameters is different:

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

(3) The method has the same number of parameters and parameter types, but the parameter types appear in different order of precedence,

It is important to note that the method return value type is not a judgment condition for the method overload.

3. Methods of hiding

The code is as follows:

Namespace method hides {     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");     }}}

The code is as follows:

Namespace method hides {    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");    }}}


A member is private if it does not indicate that the member stores permissions.

The code is as follows:

Namespace method hides {     class program     {         static void Main (string[] args)         {             parent p1= new parent ();             Parent P2 = new Child ();             P1.show ();//Parent class Method             p2.show ();//Parent Method             ((child) P2). Show ();//Parent 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 overrides and calls to virtual methods

The code is as follows:

namespace method override {class Program {static void Main (string[] args)             {Parent P1 = new parent ();             Parent P2 = new Child ();             P1.show ();             P2.show ();         ((Parent) p2). Show ();//Sub-class method Console.readkey ();         }} class Parent {public virtual void Show () {Console.WriteLine ("Parent Method"); }} class Child:parent {public override void Show () {Console.WriteLine ("Subclass Method")         ; }     } }
Related Article

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.