相关疑难解决方法(0)

我应该使用int还是Int32

在C#中,int并且Int32是同样的事情,但我读了许多那个时代int优于Int32没有给出理由.有原因,我应该关心吗?

c# variable-types

345
推荐指数
12
解决办法
18万
查看次数

为什么类字段不能是var?

class A
{
    A()
    {
        var x = 5;   // this is allowed
    }

    var _x = 5;   // the compiler is unhappy
}
Run Code Online (Sandbox Code Playgroud)

我想编译器必须能够推导出成员变量的类型,就像它对本地变量一样.那有什么区别?

c#

24
推荐指数
2
解决办法
7230
查看次数

辅助函数用于从字符串安全转换

早在VB6,我写了几个功能,将让我的代码,而不必关心和0号"字符串空之间"的区别,空,等不必添加特殊的情况下,编码时没有杀死我的工作效率更用于处理可能导致某些无关错误的数据的代码; 9999/10000如果我用作数字的东西是空的,那么我真的把它当作0.

我现在在C#和VB6和C#2005之间的差别是相当广泛的,所以,我真的不知道从哪里开始写我的一套新的辅助功能,或者如果我甚至需要做的他们都.

所以,我需要编写将接受一个字符串,数据库字段,申请表/ querysting场,???,然后尽一切可能做到把它转换成一个双,并返回给调用程序的功能.

我还需要为短裤,int16,int32,long,以及其他我可能关心的事情做这件事.

然后我会为字符串做这个.和日期.

这是值得追求的吗?我可以使用框架或C#中的某些东西吗?我真的希望能够在调用其他函数时使用内联数据,而不必创建临时变量等.

c# vb6 type-conversion

10
推荐指数
2
解决办法
8163
查看次数

使用out的泛型类型参数

我试图使用泛型类型参数制作通用解析器,但我无法掌握100%的概念

    private bool TryParse<T>(XElement element, string attributeName, out T value) where T : struct
    {
        if (element.Attribute(attributeName) != null && !string.IsNullOrEmpty(element.Attribute(attributeName).Value))
        {
            string valueString = element.Attribute(attributeName).Value;
            if (typeof(T) == typeof(int))
            {
                int valueInt;
                if (int.TryParse(valueString, out valueInt))
                {
                    value = valueInt;
                    return true;
                }
            }
            else if (typeof(T) == typeof(bool))
            {
                bool valueBool;
                if (bool.TryParse(valueString, out valueBool))
                {
                    value = valueBool;
                    return true;
                }
            }
            else
            {
                value = valueString;
                return true;
            }
        }

        return false;
    }
Run Code Online (Sandbox Code Playgroud)

正如您可能猜到的,代码无法编译,因为我无法将int | bool …

.net c# polymorphism parsing

5
推荐指数
1
解决办法
345
查看次数

标签 统计

c# ×4

.net ×1

parsing ×1

polymorphism ×1

type-conversion ×1

variable-types ×1

vb6 ×1