– 登場人物 –
万座 まりん(まんざ まりん)
小学6年生
好奇心旺盛で、何でも兄の真似をしたがるお年頃。
万座 備一(まんざ びいち)
大学1年生。
子供の時からプログラミングを勉強して、自分でいろいろと作ったりしている。
将来プログラマーになるかどうかは、まだ考えていない
プログラミングの基礎の基礎の基礎
前回はプログラムってなんなのかって話と、Xamarinでプログラムを書くことについてちょっとだけ触れてみました。
今回はさらに具体的にプログラムの書き方をXamarin(C#)を中心に勉強していきますよ。
変数と代入と肩宣言
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi1.png)
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma1.png)
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi1.png)
そうそう。よく覚えてたね!
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma1.png)
えっへん!
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi1.png)
C#には他にも覚えておかなくちゃいけないルールがあるんだ。
ちなみに、プログラムを書く事をコーディングって言ったりするんだ。そこで、プログラムを書く時のルールのことをコーディングルールって言う事もあるよ。
少しずつ覚えていこうね。
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma1.png)
はーい。
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi3.png)
MainPage = new MainPage();
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma2.png)
「代入」って何?
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi2.png)
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma1.png)
ふーん。
変数はよくわかんないけど代入は何となくわかった!
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi2.png)
a = 1 + 1;
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma2.png)
「a」って言うのは変数ってことなのね。「a」って変数の箱の中に数字の2が入っているってこと?
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi1.png)
b = “abc”;
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma2.png)
文字列?
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi2.png)
文字列っていうのは、文字の並びのことだよ。一文字だけのCharacter(キャラクター)と区別してそう呼んでるんだ。
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma3.png)
ふーん。1文字だけは文字で、文字が並んでるのは文字列って言うのね。
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi3.png)
int a;
string b;
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma3.png)
ふーん。
使う前に変数に何を入れるかを言っておかないといけないんだね。
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi3.png)
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma3.png)
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi3.png)
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi3.png)
他にも型宣言で使える「var」っていうのがあるんだ。
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma2.png)
「var」?
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi1.png)
「var」は型推論と言って、宣言の時に代入する値からその変数の型を推測して決めてくれるんだ。
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma2.png)
推測しちゃうの?どうやって??
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi2.png)
それは、宣言と同時に代入をするんだ。そうすると代入する右辺の型が変数の型ってことになるだろ?
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma1.png)
なるほど〜!
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi3.png)
var c;
var c = 1;
var c = “xyz”;
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma2.png)
int a = 1 + 1;
と
var a = 1 + 1;
は同じってこと?
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi3.png)
どっちも「a」は整数型ってことになるから、結果同じってことだね。
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma3.png)
へー。
型を書かなくても定義できちゃんだ。
便利だね〜。
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi2.png)
Dictionary<string, List<int>> a = new Dictionary<string, List<int>>;
var a = new Dictionary<string, List<int>>;
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma2.png)
Dictionary<string, List<int>>
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi3.png)
だろ?
Dictionary<string, List<int>>
はそんな型もあるんだくらいの理解でいいよ。
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma2.png)
あれ?
でも、「MainPage」には型がないよ?
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi2.png)
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi3.png)
これが「Page」っていう型だってことがわかるね。
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma3.png)
へー。
で、「Page」って何の型なの?
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi2.png)
「Page」は表示する画面の型だよ。
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma3.png)
ということは、「MainPage」にMainPageのオブジェクトを代入してるってこと?
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi3.png)
どっちも「MainPage」だからややこしいね。
オブジェクトのMainPageをMyPageと置き換えてみよう。
MainPage = new MyPage();
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma2.png)
そういう事なのか!
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi1.png)
とりあえず、変数と代入と型宣言についてはわかったかな?
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma1.png)
はーい。
なんとなくわかった気がする!
変数の型について
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi1.png)
じゃあ、型にはどんな形があるのか見てみよう。よく使われるのはこんな感じかな。
型 | 意味 | |
---|---|---|
int | 整数 | 整数、つまり自然数と0から自然数を引いた負(マイナス)の数字のこと。 |
float | 小数 | 浮動小数点と言われる実数型。 |
string | 文字列 | 0個以上の文字を並べたもの。文字コードはcharと同じくUTF-16。 |
bool | 真偽型 | true(トゥルー・真)かfalse(フォールス・偽)の2つの値をとる型。 |
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi3.png)
「int」はわかるよね。さっきも説明した通り整数の型だ。とてもよく使われる型だよ。
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma1.png)
「string」もわかるよ!さっき教えてもらった文字列だよね!
「UTF-8」てのはわかんないけど。
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi2.png)
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma3.png)
ふーん。文字に番号が振られてるのか。
文字の61番っていったら「a」の事だってコンピューターがわかるって事?
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi1.png)
そういう事!
文字だけじゃなくて記号とか絵文字とかにも番号が振られてるよ。
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma3.png)
ふーん。じゃあ「float」は?
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi2.png)
小数はわかるかな?
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma1.png)
うん!
点「.」が付いてる数字だよね!
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi3.png)
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma2.png)
「bool」の真偽ってなに?
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi3.png)
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma3.png)
へー。色々あるんだね〜。
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi2.png)
変数名と予約語
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi1.png)
あ!
変数名やクラス名をつける時に気をつけなくちゃいけないルールがあるんだ。
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma2.png)
まだ何かあるの?
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi3.png)
プログラミング言語にはその言語ごとに「キーワード(予約語)」と言ってあらかじめ使い方が決まった文字列があるんだ。
その文字列は変数やクラス名に使っちゃダメなんだよ。
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma3.png)
なんで?
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi3.png)
その言葉はプログラムで既に使っているんだ。例えば、「Class」とかはクラスの型だろ?だから「Class」は変数名にできないんだ。
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma1.png)
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi2.png)
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma3.png)
えー。
こんなたくさんあるの!?
覚えられないよ〜(泣
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi2.png)
仕方ないなー。
ちょこっとだけ教えてあげるから、少しずつでも覚えてね。
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma1.png)
はぁーい。
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi3.png)
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma1.png)
はーい。
これは変数名とかに使っちゃいけないのね。
わかった!!
けど、いっぱいあるね。。。
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi3.png)
うっかり予約語を使ってしまっても、VisualStudioが警告を出してくれるから、気づけるよ。
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma2.png)
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi3.png)
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma3.png)
んん?
大文字と小文字は区別されるけど、それに頼ったらダメって事?
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi2.png)
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma3.png)
へー。
とりあえず頭文字を大文字にしたらいいのね?
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi2.png)
ちょっと待った!
大抵の名前はそれでいいんだけど、パラメータだけは小文字始まりってなってるよ。
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma3.png)
パラメーターって何?
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi2.png)
ん?
難しいこと聞くなぁ。。。
パラメータっていうのはメソッドに渡す引数を受け取る変数のことなんだけど、意味わかる?
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma1.png)
わかんない!!
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi3.png)
だよなぁ。
メソッドは覚えてるかな?
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma3.png)
えーっとぉ。。。
どんな時に何をするのかってのだよね?
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi1.png)
var SumMethod(int a, int b)
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma1.png)
なんとなくわかった!
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi2.png)
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma1.png)
パラメータ以外は大文字始まりって覚えておけばいいのね!
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi3.png)
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma3.png)
はーい。
プログラミングって英語が分かってないと難しいね。。。
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi2.png)
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma3.png)
ローマ字でもいいの?
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi3.png)
仕事でやるなら英語表記の方がいいけど、自分だけならわかりやすい方がいいからね。逆に英語がわからなくて調べたりして時間をかけるくらいならローマ字にした方が良いって人もいる。
例えば三角を書くメソッドなら英語で書くと”DrawTriangle()”だけど、”SankakuKaku()”ってしても動くことは動く。
ただ、ローマ字って後から見た時に分かりづらかったりするから、できるだけ英語に慣れて行こう。
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma1.png)
はーい。
頑張る〜。
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/bi2.png)
![](https://techlog.yoshieya.com/wp-content/uploads/2018/09/ma1.png)
はーい。
void SumMethod(int a, int b) { var C = a + b; }
SumMethod(1, 2);
ここまで出てきた言葉のまとめ
- コーティング:
- プログラムのソースコードを書くこと。
- 代入:
- 変数に値を入れる事。
- 文字列:
- 0文字以上の文字の並び。
- 予約語:
- プログラム言語で既に役割が決まっているため、変数名・メソッド名・クラス名などに使用できない字句要素。キーワードとも言う。
- パラメータ:
- メソッドに渡す引数を受け取る変数。
次はもっとプログラムっぽいことをするよ!
コメント