2

I am probably making a dumb error but I cannot see it.

I added a custom class to my project following the simple Newtownsoft example here.

namespace MyApp
{
     public class Secrets
     {
          public string Test {get;set;}
     } 
}

But when I go to reference the class in my Program.cs:

      namespace MyApp
      {
          static class Program
           {
              [STAThread]
              static void Main()
               {
                 <snip>

                 string json = FetchJson();
                 var MySecrets = JsonConvert.DeserializeObject<Secrets>(json);   // this line
               }
           }
      }
 

Visual Studio underlines <Secrets> and says the type or namespace 'Secrets' cannot be found. But the project compiles without error.

If I change the name of the class to Sekrets then Visual Studio finds it and is happy.

P.S. And if I then rename the file from Sekrets.cs back to Secrets.cs and let VS make the global change, then VS remains happy, no error, no red underlining.

Tim
  • 8,669
  • 31
  • 105
  • 183

1 Answers1

0

P.S. And if I then rename the file from Sekrets.cs back to Secrets.cs and let VS make the global change, then VS remains happy, no error, no red underlining.

Sounds like an Intellisense database problem, you generally fix those by closing VS and then deleting your .vs folder. Next time you open your solution, VS will rebuild the database from scratch.

Luckily it almost never happens in C#, since it's a much easier language to parse than C++. Intellisense really has problems parsing that.

Blindy
  • 65,249
  • 10
  • 91
  • 131
  • what is it about c# that it's easier to parse than c++? I would say it's much more complicated to analyze C# because it doesn't use declarations and definitions can be anywhere in the codebase whilst there are still things like pointers and references – Adassko Oct 30 '20 at 16:08
  • C++ has templates and the whole template meta-programming system, even the most basic standard classes, like `string`, are actually deeply nested templates. Parsing C# correctly is a solved problem, since Roslyn, the C# compiler, is available as a service for anyone to use. – Blindy Oct 30 '20 at 16:11