Você está na página 1de 27

프로그램 구조 및 데이터 관련 개념

Namespace

Class 구조체 인터페이스


생성자 생성자 메소드
소멸자 상수 속성 ,
이벤트
상수 필드
인덱서
필드 메소드
메소드 속성 열거형
속성 인덱서
인덱서 연산자
연산자 대리자
이벤트
다른 클래스 다른 Namespace

2021년 11월 2일 C# 프로그래밍 1


값형식 : Value Type
사용자 구조형 (User - Defined) struct
sbyte
byte
short
ushort
정수계열
int
단순형식 숫자형식 uint
구조형
구조체 long
(Built - in) ulong
float
실수계열 double
decimal
문자형식 char
부울형식 bool
열거형 (User - Defined) enum

2021년 11월 2일 C# 프로그래밍 2


참조형식 : Reference Type

class
class
string
참조형식
interface
Reference
Type array

delegate

2021년 11월 2일 C# 프로그래밍 3


Overview

 Overview of Arrays

 Creating Arrays

 Using Arrays

2021년 11월 2일 C# 프로그래밍 4


• Overview of Arrays

 What is an Array ?
 Array Notation in C#
 Array Rank
 Accessing Array Elements
 Checking Array Bounds
 Comparing Arrays to Collections

2021년 11월 2일 C# 프로그래밍 5


What is an Array ?

 An Array is a Sequence of Elements


• All elements in an array have the same type
• Struct can have elements of different types
• Individual elements are accessed using integer
indexes

Integer index 0 Integer index 4

2021년 11월 2일 C# 프로그래밍 6


Array Notation in C#

 You Declare an Array Variable by Specifying :


• The element type of the array
• The rank of the array
• The name of the variable

type [ ] name ;

2021년 11월 2일 C# 프로그래밍 7


Array Rank

 Rank is Also Known as the Array Dimension


 The Number of Indexes Associated with Each
Element

long [ ] row ; int [ , ] grid ;

Rank 1 : One dimensional Rank 2 : Two dimensional


Single index associated Two indexes associated
with each long element with each int element

2021년 11월 2일 C# 프로그래밍 8


Accessing Array Elements

 Supply an Integer Index for Each Rank


• Indexes are Zero-Based

long [ ] row ; int [ , ] grid ;


… …
row [ 3 ] ; grid [ 1, 2 ] ;

2
3

2021년 11월 2일 C# 프로그래밍 9


Checking Array Bounds

 All Array Access Attempts Are Bounds Checked


• A bad index throws an IndexOutOfRangeException
• Use the Length property and the GetLength method

row grid

grid.GetLength(0) == 2
row.GetLength(0) == 5
grid.GetLength(1) == 5
row.Length == 5
grid.Length == 2*5

2021년 11월 2일 C# 프로그래밍 10


Comparing Arrays to Collections

 An Array Cannot Resize Itself When Full


• A collection class, such as ArrayList, can resize
 An Array Is Intended to Store Elements of One
Type
• A collection is designed to store elements of
different types
 Elements Of An Array Cannot Have Read-Only
Access
• A collection can have read-only access
 In General, Arrays Are Faster but Less Flexible
• Collections are slightly slower but more flexible

2021년 11월 2일 C# 프로그래밍 11


 int [ ] rigid = new int [42]

 ArrayList flexible = new ArrayList( );


 flexible.Add (“one”);
 flexible.Add (99);

 const int [ ] array = {0, 1, 2, 3}; //Compile time error


 readonly int [ ] array = {4, 2, 1}; //Compile time error

2021년 11월 2일 C# 프로그래밍 12


• Creating Arrays

 Creating Array Instances


 Initializing Array Elements
 Initializing Multidemensional Array Elements
 Creating a Computed Size Array
 Copying Array Variables

2021년 11월 2일 C# 프로그래밍 13


Creating Array Instances

 Declaring an Array Variables Does Not Create an


Array
• You must use new to explicitly create the array instance
• You must specify the size of all rank lengths
• Array elements have an implicit default value of zero

long [ ] row = new long [4] ; row 0 0 0 0

Variable Instance

int [ , ] grid = new int [2,3] ; grid 0 0 0


0 0 0

2021년 11월 2일 C# 프로그래밍 14


Initializing Array Elements

 The Elements of an Array Can Be Explicitly


Initialized
• You can use a convenient shorthand

long [ ] row = new long [4] { 0, 1, 2, 3 } ;

long [ ] row = { 0, 1, 2, 3 } ;

row 0 1 2 3

2021년 11월 2일 C# 프로그래밍 15


Initializing Multidimensional Array Elements

 You Can Also Initialize Multidimensional Array


Elements
• All elements must be specified

int [ , ] grid = { Implicitly a new


{ 5, 4, 3 } , int [2,3] array
{ 2, 1, 0 } } ;
grid 5 4 3
int [ , ] grid = { 2 1 0
{ 5, 4, 3 } ,
{ 2, 1 } };

2021년 11월 2일 C# 프로그래밍 16


Creating a Computed Size Array

 The Array Size Does Not Need to Be a Compile-


Time Constant
• Any valid integer expression will work
• Accessing elements is equally fast in all
• Array size specified by compile-time integer
constant :
long [ ] row = new long [4] ;

• Array size specified by run-time integer value :


string s = Console.ReadLine( ) ;
int size = int.Parse(s) ;
long [ ] row = new long [size] ;
2021년 11월 2일 C# 프로그래밍 17
Copying Array Variables

 Copying an Array Variable Copies Just the Array


Variable
• It does not copy the array instance
• Two array variables can refer to the same array
instance

long [ ] row = new long [4]; row 5 4 3 0


long [ ] copy = row ;
.... copy
row [0]++;
int value = copy[0]; Variable Instance
Console.WriteLine(value);

2021년 11월 2일 C# 프로그래밍 18


참조형식 : Reference Type

class
class
string
참조형식
interface
Reference
Type array

delegate

2021년 11월 2일 C# 프로그래밍 19


• Using Arrays

 Array Properties
 Array Methods
 Returning Arrays from Methods
 Passing Arrays as Parameters
 Command-Line Arguments
 Demonstration : Arguments for Main
 Using Arrays with foreach
 Quiz : Spot the Bugs

2021년 11월 2일 C# 프로그래밍 20


프로그램 구조 및 데이터 관련 개념
Namespace
인터페이스
Class 구조체
메소드
생성자 생성자
속성 ,
소멸자 상수 이벤트
상수 필드 인덱서
필드 메소드
메소드 속성
열거형
속성 인덱서
인덱서 연산자
연산자 대리자
이벤트
다른 클래스
다른 Namespace

2021년 11월 2일 C# 프로그래밍 21


Array Properties

long [ ] row = new long [4];


row.Rank => 1
row 0 0 0 0 row.Length => 4

long [ , ] grid = new long [2,3];


grid 0 0 0 grid.Rank => 2
0 0 0 grid.Length => 6

2021년 11월 2일 C# 프로그래밍 22


Array Methods

 Commonly Used Method ( System.Array class)


• Sort - sorts the elements in an array of rank 1
• Clear - sets a range of elements to zero or null
• Clone - creates a copy of the array
• GetLength - returns the length of a given
dimension
• IndexOf - returns the index of the first
occurrence of a value

• 주의 : static method, instance method

2021년 11월 2일 C# 프로그래밍 23


Returning Arrays from Methods

 You Can Declare Methods to Return Arrays

class Example {
static void Main ( ) {
int [ ] array = CreateArray (42) ;
... }
static int [ ] CreateArray (int size) {
int [ ] created = new int [size] ;
return created; }
}

2021년 11월 2일 C# 프로그래밍 24


Passing Array as Parameters

 An Array Parameter is a Copy of the Array Variable


• Not a copy of the array instance

class Example2 {
static void Main( ) {
int [ ] arg = {10, 9, 8, 7};
Method(arg) ;
Console.WriteLine(arg[0]); } // ans=11
static void Method (int [ ] parameter) {
parameter[0]++ ; }
}

2021년 11월 2일 C# 프로그래밍 25


Command-Line Arguments

 The Runtime Passes Command Line Arguments to


Main
• Main can take an array of strings as a
parameter
• The name of the program is not a member of
the array

class Example3 {
static void Main( string [ ] args ) {
for (int j = 0; j < args.Length; j++) {
Console.WriteLine( args[j]); }
}
}
2021년 11월 2일 C# 프로그래밍 26
Using Arrays with foreach

 The foreach Statement Abstracts Away Many


Details of Array Handling

class Example4 {
static void Main( string[ ] args ) {
foreach ( string arg in args) {
Console.WriteLine (arg); }
}
}

2021년 11월 2일 C# 프로그래밍 27

Você também pode gostar