Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Code Analysis CA1822 Warning - Why?

I have the method shown below which is generating a CA1822 Code Analysis warning. CA1822 says this:

"The 'this parameter (or 'Me' in Visual Basic) of 'ImportForm.ProcessFile(StreamReader)' is never used. Mark the member as static (or Shared in Visual Basic) or use 'this/Me' in the method body or at least one property accessor, if appropriate."

Can anyone tell me why I am getting this warning, since the 'reader' parameter is in fact being used?

private void ProcessFile(StreamReader reader) {    string[] lines;     lines = reader.ReadToEnd().Split(new string[] { Environment.NewLine }, StringSplitOptions.None);     ParseFile.IVAFile(lines); } 
like image 338
Randy Minder Avatar asked Feb 09 '10 14:02

Randy Minder


2 Answers

It means you use no members of the object. All the items in the method come from the parameters.

Therefore the method can safely be made static.

like image 62
Jeff Foster Avatar answered Sep 27 '22 18:09

Jeff Foster


"reader" is being used, but you're not using "this" anywhere, so you can make the method static.

The only reason not to make it static would be if you want to use polymorphism later - e.g. making it virtual and overriding it elsewhere.

like image 30
Jon Skeet Avatar answered Sep 27 '22 20:09

Jon Skeet