gpt4 book ai didi

c# - 读取文件的差异

转载 作者:行者123 更新时间:2023-12-03 03:11:06 26 4
gpt4 key购买 nike

string value1 = File.ReadAllText("C:\\file.txt");
string value2 = File.ReadAllText(@"C:\file.txt");

在上述语句中,什么时候使用@"C:\file.txt"C:\file.txt

最佳答案

编译器将按原样读取@"C:\file.txt"。逐字删除 (@) 会将 '\f' 视为单个转义字符 ( Form feed )。换句话说:

@"C:\file.txt" == "C:\\file.txt"
@"C:\file.txt" != "C:\file.txt" // treated as C: + FormFeed + ile.txt

Verbatim string literals start with @ and are also enclosed in double quotation marks. For example:

@"good morning"  // a string literal

The advantage of verbatim strings is that escape sequences are not processed, which makes it easy to write, for example, a fully qualified file name:

@"c:\Docs\Source\a.txt"  // rather than "c:\\Docs\\Source\\a.txt"

String literals :

A regular string literal consists of zero or more characters enclosed in double quotes, as in "hello", and may include both simple escape sequences (such as \t for the tab character) and hexadecimal and Unicode escape sequences.

A verbatim string literal consists of an @ character followed by a double-quote character, zero or more characters, and a closing double-quote character. A simple example is @"hello". In a verbatim string literal, the characters between the delimiters are interpreted verbatim, the only exception being a quote-escape-sequence. In particular, simple escape sequences and hexadecimal and Unicode escape sequences are not processed in verbatim string literals. A verbatim string literal may span multiple lines.

关于c# - 读取文件的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7542690/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com