【Unity/C#】delegateが全然理解できません。

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
Math

Math

#31

投稿記事 by Math » 6年前

>[雑談]
>Visual Basic 2015は2015年に.NET Framework 4.6とともに公開された。Roslynと呼ばれるコンパイラレイヤーにより、Visual C#と同等のIDE機
>能を備えるに至った。
(C#の開発責任者がVBも兼任し約2000万行を使って開発したときいている。)
--------------------------------------------------------------------------------
VBの場合の ラムダ式 の書き方です。VBはC#より容易に取り組む事が出来てC#と同様のことが出来るようになりました。

C#の場合と同様のVBのプログラムです。
vb1.vb

コード:

Module Module1

    Private f As Form
    Private t As TextBox
    Private b As Button
    Sub Main()

        f = New Form()
        t = New TextBox()
        b = New Button()
        f.Text = "Form"
        t.Text = "TextBox: ABCDE"
        b.Text = "Button"
        f.Size = new Size(300, 200)
        f.Location = new Point(500, 400)
        t.Location = new Point(30, 20)
        b.Location = new Point(30, 60)
        f.Controls.Add(t)
        f.Controls.Add(b)
        AddHandler b.Click, Sub(sender, e) t.Text = "C# ボタン: Hello!"

        Application.Run(f)

    End Sub

End Module

AddHandler b.Click, Sub(sender, e) t.Text = "C# ボタン: Hello!"の部分が 「ラムダ式」 です。


VBの応答ファイルには
/imports:System
/imports:Microsoft.VisualBasic
/imports:System.Linq
/imports:System.Xml.Linq
が書かれているのでソースには書く必要がありません。追加のimportsはPowerShell, バッチファイル側に書くことが出来ます。

vb.rsp

コード:

# This file contains command-line options that the VB
# command line compiler (VBC) will process as part
# of every compilation, unless the "/noconfig" option
# is specified.

# Reference the common Framework libraries
/r:Accessibility.dll
/r:System.Configuration.dll
/r:System.Configuration.Install.dll
/r:System.Data.dll
/r:System.Data.OracleClient.dll
/r:System.Deployment.dll
/r:System.Design.dll
/r:System.DirectoryServices.dll
/r:System.dll
/r:System.Drawing.Design.dll
/r:System.Drawing.dll
/r:System.EnterpriseServices.dll
/r:System.Management.dll
/r:System.Messaging.dll
/r:System.Runtime.Remoting.dll
/r:System.Runtime.Serialization.Formatters.Soap.dll
/r:System.Security.dll
/r:System.ServiceProcess.dll
/r:System.Transactions.dll
/r:System.Web.dll
/r:System.Web.Mobile.dll
/r:System.Web.RegularExpressions.dll
/r:System.Web.Services.dll
/r:System.Windows.Forms.Dll
/r:System.XML.dll

/r:System.Workflow.Activities.dll
/r:System.Workflow.ComponentModel.dll
/r:System.Workflow.Runtime.dll
/r:System.Runtime.Serialization.dll
/r:System.ServiceModel.dll

/r:System.Core.dll
/r:System.Xml.Linq.dll
/r:System.Data.Linq.dll
/r:System.Data.DataSetExtensions.dll
/r:System.Web.Extensions.dll
/r:System.Web.Extensions.Design.dll
/r:System.ServiceModel.Web.dll

# Import System and Microsoft.VisualBasic
/imports:System
/imports:Microsoft.VisualBasic
/imports:System.Linq
/imports:System.Xml.Linq

/optioninfer+
http://www2.koyoen.birdview.co.jp/~abcxyz/n.jpg

Math

Re: 【Unity/C#】delegateが全然理解できません。

#32

投稿記事 by Math » 6年前

従来のコマンドプロンプトからコンパイル&実行するにはバッチ・ファイルをつくります。

vb.bat

コード:

DIR

C:\windows\Microsoft.NET\Framework64\v4.0.30319\vbc.exe vb1.vb /imports:System.Windows.Forms /imports:System.Drawing

.\vb1.exe

pause
http://www2.koyoen.birdview.co.jp/~abcxyz/o.jpg

Math

Re: 【Unity/C#】delegateが全然理解できません。

#33

投稿記事 by Math » 6年前

C# [継承] を使用した場合の例

従来のコマンドプロンプトからコンパイル&実行する。バッチ・ファイルを使用します。
C#
cs.bat

コード:

DIR

C:\windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe cs1.cs

.\cs1.exe
cs1.cs

コード:

/////
using System;
using System.Windows.Forms;
using System.Drawing;
public class f : Form
{
    Button b;
    TextBox t;

    public f()
    {
        InitializeComponent();
    }
    private void InitializeComponent()
    {
        b = new Button();
        t = new TextBox();
        this.Text = "Form";
        t.Text = "TextBox: ABCDE";
        b.Text = "Button";
        this.Size = new Size(300, 200);
        this.Location = new Point(500, 400);
        t.Location = new Point(30, 20);
        b.Location = new Point(30, 60);
        this.Controls.Add(b);
        this.Controls.Add(t);
        b.Click += (sender, e) => { t.Text = "C# : Form 継承"; };
        b.Click += new System.EventHandler(button1_Click);
    }
    static void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show("AAA");
    }
}
/////
public class Program
{
    [STAThread]
    static void Main()
    {
        Application.Run(new f());
    }
}
http://www2.koyoen.birdview.co.jp/~abcxyz/p.jpg

http://www2.koyoen.birdview.co.jp/~abcxyz/r.jpg

Math

Re: 【Unity/C#】delegateが全然理解できません。

#34

投稿記事 by Math » 6年前

VisualStudio風に3分割して書き直せば
cs.bat

コード:

DIR

C:\windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe Program.cs Form1.cs Form1.Designer.cs 

.\Program.exe

pause
Program.cs

コード:

/////
using System;
using System.Windows.Forms;

public class Program
{
    [STAThread]
    static void Main()
    {
        Application.Run(new f());
    }
}
/////
Form1.cs

コード:

/////
using System;
using System.Windows.Forms;

partial class f : Form
{
    public f()
    {
        InitializeComponent();
    }

    static void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show("AAA");
    }
}
/////
Form1.Designer.cs

コード:

/////
using System;
using System.Windows.Forms;
using System.Drawing;

partial class f : Form
{
    Button b;
    TextBox t;
 
    private void InitializeComponent()
    {
        b = new Button();
        t = new TextBox();
        this.Text = "Form";
        t.Text = "TextBox: ABCDE";
        b.Text = "Button";
        this.Size = new Size(300, 200);
        this.Location = new Point(500, 400);
        t.Location = new Point(30, 20);
        b.Location = new Point(30, 60);
        this.Controls.Add(b);
        this.Controls.Add(t);
        b.Click += (sender, e) => { t.Text = "C# : Form 継承"; };
        b.Click += new System.EventHandler(button1_Click);
    }
}
/////
http://www2.koyoen.birdview.co.jp/~abcxyz/q.jpg

Math

Re: 【Unity/C#】delegateが全然理解できません。

#35

投稿記事 by Math » 6年前

[ ① C#,  DataGridView ]
(② VB,  ③ PowerShell,  ④ C++ (C++/CRL) で同じようなプログラムを作って比較してみる)

① C#
cs.bat

コード:

DIR

C:\windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe cs1.cs

.\cs1.exe
cs1.cs

コード:

using System;
using System.Data;
using System.Windows.Forms;
using System.Drawing;
public class Program
{
    [STAThread]
    static void Main()
    {
        Form f;
        Button b;
        TextBox t;
        DataGridView dg;
        DataTable dt;
        f = new Form();
        b = new Button();
        t = new TextBox();
        dg = new DataGridView();
        dt = new DataTable();
        f.Text = "Form";
        t.Text = "TextBox";
        b.Text = "Button";
        f.Size = new Size(300, 300);
        f.Location = new Point(500, 400);
        t.Location = new Point(20, 20);
        b.Location = new Point(150, 20);
        dg.Location = new Point(20, 60);
        f.Controls.Add(b);
        f.Controls.Add(t);
        f.Controls.Add(dg);
        dg.Anchor =  (AnchorStyles)15;
        dt.Columns.Add("A");
        dt.Columns.Add("B");
        dt.Columns.Add("C");
        dg.DataSource = dt;
        dt.TableName="dtSheet1";
        dt.ReadXml("DataGridView.xml");
        dg.CurrentCell = dg[2, 100];
        dg.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
        b.Click += (sender, e) => t.Text = dg.CurrentRow.Cells[2].Value.ToString();
        Application.Run(f);
    }
}
Xmlデータ
http://www2.koyoen.birdview.co.jp/~abcx ... idView.xml

実行
http://www2.koyoen.birdview.co.jp/~abcxyz/s.jpg

Math

Re: 【Unity/C#】delegateが全然理解できません。

#36

投稿記事 by Math » 6年前

[ ② VB,,  DataGridView ]の場合

vb.bat

コード:

DIR

C:\windows\Microsoft.NET\Framework64\v4.0.30319\vbc.exe vb1.vb /imports:System.Windows.Forms /imports:System.Drawing ^
/imports:System.Data

.\vb1.exe

pause
 

vb1.vb

コード:

Module Module1

    Private f As Form
    Private t As TextBox
    Private b As Button
    Private dg As DataGridView
    Private dt As DataTable

    Sub Main()

        f = New Form()
        t = New TextBox()
        b = New Button()
        dg = New DataGridView()
        dt = New DataTable()

        f.Text = "Form"
        t.Text = "TextBox: ABCDE"
        b.Text = "Button"

        f.Size = new Size(300, 300)
        f.Location = new Point(500, 400)
        t.Location = new Point(20, 20)
        b.Location = new Point(150, 20)
        dg.Location = new Point(20, 60)

        f.Controls.Add(t)
        f.Controls.Add(b)
        f.Controls.Add(dg)

        dg.Anchor = 
(AnchorStyles.Top Or AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right)
        dt.Columns.Add("A")
        dt.Columns.Add("B")
        dt.Columns.Add("C")

        dg.DataSource = dt
        dt.TableName="dtSheet1"
        dt.ReadXml("DataGridView.xml")
        dg.CurrentCell = dg(2, 100)
        dg.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells

        AddHandler b.Click, Sub(sender, e) t.text=dg.CurrentRow.Cells(2).Value.ToString()

        Application.Run(f)

    End Sub

End Module
http://www2.koyoen.birdview.co.jp/~abcxyz/t.jpg

http://www2.koyoen.birdview.co.jp/~abcxyz/u.jpg

Math

Re: 【Unity/C#】delegateが全然理解できません。

#37

投稿記事 by Math » 6年前

[ ③ PowerShell,  DataGridView ]の場合

p.bat

コード:

powershell_ise ./ps1.ps1
ps1.ps1

コード:

ls

# Add-Type -AssemblyName System.Windows.Forms #不要:デフォルトで
# Add-Type -AssemblyName System.Drawing       #参照設定される
Add-Type -AssemblyName System.Data

$f = New-Object System.Windows.Forms.Form 
$b = New-Object System.Windows.Forms.Button
$t = New-Object System.Windows.Forms.TextBox
$dg = New-Object System.Windows.Forms.DataGridView
$dt = New-Object System.Data.DataTable

$f.Text = "Form"
$t.Text = "TextBox:ABCDE"
$b.Text = "Button"

$f.Size = New-Object System.Drawing.Size(300,300) 
$f.Location = New-Object System.Drawing.Point(500, 400);
$t.Location = New-Object System.Drawing.Point(20, 20);
$b.Location = New-Object System.Drawing.Point(150, 20);
$dg.Location = New-Object System.Drawing.Point(20, 60);

$dg.Anchor = 15

$f.Controls.Add($b);
$f.Controls.Add($t);
$f.Controls.Add($dg);

$dt.Columns.Add("A")
$dt.Columns.Add("B")
$dt.Columns.Add("C")

$dg.DataSource = $dt
$dt.TableName ="dtSheet1"

$dt.ReadXml("DataGridView.xml")
$dg.AutoSizeColumnsMode = 8

$b_Click = { $t.Text = "P.shell ボタン: Hello!:AAA" }
$b.Add_Click($b_Click)
[System.Windows.Forms.Application]::Run($f)
http://www2.koyoen.birdview.co.jp/~abcxyz/v.jpg

Math

Re: 【Unity/C#】delegateが全然理解できません。

#38

投稿記事 by Math » 6年前

[ ④ C++ (C++/CRL),  DataGridView ]の場合

C#,VB,PowerShell はWindows10だけあればよいのですが C++はVisualStudioが必要です。開発者用コマンドプロンプトを使います。
今回はVS2017Communityを使います。

f_bat.bat

コード:

DIR

cl /clr f.cpp
f.exe
pause
f.cpp

コード:

/////
#using <System.dll>
#using <System.Xml.dll>
#using <System.Data.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>
/////
using namespace System;
using namespace System::Xml;
using namespace System::Data;
using namespace System::Windows::Forms ;
using namespace System::Drawing ;
/////
void main(){
    Form^ f;
    Button^ b;
    TextBox^ t;
    DataGridView^ dg;
    DataTable^ dt;

    f = gcnew Form();
    b = gcnew Button();
    t = gcnew TextBox();
    dg = gcnew DataGridView();
    dt = gcnew DataTable();

    f->Text = L"Form";
    t->Text = L"TextBox: ABCDE";
    b->Text = L"Button";

    f->Size = Size(300, 300);
    f->Location = Point(500, 400);
    t->Location = Point(20, 20);
    b->Location = Point(150, 20);
    dg->Location = Point(20, 60);

    f->Controls->Add(b); 
    f->Controls->Add(t);
    f->Controls->Add(dg);

    dg->DataSource = dt;
    dt->TableName="dtSheet1";
    dt->Columns->Add(L"A");
    dt->Columns->Add(L"B");
    dt->Columns->Add(L"C");

    dt->ReadXml("DataGridView.xml");
    
    dg->AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode::AllCells;
    dg->Anchor = (AnchorStyles)15;

    Application::Run(f);
}
/////
http://www2.koyoen.birdview.co.jp/~abcxyz/w.jpg

Math

Re: 【Unity/C#】delegateが全然理解できません。

#39

投稿記事 by Math » 6年前

今幼稚園・年長組~小学生にむけてスクラッチの教室がふえています。 スクラッチhttps://ja.wikipedia.org/wiki/Scratch_( ... %E8%AA%9E)
の本を5冊ほど読んでますが Scratch のつぎは Unity が推奨されています。
C#の需要はましてくるとおもわれるしC++のオブジェクト指向の勉強にてきしています。

C++ とDelegate
http://marupeke296.com/DP_Delegate.html

C11,C14 の ラムダ式
https://cpprefjp.github.io/lang/cpp11/l ... sions.html

Math

Re: 【Unity/C#】delegateが全然理解できません。

#40

投稿記事 by Math » 6年前

delegate (基本的な)

f.bat

コード:

del f.exe
C:\windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe ^
f.cs
f.exe
pause
f.cs

コード:

using System;
using System.Windows.Forms;
using System.Drawing;

delegate int del(int i);

public class Program
{
    static int Func(int i){ return i * i; }

    [STAThread]
    static void Main()
    {
        del d = new del(Func);

        Form f;
        Button b;
        TextBox t;
        f = new Form();
        b = new Button();
        t = new TextBox();
        f.Text = "Form";
        t.Text = "TextBox: ABCDE";
        b.Text = "Button";
        f.Size = new Size(300, 200);
        f.Location = new Point(500, 400);
        t.Location = new Point(30, 20);
        b.Location = new Point(30, 60);
        f.Controls.Add(b);
        f.Controls.Add(t);

        t.Text = d(3).ToString(); 
        f.Text = d(5).ToString();

        b.Click += (sender, e) => MessageBox.Show("delegate");

        Application.Run(f);
    }
}
実行

コード:

G:\zCS\002\001>del f.exe

G:\zCS\002\001>C:\windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe f.cs
Microsoft (R) Visual C# Compiler version 4.7.2556.0
for C# 5
Copyright (C) Microsoft Corporation. All rights reserved.

This compiler is provided as part of the Microsoft (R) .NET Framework, but only supports language versions up to C# 5, which is no longer the latest version. For compilers that support newer versions of the C# programming language, see http://go.microsoft.com/fwlink/?LinkID=533240


G:\zCS\002\001>f.exe
http://www2.koyoen.birdview.co.jp/~abcxyz/さ.jpg

Math

Re: 【Unity/C#】delegateが全然理解できません。

#41

投稿記事 by Math » 6年前

Windows10 のみで C# をMSBuild.exe を使ってVisual Studio風にプロジェクトとしてビルド(Compile)することによって XAML、WPFプログラムもビルドできる。

いま簡単なC# プログラムをプロジェクトとしてビルドしてみる。

C# ソースファイルをメモ帳で簡単につくれるようにcs.txtとする。
cs.txt

コード:

using System.Windows.Forms;
namespace HelloWorld
{
    class Hello 
    {
        static void Main() 
        {
            MessageBox.Show("Hello World!");
        }
    }
}
プロジェクト・ファイルもテキストファイルでcsproj.txtとする。

csproj.txt

コード:

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0"
  xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <ItemGroup>
    <Compile Include="cs.txt" />
  </ItemGroup>

  <Target Name="Build">
    <Csc Sources="@(Compile)"/>
  </Target>

</Project>
起動するバッチファイルをBuild.batとする。

Build.bat

コード:

C:\windows\Microsoft.NET\Framework64\v4.0.30319\MSbuild.exe ^
csproj.txt
cs.exe
実行結果

コード:


G:\zCS\001\099_Build_bat_csproj\csc\cs_Build>C:\windows\Microsoft.NET\Framework64\v4.0.30319\MSbuild.exe csproj.txt
Microsoft (R) Build Engine バージョン 4.7.2556.0
[Microsoft .NET Framework、バージョン 4.0.30319.42000]
Copyright (C) Microsoft Corporation. All rights reserved.

2018/01/28 20:47:00 にビルドを開始しました。
ノード 1 上のプロジェクト "G:\zCS\001\099_Build_bat_csproj\csc\cs_Build\csproj.txt" (既定のターゲット)。
Build:
  C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Csc.exe /out:cs.exe cs.txt
プロジェクト "G:\zCS\001\099_Build_bat_csproj\csc\cs_Build\csproj.txt" (既定のターゲット) のビルドが完了しました。


ビルドに成功しました。
    0 個の警告
    0 エラー

経過時間 00:00:06.25

G:\zCS\001\099_Build_bat_csproj\csc\cs_Build>cs.exe
http://www2.koyoen.birdview.co.jp/~abcxyz/つ.jpg

Math

Re: 【Unity/C#】delegateが全然理解できません。

#42

投稿記事 by Math » 6年前

[Windows10のみで出来る 簡単な C# xaml (WPF) の  ビルド ]

2つ xaml

App.xaml

コード:

<Application x:Class="Application"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="MainWindow.xaml">

</Application>
MainWindow.xaml

コード:

<Window x:Class="MainWindow"
      xmlns=
      "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="wpf-App-MainWindow" Height="200" Width="300">
  <Grid>
    <Label Content="wpf:Hello World! " FontSize="32"
      HorizontalAlignment="Center" VerticalAlignment="Center" />
  </Grid>
</Window>
csproj.txt

コード:

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0"
 xmlns=
  "http://schemas.microsoft.com/developer/msbuild/2003">

  <PropertyGroup>
    <AssemblyName>App1</AssemblyName>
    <Platform>x64</Platform>
    <OutputType>WinExe</OutputType>
    <OutputPath>.\bin\Release</OutputPath>
  <TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
  </PropertyGroup>

  <ItemGroup>
    <ApplicationDefinition Include="App.xaml">
      <Generator>MSBuild:Compile</Generator>
      <SubType>Designer</SubType>
    </ApplicationDefinition>

    <Page Include="MainWindow.xaml">
      <Generator>MSBuild:Compile</Generator>
      <SubType>Designer</SubType>
    </Page>

    <Reference Include="PresentationCore" />
    <Reference Include="PresentationFramework" />
    <Reference Include="WindowsBase" />
    <Reference Include="System" />
    <Reference Include="System.Xaml">
    <RequiredTargetFramework>4.0</RequiredTargetFramework>
    </Reference>
  </ItemGroup>

<Import Project=
   "$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

</Project>
実行結果

コード:

C:\windows\Microsoft.NET\Framework64\v4.0.30319\MSbuild.exe ^
csproj.txt

.\bin\Release\App1.exe
pause

コード:


G:\zCS\wpf\001\0129>C:\windows\Microsoft.NET\Framework64\v4.0.30319\MSbuild.exe csproj.txt
Microsoft (R) Build Engine バージョン 4.7.2556.0
[Microsoft .NET Framework、バージョン 4.0.30319.42000]
Copyright (C) Microsoft Corporation. All rights reserved.

2018/01/29 18:56:18 にビルドを開始しました。
ノード 1 上のプロジェクト "G:\zCS\wpf\001\0129\csproj.txt" (既定のターゲット)。
MainResourcesGeneration:
すべての出力ファイルが入力ファイルに対して最新なので、ターゲット "MainResourcesGeneration" を省略します。
GenerateTargetFrameworkMonikerAttribute:
すべての出力ファイルが入力ファイルに対して最新なので、ターゲット "GenerateTargetFrameworkMonikerAttribute" を省略します 。
CoreCompile:
すべての出力ファイルが入力ファイルに対して最新なので、ターゲット "CoreCompile" を省略します。
CopyFilesToOutputDirectory:
  csproj -> G:\zCS\wpf\001\0129\bin\Release\App1.exe
プロジェクト "G:\zCS\wpf\001\0129\csproj.txt" (既定のターゲット) のビルドが完了しました。


ビルドに成功しました。
    0 個の警告
    0 エラー

経過時間 00:00:05.48

G:\zCS\wpf\001\0129>.\bin\Release\App1.exe
http://www2.koyoen.birdview.co.jp/~abcxyz/と.jpg

Math

Re: 【Unity/C#】delegateが全然理解できません。

#43

投稿記事 by Math » 6年前

bld.bat

コード:

C:\windows\Microsoft.NET\Framework64\v4.0.30319\MSbuild.exe ^
csproj.txt

.\bin\Release\App1.exe
pause
が抜けていました。

出来た中間コード

App.g.cs

コード:

#pragma checksum "..\..\..\App.xaml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "EEC667FD340FC0261FC4557CA342AE31B26D446E"
//------------------------------------------------------------------------------
// <auto-generated>
//     このコードはツールによって生成されました。
//     ランタイム バージョン:4.0.30319.42000
//
//     このファイルへの変更は、以下の状況下で不正な動作の原因になったり、
//     コードが再生成されるときに損失したりします。
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Automation;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Effects;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Media.TextFormatting;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Shell;




/// <summary>
/// Application
/// </summary>
public partial class Application : System.Windows.Application {
    
    /// <summary>
    /// InitializeComponent
    /// </summary>
    [System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
    public void InitializeComponent() {
        
        #line 4 "..\..\..\App.xaml"
        this.StartupUri = new System.Uri("MainWindow.xaml", System.UriKind.Relative);
        
        #line default
        #line hidden
    }
    
    /// <summary>
    /// Application Entry Point.
    /// </summary>
    [System.STAThreadAttribute()]
    [System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
    public static void Main() {
        Application app = new Application();
        app.InitializeComponent();
        app.Run();
    }
}
MainWindow.g.cs

コード:

#pragma checksum "..\..\..\MainWindow.xaml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "0186FAF3C4BBC47F6D6DD34CF1B1856533902667"
//------------------------------------------------------------------------------
// <auto-generated>
//     このコードはツールによって生成されました。
//     ランタイム バージョン:4.0.30319.42000
//
//     このファイルへの変更は、以下の状況下で不正な動作の原因になったり、
//     コードが再生成されるときに損失したりします。
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Automation;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Effects;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Media.TextFormatting;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Shell;




/// <summary>
/// MainWindow
/// </summary>
public partial class MainWindow : System.Windows.Window, System.Windows.Markup.IComponentConnector {
    
    private bool _contentLoaded;
    
    /// <summary>
    /// InitializeComponent
    /// </summary>
    [System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
    public void InitializeComponent() {
        if (_contentLoaded) {
            return;
        }
        _contentLoaded = true;
        System.Uri resourceLocater = new System.Uri("/App1;component/mainwindow.xaml", System.UriKind.Relative);
        
        #line 1 "..\..\..\MainWindow.xaml"
        System.Windows.Application.LoadComponent(this, resourceLocater);
        
        #line default
        #line hidden
    }
    
    [System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("PresentationBuildTasks", "4.0.0.0")]
    [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
    [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Design", "CA1033:InterfaceMethodsShouldBeCallableByChildTypes")]
    [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")]
    [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily")]
    void System.Windows.Markup.IComponentConnector.Connect(int connectionId, object target) {
        this._contentLoaded = true;
    }
}

Math

Re: 【Unity/C#】delegateが全然理解できません。

#44

投稿記事 by Math » 6年前

[ 匿名関数 ]
C# では、式中で、その場限りのメソッドを書くことができる 匿名関数(anonymous function)という機能があります。
歴史的経緯から、匿名関数には、C# 2.0 で導入された匿名メソッド式という書き方と、 C# 3.0 で導入されたラムダ式という書き方があります。
匿名メソッド式とラムダ式を合わせて「匿名関数」という総称が与えられた。ラムダ式のことを含めて匿名メソッドと呼ぶこともあります。)

No.40 を書き換えて

コード:

using System;
using System.Windows.Forms;
using System.Drawing;
 
delegate int del(int i);
 
public class Program
{
    // --- static int Func(int i){ return i * i; }
 
    [STAThread]
    static void Main()
    {
        // --- del d = new del(Func);

        del d = delegate(int i){
                      return i * i;
                };
 
        Form f;
        Button b;
        TextBox t;
        f = new Form();
        b = new Button();
        t = new TextBox();
        f.Text = "Form";
        t.Text = "TextBox: ABCDE";
        b.Text = "Button";
        f.Size = new Size(300, 200);
        f.Location = new Point(500, 400);
        t.Location = new Point(30, 20);
        b.Location = new Point(30, 60);
        f.Controls.Add(b);
        f.Controls.Add(t);
 
        t.Text = d(3).ToString(); 
        f.Text = d(5).ToString();
 
        b.Click += (sender, e) => MessageBox.Show("delegate");
 
        Application.Run(f);
    }
}
http://www2.koyoen.birdview.co.jp/~abcxyz/ね.png

返信

“C言語何でも質問掲示板” へ戻る