Sharing variables between languages
When I first tried Polyglot Notebooks, I had an immediate thought to try to work directly with an F# variable from C# code.
Let’s try that now by adding a new code cell and changing its language to C#. Next, add a line of code attempting to reference the maxNum
variable:
Console.WriteLine($"The value in maxNum is {maxNum}");
When you run this, you should see a “red squiggly line” under maxNum
and get a CS0103
compiler error below your cell, as shown in Figure 2.20:
Figure 2.20 – A compiler error shown in the notebook
As we saw earlier, maxNum
is available in the Variables view, so what’s going on?
It turns out that each language has its own kernel that is part of .NET Interactive. This means that your C# variables are kept in a separate area from your F# variables, and so on for other languages.
Effectively, your C# code cannot reference F# variables and vice...