クラスを定義する
クラスを定義する
C#においてクラス定義は以下のように行います。
[アクセス修飾子] class クラス名{ … }
クラスは内部にメンバ変数やプロパティ、メソッドやイベントを定義することができます。
namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Calc ca = new Calc(); int c = 0; c = ca.add(5, 5); MessageBox.Show("Add: " + c.ToString()); c = ca.sub(4, 2); MessageBox.Show("Sub: " + c.ToString()); c = ca.mul(3, 4); MessageBox.Show("Mul: " + c.ToString()); c = ca.div(9, 3); MessageBox.Show("Div: " + c.ToString()); } } public class Calc { public int add(int a, int b) { return a + b; } public int sub(int a, int b) { return a - b; } public int mul(int a, int b) { return a * b; } public int div(int a, int b) { return a / b; } } }