{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Jupyter笔记 简单入门\n",
    "=============================="
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用 Jupyter NoteBook 形式,优点是代码执行和 Markdown 文档放在同一个文件中,并可以交替显示和分段执行代码。"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 显式声明使用 C# 语言\n",
    "+ csharp\n",
    "+ fsharp\n",
    "+ pwsh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [],
   "source": [
    "#!csharp"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## C# 中的引用\n",
    "+ 导入程序文件\n",
    "  > 可以导入多种文件,包括 .cs .csc .fc .js等,语法为` #!import /path/to/file ` \n",
    "+ 引用本机 Dll 文件\n",
    "  > 可以导入本机编译好的dll文件,语法为 `#!import /path/to/file`\n",
    "+ 引用 Nuget 包\n",
    "+ > 默认包源下,可以导入nuget,当然也可以设置包源,语法为 `#r 包名,可选版本号`"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## C# 中的引用\n",
    "+ 导入程序文件\n",
    "  > 可以导入多种文件,包括 .cs .csc .fc .js等,语法为` #!import /path/to/file ` "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [],
   "source": [
    "#! import \"../NotebookStudy.ConsoleApp/Persion.cs\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [],
   "source": [
    "//单纯引用.cs文件,不能直接使用,解决方法待查找\n",
    "var p = new Person()\n",
    "{\n",
    "    Id=1,\n",
    "    Name=\"小张\",\n",
    "    Address=\"上海无名路1号\",\n",
    "    Age = 28\n",
    "};\n",
    "//在NoteBook中,可以格式化显示(Notebook提供) C#对象:使用 display(对象); 如果在代码结尾的话,可以直接写 C#对象。\n",
    "display(p);\n",
    "\n",
    "//代码结尾的话,直接写 对象\n",
    "p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [],
   "source": [
    "var x = new {Name=\"xx\",Agx=33};\n",
    "display(x);"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ 引用本机 Dll 文件\n",
    "  > 可以导入本机编译好的dll文件,语法为 `#!import /path/to/file`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [],
   "source": [
    "//如果找不到文件,需要先生成 NotebookStudy.ConsoleApp 项目\n",
    "\n",
    "#r \"../NotebookStudy.ConsoleApp/bin/Debug/net7.0/NotebookStudy.ConsoleApp.dll\"\n",
    "\n",
    "using NotebookStudy.ConsoleApp;\n",
    "\n",
    "var personA= new Person()\n",
    "{\n",
    "    Id=2,\n",
    "    Name=\"本山\",\n",
    "    Age=55\n",
    "};\n",
    "\n",
    "personA"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ 引用 Nuget 包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [],
   "source": [
    "//安装库的最新版本\n",
    "#r \"nuget:newtonsoft.json\"\n",
    "var jsonObj = new {Id=2,Name=\"newtonsoft类库\", Age=6};\n",
    "\n",
    "//使用库\n",
    "var jsonText = Newtonsoft.Json.JsonConvert.SerializeObject(jsonObj);\n",
    "jsonText"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [],
   "source": [
    "//安装库的指定版本\n",
    "#r \"nuget:System.Text.Json,4.7.2\"\n",
    "\n",
    "//使用库\n",
    "var jsonObj2 = new {Id=2,Name=\"System.Text.Json类库\", Age=6};\n",
    "var jsonText2 = System.Text.Json.JsonSerializer.Serialize(jsonObj2);\n",
    "jsonText2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [],
   "source": [
    "//安装最新的预览版库\n",
    "#r \"nuget:xunit,*-*\"\n",
    "\n",
    "using Xunit;\n",
    "using Xunit.Abstractions;\n",
    "using Xunit.Sdk;\n",
    "\n",
    "public class UnitTest1\n",
    "{\n",
    "    [Fact]\n",
    "    public void Test1()\n",
    "    {\n",
    "        Assert.True(true, \"xxxx\");\n",
    "    }\n",
    "}"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Nuget 包源管理\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ 在线包源\n",
    "  > 默认包源:https://api.nuget.org/v3/index.json 使用默认包源的话,可以不添加包源引用。当然也可以添加。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [],
   "source": [
    "//添加包源:可以添加多个\n",
    "#i \"nuget:https://api.nuget.org/v3/index.json\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [],
   "source": [
    "//使用包源\n",
    "\n",
    "#r \"nuget:AutoFixture.Xunit2\""
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ 本机包源 Nuget"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [],
   "source": [
    "//添加本地包源\n",
    "#i \"nuget:C:\\Program Files (x86)\\Microsoft SDKs\\NuGetPackages\\\"\n",
    "\n",
    "//使用包源\n",
    "#r \"nuget:xunit\"\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ 经过身份验证的Nuget包源\n",
    "  > 不能直接引用验证包源,可以通过将源的 PAT 放入用户级 nuget.config 文件来访问经过身份验证的源"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 内建魔术命令"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ #!about 显示有关内核版本的信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [],
   "source": [
    "#!about"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ #!lsmagic 列出可用的魔术命令,包括可能已通过扩展安装的命令"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [],
   "source": [
    "#!lsmagic"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ #!time 显示执行时间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [],
   "source": [
    "#!time"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ -h 参数 显示命令的帮助信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [],
   "source": [
    "#!time -h"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".NET (C#)",
   "language": "C#",
   "name": ".net-csharp"
  },
  "language_info": {
   "name": "polyglot-notebook"
  },
  "polyglot_notebook": {
   "kernelInfo": {
    "defaultKernelName": "csharp",
    "items": [
     {
      "aliases": [],
      "name": ".NET"
     },
     {
      "aliases": [
       "C#",
       "c#"
      ],
      "languageName": "C#",
      "name": "csharp"
     },
     {
      "aliases": [
       "js"
      ],
      "languageName": "JavaScript",
      "name": "javascript"
     },
     {
      "aliases": [],
      "languageName": "KQL",
      "name": "kql"
     },
     {
      "aliases": [
       "frontend"
      ],
      "name": "vscode"
     },
     {
      "aliases": [],
      "name": "webview"
     }
    ]
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}