在C#中将int转换为字符串的更好方法? [英] Better way to convert int to string in C#?

查看:254
本文介绍了在C#中将int转换为字符串的更好方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个小型C#应用程序,它使用轨道栏来更改整数。它最终会影响对象的下降速度。



我的问题是我怎样才能将其转换为int并将其转换为C#中的字符串?我使用以下代码来执行此操作。



I am making a small C# application that uses a trackbar to change an integer. It will eventually affect the falling rate of an object.

My question is how else can I convert and int to a string in C#? I am using the following code to do so.

private void timer1_Tick(object sender, EventArgs e)
{
    int myNum;
    String myString;
    myNum = trackBar1.Value;
    myString = myNum.ToString();
    label1.Text = myString;
}





当我使用这种方法时,一切正常,但有更好的方法吗?



我应该创建一个为我做这个转换的函数吗?如果是这样我怎么能让它能够用于任何变量,我希望能够做如下事情:





Everything is working fine when I use this method, but it there a better way to do so?

And should I create a function that does this conversion for me? If so how could I make it be able to be used for any variables, I would like to be able to do something like follows:

convertIntString(int1, string1)





第一个参数是我要通过转换的整数,第二个参数是参数将是我想要将整数转换为的字符串的名称。



这是否会产生?



谢谢!



The first argument would be the integer that I want to pass through to convert, and the second argument would be the name of the string that I want the integer to be converted to.

Does this make since?

Thanks!

推荐答案

只是为了解释为什么尝试将该方法与第二个代码示例中显示的签名一起使用并不具有建设性意义。



字符串名称参数可以是变量的名称或成员的名称。它们在本质上是完全不同的,所以,即使你的方法是可能的,它也不会是普遍的。通常,您无法访问变量名。



关于成员名称,可以在程序集元数据中找到,所以可以使用反射。但是名称并不足够:对于即时成员,它还需要类的全名和类实例的引用。即便如此,为了这么简单的目的,反射也太慢了。方式太慢了。但更重要的是:这种方法不可支持,因为编译器不支持这样的名称。如果拼错了类和成员的名称,或者更改了任何名称,编译器将无法检测到错误。



通常,使用名称在大多数情况下,在运行时编程实体是一个坏主意。有更好的方法,特别是基于接口。



在你的情况下,没有什么比 System.Object.ToString <更好了/ code>表示任何对象,引用或值。请注意,您还可以使用 ToString 格式参数支持格式化的不同类型的方法。



- SA
Just of bit of explanation why the attempt to use of the method with the signature shown in a second code sample is not really constructive.

The "name of the string" parameter may me the name of a variable or a name of a member. They are totally different in nature, so, even if your method was possible, it would not be universal anyway. Generally, you don't have an access to variable names.

As to member name, it can be found in assembly metadata, so reflection can be used. But the name along won't be enough: it would need also the full name of the class and the reference to the instance of the class, for an instant member. And even then, reflection is too slow to do it all for such a simple purpose. Way too slow. But more important is this: this approach cannot be supportable, as such names would not be supported by a compiler. Should you misspell the name of the class and member, or change any of the names, a compiler won't be able to detect a bug.

Generally, using names of programming entities during runtime is a bad idea, in most cases. There are much better approaches, in particular, based on interfaces.

In your case, there is nothing better then System.Object.ToString for any object, reference or value. Note that you also can use format parameters for ToString methods of different types supporting formatting.

—SA


label1.Text = trackBar1.Value.ToString();





这是一个方法思路,你可以从



Here is a method idea you can extrapolate from"

public static string ConvertInt(int num, string name)
{
    name = num.ToString();
    return name;
    
}


这篇关于在C#中将int转换为字符串的更好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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