小编Bac*_*ave的帖子

StringBuilder的MaxCapacity

为什么这段代码会在i = 690864192上抛出OutOfMemoryException?

StringBuilder sb = new StringBuilder();
                for (int i = 0; i < Int32.MaxValue; i++)
                {
                    sb.Append("s");
                }
                Console.WriteLine(sb.ToString());
                Console.Read();
Run Code Online (Sandbox Code Playgroud)

默认容量是16个字符,但是这需要增加到最大值,即int.MaxValue = 2,147,483,647.那么为什么当chars的数量是690,864,192而远远低于最大容量时,它会抛出异常?

c# stringbuilder

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

$MyInspiration.MyCommand.Name 提供奇怪的结果

为什么$MyInvocation.MyCommand.Name在函数内执行时会给出函数的名称?据此它旨在为您提供脚本文件名。这是我正在使用的代码:

function startScript
{
    $ScriptName = $MyInvocation.MyCommand.Name
    $ScriptName
}

$ScriptName = $MyInvocation.MyCommand.Name
$ScriptName

startScript

<#
Output:

testing.ps1
startScript
#>
Run Code Online (Sandbox Code Playgroud)

powershell

2
推荐指数
1
解决办法
9369
查看次数

Dapper NullReferenceException

我执行此代码时收到NullReferenceException:

var insertTransaction = @"INSERT INTO [192.168.1.55].databaseName.dbo.tableName
(Date, Desc) 
VALUES(GETDATE(), 
@Description)
SELECT Scope_identity()";

var result = _sqlMapper.Query<int>(insertTransaction,
             new
             {                           
                 Description = "some description"
             });
Run Code Online (Sandbox Code Playgroud)

其中_sqlMapper是一个实例 Dapper.SqlMapper

如果我删除SELECT Scope_identity()我不会得到例外.

异常堆栈跟踪说这里引发了异常:

在Dapper.SqlMapper.d__11`1.MoveNext()在d:\ Dev\dapper-dot-net\Dapper NET40\SqlMapper.cs:第1583行

  1. 为什么要SELECT Scope_identity()创建一个null对象,我该如何修复它?
  2. 为什么堆栈跟踪显示本地计算机上不存在的文件路径?

更新:Dapper版本= 1.40.0.0,运行时版本= v4.0.30319 DLL = C:\ src\packages\Dapper.1.40\lib \net45\Dapper.dll

更新:如果我在Management Studio中执行查询,则会插入行,但返回的scope_identity为null.

c# sql dapper

2
推荐指数
1
解决办法
1214
查看次数

在方法中使用const

如果只需要在一个方法内使用常量,那么你应该在方法中还是在字段中声明它?

另外,如果你应该在方法中声明它,那么你应该使用小写或大写的第一个字母作为名称吗?

编辑:这不是重复,因为我问你是否应该在你的方法或字段中声明const.

c#

2
推荐指数
1
解决办法
808
查看次数

查看C#编译器预编译的代码

我怎么能看到C#"语法糖"在幕后真正做了什么?例如,我知道C#lock语句已预编译为:

var temp = obj;

Monitor.Enter(temp);

try
{
    // body
}
finally
{
    Monitor.Exit(temp);
}
Run Code Online (Sandbox Code Playgroud)

我知道如果你直接在类中声明并初始化实例字段(而不是在构造函数中),那么它是用于在类中声明这些字段并在构造函数中初始化它们的语法糖.

我的问题是,如果我使用这些语法糖编写代码,我怎么能看到生成的C#代码是什么?

是否存在"预编译"过程,首先将这些语法糖转换为更复杂的C#代码,然后将其编译为CIL?

c#

2
推荐指数
1
解决办法
293
查看次数

C# lock 语句中的 Nop 生成

我刚刚读了 Eric Lippert 的一篇文章http://ericlippert.com/2009/03/06/locks-and-exceptions-do-not-mix/

对于以下代码,他提到编译器可以在监视器输入和受保护区域之间生成无操作指令:

var temp = obj;
Monitor.Enter(temp);
try { body }
finally { Monitor.Exit(temp); }
Run Code Online (Sandbox Code Playgroud)

为什么编译器会在那里生成一个 nop?

c# locking

2
推荐指数
1
解决办法
213
查看次数

const 字符串声明/初始化上的断点

为什么我不能在私有实例方法中的以下行上放置断点:

const string errorMsg = "Test Exeption";
Run Code Online (Sandbox Code Playgroud)

我正在使用 Visual Studio 2015 企业版。

c# debugging breakpoints visual-studio

2
推荐指数
1
解决办法
249
查看次数

声明方法返回类型 Task&lt;int&gt; 没有 async 关键字

在什么情况下你会返回 aTask<T>而不async在方法签名中使用?

我在下面的代码中有这样一个方法,但我无法理解发生了什么。

为什么我下面的示例代码没有执行任何await语句?IE。为什么Console.WriteLine("4)");Console.WriteLine("3)");return x;从来没有得到执行?

class Program
    {
        static void Main(string[] args)
        {
            TestAsync testAsync = new TestAsync();
            testAsync.Run();

            Console.Read();
        }
    }

    public class TestAsync
    {
        public async void Run()
        {
            Task<int> resultTask = GetInt();
            Console.WriteLine("2)");
            int x = await resultTask;
            Console.WriteLine("4)");
        }
        public async Task<int> GetInt()
        {
            Task<int> GetIntAfterLongWaitTask = GetIntAfterLongWait();
            Console.WriteLine("1)");
            int x = await GetIntAfterLongWaitTask;
            Console.WriteLine("3)");
            return x;
        }

        public Task<int> GetIntAfterLongWait()
        {            
            Task.Run(() => …
Run Code Online (Sandbox Code Playgroud)

c# task async-await

2
推荐指数
1
解决办法
166
查看次数

为什么 List&lt;T&gt; 的 MoveNext() 实现使用 localList?

我读了实现的MoveNext()List<T>

public bool MoveNext() {
    List<T> localList = list;
 
    if (version == localList._version && ((uint)index < (uint)localList._size)) 
    {                                                     
        current = localList._items[index];                    
        index++;
        return true;
    }
    return MoveNextRare();
}
Run Code Online (Sandbox Code Playgroud)

将引用复制list到本地 var 有localList什么意义?

如果它们都指向同一个对象,为什么不能替换localListwith 的所有实例list

c# ienumerable

2
推荐指数
1
解决办法
156
查看次数

为什么 List&lt;T&gt; 声明 GetEnumerator() 和 IEnumerable&lt;T&gt;.GetEnumerator()?

为什么List定义了这三个方法?

    public Enumerator GetEnumerator()
        => new Enumerator(this);

    IEnumerator<T> IEnumerable<T>.GetEnumerator()
        => new Enumerator(this);

    IEnumerator IEnumerable.GetEnumerator()
        => new Enumerator(this);
Run Code Online (Sandbox Code Playgroud)

他们都在做同样的事情。仅仅拥有这个还不够:

public Enumerator GetEnumerator()
        => new Enumerator(this);
Run Code Online (Sandbox Code Playgroud)

c# ienumerable ienumerator interface explicit-interface

2
推荐指数
1
解决办法
103
查看次数