C#重载的构造函数 [英] C# Overloaded Constructor

查看:83
本文介绍了C#重载的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我在重载类构造函数时遇到错误。



无法从'string'转换为'System.Collections.Generic.List< string> //删除了< bcos字符串介于<>之间未显示



代码如下所示:

Hi,

I am getting a error while overloading the a class constructor.

"cannot convert from 'string' to 'System.Collections.Generic.List<string>" //Removed the "<" bcos string is between <> is not shown

code looks like this:

Class Test
{
   Test(string arg)
   {
      //Code
   }

   Test(List<string> args) //Removed the "<" bcos string is between <> is not shown
   {
      //Code
   }
}

public static void main()
{
   Test obj = new Test("ABC");
}







如果您有任何解决方案,请告诉我



提前致谢!



问候

Govardhan




Let me know if you have any solution

Thanks in advance!

Regards
Govardhan

推荐答案

没有解决方案。你发布的代码,它的内容很少,是正确的。问题出现在构造函数代码中的某个地方,而不是标题本身。异常消息会告诉你,但你没有阅读整个事情。它会告诉你发生异常的代码行。



消息告诉你,你正在尝试将String转换为List,这显然是,无法正常工作。
There is no solution. The code you posted, what little there is of it, is correct. The problem comes in somewhere inside the constructor code, NOT with the header itself. The exception message would have told you that, but you didn't read the entire thing. It will tell you the line of code the exception occurs on.

The message is telling you that you're trying to convert a String to a List, which, obviously, won't work.


另外解决方案1和解决方案2.



这是实用的建议。不要使用签名 Test(List< string> args)< / string> 创建构造函数,因为它不够灵活。您可以使用一个或两个稍微不同的签名,使其更加灵活。



变体#1:
In addition Solution 1 and Solution 2.

This is the practical advice. Don't create a constructor with the signature Test(List<string> args)</string>, because it is not flexible enough. You could make it more flexible without any additional effort with one or two slightly different signatures.

Variant #1:
using System.Collections.Generic;

internal Test {
    List list<string> = new List<string>();
    internal Test(params string[] values)  { list.AddRange(values);  }
    //...
}

//...
//uses:

Test myTest1 = new Test("one parameter");
Test myTest2 = new Text("first parameter", "second parameter"); // any number of them

System.Collections.Generic.List<string> list = //...
Test myTestFromList = new Test(list.ToArray());

string[] array = //...
Test myTestFromArray = new Test(array); // only one parameter





变体#2:



Variant #2:

internal Test {
    IList<string> list = new List<string>();
    //can be initialized with any array or collection implementing IList:
    internal Test(System.Collections.Generic.IList<string> list)  { this.list =  list; }
    //...
}

//...
//uses:

Text myTestFromArray = new Test(array); // array declaration is shown in the code sample above
Test myTestFromList = new Test(list); // no need to convert to array, list declaration is shown in the code sample above





当然,你可以在一个类中组合两个构造函数,但是它可以产生歧义,其中构造函数调用在第一个和第二个样本中是相同的。 />


-SA


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConstructorOverloading
{
    class constructorOverloadingExample
    {
        string member1;
        List<string> member2;
        public constructorOverloadingExample(string arg1)
        {
            member1 = arg1;
        }
        public constructorOverloadingExample(List<string> arg1)
        {
            member2 = arg1;
        }
        public void displayData()
        {
            
            //Displaying data updated by any of the constructorOverloadingExample constructor

            Console.WriteLine("member1 = " + member1);


        }


    }


    public class testConstructorOverloading
    {


        public static void Main(string[] args)
        {


            constructorOverloadingExample instance1 = new constructorOverloadingExample("ABC");


            Console.WriteLine("Member values of instance1:");


            instance1.displayData();

            List<string> abc = new List<string>();
            abc.Add("abc");
            abc.Add("xyz");
            constructorOverloadingExample instance2 = new constructorOverloadingExample(abc[0]);


            Console.WriteLine("Member values of instance2:");


            instance2.displayData();



            Console.Read();


        }


    }
}





试试这样。



Try like this.


这篇关于C#重载的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆