Search code examples
c#stringdesign-patternsstylecopstatic-code-analysis

StyleCop custom rule to validate new List(size);


How can I check the syntax "new List(size);" from a code? size can be any numeric value to define the List size. this is to create a custom rule in StyleCop. I could go with regular expressions as well. what is the best way to solve this pattern. need to show a warning if developers use this pattern to initialise a List.


Solution

  • ^new(\s{1,})list(<(.*)>)?\((\d+)?\)(\s+)?;(\s+)?$ demo @ https://dotnetfiddle.net/DOC4pz