The Syntax Parsing Engine cannot recognize semantic errors. Finding these errors currently needs to be done by the developer after the syntax tree is constructed. Here are some examples of semantic errors:
A semantic error is text which is grammatically correct but doesn’t make any sense. An example in the context of the C# language will be “int x = 12.3;” - 12.3 is not an integer literal and there is no implicit conversion from 12.3 to int, so this statement does not make sense. But it is grammatically correct. It conforms to the rules built into the C# grammar definition.
Here are some general cases of semantic errors in languages like C# and VB:
Type mismatches – There are a lot of ways to use the wrong type in C#. The example above is one way. Here are some other possible ways:
Passing the wrong type of value to a method.
Returning the wrong type of value from a method.
Setting the wrong type of value on a property.
Setting the wrong type of value on a field.
Setting the wrong type of value on a local variable.
Using a type in a generic argument list which doesn’t conform to the generic type constraints.
Arity mismatch – This means using the wrong number of arguments for something and there are many ways to do this as well:
Passing the wrong number of arguments to a method call.
Passing the wrong number of indexes to an array or indexer.
Using the wrong number of generic type arguments for a generic type or method.
Using undefined names – Using a namespace, type, method, field, variable, or property name which doesn’t exist.
Naming collisions – Here are some ways names can collide:
Defining two types in the same namespace with the same name
Defining two methods in the same class with the same argument list and name.
Defining a property, field, or nested type which has the same name as another property, field, or nested type in the same owning type.
Defining two local variables in the same or descendant scopes with the same names.
The following topics provide additional information related to this topic.