简体   繁体   English

为什么System.String.EndsWith()有一个char重载而System.String.StartsWith()没有?

[英]Why does System.String.EndsWith() have a a char overload and System.String.StartsWith() does not?

I was looking through System.String and I was wondering why the EndsWith and StartsWith methods aren't symmetric in terms of parameters they can take. 我正在查看System.String ,我想知道为什么EndsWithStartsWith方法在它们可以采用的参数方面不对称。

Specifically, why does System.String.EndsWith support a char parameter while System.String.StartsWith does not? 具体来说,为什么System.String.EndsWith支持char参数而System.String.StartsWith不支持? Is this because of any limitation or design feature? 这是因为任何限制或设计特征吗?

// System.String.EndsWith method signatures
[__DynamicallyInvokable]
public bool EndsWith(string value)

[ComVisible(false)]
[SecuritySafeCritical]
[__DynamicallyInvokable]
public bool EndsWith(string value, StringComparison comparisonType)

public bool EndsWith(string value, bool ignoreCase, CultureInfo culture)

[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
internal bool EndsWith(char value)
{
  int length = this.Length;
  return length != 0 && (int) this[length - 1] == (int) value;
}

// System.String.StartsWith method signatures
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
[__DynamicallyInvokable]
public bool StartsWith(string value)

[SecuritySafeCritical]
[ComVisible(false)]
[__DynamicallyInvokable]
public bool StartsWith(string value, StringComparison comparisonType)

public bool StartsWith(string value, bool ignoreCase, CultureInfo culture)

Looking in ILSpy, this overload seems overwhelmingly to be called in IO code as 看看ILSpy,这个重载似乎压倒性地在IO代码中调用

s.EndsWith(Path.DirectorySeparatorChar)

Presumably it's just something the C# team decided it would be useful to have to avoid repetitive code. 据推测,这只是C#团队认为必须避免重复代码有用的东西。

Note that it's much easier to make this check at the start ( s[0] == c vs s[s.Length - 1] == c ) which may also explain why they didn't bother to make a StartsWith overload. 请注意,在开始时进行此检查要容易得多( s[0] == c vs s[s.Length - 1] == c ),这也可以解释为什么他们不打算使StartsWith超载。

This is an internal method that only is used in the following 8 methods in mscorlib : 这是一个内部方法,仅用于mscorlib中的以下8个方法:

  • System.Security.Util.StringExpressionSet.CanonicalizePath(string path, bool needFullPath):string System.Security.Util.StringExpressionSet.CanonicalizePath(string path,bool needFullPath):string
  • System.IO.IsolatedStorage.IsolatedStorageFile.DirectoryExists(string path):bool System.IO.IsolatedStorage.IsolatedStorageFile.DirectoryExists(string path):bool
  • System.IO.Directory.GetDemandDir(string fullPath, bool thisDirOnly):string System.IO.Directory.GetDemandDir(string fullPath,bool thisDirOnly):string
  • System.IO.DirectoryInfo.GetDirName(string fullPath):string System.IO.DirectoryInfo.GetDirName(string fullPath):string
  • System.Security.Util.URLString.IsRelativeFileUrl:bool System.Security.Util.URLString.IsRelativeFileUrl:布尔
  • System.IO.DirectoryInfo.MoveTo(string destDirName):void System.IO.DirectoryInfo.MoveTo(string destDirName):void
  • System.IO.DirectoryInfo.Parent:DirectoryInfo System.IO.DirectoryInfo.Parent:DirectoryInfo的
  • System.Globalization.CultureData.SENGDISPLAYNAME:string System.Globalization.CultureData.SENGDISPLAYNAME:字符串

Probably just for convenience and code reuse :) 可能只是为了方便和代码重用:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么每个Char静态“Is ...”都有一个字符串重载,例如IsWhiteSpace(string,Int32)? - Why does every Char static “Is…” have a string overload, e.g. IsWhiteSpace(string, Int32)? System.String不会重载operator + =但是String Concatenation有效,怎么样? - System.String does not overload operator += But String Concatenation works, How? 为什么字符数组方法“ToString”返回提示 System.Char 而不是反转字符串? - Why does the char array method "ToString" returns a promt System.Char instead of reversed string? System.FormatException:&#39;输入字符串的格式不正确。 - System.FormatException: 'The input string does not have the correct format.' 为什么Linq ToList <string> 返回System.Data.DataRow? - Why does Linq ToList<string> return System.Data.DataRow? 为什么JSON字符串中的System.Version没有正确反序列化? - Why System.Version in JSON string does not deserialize correctly? String.StartsWith无法正常工作 - String.StartsWith does not work as I expect LINQ to Entities无法识别方法&#39;System.String getFullname(System.String,System.String)&#39; - LINQ to Entities does not recognize the method 'System.String getFullname(System.String, System.String)' LINQ to Entities无法识别方法&#39;System.String [] Split(Char [])&#39;方法, - LINQ to Entities does not recognize the method 'System.String[] Split(Char[])' method, LINQ to Entities无法识别方法&#39;System.String [] Split(Char [])&#39;,并且该方法无法转换为商店表达式 - LINQ to Entities does not recognize the method 'System.String[] Split(Char[])' method, and this method cannot be translated into a store expression
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM