Home
Search

Start | Blogs | IT | Custom vs field name

2023-03-11

IT

Custom auto generated field name in Visual Studio

Here is how to change the default Visual Studio auto generated field naming convention from PascalCase and the .this keyword to the more standard underscore and camelCase.

So this default code will change:

	private readonly IPublishedContentMapper AnaxiMapper;

		public BlogPostPageController(IPublishedContentMapper anaxiMapper)
		{
			this.AnaxiMapper = anaxiMapper;
		}

Into this:

	private readonly IPublishedContentMapper _anaxiMapper;

		public BlogPostPageController(IPublishedContentMapper anaxiMapper)
		{
			_anaxiMapper = anaxiMapper;
		}

Go to Tools -> Text Editor -> C# -> Code style -> Naming

An image of field settings in Visual Studio

 

Click on "Manage naming styles".

An image of managing naming styles

Click on the plus sign at the bottom left to add a new naming style.

An image of adding a new naming style

 

Fill in the name of the naming style at the top, e.g _fields.

Fill in the prefix at "Required prefix", in my example an underscore: _. An example on how it will look will appear in the bottom text box.

An image of a filled in example naming style