- Quick Start
- Create a New Project
- Show snippets using regions
- Create Sessions
- Verify your Project
- Passing Arguments
- Using Read-only Snippets
- Reference
Let's make this a little more interesting. Your project probably has more than one snippet you want people to be able to run. Sessions allow you to run these code snippets independently of one another.
-
In your
MyConsoleAppfolder, add a new file calledCat.csand add the following code:using System; namespace MyConsoleApp { class Cat { #region what_the_cat_says public string Say() { return "meow!"; } #endregion } }
-
Update your
Program.cs, replacing the contents of thesay_helloregion with this:Console.WriteLine(new Cat().Say());
-
In
doc.md, create two separate snippets with one pointing toProgram.csand thesay_helloregion and the other pointing toCat.csand thewhat_the_cat_saysregion.# My code sample: ```cs --source-file .\MyConsoleApp\Program.cs --project .\MyConsoleApp\MyConsoleApp.csproj --region say_hello ``` ```cs --source-file .\MyConsoleApp\Cat.cs --project .\MyConsoleApp\MyConsoleApp.csproj --region what_the_cat_says ```
Once you've made these changes, refresh the page and hit
Run.These code snippets compile and execute together. You can see this by editing the method name in the second editor. After a moment, you'll see a red squiggle indicating a compile error:
But you might want these snippets to be able to compile and execute independently of one another. That way, if your user breaks the code in one, the others will still work. To do this, you can use a
--sessionoption, providing a new session name for each snippet. Any snippets that share a session name will compile together. -
Go back to the code fence snippets we created in step 3 and add a
--sessionoption to each one:# My code sample: ```cs --source-file .\MyConsoleApp\Program.cs --project .\MyConsoleApp\MyConsoleApp.csproj --region say_hello --session one ``` ```cs --source-file .\MyConsoleApp\Cat.cs --project .\MyConsoleApp\MyConsoleApp.csproj --region what_the_cat_says --session two ```
Once you've made this change, refresh the page. You should see two separate output panels and two run buttons containing the text that you specified for the session names.
In the second editor, change
"meow"to"purrrr". Clickoneand then clicktwo.
Well done! Now, you have a project that you are almost ready to share with others. A big part of good documentation is making sure everything works. In dotnet try we do this by using dotnet try verify which we will explore in the next module.
NEXT: Verify your Project »
