相关疑难解决方法(0)

当对象确实是一个字符串时,转换vs将对象转换为String

这不是一个真正的问题,但我很好奇.当我将一个字符串保存在一个DataRow中时,它会被转换为Object.当我想使用它时,我必须将其转换为ToString.据我所知,有几种方法可以做到这一点,首先是

string name = (string)DataRowObject["name"]; //valid since I know it's a string
Run Code Online (Sandbox Code Playgroud)

另一个是:

string name = DataRowObject["name"].ToString();
Run Code Online (Sandbox Code Playgroud)

我感兴趣的是两者有什么区别?第一个更有效率吗?(这只是一个推测,在我的脑海中ToString()方法是通过一些循环机制实现的,只是把它"变得"更快,但这只是一种"直觉"我有).

有没有更快/更优雅的方式这样做?

任何人都可以为我清除这个吗?

c# string parsing casting

70
推荐指数
5
解决办法
8万
查看次数

从DB读取布尔值?

在C#中,使用SqlDataReader,有没有办法从DB读取布尔值?

while (reader.Read())
{
    destPath = reader["destination_path"].ToString();
    destFile = reader["destination_file"].ToString();
    createDir = reader["create_directory"].ToString();
    deleteExisting = Convert.ToBoolean(reader["delete_existing"]);
    skipIFolderDate = reader["skipifolderdate"].ToString();
    useTempFile = reader["useTempFile"].ToString();
    password = reader["password"].ToString();
}
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,delete_existing在DB中始终为1或0.我在MSDN上读到,Convert.ToBoolean()不接受1或0作为有效输入.它只接受真或假.有没有其他方法将DB值转换为bool?或者我是否需要在SqlDataReader之外执行此操作?

另外,我无法更改数据库值,所以请不要回答"将数据库值从1和0更改为true和false".

谢谢!

.net c# sqldatareader

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

C#:自定义隐式转换运算符失败

好吧,我一直试图找到有关这方面的任何信息.我构建了一个小类来查看为字符串实现类型安全枚举的难度,因为我想将它们用于数据库字段名等.我从来不喜欢枚举只能用于整数的事实.
但是,即使我已经implicit operator为这个类实现了一个,每次我尝试使用它时,它都会给我一个无效的强制转换异常.我很茫然,因为我觉得我的代码在这一点上没有任何问题.
这是班级:

/// <summary>
/// SBool - type-safe-enum for boolean strings
/// </summary>
public sealed class SBool
{

    private readonly String name;
    private readonly int value;

    // these guys were for conversions. They might work better in another case,
    //  but for this one, they weren't very helpful.
    // ((I.e. they didn't work either.))
    //private static readonly Dictionary<SBool, String> stringsByBool = new Dictionary<SBool, String>();
    //private static readonly Dictionary<String, SBool> boolsByString = new Dictionary<String, SBool>();

    public static …
Run Code Online (Sandbox Code Playgroud)

c# casting implicit asp.net-4.0

9
推荐指数
1
解决办法
2552
查看次数

标签 统计

c# ×3

casting ×2

.net ×1

asp.net-4.0 ×1

implicit ×1

parsing ×1

sqldatareader ×1

string ×1