C# 기본문법

Hojun Jeong·2024년 12월 11일

기본용어 정리

  • 컴파일 : 프로그램을 생성하는 과정
  • 런타임 : 프로그램이 실행되는 시간
순서는 컴파일 -> 런타임

참조: https://engineer-mole.tistory.com/172


C# member

  • fields : variables declared(선언) at class scope
  • constants : fields that are set(초기화) at compile time
  • properties : methods on a class that are accessed as if they were fields on that class. also they can provide protection for a class field to keep it from being changed withouth knowledge of the object
  • methods : define the actions that a class can perform
  • events : events provide notifiations about occurrences. Events are defined and triggered by using delegates

연산우선순위

  1. not
  2. and
  3. or

전항 후항 연산자

전항연산 ++i 같은 경우 +1의 연산을 먼저하고 값을 평가한다(값을 평가한다는 것은 어떤 장소에 이값을 넣어준다는 의미이므로 return값이 없고 할당해주지 않는경우에는 순서가 무관)

후항연산 i++ 같은 경우 값을 평가한 후 +1의 연산을 한다(이 값을 어디에 할당해준다면 할당먼저 이루어지고 +1은 나중에)

func

이러한 함수가 있다,
이 함수를 사용할 때, 파라미터의 타입은 function타입이므로 람다를 이용한다


list, ienumerable 등의 자료구조 공부


✔ nullable

boolean? flag = null;


✔ is | as

isas
객체가 해당 형식에 해당하는지를 검사하여 그 결과를 bool값으로 반환(캐스팅)형식 변환 연산자와 동일 한 역할
참이면 true참이면 오른쪽타입으로 변환
거짓이면 false거짓이면 null

Execute(), ExecuteDb(), ExecuteNonQuery()

이러한 함수는 int를 return 하며, 여기서 리턴된 값은 execute가 실행된 쿼리가 영향을 준 db속 row 수를 의미합니다.
private int Execute(object[] args, string sqlType)
        {
            int ret;
            try
            {
                handler.OpenTx();
                ret = ExecuteDb(args, sqlType);
                handler.CommitTx();
            }
            catch
            {
                ret = -1;
                handler.RollbackTx();
            }
            return ret;
        }

DateTime.ParseExact(string,format(that string looks like), cultureinfo.invariantCulture)

is _ ex

public class Base { }

public class Derived : Base { }

public static class IsOperatorExample
{
    public static void Main()
    {
        object b = new Base();
        Console.WriteLine(b is Base);  // output: True
        Console.WriteLine(b is Derived);  // output: False

        object d = new Derived();
        Console.WriteLine(d is Base);  // output: True
        Console.WriteLine(d is Derived); // output: True
    }
}

as _ ex

Cat cat = new Mammal();
Mammal mammal = cat as Mammal;
if(mammal != null)
{
cat.Meow();
}

# result => cat.Meow() 실행

try catch finally

👀 참조형 변수 형변환 더 알아보기!!


1. 식 결과의 런타임 형식이 지정된 형식과 호환되는지 확인
2. 식 결과의 런타임 형식이 지정된 형식과 호환되는지 확인

ref?


View 띄울때 ViewModel 연결해서 꼭 띄우기 Window.Show{Datacontext = viewModel} 이런식으로 !!!

converter 은 반드시 해당 binding된 그값으로 형변환이 이루어져야한다

ex: ObservableCollection => observableCollection , not list

multibinding 에서 return 하는 값이 Tuple


액세스 한정자

github pull request?

https://wayhome25.github.io/git/2017/07/08/git-first-pull-request-story/


인터페이스??

??의미 -
https://epdev.tistory.com/32
string name = param ?? "default";
// param이 null이 아니라면 name에 param을 넣고, null 이라면 default를 넣어라. 이다.


하나의 namespace에 여러 class 가능


deep copy shallow copy

https://velog.io/@ellyheetov/Shallow-Copy-VS-Deep-Copy#%EA%B9%8A%EC%9D%80-%EB%B3%B5%EC%82%ACdeep-copy%EB%9E%80


단축키 모음

https://jsmun.com/30

-private readonly 에서 readonly 의미

  • converter 쓰는 이유 : xaml에서 주어진 값을 원하는 값으로 변환하기 위해 - 컨버터 코드 이해 필요!!👀
  • as 문법 - https://guyaga.tistory.com/150

  • get,set property

  • xaml.cs에서 viewmodel을 생성할때 생성자 사용이 아닌 (~~viewModel)DataContext를 이용함

내장 함수

  • ToUpper()
string txt = "Hojun Jeong";
Console.WriteLine(txt.ToUpper());

## output => Hojun

-Array.Sort()

string[] list = ["hojun","Jeong","a"]
Array.Sort(list)
foreach(string i in list)
{
Console.WriteLine(i)
}

## output 
a
hojun
Jeong

null 에대한 이해

list.count==0이라는 말은 null인 list가 아니라 list가 emptylist라는 말

cs 조건문

  • 의존성 주입

  • db에서 특정 column을 auto-increment 했을 시에 , cs 에서 entity부분에 databaseGeneratedOption.Identity 해주어야함!!

  • XAML에서 Path=

  • await?

  • 제네릭?

  • ienumerable?

  • lazy? 변수를 동적으로 할당하게 하는 데이터타입

  • Dto자체에서 담을 수 없는 데이터를 꼭 담아야하는 경우에는 그 dto를 생성해서 사용하는 부분에서 데이터를 넣어주는 방법도 하나이다


Array.FindIndex(array, 조건문):

if(조건문==true) => return (array 속 조건을 만족하는 숫자의 index)
else => return -1

function 주체 정해주기

어셈블리 = .dll , .exe

linq

getHashCode()
single -> 값이 하나 있으면 그값나옴, 값 여러개있으면 error 값없으면 null
any(조건) => 조건에 만족하는 값이 하나라도 있으면 true를 리턴, 아닌경우에는 false를 리턴
all(조건) => 조건에 모든값이 만족할 때 true를 리턴, 아닌경우에는 false를 리턴

include()?

IQueryable linq문은 Parsing을 할 능력이 없다
그렇기 때문에 parsing을 linq문 안에서 하려면
IQueryable => IEnumerable linq로 변환 (ToList()사용 )한 후에 사용하면 된다

profile
Student , Junior Developer

0개의 댓글