该规则是否适用于C#?
如果我们尝试使用枚举而不显式设置值,编译器会给出错误"使用未分配的局部变量"?
要求的视角是FxCop规则的有效性,因为我不能使用枚举的默认值.
public enum TraceLevel
{
Off = 0,
Error = 1,
Warning = 2,
Info = 3,
Verbose = 4
}
class Program
{
static void Main(string[] args)
{
TraceLevel traceLevelOptions;
Console.WriteLine(traceLevelOptions.ToString());
Console.ReadLine();
}
}
Run Code Online (Sandbox Code Playgroud)
获得正确答案后更新.以下代码应该有效:
public class SerializeMe
{
public int Id { get; set; }
public TraceLevel MyTrace { get; set; }
}
public enum TraceLevel
{
Off = 0,
Error = 1,
Warning = 2,
Info = 3,
Verbose = 4
}
class Program
{ …Run Code Online (Sandbox Code Playgroud) 我有以下内容:
Option Strict On
Public NotInheritable Class Root
Public Overrides Function Equals(obj As Object) As Boolean
If TypeOf obj Is Root Then
Dim rt As Root = DirectCast(obj, Root)
Return rt.container.Equals(Me.container) AndAlso
rt.question.Equals(Me.question)
End If
Return False
End Function
End Class
Run Code Online (Sandbox Code Playgroud)
FxCop正在给我这个警告:
Warning, Certainty 95, for DoNotCastUnnecessarily
{
Target : #Equals(System.Object) (IntrospectionTargetMember)
Location : file:///C:/..../Root.vb<46> (String)
Resolution : "'obj', a parameter, is cast to type 'Root' multiple
times in method 'Root.Equals(Object)'. Cache the result
of the 'as' operator or direct …Run Code Online (Sandbox Code Playgroud) FxCop认为我(基本上是从内存中)应该在MajorCamelCase中编写函数,类和属性,而私有变量应该在minorCamelCase中.
我在讨论IRC上一个相当受欢迎的项目,并引用了一些代码.另一个人,一个相当臭名昭着的巨魔,也是一个半操作(喘气!)似乎不同意.所有东西都应该在同一个外壳中,他非常热衷于MajorCamelCase,甚至是下划线.
当然,他只是一个巨魔,所以我估计我会像往常一样继续这样做.之前我学会了上述原则,我几乎连有一个连贯的命名风格.
他让我想到了 - 这样的事情真的很重要吗?
我一直在使用以下代码共享数据库变量:
Namespace DataAccessVariables
Public Class Vars
Public Shared s As String
Public Shared con As String = WebConfigurationManager.ConnectionStrings("Dev").ToString()
Public Shared c As New SqlConnection(con)
Public Shared x As New SqlCommand(s, c)
End Class
End Namespace
Run Code Online (Sandbox Code Playgroud)
然后我将其导入我的项目,如下所示:
Imports DataAccessVariables.Vars
Run Code Online (Sandbox Code Playgroud)
当我用FXCop检查网站时,我收到以下消息:
Error, Certainty 90, for StaticHolderTypesShouldNotHaveConstructors
{
Target : DBVars (IntrospectionTargetType)
Resolution : "Remove the public constructors from 'Vars'."
Help : http://msdn2.microsoft.com/library/ms182169(VS.90).aspx (String)
Category : Microsoft.Design (String)
CheckId : CA1053 (String)
RuleFile : Design Rules (String)
Info : "Instances of types that …Run Code Online (Sandbox Code Playgroud) 我已经定义了一个名为StringResourceCollection的类.我的班级声明如下:
namespace EPGObjectModel.IDE
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
public class StringResourceCollection : CollectionBase, IEnumerator
{
#region Fields
private int index = -1;
#endregion Fields
#region Properties
public object Current
{
get { return this.List[index]; }
}
#endregion Properties
#region Indexers
public EPGString this[string index]
{
get
{
Reset();
while (this.MoveNext())
{
if (((EPGString)Current).StringId == index || ((EPGString)Current).StringName.Equals(index))
return (EPGString)Current;
}
return null;
}
}
#endregion Indexers
#region Methods
public int Add(EPGString item)
{
try
{
return …Run Code Online (Sandbox Code Playgroud) 我有以下方法:
public byte[] HtmlToDoc(string hmtl, string userId)
{
byte[] data;
var auditor = new ServiceAuditor
{
User = userId
};
try
{
using (var tx = new ServerText())
{
tx.Create();
tx.Load(Server.HtmlDecode(hmtl), StringStreamType.HTMLFormat);
tx.Save(out data, BinaryStreamType.MSWord);
}
}
catch (Exception e)
{
auditor.Errormessage = e.Message + "/n " + e.StackTrace;
data = new byte[0];
}
finally
{
auditor.Save();
auditor.Dispose();
}
return data;
}
Run Code Online (Sandbox Code Playgroud)
我在编译期间收到以下警告:
警告 CA2000:Microsoft.Reliability:在方法“DocCreator.HtmlToDoc(string, string)”中,对象“new ServiceAuditor()”未沿所有异常路径处理。在对象“new ServiceAuditor()”上调用 System.IDisposable.Dispose 在对它的所有引用都超出范围之前。
奇怪的是,即使我正在处理该对象,我也不明白为什么它会抱怨。你能指出问题出在哪里吗?
fxcop ×6
c# ×3
.net ×1
asp.net ×1
camelcasing ×1
code-sharing ×1
coding-style ×1
database ×1
declaration ×1
dispose ×1
enums ×1
vb.net ×1
warnings ×1