This article is a mirror article of machine translation, please click here to jump to the original article.

View: 3984|Reply: 2

[Source] Code execution in Visual Studio using the C# Interactive window

[Copy link]
Posted on 2024-3-9 12:06:50 | | | |
Requirements: In daily development, you may need to simply test some C# code or write a small demo, and it may be troublesome to create a new console application to execute each time.

First, open the "C# Interactive", run VS 2022 and select Continue without code, click "View" - > "Other Windows", as shown below:



Using the Environment.Version code, output the version of the current runtime.

For .NET Core 3.x and .NET 5+, the Environment.Version property returns the .NET runtime version number. For .NET Framework versions 4, 4.5, 4.5.1, and 4.5.2, the Environment.Version property returns a Version object with a string representation of 4.0.30319.xxxxx. For .NET Framework 4.6 and later and versions prior to .NET Core 3.0, it is in the form of 4.0.30319.42000.
Reference:The hyperlink login is visible.

Check out the help on #help as follows:

Keyboard shortcuts:
  Enter evaluates the current commit if it shows complete. Otherwise, insert a new line.
  Ctrl-Enter evaluates the current commit within the current commit.
                       Within the previous commit, append the previous commit to the current commit.
  Shift-Enter inserts a new line.
  Escape clears the current commit.
  Alt-UpArrow replaces the current commit with the previous commit.
  Alt-DownArrow replaces the current commit with the next commit (after it has previously navigated backwards).
  Ctrl-Alt-UpArrow replaces the current commit with the previous commit that starts with the same text.
  Ctrl-Alt-DownArrow replaces the current commit with the next commit that starts with the same text (after it has previously been navigated backwards).
  Ctrl-K, Ctrl-Enter paste the selected content at the end of the interactive buffer, placing the insertion point at the end of the input.
  Ctrl-E, Ctrl-Enter paste the selected content and execute it before any pending input in the interactive buffer.
  Ctrl-A selects the commit containing the cursor on the first press and selects all the text in the window the second time.
REPL command:
  #cls, #clear clears the content in the editor window, keeping the history and execution context intact.
  #help Displays help for specified commands, or for all available commands and keybindings if not specified.
  #reset Reset the execution environment to its initial state and preserve history by selecting to switch the runtime of the host process.
Script Instructions:
  #r Add metadata references to a given assembly and all of its dependencies, such as #r "myLib.dll".
  #load Load the specified script file and execute, e.g. #load "myScript.csx".
useThe "Ctrl-Enter" key executes the code to be committed, and the "Alt-Up Arrow Symbol" selects to enter the historical code

Run simple C# code to test:



Referencing other dll dependencies, if you want to serialize an object, you may need to use the Newtonsoft.Json library to load the dll with the #r command, the code is as follows:



At the same time, we can also save the code as a .csx file for easy execution next time, the file content is as follows:

The execution effect is as follows:



(End)





Previous:SQL Server uses extended events to track deadlock issues
Next:.NET/C# Roslyn compiler platform
 Landlord| Posted on 2024-3-9 12:13:42 |
The Visual Studio Code plugin adds Polyglot Notebooks (formerly .NET Interactive Notebooks) to also execute C# code.

.NET Interactive address:https://github.com/dotnet/interactive
Try .NET address:https://github.com/dotnet/try
 Landlord| Posted on 2024-3-9 13:52:41 |
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com