「労働者が自分の仕事をうまくやりたいなら、まず自分の道具を研ぎ澄まさなければなりません。」 - 孔子、「論語。陸霊公」
表紙 > プログラミング > C# Basic: JavaScript 開発者の観点から

C# Basic: JavaScript 開発者の観点から

2024 年 11 月 1 日に公開
ブラウズ:940

C# Basic: From a javascript developer perspective

ジュニア開発者として、私は主に OOP パラダイムを使用する「古い」プログラミング言語を学ぶのをいつも怖がっていました。しかし、今日私はそれを吸い取って、少なくとも試してみることにしました。これは私が思っているほど悪くはありませんが、JavaScript にも類似点が引き継がれています。まずは基本を見ていきましょう。

このブログはJavaScriptを理解していることを前提としています


基本

データ型

動的型付け言語である JavaScript とは異なり、C# は静的型付け言語です。変数のデータ型はコンパイル時にわかります。つまり、プログラマはコンパイル時に変数のデータ型を指定する必要があります。宣言。

int: number (32bit)
decimal: number (128bit)
string: string
bool: Boolean
list[]: Array
dictionary{}: Object
-------------- Declaration ----------------
int myInt = 2147483647;
decimal myDecimal = 0.751m; // The m indicates it is a decimal
string myString = "Hello World"; // Notice the double-quotes
bool myBool = true;

リスト/配列

注: 方法 1 と 2 を使用する場合、長さを追加または延長することはできません
リストメソッド1の宣言と代入

string[] myGroceryArray = new string[2]; // 2 is the length
myGroceryArray[0] = "Guacamole";

リストメソッド2の宣言と代入

string[] mySecondGroceryArray = { "Apples", "Eggs" };

リストメソッド3の宣言と代入

List myGroceryList = new List() { "Milk", "Cheese" };
Console.WriteLine(myGroceryList[0]); //"Milk"
myGroceryList.Add("Oranges"); //Push new item to array

多次元リストの宣言と割り当て

「,」の数によって寸法が決まります

string[,] myTwoDimensionalArray = new string[,] {
    { "Apples", "Eggs" },
    { "Milk", "Cheese" }
};

IEnumerable/配列

列挙またはループスルーに特に使用される配列。

「リストと何が違うの?」と疑問に思うかもしれません。答えは次のとおりです:

IEnumerable と List の重要な違いの 1 つは (一方がインターフェイスであり、もう一方が具象クラスであることを除けば)、IEnumerable は読み取り専用であり、List は読み取り専用ではないことです。

List myGroceryList = new List() { "Milk", "Cheese" };

IEnumerable myGroceryIEnumerable =  myGroceryList;

辞書/オブジェクト

Dictionary myGroceryDictionary = new Dictionary(){
    {"Dairy", new string[]{"Cheese", "Milk", "Eggs"}}
};

Console.WriteLine(myGroceryDictionary["Dairy"][2]);

オペレーター

C# の演算子は JavaScript と非常によく似た動作をするため、ここでは説明しません

条件文

//Logic gate
  //There's no === in C#
myInt == mySecondInt 
myInt != mySecondInt 

myInt >= mySecondInt
myInt > mySecondInt
myInt 



ループ

? foreach を使用すると、通常の for ループ

よりもはるかに高速になります。
int[] intArr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

int totalValue = 0;

for (int i = 0; i 



方法

C# は何よりもまず OOP 指向の言語です。

namespace HelloWorld
{
    internal class Program
    {
        static void Main()
        {
            int[] numArr = [1, 2, 3, 4, 5];
            int totalSum = GetSum(numArr);
        }

        static private int GetSum(int[] numArr)
        {
            int totalValue = 0;
            foreach (var item in numArr)
            {
                totalValue  = item;
            }
            return totalValue;
        }
    }
}

名前空間とモデルの宣言

名前空間は組織化の目的、通常はクラスの組織化に使用されます

namespace HelloWorld.Models
{
    public class Computer
    {
        public string Motherboard { get; set; } = "";
        public int CPUCores { get; set; }
        public bool HasWIfi { get; set; }
        public bool HasLTE { get; set; }
        public DateTime ReleaseDate { get; set; }
        public decimal Price { get; set; }
        public string VideoCard { get; set; } = "";
    };
}

C# 10 以降では、名前空間をそのように宣言することもできます

namespace SampleNamespace;

class AnotherSampleClass
{
    public void AnotherSampleMethod()
    {
        System.Console.WriteLine(
            "SampleMethod inside SampleNamespace");
    }
}

ネームスペースのインポート

using HelloWorld.Models;
リリースステートメント この記事は次の場所に転載されています: https://dev.to/itstomlie/c-basic-from-a-javascript-developer-perspective-lnd?1 侵害がある場合は、[email protected] に連絡して削除してください。
最新のチュートリアル もっと>

免責事項: 提供されるすべてのリソースの一部はインターネットからのものです。お客様の著作権またはその他の権利および利益の侵害がある場合は、詳細な理由を説明し、著作権または権利および利益の証拠を提出して、電子メール [email protected] に送信してください。 できるだけ早く対応させていただきます。

Copyright© 2022 湘ICP备2022001581号-3