LoginSignup
5
18

More than 3 years have passed since last update.

C# 入門

Last updated at Posted at 2020-07-24

C#の学習メモです。

言語の特徴

  • C言語を始めとした各言語から継承した言語仕様(CC++JAVAC#
  • オブジェクト指向言語(クラスの定義)
  • プロジェクト単位でプログラムを管理
  • PC、スマホなど幅広いプラットフォームに対応
  • .NETフレームワーク上(windows上で動くアプリ)で動作

.NET

C#は、.NET上で動く言語。(CLI:Common Language Infrastructure規格上で動く)
.NET Coreは、クロスプラットフォームで互換性あり(LinuxやMacOSも対応)

  • CLR(Common Language Runtime)
    C# → IL Code(中間コード) → Native Code コンパイルされる
    .NET Frameworkに対応した言語のライブラリを共通して使える(e.g. C#、VB.NET、F#、C++...)

  • Class Library

開発環境

Visual Studio(IDE:統合開発環境) → Visual Studio Codeよりも高機能。

macでは、windowsアプリ(フォームなど)は作れないみたいですね...

プロジェクト

C#は、プロジェクト単位で各種ファイルを管理します。ひとつのプログラムを作るのに必要な複数のファイルとアセット(画像などの素)をひとまとめにしてプロジェクトとします。

Program.cs
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

静的型付け言語

C#は静的片付け言語なので、変数や定数を宣言する際には、型を宣言する必要があります。また、データ型によって確保するメモリのビット数が異なる為、型変換の際に大きい型から小さい型に変換しようとすると、「暗黙的に変換できません。明示的な変換が存在しません。」とエラーがでます。

データ型 説明 ビット
byte 符号なし整数 8
sbyte 符号つき整数 8
int 符号つき整数 32
uint 符号なし整数 32
short 符号つき整数 16
ushort 符号なし整数 16
long 符号つき整数 64
ulong 符号なし整数 64
float バイナリ浮動小数点数 32
double バイナリ浮動小数点数 64
char Unicode文字列 16
bool ブール値 8
string 文字列
decimal 高精度10進浮動小数点数 128

整数は、小数点を含まない整数。
浮動小数点は、小数点いかを含む実数。
符号つきは、負の値を含む。

floatdoubleだと小数点の値に誤差が生じるので、絶対に誤差を出したくない場合はdecimalを使います。(金額計算など)

Microsoft/ドキュメント/C#/型/種類

Program.cs
using System;

class Program
{
    static void Main(string[] args)
    {
        byte    val1  = 255;
        short   val2  = 10;
        short   val3  = short.MaxValue; // 32767
        short   val4  = short.MinValue; // -32768
        int     val5  = 10;
        long    val6  = 10;
        float   val7  = 1.23f; // サフィックス(数値の末尾にデータ型を示す文字列)が必要
        double  val8  = 1.23; // 1.23dと明示的に表記してもOK
        decimal val9  = 1.23m;
        bool    val10 = true;
        string  val11 = "string";
    }
}

サフィックスは、大文字でも小文字でもどちらでもOKです。

値型と参照型

  • 値型 → 数値 → int コピーした場合、値そのものが別のメモリに確保される
  • 参照型 → クラス → コピーした場合、参照先自体がコピーされる

Javascriptの変数とオブジェクトの「コピー・参照」と挙動と同じになっています。

学習項目

C#で押さえておきたい項目は下記の通りです。

  • 文法
  • コーディングルール、お作法
  • オブジェクト指向
  • デザインパターン
  • TDD(テスト駆動開発)
  • リファクタリング
  • DDD(ドメイン駆動開発)

オブジェクト指向

DAOパターン ストラテジーパターン + ファクトリーパターン

エラー

dot.netエラー

Could not execute because the specified command or file was not found.
Possible reasons for this include:
  * You misspelled a built-in dotnet command.
  * You intended to execute a .NET Core program, but dotnet-aspnet-codegenerator does not exist.
  * You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.

dotnet-aspnet-codegeneratorが存在しないことで発生するエラー。

You can invoke the tool using the following command: dotnet-aspnet-codegenerator

dotnet-aspnet-codegeneratorをインストールするコマンド。

dotnet tool install --global dotnet-aspnet-codegenerator

プロジェクトのディレクトリ直下でコマンドを実行していない場合に起こるエラー。
Controllerや、Modelsのあるディレクトリ内でコマンドを実行します。

Scaffolding failed.
Could not find project file in /Users/tetsu/Projects/MvcMovie
To see more information, enable tracing by setting environment variable 'codegen_trace' = 1.
RunTime 00:00:00.07

やり直した結果

cd MvcMovie
dotnet aspnet-codegenerator controller -name MoviesController -m Movie -dc MvcMovieContext --relativeFolderPath Controllers --useDefaultLayout --referenceScriptLibraries

Building project ...
Finding the generator 'controller'...
Running the generator 'controller'...
Attempting to compile the application in memory.
Attempting to figure out the EntityFramework metadata for the model and DbContext: 'Movie'
Added Controller : '/Controllers/MoviesController.cs'.
Added View : /Views/Movies/Create.cshtml
Added View : /Views/Movies/Edit.cshtml
Added View : /Views/Movies/Details.cshtml
Added View : /Views/Movies/Delete.cshtml
Added View : /Views/Movies/Index.cshtml
RunTime 00:00:13.72

参考本

リーダブルコード
.NETのクラスライブラリ設計

ドキュメント

5
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
18