Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The meaning of Dictionary`2 in a stack trace

Tags:

c#

stack-trace

Sometimes I see this `2 in a stack trace. For example:

System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary. at System.Collections.Generic.Dictionary`2.get_Item(TKey key)

What is the meaning of `2 after Dictionary?

like image 379
Ozkan Avatar asked Jan 29 '18 11:01

Ozkan


People also ask

What is a stack trace in programming?

Stack Trace: A stack trace is a report that provides information about program subroutines. It is commonly used for certain kinds of debugging, where a stack trace can help software engineers figure out where a problem lies or how various subroutines work together during execution. A stack trace is also known as a stack traceback or a stack ...

What should a developer look for in a stack trace?

Initially, developers should look in the application logs for the stack trace, because often, the stack trace tells you the exact line or function call that caused a problem. It’s a very valuable piece of information for quickly resolving bugs because it points to the exact location where things went wrong.

What is a “caused by” clause in stack trace?

Each individual exception is marked by the words “Caused by”. The lowest “Caused by” statement is often the root cause, so that’s where you should look at first to understand the problem. Here’s an example stack trace that includes several “Caused by” clauses. As you can see, the root issue in this example is the database call dbCall.

How do I get a stack trace from a throwable instance?

It’s an effective and simple solution to make your stack traces easy to understand. On top of that, when your program throws a Throwable instance, you can call the getStackTrace () function on the instance to access the stack trace. You can then decide to print the stack trace to your terminal so you can collect the information for log analysis.


2 Answers

The System.Collections.Generic.Dictionary`2 means that the type is System.Collections.Generic.Dictionary, with two type arguments. So in this case it means that the type is System.Collections.Generic.Dictionary<TKey, TValue>, as we all know it.

like image 185
Patrick Hofman Avatar answered Sep 16 '22 18:09

Patrick Hofman


This's the way .Net makes classes' names. The initial declaration

 Dictionary<K, V>

will be turned into Dictionary'2 type name where '2 means two generic parameters:

 // Dictionary`2 - two generic parameters
 Console.WriteLine(typeof(Dictionary<int, string>).Name);

 // List`1 - one generic parameter
 Console.WriteLine(typeof(List<int>).Name);

Please compare:

 // IDictionary`2 - two generic parameters
 Console.WriteLine(typeof(IDictionary<int, string>).Name);

 // IDictionary - no generic parameters
 Console.WriteLine(typeof(System.Collections.IDictionary).Name);
like image 34
Dmitry Bychenko Avatar answered Sep 16 '22 18:09

Dmitry Bychenko