{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "[this doc on github](https://github.com/dotnet/interactive/tree/main/samples/notebooks/csharp/Docs)\n", "\n", "# Displaying output in a .NET notebook" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When writing C# in a .NET notebook, the C# scripting language is used, which you might be familiar with from using the C# Interactive window in Visual Studio. This dialect of C# allows you to end a code submission without a semicolon, which tells C# scripting to return the value of the expression." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [], "source": [ "var x = \"Hello!\";\n", "display(x);\n", "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When you end a cell with an expression evaluation like this, it is the return value of the cell. There can only be a single return value for a cell. If you add more code after a return value expression, you'll see a compile error.\n", "\n", "There are also several ways to display information without using the return value. The most intuitive one for many .NET users is to write to the console:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [], "source": [ "Console.Write(\"hello\");\n", "Console.Write(\" \");\n", "Console.WriteLine(\"world\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "But a more familiar API for many notebook users would be the `display` method." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [], "source": [ "display(\"hello\");\n", "display(\"world\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Each call to `display` writes an additional display data value to the notebook.\n", "You can also update an existing displayed value by calling `Update` on the object returned by a call to `display`.\n", "\n", "Note, in some environments such as Jupyter Lab, only calls to `Update` executed during the execution of the cell are respected." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [], "source": [ "var fruitOutput = display(\"Let's get some fruit!\");\n", "var basket = new [] {\"apple\", \"orange\", \"coconut\", \"pear\", \"peach\"};\n", "\n", "foreach (var fruit in basket)\n", "{\n", " System.Threading.Thread.Sleep(1000);\n", " fruitOutput.Update($\"I have 1 {fruit}.\"); \n", "}\n", "\n", "System.Threading.Thread.Sleep(1000);\n", "\n", "fruitOutput.Update(basket);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "**_See also_**\n", "* [Formatting outputs](Formatting-outputs.ipynb)\n", "* [HTML](HTML.ipynb)" ] } ], "metadata": { "kernelspec": { "display_name": ".NET (C#)", "language": "C#", "name": ".net-csharp" }, "language_info": { "file_extension": ".cs", "mimetype": "text/x-csharp", "name": "C#", "pygments_lexer": "csharp", "version": "8.0" } }, "nbformat": 4, "nbformat_minor": 4 }