動くコード図鑑技術記事現場の渡り方キャリア論すべての記事About

動くコード図鑑

$ ls -1 /library | wc -l → 619

触って動く、検証済みの正解集。

公開記事から抽出した全コードブロックを 1 つずつページ化。 ▶ ボタンで 実行ログを再生し、 さも今動いてるかのように出力を流す。

この図鑑の使い方

言語で絞る

C# / SQL / TypeScript / PowerShell / Bash でフィルタ。

▶ で実行

事前収録の出力を 1 行ずつ再生。 ぱっと結果が見える。

記事と接続

各 snippet は出典記事へのリンク付き。 文脈ごと読める。

絞り込み

snippet 一覧

473
C#
// ✅定石6: TempDataでリクエスト跨ぎの状態渡し
[HttpPost]
public ActionResult Save(CustomerVm model)
{
    _service.Save(model);

定石6: TempDataで状態をリクエスト跨ぎで渡す

ASP.NET MVC 5 のルーティングを WinForms の Form 切替で理解する未収録#912ca2d90961
C#
// ✅ WinForms最小Formクラス(C#)
public partial class CustomerForm : Form
{
    public CustomerForm()
    {

対応1: Formクラス↔ Viewファイル(.cshtml)

WinForms の Form と Razor View の対応関係を業務SE が一日で腹落ちさせる未収録#d8188b08da10
C#
// ✅ WinForms版: Form_LoadでDBアクセス+画面表示を全部やる
public partial class CustomerForm : Form
{
    private void CustomerForm_Load(object sender, EventArgs e)
    {

対応2: Form_Load ↔ Controller Action

WinForms の Form と Razor View の対応関係を業務SE が一日で腹落ちさせる未収録#c648300ff80e
C#
// ✅ ASP.NET MVC版: ControllerのActionでDBアクセス、Modelに詰めてViewに渡す
public class CustomerController : Controller
{
    public ActionResult Index()
    {

対応2: Form_Load ↔ Controller Action

WinForms の Form と Razor View の対応関係を業務SE が一日で腹落ちさせる未収録#ce6b30c3260d
C#
// ✅ WinForms版: TextBox + Buttonで入力フォーム
public partial class SearchForm : Form
{
    public SearchForm()
    {

対応3:コントロール↔ HTML要素

WinForms の Form と Razor View の対応関係を業務SE が一日で腹落ちさせる未収録#fe49d8bb74e2
C#
// ✅ Controller側: POSTを受けるAction
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Search(string keyword)
{

対応3:コントロール↔ HTML要素

WinForms の Form と Razor View の対応関係を業務SE が一日で腹落ちさせる未収録#38f824ec39aa
C#
// ✅ WinForms:最小Form
public partial class HelloForm : Form
{
    public HelloForm()
    {

ミニマム検証の実演—最小Form ↔ View対比

WinForms の Form と Razor View の対応関係を業務SE が一日で腹落ちさせる未収録#e50e84d6948a
C#
// ✅ ASP.NET MVC:最小Controller
public class HelloController : Controller
{
    public ActionResult Index()
    {

ミニマム検証の実演—最小Form ↔ View対比

WinForms の Form と Razor View の対応関係を業務SE が一日で腹落ちさせる未収録#4c3f41f61d8c
C#
namespace IncludeStudy.Models
{
    public class Company
    {
        public int Id { get; set; }

Asp.NetCoreApiのModel外部参照でNullが出る

Asp.NetCoreApiのModel外部参照でNullを防ぐならIncludeを使え!未収録#081405cf0fee
C#
namespace IncludeStudy.Models
{
    public class Employee
    {
        [Key]

Asp.NetCoreApiのModel外部参照でNullが出る

Asp.NetCoreApiのModel外部参照でNullを防ぐならIncludeを使え!未収録#77f64dbdbb38
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;

Asp.NetCoreApiのModel外部参照でNullが出る

Asp.NetCoreApiのModel外部参照でNullを防ぐならIncludeを使え!未収録#87439b15ae9e
C#
        // GET: api/Companies/5
        [HttpGet("{id}/emp")]
        public async Task<ActionResult<ICollection<Employee>>> GetCompanyEmp(int id)
        {
            //var company = await _context.Company.FindAsync(id);

Nullが出ないようにするにはIncludeを使う

Asp.NetCoreApiのModel外部参照でNullを防ぐならIncludeを使え!未収録#d7106e5aa49e
C#
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddDbContext<DemoContext>(opt => opt.UseInMemoryDatabase("includeTest"));

JsonExceptionが出た場合は下記をする。

Asp.NetCoreApiのModel外部参照でNullを防ぐならIncludeを使え!未収録#7cf68e77babf
C#
    class Program
    {
        static void Main(string[] args)
        {

▶ 実行可

一時変数ありの場合

C#で変数の入れ替えをタプルでやってみた#b72acd62a946
C#
   class Program
    {
        static void Main(string[] args)
        {

▶ 実行可

一時変数ありの場合

C#で変数の入れ替えをタプルでやってみた#6f6208fdaa83
C#
    class Program
    {
        static void Main(string[] args)
        {

▶ 実行可

タプルでやってみた場合

C#で変数の入れ替えをタプルでやってみた#20be75b170a6
C#
    class Program
    {
        static void Main(string[] args)
        {
            var user = (1, "Taro", "Japan");
▶ 実行可

タプルを使ってみた。

C#で変数の入れ替えをタプルでやってみた#5f5e9fa3ebab
C#
    class Program
    {
        static void Main(string[] args)
        {
            var userList = new List<(int No,string Name,string address)>();
▶ 実行可

配列やリストを作ることもできる。

C#で変数の入れ替えをタプルでやってみた#eed089d96c0b
C#
    class Program
    {
        static void Main(string[] args)
        {
            var nums = (1,2);
▶ 実行可

引数にもできる。

C#で変数の入れ替えをタプルでやってみた#eb66ff26b2d7
C#
    class Program
    {
        static void Main(string[] args)
        {
            var nums = (10,3);
▶ 実行可

戻り値にも使える。

C#で変数の入れ替えをタプルでやってみた#c39cb6e992d5
C#
   class Program
    {
        static void Main(string[] args)
        {

▶ 実行可

タプルのネストも可能

C#で変数の入れ替えをタプルでやってみた#7c35180b3626
C#
        public static T Convert<T>(object obj)
        {
            if (obj != DBNull.Value && obj != null)
            {
                return (T) obj;

public static T Convert<T>(object obj)

C#でDBNullチェック時にエラーを出さずにキャストする最適解未収録#0b585de72732
C#
                    using (var dataReader = command.ExecuteReader())
                    {
                        if (dataReader.HasRows)
                        {
                            while (dataReader.Read())

using (var dataReader = command.ExecuteReader())

C#でDBNullチェック時にエラーを出さずにキャストする最適解未収録#188d86a8b3a8
C#
    public static class ObjectHelper
    {
        public static T ReplaceDBNull<T>(this object obj)
        {
            if (obj != DBNull.Value && obj != null)

拡張メソッドで書いてみる

C#でDBNullチェック時にエラーを出さずにキャストする最適解未収録#634a4ba2b038
C#
                    using (var dataReader = command.ExecuteReader())
                    {
                        if (dataReader.HasRows)
                        {
                            while (dataReader.Read())

拡張メソッドで書いてみる

C#でDBNullチェック時にエラーを出さずにキャストする最適解未収録#6b7ea235199c
C#
            var dts = dt.AsEnumerable()
                .GroupBy(r => new{group1 = r.Field<string>("売れたもの"),
                    group2 = r.Field<string>("買った人") })
                .Select(r => r.CopyToDataTable());

C#でDataTable指定の列で分割!

C#でDataTableをLinqを用いて分割する方法!未収録#11b39dfef54b
C#
namespace ConsoleApplication2
{
    class Program
    {

▶ 実行可

デリゲート型を宣言していくパターン

C#のデリゲートまとめ!型とFuncとActionと!#82fa2d1cc2a1
C#
namespace ConsoleApplication2
{
    class Program
    {

▶ 実行可

Func<>で同じことをやってみる。

C#のデリゲートまとめ!型とFuncとActionと!#48a78219b48c
C#
namespace ConsoleApplication2
{
    class Program
    {

▶ 実行可

Action<>で同じことをやってみる。

C#のデリゲートまとめ!型とFuncとActionと!#822e883e6ddf
C#
    public class Todo
    {
        public int Id { get; set; }
        public string Sammary { get; set; }
        public string Detail { get; set; }

TodoMODELの構造

ASP.NETでDetails時にログイン中ユーザーのデータのみ出力する方法未収録#88121ca71021