LoginSignup
1
0

More than 1 year has passed since last update.

C# まとめ

Last updated at Posted at 2023-03-21

コードまとめ

Base

  • 各処理の最後には;を付ける
  • char型は'シングルクォーテーション'
  • string型は"ダブルクォーテーション"
  • using System;
  • using System.Linq;

I/O

  • Console.WriteLine("Message");
  • Console.WriteLine(S.ToString("0.000"));
  • Console.Read();
  • string[] input = Console.ReadLine().Split(' ');
  • int[] input = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  • Console.WriteLine(string.Join("\n",positive));

型変換

  • int i = int.Parse("123");
  • 16進:Console.WriteLine(input.ToString("x2"));
  • ASCII
    • int ACSII_NUM = (int)input[0];
    • char ACSII = (char)num;
    • A-Z(65-90)
    • a-z (97-122)

変数

  • plibate, public は明示
  • int 変数名
  • double 変数名

文字列

  • string reverse_N = new string(N.Reverse().ToArray());
  • S.Length
  • S.ToUpper()
  • S.ToLower()
  • S.Substring(start,end)
  • indexはReadOnly.書き換えたい場合はCharに置き換える
    • S[index]
    • char[] C =S.ToCharArray();

List

  • var A_List = new List();
  • A_List = A.ToList();
  • A_List.RemoveAt(0);
  • A_List.Add(0);
  • A_List.IndexOf('a');
  • A_List.Reverse;
  • A_List.GetRange(start, slice_num);
  • A_List.AddRange(addlist);

Arrray

  • int[] list = new int [10];
  • Array.IndexOf(List,'a')
  • Array.Sort(input)
  • Array.Reverse(input);

制御文

  • for(初期化; 条件判定; インクリメント) 文;
  • switch(input){case VALUE: 処理; break; .... default: 処理; break;}
  • do-while文(処理の後に終了判定)
  • goto(基本使わない)

論理計算

  • & AND
  • | OR
  • ^ XOR

Math

  • Math.Abs(input)
  • Math.Pow(A,B)
  • Math.Sqrt(input)

例外処理

  • try{処理}catch{例外時の処理}

クラス

// 元クラス
public class Animal{
    public string Name { get; private set; } 
    public int Age { get; private set; }
    // コンストラクタ
    public Animal(string name, int age){
        Name = name;
        Age = age;
    }
    public void ShowProfile(){
        Console.WriteLine(Name + "," + Age + "歳");
    }
    // Override
    public virtual void Sleep()
    {
        Console.WriteLine("......");
    }
}

// 継承
public class Cat:Animal{
    // Catのコンストラクタ
    public Cat(string name, int age)
        : base(name, age)
    {
    }
    public override void Sleep(){
        Console.WriteLine("( ˘ω˘)スヤァ");
    }
}

// 使用
public class Hello{
    public static void Main(){
        Animal[] Mypets = new Animal[2];
        Mypets[0] = new Cat("たま", 2);
        Mypets[1] = new Cat("ぽち", 2);
        foreach(Animal pet in Mypets){
            pet.ShowProfile();
            pet.Sleep();
        }
    }
}

LINQ

using System.Linq;

/* Postitibe Num */
int PostitiveNum  = list.Where(n => n > 0)
                        .OrderByDescending(n => n)
                        .Select(n => n);

/* Count duplication */
var CountList     = list.GroupBy(x => x)
                        .Where(x => x.Count() > 1)
                        .ToDictionary(x => x.Key, y => y.Count());

/* String */
int FreqV = Input.Count(x => (x == 'v'));

/* Sum, Min, Max, Average */
list.Sum()
list.Min()
list.Max()
list.Average()

独習C#

独習C# 第3版, ハーバート・シルト著, エディフィストラーニング株式会社, 矢嶋 聡 監訳・改訂 より

  • 1.1 C#の基礎(理論)

    • .NET Framework
      • アプリケーション開発と実行をサポートする環境
      • 本Frameworkに沿ってコーディングする限り,どこでも実行できる(移植性が非常に高い
      • 特定の実行環境に依存しない
    • オブジェクト指向
      • カプセル化(Class)
        • 処理とデータをひとまとめにして,ブラックボックス化する
        • 中のコードが外からわからなくても,処理だけわかれば良い
      • ポリモーフィズム
        • 1つのインターフェースで様々な種類のクラスの動作を制御できる
        • データタイプが変わってもアルゴリズムは変わらない
      • 継承
        • 他クラスの性質を継承したクラスを作成できる
  • 1.2 C#の基礎(コード)

    • 実行環境 本書はVScode2010/C#4.0
    • 各処理の最後には;を付ける(Pythonにはないので注意!
    • Pythonとほぼ同じ部分の説明は省く(四則演算等)
    • using System; /* Namespace Declaration */
    • public class Classneme{}
    • public static void Main(){}
    • Console.WriteLine("Message");
    • int 変数名
    • double 変数名
    • for(初期化; 条件判定; インクリメント) 文;
  • 2 データ型と演算子

    • 値型:bool, byte, char, decimal, double, float, int, long, sbyte, short, uint, ulong, ushort
    • decimal = 財務計算用の型
  • 3 制御文

    • Console.Read();
    • switch文(Pythonにはない)
    • do-while文(処理の後に終了判定)
    • goto(基本使わない)
  • 4 クラス,オブジェクト,メソッドの基礎

    • クラスの定義と使用法
    • コンストラクタ = クラスをnewするときに値を入れるようにする
      • Class Vehicle{public int XX; ...; public Vehicle(int i, ...){XX = i; ...;}}
      • Vehicle minivan = new Vihicle(10,...);
  • 5 その他のデータ型と演算子

    • int[] list = new int[1024]
    • ジャグ配列もおk
    • foreach(型 変数名 in コレクション) 文;
  • 6 メソッドとクラスの詳細

    • plibate, public は明示
  • 7 演算子のオーバーロード,インデクサー,プロパティ

    • 演算子メソッド 以下を定義することで,Class Object同士の演算が定義できる
      • public static 戻り値 operator 演算子(仮引数型 オペランド){処理}
      • public static 戻り値 operator 演算子(仮引数型1 オペランド, 仮引数型2 オペランド){処理}
    • インデクサー:オブジェクトを配列Likeに扱う
    • プロパティ :privateにアクセスするためのワンクッション
  • 8 継承

    • class 派生クラス名 : 基本クラス名{追加の変数や関数}
    • protected (継承先にのみpublic)
    • sealed (対象クラスを継承不可にする)
  • 9 インターフェイス,構造体,列挙型

    • インターフェイス:Classの中身は書かずに表面上の構造だけ作っておく
    • 構造体
      • 値型のClass(Classは参照型)
      • Classに比べて軽い.が制限も多い(継承ができない等)
    • 列挙型
      • enum 名前 {列挙値のリスト}
  • 10 例外処理

    • try{処理}catch{例外時の処理}
  • 11章 I/O処理

    • Console.Read()とか.省略.
  • 12章以降:よーわからんので省略

1
0
2

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
1
0