有没有一种简单的方法在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#有类似的东西吗?
鉴于:
DateTime.UtcNow
Run Code Online (Sandbox Code Playgroud)
如何获得符合ISO 8601格式的相同值的字符串?
请注意,ISO 8601定义了许多类似的格式.我正在寻找的具体格式是:
yyyy-MM-ddTHH:mm:ssZ
Run Code Online (Sandbox Code Playgroud) 与此问题类似,但对于VB.NET,因为我了解到这是一种语言问题.
例如,编译器是否知道要翻译
Dim s As String ="test"+"this"+"function"
至
Dim s As String = "test this function"
Run Code Online (Sandbox Code Playgroud)
从而避免字符串连接的性能损失?
可能重复:
C#是否优化了字符串文字的串联?
string foo = "bar1" + "bar2" + "bar3";
Run Code Online (Sandbox Code Playgroud)
c#编译器是否在内部应用了string.Concat方法?
那么为了便于阅读,最好使用+运算符.
嘿! 我已经看到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 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?
谢谢.
当我需要连接两个字符串时,我使用String.Format(或StringBuilder,如果它发生在代码中的几个地方).
我看到一些优秀的程序员不会注意字符串加入复杂性而只是使用'+'运算符.
我知道使用'+'运算符会使应用程序使用更多内存,但复杂性又如何呢?
我正在优化我们的调试打印设施(类)。该类大致简单,具有全局“启用”布尔值和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) 我刚刚在一个源代码中看到这样的事情:
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 = "" +....有效.这些是""为了什么?我尽可能地看着,但没有找到答案.很抱歉,如果答案非常简单,我是编程新手,所以我将非常感谢您的理解.
我一直在阅读不同的答案,其中字符串连接操作更好.我读到某处"+"内部调用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) c# ×8
string ×7
.net ×2
performance ×2
c++ ×1
datetime ×1
iso8601 ×1
optimization ×1
shorthand ×1
vb.net ×1