Detailed introduction to common methods in C#

Y2J
Release: 2017-04-21 14:15:14
Original
1342 people have browsed it

This article introduces the detailed description of methods in C#. Friends who need it can refer to it

1. Let the method return multiple parameters

1.1 Define variables outside the method to save the results

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(); } } }
Copy after login

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(); } } }
Copy after login

2. Method overloading

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

The methods that constitute overloading have the following characteristics:

(1) The method names are the same

(2) The method parameter lists are different

There are three criteria for judging the second point above. If any one of the points is met, the method parameter lists can be determined to be different:

(1) The number of method parameters is different:

(2) The methods have the same number of parameters, but the types of parameters are different.

(3) Methods have the same number of parameters and parameter types, but the order in which the parameter types appear is different.

It should be noted that the method return value type is not the judgment of method overloading condition.

3. Hiding of methods

The code is as follows:

namespace 方法隐藏 { class Program { static void Main(string[] args) { Parent p = new Child(); p.show(); Console.ReadKey(); } } class Parent { public void show() { Console.Write("父类方法"); } } class Child : Parent { public new void show() { Console.Write("子类方法"); } } }
Copy after login

The code is as follows:

namespace 方法隐藏 { class Program { static void Main(string[] args) { Parent.show(); Console.ReadKey(); Child.show();//父类方法 } } class Parent { public static void show() { Console.Write("父类方法"); } } class Child : Parent { public static new void show() { Console.Write("子类方法"); } } }
Copy after login


On the premise that member storage permissions are not specified, all members are private.

The code is as follows:

namespace 方法隐藏 { class Program { static void Main(string[] args) { Parent p1= new Parent(); Parent p2 = new Child(); p1.show();//父类方法 p2.show();//父类方法 ((Child)p2).show();//父类方法 Console.ReadKey(); } } class Parent { public void show() { Console.WriteLine("父类方法"); } } class Child : Parent { new void show() { Console.WriteLine("子类方法"); } } }
Copy after login

4. Method rewriting and virtual method calling

The code is as follows:

namespace 方法重写 { class Program { static void Main(string[] args) { Parent p1 = new Parent(); Parent p2 = new Child(); p1.show(); p2.show(); ((Parent)p2).show();//子类方法 Console.ReadKey(); } } class Parent { public virtual void show() { Console.WriteLine("父类方法"); } } class Child:Parent { public override void show() { Console.WriteLine("子类方法"); } } }
Copy after login

The above is the detailed content of Detailed introduction to common methods in C#. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!