Saturday 11 February 2017

Creating public class by default Visual Studio


Recently, I came across a silly mistake – where, I was not able to see my newly created class in intellisense in visual studio.

After starting from the scratch, it came to notice that by default visual studio creates class without access specifiers and that is the reason, it became private and it didn't show up in Intellisense.
You can fix it. Here is how. When you try creating new class, it uses the template located under path:

%systemdrive%\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\ItemTemplates\CSharp\Code\1033\Class

You should see the file named class.cs which has content as below.

using System;
using System.Collections.Generic;
$if$ ($targetframeworkversion$ >= 3.5)using System.Linq;
$endif$using System.Text;
$if$ ($targetframeworkversion$ >= 4.5)using System.Threading.Tasks;
$endif$
namespace $rootnamespace$
{
class $safeitemrootname$
{
}
}
Here, change the class definition by adding Public access specifier and save/override the file.
public class $safeitemrootname$

You should be good to go.
Please leave your comments for any question concerns.
Please note that my post is open for suggestion(s) or correction(s).

1 comment: