신입 개발자 공부 과정

12/28 모르는 부분들 C# 구조체Struct, 배열Array 본문

C#/모르는 내용들 공부

12/28 모르는 부분들 C# 구조체Struct, 배열Array

Lewisjkim 2021. 12. 29. 15:01

 

 

 

-구조체 Struct

구조체 형식은 사용자 정의 형식.
데이터와 관련 기능을 캡슐화(?)할 수 있는 값 형식.
동작을 거의 제공하지 않거나 작은 데이터 중심 형식을 설계하는데 사용.(?)

구조=

Struct(키워드) Position(정한 구조체 형식 이름)
struct Position
{

}​


필드 선언=

public(접근 제한자를 붙혀 준다) int(변수 타입) x; (변수명)

struct Position
{
    public int x;
}


생성자=

public Position() 안에 매개변수가 있어야된다 
왜??

struct Position
{
    public int x;
    public int y;
    
    public Position()  //매개변수 없는 생성자는 안된다
    {
    
    }
}


------

struct Position
{
    public int x;
    public int y;
    
    public Position(int x, int y)  //매개변수 없는 생성자는 안된다
    {
        this.x = x;
        this.y = y; //모든 맴버에 매개변수를 할당해야됨
    }
}


매서드 정의=

struct Position
{
    public int x;
    public int y;
    
    public Position(int x, int y)  //매개변수 없는 생성자는 안된다
    {
        this.x = x;
        this.y = y; //모든 맴버에 매개변수를 할당해야됨
    }
    
    public void SetOrigin() //매서드 정의도 할 수 있다
    {
        this.x = 0;
        this.y = 0;
    }
}


인스턴스화=
new Position(3,10); // new 키워드를 사용하여 인스턴스화
모든 구조체 형식은 하나 이상의 생성자를 갖습니다.(?)

-구조체는 값 형식
-구조체는 상속 불가능
-구조체는 매개변수 없는 생성자를 포함할 수 없음
-맴버 메서드에 virtual, protected 사용불가

언제 써야 되나?
유형의 인스턴스가 작고(?) 수명이 짧고(?)
다른 개체에 포함되는 경우 클래스 대신 구조체를 고려
ex)

 

 

-배열

형식이 동일한 변수를 여러개 그룹핑 할 수 있다.
ex) 
string a;
string b;
string c;
string d;
string e; //if you have several same types of variables you can use 'Array'​


How to make Array=
Use 'new' operator to make 1 demension Array
'New' operator lesson #30 in 눈코딩 = creates new type of Instants

Intiger type Array=
new int(Array type) [5]; (number of array elemenets)

new int [5]; //int type Array including 5 elements created

string a;
string b;
string c;
string d;
string e; //if you have several same types of variables you can use 'Array'

​

creating new Array = creating Array instants

Lets assign Array instants into variables=
int (array element type)[]  numbers;(variable name) = new int[5] 
//announce array instance into int type array variables

default number elements set as 0
reference elemenst set as null
array type can be any type= such as 'int' 'bool' 'string' 'marine' etc.

Array Initialization=

int[] numbers = new int []; {1,2,3,4,5}

numbers - int type array instants

1 2 3 4 5


You can you index to search the Array Data=

int[] numbers = new int []; {1,2,3,4,5}

Console.WriteLine(numbers[0]);
Console.WriteLine(numbers[1]);
Console.WriteLine(numbers[2]);
Console.WriteLine(numbers[3]);
Console.WriteLine(numbers[4]);


Anncounce values in Array Elements=

int[] numbers = new int []; {1,2,3,4,5}

numbers[0]=2;//elements 2 instead of 1
numbers[1]=4;//elements 4 instead of 2
numbers[2]=6;//elements 6 instead of 3


print array elements with using for sentence

int[] numbers = new int []; {1,2,3,4,5}

for(int i =0; i<numbers.Length; i++) //numbers.Length shows the numbers of array elements
{
    Console.WriteLine(numbers[i]); //from 0 to number of elements it will print elements of array numbers
}​


다차원 배열=
2demension array
ex)

         
         
         

creating 2demension array instants=
new int [3,4]; [row, column]

       
       
       

Array initialization=
create array instants and announce in variables
int[ , ] array = new int[3 , 4 ];

*other way to initaliaze=
int[ , ] array =
{
    {1 , 2},
    {3 , 4},
    {5 , 6},
};

int[ , ] array = new int[3,2] 
{
    {1 , 2},
    {3 , 4},
    {5 , 6},
};

int[ , ] array = new int[ , ]
{
    {1 , 2},
    {3 , 4},
    {5 , 6},
};​

default and reference array are 0 and null
int[ , ] array = new int [3, 4];​​
0 0 0 0
0 0 0 0
0 0 0 0

 

int[ , ] array = new string [3, 4];​​
null null null null
null null null null
null null null null

To announce values in to elements

int[,] array = new int[3,4];

array[1,1] = 10;​
0 0 0 0
0 10 0 0
0 0 0 0

print array elements=
int[,] array = new int[3,4];
arr[1,1] = 10;
Console.WriteLine(arr[1,1]);​
10

Length of 2demention array=
int[ , ] array = new string [3, 4];
Console.WriteLine(arr.GetLength(0));//length of row
Console.WriteLine(arr.GetLength(1));//length of column
​​​


using for sentence to go over 2demention array=
for(int row = 0; row<3; row++)
	{
    for(int col = 0; col<4; col++)
    	{
        Console.Write("{0}", arr[row, col]);
        }
        Console.WriteLine();
    }​


List


 

 

 

'C# > 모르는 내용들 공부' 카테고리의 다른 글

c# 화살표 함수 =>  (0) 2022.01.13
c# 재귀함수  (0) 2022.01.13
C# Callback ,Json 공부  (0) 2022.01.05
1월3~4일 추가로 공부한 것  (0) 2022.01.04
1월1일~1월2일 C# 공부한 부분들  (0) 2021.12.29