相关疑难解决方法(0)

C#中的多行字符串文字

有没有一种简单的方法在C#中创建多行字符串文字?

这就是我现在拥有的:

string query = "SELECT foo, bar"
+ " FROM table"
+ " WHERE id = 42";
Run Code Online (Sandbox Code Playgroud)

我知道PHP有

<<<BLOCK

BLOCK;
Run Code Online (Sandbox Code Playgroud)

C#有类似的东西吗?

c# string shorthand

962
推荐指数
12
解决办法
56万
查看次数

给定DateTime对象,如何以字符串格式获取ISO 8601日期?

鉴于:

DateTime.UtcNow
Run Code Online (Sandbox Code Playgroud)

如何获得符合ISO 8601格式的相同值的字符串?

请注意,ISO 8601定义了许多类似的格式.我正在寻找的具体格式是:

yyyy-MM-ddTHH:mm:ssZ
Run Code Online (Sandbox Code Playgroud)

c# datetime iso8601 datetime-format

738
推荐指数
15
解决办法
47万
查看次数

VB.NET是否优化了字符串文字的串联?

问题类似,但对于VB.NET,因为我了解到这是一种语言问题.

例如,编译器是否知道要翻译

Dim s As String ="test"+"this"+"function"

Dim s As String = "test this function"
Run Code Online (Sandbox Code Playgroud)

从而避免字符串连接的性能损失?

.net vb.net optimization performance

9
推荐指数
2
解决办法
2069
查看次数

c#编译器如何连接字符串

可能重复:
C#是否优化了字符串文字的串联?

string foo = "bar1" + "bar2" + "bar3";
Run Code Online (Sandbox Code Playgroud)

c#编译器是否在内部应用了string.Concat方法?

那么为了便于阅读,最好使用+运算符.

c# string string-concatenation

7
推荐指数
1
解决办法
247
查看次数

长串

嘿! 我已经看到c#代码使用@来告诉编译器字符串中有换行符,并且它应该全部在一行中.c/c ++有类似的东西吗?

就像我想要这样的东西:

73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 12540698747158523863050715693290963295227443043557 66896648950445244523161731856403098711121722383113 62229893423380308135336276614282806444486645238749 30358907296290491560440772390713810515859307960866 70172427121883998797908792274921901699720888093776 65727333001053367881220235421809751254540594752243 52584907711670556013604839586446706324415722155397 53697817977846174064955149290862569321978468622482 83972241375657056057490261407972968652414535100474 82166370484403199890008895243450658541227588666881 16427171479924442928230863465674813919123162824586 17866458359124566529476545682848912883142607690042 24219022671055626321111109370544217506941658960408 07198403850962455444362981230987879927244284909188 84580156166097919133875499200524063689912560717606 05886116467109405077541002256983155200055935729725 71636269561882670428252483600823257530420752963450

在一个字符串中,我不想将它全部放在一行中,只是把它放在那一行,让编译器知道那只是一行.

c++ string

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

C#+运算符调用string.concat函数?

可能重复:
C#是否优化了字符串文字的串联?

我刚刚发现我们写了这样的一行:

string s = "string";
s = s + s; // this translates to s = string.concat("string", "string");
Run Code Online (Sandbox Code Playgroud)

但是我通过反射器打开了字符串类,我没看到这个+运算符在哪里重载?我可以看到==和!=超载.

[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
    public static bool operator ==(string a, string b)
    {
      return string.Equals(a, b);
    }
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
    public static bool operator !=(string a, string b)
    {
      return !string.Equals(a, b);
    }
Run Code Online (Sandbox Code Playgroud)

那么为什么当我们使用+来组合字符串时会调用concat?

谢谢.

c# string operator-overloading concatenation

5
推荐指数
2
解决办法
700
查看次数

字符串加入和复杂性?

当我需要连接两个字符串时,我使用String.Format(或StringBuilder,如果它发生在代码中的几个地方).

我看到一些优秀的程序员不会注意字符串加入复杂性而只是使用'+'运算符.

我知道使用'+'运算符会使应用程序使用更多内存,但复杂性又如何呢?

c# string complexity-theory

4
推荐指数
2
解决办法
1730
查看次数

为什么 C# 字符串插值比常规字符串连接慢?

我正在优化我们的调试打印设施(类)。该类大致简单,具有全局“启用”布尔值和PrineDebug例程。

我正在研究该方法在“禁用”模式下的性能PrintDebug,尝试创建一个在不需要调试打印的情况下对运行时影响较小的框架。

在探索过程中,我发现了以下结果,这让我感到惊讶,我想知道我在这里错过了什么?

public class Profiler
{
     private bool isDebug = false;

     public void PrineDebug(string message)
     {
         if (isDebug)
         {
             Console.WriteLine(message);
         }
     }
}

[MemoryDiagnoser]
public class ProfilerBench
{
    private Profiler profiler = new Profiler();
    private int five = 5;
    private int six = 6;

    [Benchmark]
    public void DebugPrintConcat()
    {
        profiler.PrineDebug("sometext_" + five + "_" + six);
    }

    [Benchmark]
    public void DebugPrintInterpolated()
    {
        profiler.PrineDebug($"sometext_{five}_{six}");
    }
}
Run Code Online (Sandbox Code Playgroud)

在 BenchmarkDotNet 下运行此基准测试..结果如下:

|                 Method |     Mean |   Error | …
Run Code Online (Sandbox Code Playgroud)

c# string performance string-interpolation

4
推荐指数
1
解决办法
2119
查看次数

从char数组到字符串C#的特定(精确)字符

我刚刚在一个源代码中看到这样的事情:

char[] letters = { 'k', 'n', 'p', 's' };

    for (int d1 = 0; d1 < letters.Length; d1++)
    {
        for (int d2 = 0; d2 < letters.Length; d2++)
        {
            for (int d3 = 0; d3 < letters.Length; d3++)
            {
                for (int d4 = 0; d4 < letters.Length; d4++)
                {
                    string left = ""+ letters[d1] + letters[d2] + letters[d3] + letters[d4];
                }
           }
        }
    }
Run Code Online (Sandbox Code Playgroud)

我真正想知道的是这究竟是多么string strName = "" +....有效.这些是""为了什么?我尽可能地看着,但没有找到答案.很抱歉,如果答案非常简单,我是编程新手,所以我将非常感谢您的理解.

c#

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

哪个字符串连接操作更快 - "+"或string.Concat

我一直在阅读不同的答案,其中字符串连接操作更好.我读到某处"+"内部调用string.Concat方法,string.Concat两者之间更快.当我查看IL代码时,它似乎没有提出上述陈述.

对于

string s1 = "1" + "2";
Run Code Online (Sandbox Code Playgroud)

IL代码

.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       8 (0x8)
  .maxstack  1
  .locals init ([0] string s1)
  IL_0000:  nop
  IL_0001:  ldstr      "12"
  IL_0006:  stloc.0
  IL_0007:  ret
} // end of method Program::Main
Run Code Online (Sandbox Code Playgroud)

另外,我从IL代码中注意到"+"只初始化一个字符串,而string.Concat初始化两个字符串以连接.我也试过多个字符串.在使用内存方面,"+"似乎只使用一个字符串变量,而另一个选项在内部使用更多变量.

对于,

string s1 = string.concat("1", "2");
Run Code Online (Sandbox Code Playgroud)

IL代码

.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       18 (0x12)
  .maxstack  2
  .locals init ([0] …
Run Code Online (Sandbox Code Playgroud)

.net c# string

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