我在公共静态类中有以下代码:
public static class MyList
{
public static readonly SortedList<int, List<myObj>> CharList;
// ...etc.
}
Run Code Online (Sandbox Code Playgroud)
..但即使使用readonly我仍然可以从另一个类添加项目到列表:
MyList.CharList[100] = new List<myObj>() { new myObj(30, 30) };
Run Code Online (Sandbox Code Playgroud)
要么
MyList.CharList.Add(new List<myObj>() { new myObj(30, 30) });
Run Code Online (Sandbox Code Playgroud)
有没有办法让事物只读而不改变CharList的实现(它会打破一些东西)?如果我必须改变实现(使其不可更改),那么最好的方法是什么?我需要它是List <T,T>,所以ReadOnlyCollection不会这样做
我正在编写ac#app并希望将错误消息输出到控制台或消息框(取决于应用程序类型:enum AppTypeChoice {Console,Windows}),并且还控制应用程序是否继续运行(bool StopOnError).
我想出了这个检查所有标准的方法,但是我收到了"无法检测到的代码"警告.我看不出原因!
这是整个方法(为一些业余爱好者代码支持你自己!)
public void OutputError(string message)
{
string standardMessage = "Something went WRONG!. [ But I'm not telling you what! ]";
string defaultMsgBoxTitle = "Aaaaarrrggggggggggg!!!!!";
string dosBoxOutput = "\n\n*** " + defaultMsgBoxTitle + " *** \n\n Message was: '" + message + "'\n\n";
AppTypeChoice appType = DataDefs.AppType;
DebugLevelChoice level = DataDefs.DebugLevel;
// Decide how much info we should give out here...
if (level != DebugLevelChoice.None)
{
// Give some info....
if (appType == AppTypeChoice.Windows)
MessageBox.Show(message, defaultMsgBoxTitle, …Run Code Online (Sandbox Code Playgroud) 可能重复:
.NET异常有多慢?
我一直在阅读(包括这里)关于何时应该/不应该使用异常的地方.我现在想要改变我的代码,它会抛出使方法返回false并像这样处理它,但我的问题是:它是投掷还是尝试......可能会阻碍性能...... 我的意思是,这是否可以接受:
bool method someMmethod()
{
try
{
// ...Do something
catch (Exception ex) // Don't care too much what at the moment...
{
// Output error
// Return false
}
return true // No errors
Run Code Online (Sandbox Code Playgroud)
或者有更好的方法来做到这一点?(我很难看到"未处理的异常......"哈哈!)