Fred Smith Fred Smith - 1 year ago 72 Question

Visual Studio how bad is it to add code in the designer files?

I often read other people's code and once in a while, I saw some code added in a Designer file.
when I try to understand why some developers write code in a designer file, I often find myself arguing for hours with them.
Is there a Microsoft rule not to do it or is it open for anybody ?

Answer Source

The comment in the designer file should say enough:

This code is generated. Modifications will be lost when this code is regenerated.

Therefore, there is a risk: you can lose your modifications and / or additions.

Do note, some generators use #region markup to mark what they will generate. For example LLBLgen. However usually these files are not named with .designer.cs.

As for the discussion, a few arguments:

  • usually designer generated files are partial. So the class can be edited in the other file. If something in the designer file needs changes, best is to use the generator tool instead.
  • Code quality tools like Stylecop and code metrics usually are configured to ignore designer generated files. You might miss important feedback.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download