如何删除错误:无法将类型'string'隐式转换为'int' [英] How do I remove the error: cannot implicitly convert type 'string' to 'int'

查看:109
本文介绍了如何删除错误:无法将类型'string'隐式转换为'int'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Text;



名称空间SortNames

{

班级计划

{

static void Main(params string [] sortNames)

{

字符串名称;

Console.Write(输入名称:);

name = Console.ReadLine();

sortNames = new string [name]; //错误在于此行。它被称为[]。//

中的名称(int x = 0; x< sortNames.Length; x ++)

{

Console.WriteLine(排列之前的名字列表:/ n);

Console.WriteLine(sortNames [x]);

}

Array.Sort(sortNames);

for(int y = 0; y< sortNames.Length; y ++)

{

Console.WriteLine(排列后的名字列表:/ n);

Console.WriteLine(sortNames [y]);

}

Console.ReadKey();

}

}

}



我尝试了什么:



我没有看到转换为int的必要性..我只是在使用strings。

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

namespace SortNames
{
class Program
{
static void Main(params string[] sortNames)
{
string name;
Console.Write("Enter name: ");
name = Console.ReadLine();
sortNames = new string [name];//error lies in this line. it is referred to the name in [].//
for (int x = 0; x < sortNames.Length; x++)
{
Console.WriteLine("List of Names before arrangement: /n");
Console.WriteLine(sortNames[x]);
}
Array.Sort(sortNames);
for (int y = 0; y < sortNames.Length; y++)
{
Console.WriteLine("List of Names after arrangement: /n");
Console.WriteLine(sortNames[y]);
}
Console.ReadKey();
}
}
}

What I have tried:

I don't see the necessity to convert to an int.. I'm only working with strings.

推荐答案

sortNames = new string [name];

这行实际上要做的是将sortNames设置为一个带有 name 元素的数组,其中 name 是长度。但它不能是一个长度,因为它是一个字符串。



我假设你打算附加 name 到阵列。为此,请创建一个增加长度的新数组,然后将原始元素复制到此新数组:

What this line actually attepts to do, is set sortNames to an array with name elements, where name is the length. But it can't be a length, because it's a string.

I assume your intention is to append name to the array. To do this, create a new array with an incremented length, and then copy the original elements to this new array:

string[] completeSortNames = new string[sortNames.Length + 1];
sortNames.CopyTo(completeSortNames, 0);
completeSortNames[completeSortNames.Length - 1] = name;
// from now on, use completeSortNames rather than sortNames, because sortNames doesn't have the last element





除此之外, args 不需要PARAMS; args 将在运行时使用命令行参数填充, params 不是必需的。



Aside from that, it's not necessary for args to be params; args will be populated with the command-line arguments at runtime, params is not necessary.


sortNames是一个字符串数组(来自方法声明)和你的代码:

sortNames is an array of strings (from the method declaration) and your code:
sortNames = new string [name]

is试图创建一个新的数组。 (为什么?我不知道 - 它会覆盖你传入应用程序的命令行参数。)

但是......你得到错误,因为新数组需要知道它应该有多大是,并且字符串不告诉它 - 你 需要 方括号内的整数,以便可以创建正确数量的元素。

is trying to create a new array. (Why? I have no idea - it would overwrite the command line parameters you passed in to the application.)
But ...you get the error because the new array needs to know how big it should be, and a string doesn't tell it that - you need an integer inside the square brackets so that the right number of elements can be created.


这篇关于如何删除错误:无法将类型'string'隐式转换为'int'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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