주니어 개발자로서 저는 OOP 패러다임을 주로 사용하는 '구형' 프로그래밍 언어를 배우는 것이 항상 두려웠습니다. 그러나 오늘 나는 그것을 빨아들이고 적어도 시도해 보기로 결정했습니다. 제가 생각하는 것만큼 나쁘지는 않습니다. 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를 사용하는 경우 길이를 추가하거나 연장할 수 없습니다.
List 메소드 선언 및 할당 1
string[] myGroceryArray = new string[2]; // 2 is the length myGroceryArray[0] = "Guacamole";
목록 방법 2 선언 및 할당
string[] mySecondGroceryArray = { "Apples", "Eggs" };
목록 방법 3 선언 및 할당
ListmyGroceryList = 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과 List의 한 가지 중요한 차이점(하나는 인터페이스이고 다른 하나는 구체적인 클래스임)은 IEnumerable은 읽기 전용이고 List는 그렇지 않다는 것입니다.
ListmyGroceryList = new List () { "Milk", "Cheese" }; IEnumerable myGroceryIEnumerable = myGroceryList;
DictionarymyGroceryDictionary = 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;
부인 성명: 제공된 모든 리소스는 부분적으로 인터넷에서 가져온 것입니다. 귀하의 저작권이나 기타 권리 및 이익이 침해된 경우 자세한 이유를 설명하고 저작권 또는 권리 및 이익에 대한 증거를 제공한 후 이메일([email protected])로 보내주십시오. 최대한 빨리 처리해 드리겠습니다.
Copyright© 2022 湘ICP备2022001581号-3