小编Jaw*_*har的帖子

多个方法重载匹配时的优先级是多少?

我正在尝试了解C#中的OOP概念.

在以下示例代码中:

  1. 为什么ins1更喜欢通用方法
  2. 为什么ins2,ins3更喜欢非泛型方法

注意:当我注释掉任何一个"MyTestMethod"方法时,程序仍然会继续成功运行.这段代码不是来自制作的东西.这只是我的训练样本.所以,请不要介意命名约定和标准.

using System;

namespace ConsoleApplication1
{
    class Program
    {
        public static void MyTestMethod(J input)
        {
            Console.WriteLine($"Program.MyTestMethod: {input.Val}");
        }
        public static void MyTestMethod<T>(T input) where T : J
        {
            Console.WriteLine($"Program.MyTestMethod<T>: {input.Val}");
        }

        static void Main(string[] args)
        {
            J2 ins1 = new J2(1);
            MyTestMethod(ins1);

            J ins2 = new J(2);
            MyTestMethod(ins2);

            J ins3 = new J2(3);
            MyTestMethod(ins3);

            Console.ReadKey();
        }
    }
    internal class J
    {
        public int Val { get; set; }
        public J(int …
Run Code Online (Sandbox Code Playgroud)

c# oop

3
推荐指数
1
解决办法
116
查看次数

标签 统计

c# ×1

oop ×1