1

I came across some code (posted here) and I can't seem to figure out what I'm supposed to do with it. @namespace appears three times in the code. Here's what I'm looking at.

 public string GetFullSchema()
 {

     string @namespace = "yourNamespace";

     var q = from t in Assembly.GetExecutingAssembly().GetTypes()
             where t.IsClass && t.Namespace == @namespace
             select t;

     XmlReflectionImporter importer = new XmlReflectionImporter(@namespace);

     XmlSchemas schemas = new XmlSchemas();
     XmlSchemaExporter exporter = new XmlSchemaExporter(schemas);


    foreach (var x in q)
     {
         var map = importer.ImportTypeMapping(x);
         exporter.ExportTypeMapping(map);
     }

     using (MemoryStream ms = new MemoryStream())
     {
         schemas[0].Write(ms);
         ms.Position = 0;
         return new StreamReader(ms).ReadToEnd();
     }

 }

Am I assuming correctly this is some sort of coding convention I'm unaware of? Should that line be replaced by something?

Community
  • 1
  • 1
  • That one really didn't come up and I tried finding something similar to my case. But thanks, that really solved my problem! – Neckbeard2016 Apr 07 '16 at 08:03

4 Answers4

1

The @ symbol will allow you to use reserved word. So whenever you want to use(which you may try to avoid) the identifier or keywords in your variable then you need to put the @ symbol next to it.

MSDN says:

The prefix "@" enables the use of keywords as identifiers, which is useful when interfacing with other programming languages.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

This is because namespace is a reserved word in C#. If you want to have an identifier in your code that matches some reserved word, you have to change the identifier. You can use underscore and sometimes people use @. Like _namespace or @event.

Alexey Zimarev
  • 17,944
  • 2
  • 55
  • 83
1

https://msdn.microsoft.com/en-us/library/aa664670.aspx

The prefix "@" enables the use of keywords as identifiers, which is useful when interfacing with other programming languages. The character @ is not actually part of the identifier, so the identifier might be seen in other languages as a normal identifier, without the prefix. An identifier with an @ prefix is called a verbatim identifier. Use of the @ prefix for identifiers that are not keywords is permitted, but strongly discouraged as a matter of style.

Darren Lamb
  • 1,535
  • 14
  • 18
1

The @-symbol as an prefix allows you to use reserved keywords as variables again. The keyword namespace e.g. is already reserved by the language itself. But in my oppinion this is not the common way to solve the problem. It is a better approach to rename the variable so that there is no conflict.

zimmerrol
  • 4,872
  • 3
  • 22
  • 41