首页 \ 问答 \ ToString的约定(Conventions for ToString)

ToString的约定(Conventions for ToString)

ToString()覆盖的约定是什么? 有问题的类既有Name属性,也有Id


What are the conventions for ToString() overrides? The class in question has both a Name property, but also an Id.


原文:https://stackoverflow.com/questions/13565159
更新时间:2023-09-04 09:09

最满意答案

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.

相关问答

更多
  • 对于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 ...
  • 说到赋值错误,此表达式中没有赋值错误。 当没有[更多]结果要获取时, fetch_array()返回null 。 说到您的特定问题,请检查您的查询,数据库凭据,拼写错误等。 然后仔细检查。 此外,不相关但比这个简单的错误更重要: 如何防止PHP中的SQL注入? 另外,请记住mysqli或死,它是否必须死? Speaking of assignment errors, there are NO assignment errors in this expression. fetch_array() return ...
  • 您将刮刀命名为与导入的WikipediaItem相同: from wikipedia.items import WikipediaItem class WikipediaItem(BaseSpider): # ... parse因此使用您的BaseSpider子类,而不是您在wikipedia.items定义的BaseSpider类。 也许你想重命名这个类: class WikipediaSpider(BaseSpider): # ... You named your scraper ...
  • 您不能使用=复制数组。 你也不能指定一个数组的地址; 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 ...
  • 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;

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。