ToString的约定(Conventions for ToString)
ToString()
覆盖的约定是什么? 有问题的类既有Name
属性,也有Id
。What are the conventions for
ToString()
overrides? The class in question has both aName
property, but also anId
.
原文:https://stackoverflow.com/questions/13565159
最满意答案
t = t.Escape();
是.NET中用于更改字符串的常用习惯用法。 例如t = t.Replace("a", "b");
我建议你用这个。 这是必要的,因为字符串是不可变的 。有很多方法,但它们是IMO的丑陋。 例如,您可以使用
ref
参数(但不能使用扩展方法):public static string Escape (ref string string_2) { ... } Util.Escape(ref t);
或者你可以创建自己的类似String的类是可变的:
public class MutableString { /** include implicit conversions to/from string */ } public static string Escape (this MutableString string_2) { ... } MutableString t = "'"; t.Escape();
我要提醒你,如果你使用
t = t.Escape();
之外的任何东西t = t.Escape();
,因此偏离正常使用,您可能会混淆将来读取代码的任何人。
t = t.Escape();
is the usual idiom in .NET for changing a string. E.g.t = t.Replace("a", "b");
I'd recommend you use this. This is necessary because strings are immutable.There are ways around it, but they are uglier IMO. For example, you could use a
ref
parameter (but not on an extension method):public static string Escape (ref string string_2) { ... } Util.Escape(ref t);
Or you could make your own String-like class that's mutable:
public class MutableString { /** include implicit conversions to/from string */ } public static string Escape (this MutableString string_2) { ... } MutableString t = "'"; t.Escape();
I'd caution you that if you use anything besides
t = t.Escape();
, and thus deviate from normal usage, you are likely to confuse anyone that reads the code in the future.
相关问答
更多-
如何通过rails中的设计修复质量分配安全性错误?(How do I fix a Mass assignment security error from devise in rails?)[2022-05-23]
对于attr_accessible的用法我不太确定。 我从来没用过那个。 到目前为止,我已经明白了,你必须在更新质量属性时指定这个: User.new(params[:user], :as => :admin) 要么, @user = User.find(1) @user.update_attributes(params[:user], :as => :admin) I'm not that sure about the usages of :as in the attr_accessible. I'v ... -
您正在if语句中执行变量赋值。 尝试使用有效的js语法: var compareValue = "---- XXXX " + fastRotorValue + " " + medRotorValue + " " + slowRotorValue; if (compareValue === "WEATHER"){ Logger.log(compareValue + ' === "WEATHER"'); } You are doing a variable ...
-
强制LHS评估分为以下几部分: 评估[Table [p [i],{i,-3,0}]] = Flatten [{Table [0,{i,-3,-1}],1}] Force the LHS to evaluate into pieces that can be assigned to: Evaluate[Table[p[i], {i, -3, 0}]] = Flatten[{Table[0, {i, -3, -1}], 1}]
-
t = t.Escape(); 是.NET中用于更改字符串的常用习惯用法。 例如t = t.Replace("a", "b"); 我建议你用这个。 这是必要的,因为字符串是不可变的 。 有很多方法,但它们是IMO的丑陋。 例如,您可以使用ref参数(但不能使用扩展方法): public static string Escape (ref string string_2) { ... } Util.Escape(ref t); 或者你可以创建自己的类似String的类是可变的: public class M ...
-
意外分配错误(Accidental assignment error)[2024-02-25]
说到赋值错误,此表达式中没有赋值错误。 当没有[更多]结果要获取时, fetch_array()返回null 。 说到您的特定问题,请检查您的查询,数据库凭据,拼写错误等。 然后仔细检查。 此外,不相关但比这个简单的错误更重要: 如何防止PHP中的SQL注入? 另外,请记住mysqli或死,它是否必须死? Speaking of assignment errors, there are NO assignment errors in this expression. fetch_array() return ... -
我该如何解决;(How do i fix this; “TypeError: 'WikipediaItem' object does not support item assignment”)[2021-09-12]
您将刮刀命名为与导入的WikipediaItem相同: from wikipedia.items import WikipediaItem class WikipediaItem(BaseSpider): # ... parse因此使用您的BaseSpider子类,而不是您在wikipedia.items定义的BaseSpider类。 也许你想重命名这个类: class WikipediaSpider(BaseSpider): # ... You named your scraper ... -
无效的阵列分配(Invalid Array Assignment)[2023-04-04]
您不能使用=复制数组。 你也不能指定一个数组的地址; x = y; 例如,当x和y类型为char[1]时不起作用。 要将b的内容复制到a[2] ,请使用memcpy : memcpy(a[2], b, sizeof(a[2])); You can't copy an array using =. Neither can you assign an array's address; x = y; doesn't work either when x and y have types char[1] for ... -
分配 self.resultsStr += curStr 是一份声明。 语句不能成为表达式的一部分 。 因此,使用if-statement : if avId not in self.disconnectedAvIds: self.resultsStr += curStr Python禁止表达式中的赋值,因为它被认为经常导致错误。 例如, if x = 0 # banned 太近了 if x == 0 # allowed The assignment self.resultsStr ...
-
不幸的是,Fix-it不支持所有错误/警告。 我找了一份支持的列表,没有这么幸运。 在未来的版本中可能会添加更多内容。 它仍然是一个非常新的功能,因此Apple将会看到事情的进展,改进,适应和更新。 Unfortunately Fix-it doesn't support all errors/warnings. I've looked for a list of supported ones, with no such luck. It's likely that more will be added i ...
-
如何修复以下错误(How to fix the following errors)[2022-03-17]
function contains(str, substr) { return str.indexOf(substr) !== -1; } var hasTouch = window.ontouchstart !== undefined; function contains(str, substr) { return str.indexOf(substr) !== -1; } var hasTouch = window.ontouchstart !== undefined;