ASP NET 2 0 ASP NET Fundamental Park

  • Slides: 137
Download presentation
쉽게배우는 ASP. NET 2. 0 (ASP. NET Fundamental) Park Yong Jun (Red. Plus) Microsoft

쉽게배우는 ASP. NET 2. 0 (ASP. NET Fundamental) Park Yong Jun (Red. Plus) Microsoft MVP, MCSD http: //www. dotnetkorea. com/

Chapter 02. C# 기본 문법

Chapter 02. C# 기본 문법

CLR에서의 컴파일과 실행 default. aspx JIT 컴파일러 Native 코드 Compiler(C# or VB) MSIL

CLR에서의 컴파일과 실행 default. aspx JIT 컴파일러 Native 코드 Compiler(C# or VB) MSIL

객체의 암시적 선언 및 명시적 선언 n 암시적 객체 선언 예 Improts System. Web.

객체의 암시적 선언 및 명시적 선언 n 암시적 객체 선언 예 Improts System. Web. UI. Web. Controls … Dim list. Box 1 As New List. Box() list. Box 1. Items. Add(“첫번째 항목”) using System. Web. UI. Web. Controls; … List. Box list. Box 1 = new List. Box(); list. Box 1. Items. Add(“첫번째 항목”) n 명시적 객체 선언 예 Dim list. Box 1 As New System. Web. UI. Web. Controls. List. Box() list. Box 1. Items. Add(“첫번째 항목”) System. Web. UI. Web. Controls. List. Box list. Box 1 = new System. Web. UI. Web. Controls. List. Box(); list. Box 1. Items. Add(“첫번째 항목”);

Visual Basic. NET 과 C#

Visual Basic. NET 과 C#

u C# Program의 구조 n Hello, World 프로그램 n 클래스(Class) n Main 메서드(Main Method)

u C# Program의 구조 n Hello, World 프로그램 n 클래스(Class) n Main 메서드(Main Method) n using 지시자, System Namespace n 데모: Visual Studio. NET을 사용한 C# Program 생성

Hello, World 프로그램 using System; class Hello { public static void Main() { Console.

Hello, World 프로그램 using System; class Hello { public static void Main() { Console. Write. Line("Hello, World"); } }

using Directive와 System Namespace n . NET Framework는 많은 유용한 클래스 제공 l 네임스페이스로

using Directive와 System Namespace n . NET Framework는 많은 유용한 클래스 제공 l 네임스페이스로 조직화. n 최상의 네임스페이스 : System n 네임스페이스. 클래스. 메서드 System. Console. Write. Line("Hello, World"); n using 지시자(directive) using System; … Console. Write. Line("Hello, World");

u 기본 입/출력문 n Console Class n Write / Write. Line 메서드 n Read

u 기본 입/출력문 n Console Class n Write / Write. Line 메서드 n Read / Read. Line 메서드

콘솔 클래스(Console Class) n 콘솔(도스창/명령프롬프트) n 콘솔 응용프로그램만 가능 l Standard input – keyboard

콘솔 클래스(Console Class) n 콘솔(도스창/명령프롬프트) n 콘솔 응용프로그램만 가능 l Standard input – keyboard l Standard output – screen l Standard error – screen

Read / Read. Line 메서드 n Console. Read / Console. Read. Line l Read

Read / Read. Line 메서드 n Console. Read / Console. Read. Line l Read : 한 글자 입력 : 프로그램 종료 후 대기 l Read. Line : 한 줄 입력

주석 처리 n 주석 처리는 중요하다. l n 잘 선언된 주석은 개발로가 해당 애플리케이션

주석 처리 n 주석 처리는 중요하다. l n 잘 선언된 주석은 개발로가 해당 애플리케이션 을 이해하는데 도움을 준다. 한줄 주석(Single-line comments) // Get the user’s name Console. Write. Line("What is your name? "); name = Console. Read. Line( ); n 여러줄 주석(Multiple-line comments) /* Find the higher root of the quadratic equation */ x = (…);

XML 문서 생성 /// <summary> The Hello class prints a greeting /// on the

XML 문서 생성 /// <summary> The Hello class prints a greeting /// on the screen /// </summary> class Hello { /// <remarks> We use console-based I/O. /// For more information about Write. Line, see /// <seealso cref="System. Console. Write. Line"/> /// </remarks> public static void Main( ) { Console. Write. Line("Hello, World"); } }

데이터형(Data. Type)과 변수(Variable)

데이터형(Data. Type)과 변수(Variable)

데이터형(Data. Type)의 종류 C# 형식 . NET Framework 형식 sbyte System. SByte float System.

데이터형(Data. Type)의 종류 C# 형식 . NET Framework 형식 sbyte System. SByte float System. Single byte System. Byte double System. Double short System. Int 16 decimal System. Decimal ushort System. UInt 16 bool System. Boolean int System. Int 32 char System. Char uint System. UInt 32 string System. String long System. Int 64 object System. Object ulong System. UInt 64

지역 변수(Local Variables) n 데이터형과 변수명 사용해서 선언 int item. Count; n 한번에 여러

지역 변수(Local Variables) n 데이터형과 변수명 사용해서 선언 int item. Count; n 한번에 여러 개의 변수 선언 가능 int item. Count, employee. Number; --or-int item. Count, employee. Number;

암시적 형 변환 n 특정한 구문 표시 없이 연산 또는 대입에 의하여 형 변환

암시적 형 변환 n 특정한 구문 표시 없이 연산 또는 대입에 의하여 형 변환 발생 n 예상치 못한 결과를 초래할 수 있으므로 사용하지 않을 것 을 권장 n 형식 기본적으로 작은형에서 큰형으로의 변화만 가능 대상 sbyte short, int, long, float, double 또는 decimal byte short, ushort, int, uint, long, ulong, float, double 또는 decimal short int, long, float, double 또는 decimal ushort int, uint, long, ulong, float, double 또는 decimal int long, float, double 또는 decimal uint long, ulong, float, double 또는 decimal long float, double 또는 decimal char ushort, int, uint, long, ulong, float, double 또는 decimal float double ulong float, double 또는 decimal

연산자 (Operators)

연산자 (Operators)

주요 연산자 Common Operators • • Equality operators Relational operators Conditional operators Increment operator

주요 연산자 Common Operators • • Equality operators Relational operators Conditional operators Increment operator Decrement operator Arithmetic operators Assignment operators Example == != < > <= >= is && || ? : ++ -+ - * / % = *= /= %= += -= <<= >>= &= ^= |=

관계 연산자 설명 < Less than <= Less than or equal > greater than

관계 연산자 설명 < Less than <= Less than or equal > greater than >= Greater than or equal == Equal != Not equal

비트 연산자 설명 예 & AND int result = bi & bi 2; //result=0000

비트 연산자 설명 예 & AND int result = bi & bi 2; //result=0000 | OR int result = bi | bi 2; //result=0011 ^ XOR int result = bi ^ bi 2; //result=0011

switch 문 n 여러 개의 case 구문을 사용하여 여러 조건 처리 switch (trumps) {

switch 문 n 여러 개의 case 구문을 사용하여 여러 조건 처리 switch (trumps) { case Suit. Clubs : case Suit. Spades : color = "Black"; break; case Suit. Hearts : case Suit. Diamonds : color = "Red"; break; default: color = "ERROR"; break; }

do 문 n 선 실행 후 조건 처리 int i = 0; do {

do 문 n 선 실행 후 조건 처리 int i = 0; do { Console. Write. Line(i); // 구문을 먼저 실행 i++; } while (i < 10); 0123456789

foreach 문 n 배열 또는 컬렉션과 같은 데이터에 데이터가 있는 만큼 반복 Array. List

foreach 문 n 배열 또는 컬렉션과 같은 데이터에 데이터가 있는 만큼 반복 Array. List numbers = new Array. List( ); for (int i = 0; i < 10; i++ ) { numbers. Add(i); } foreach (int number in numbers) { Console. Write. Line(number); } 0123456789

break 및 continue 문 n break문은 반복문을 빠져나간다. n continue문은 다음 반복위치로 이동한다. int

break 및 continue 문 n break문은 반복문을 빠져나간다. n continue문은 다음 반복위치로 이동한다. int i = 0; while (true) { Console. Write. Line(i); i++; if (i < 10) continue; else break; }

배열(Arrays)

배열(Arrays)

배열의 경계 체크하기 n 모든 배열의 경계를 체크 가능 l l 잘못된 인덱스 지정

배열의 경계 체크하기 n 모든 배열의 경계를 체크 가능 l l 잘못된 인덱스 지정 : Index. Out. Of. Range. Exception Length 속성과 Get. Length 메서드 사용 row. Get. Length(0)==6 grid. Get. Length(0)==2 grid. Get. Length(1)==4 row. Length==6 grid. Length==2*4

배열의 배열 n 배열의 배열(Jagged Arrays) l 배열의 요소가 일정하지 않은 배열 l 달력의

배열의 배열 n 배열의 배열(Jagged Arrays) l 배열의 요소가 일정하지 않은 배열 l 달력의 12개월 각각의 날짜수가 다름 int [] [] int. Days. In. Month; int. Days. In. Month = new int [12][]; int. Days. In. Month[0] = new int[31]; int. Days. In. Month[1] = new int[28]; … int. Days. In. Month[11] = new int[31]; int. Days. In. Month[0] 0 1 … 30 int. Days. In. Month[1] 0 1 … 28

배열 관련된 속성 long[ ] row = new long[4]; 0 0 row. Rank 1

배열 관련된 속성 long[ ] row = new long[4]; 0 0 row. Rank 1 row. Length 4 row int[, ] grid = new int[2, 3]; 0 0 0 grid. Rank 2 grid. Length 6

메서드로부터 배열 반환 n 배열을 반환하는 메서드 선언 가능 class Example { static void

메서드로부터 배열 반환 n 배열을 반환하는 메서드 선언 가능 class Example { static void Main( ) { int[ ] array = Create. Array(42); . . . } static int[ ] Create. Array(int size) { int[ ] created = new int[size]; return created; } }

인수로 배열 전달 n 배열 인수는 배열 변수의 복사본 l 배열 인스턴스의 복사가 아님

인수로 배열 전달 n 배열 인수는 배열 변수의 복사본 l 배열 인스턴스의 복사가 아님 class Example 2 { static void Main( ) { int[ ] arg = {10, 9, 8, 7}; Method(arg); System. Console. Write. Line(arg[0]); } static void Method(int[ ] parameter) { parameter[0]++; 이 메서드는 Main 메서드에서 } 생성된 원본 배열을 수정함 }

명령줄 인수(Command-Line Arguments) n 런타임시 명령줄 인수를 Main 메서드로 전달 l Main 메서드는 매개변수로

명령줄 인수(Command-Line Arguments) n 런타임시 명령줄 인수를 Main 메서드로 전달 l Main 메서드는 매개변수로 문자열 배열을 전 달 받음 class Example 3 { static void Main(string[ ] args) { for (int i = 0; i < args. Length; i++) { System. Console. Write. Line(args[i]); } } }

foreach문을 이용한 배열 사용 n foreach문은 배열의 요소를 다루는데 유용한 구 문 class Example

foreach문을 이용한 배열 사용 n foreach문은 배열의 요소를 다루는데 유용한 구 문 class Example 4 { static void Main(string[ ] args) { foreach (string arg in args) { System. Console. Write. Line(arg); } } }

메서드와 파라미터 (Methods and Parameters)

메서드와 파라미터 (Methods and Parameters)

return 구문 사용 n 즉시 리턴 n 조건문을 사용한 리턴 static void Example. Method(

return 구문 사용 n 즉시 리턴 n 조건문을 사용한 리턴 static void Example. Method( ) { int num. Beans; //. . . Console. Write. Line("Hello"); if (num. Beans < 10) return; Console. Write. Line("World"); }

가변형 매개변수 n params 키워드 사용 n 값 전달 n 한 번에 여러 개의

가변형 매개변수 n params 키워드 사용 n 값 전달 n 한 번에 여러 개의 매개변수(배열형) 전달 가능 static long Add. List(params long[ ] v) { long total, i; for (i = 0, total = 0; i < v. Length; i++) total += v[i]; return total; } static void Main( ) { long x = Add. List(63, 21, 84); }

오버로드된 메서드 선언(Overloaded Methods) n 클래스 안에서 메서드 이름 공유 Distinguished by examining parameter

오버로드된 메서드 선언(Overloaded Methods) n 클래스 안에서 메서드 이름 공유 Distinguished by examining parameter lists class Overloading. Example l { } static int Add(int a, int b) { return a + b; } static int Add(int a, int b, int c) { return a + b + c; } static void Main( ) { Console. Write. Line(Add(1, 2) + Add(1, 2, 3)); }

사용자 정의 데이터 타입(열거형과 구조체형) n Enumeration Types n Structure Types

사용자 정의 데이터 타입(열거형과 구조체형) n Enumeration Types n Structure Types

열거형 n Defining an Enumeration Type enum Color { Red, Green, Blue } n

열거형 n Defining an Enumeration Type enum Color { Red, Green, Blue } n Using an Enumeration Type Color color. Palette = Color. Red; n Displaying an Enumeration Variable Console. Write. Line(“{0}”, color. Palette); // Displays Red

Structure Types n Defining a Structure Type public struct Employee { public string first.

Structure Types n Defining a Structure Type public struct Employee { public string first. Name; public int age; } n Using a Structure Type Employee company. Employee; company. Employee. first. Name = “Red. Plus”; company. Employee. age = 21;

Exception Handling using System; public class Hello { public static void Main(string[ ] args)

Exception Handling using System; public class Hello { public static void Main(string[ ] args) { try{ Console. Write. Line(args[0]); } catch (Exception e) { Console. Write. Line("Exception at � {0}", e. Stack. Trace); } } }

Exception Objects Exception System. Exception Out. Of. Memory. Exception IOException Null. Reference. Exception Application.

Exception Objects Exception System. Exception Out. Of. Memory. Exception IOException Null. Reference. Exception Application. Exception

try ~ catch 블록 n 예외(에러) 처리 l 예외가 발생할만한 구문을 try절에 놓음 l

try ~ catch 블록 n 예외(에러) 처리 l 예외가 발생할만한 구문을 try절에 놓음 l try절에서 예외가 발생하면 catch절 실행됨 try { Console. Write. Line("Enter a number"); int i = int. Parse(Console. Read. Line()); } catch (Overflow. Exception caught) { Console. Write. Line(caught); } Program logic Error handling

throw 구문 n 예외를 직접 발생시킴 throw expression ; if (minute < 1 ||

throw 구문 n 예외를 직접 발생시킴 throw expression ; if (minute < 1 || minute >= 60) { throw new Invalid. Time. Exception(minute + " is not a valid minute"); // !! Not reached !! }

finally 절 n 예외가 발생하던 발생하지 않던간에 항상 실행되 어야 하는 구문을 입력하는 곳

finally 절 n 예외가 발생하던 발생하지 않던간에 항상 실행되 어야 하는 구문을 입력하는 곳 Monitor. Enter(x); try { . . . } finally { Monitor. Exit(x); } Any catch blocks are optional

네임스페이스(The. NET Framework) System. Web Services Description UI Html. Controls Discovery Web. Controls System.

네임스페이스(The. NET Framework) System. Web Services Description UI Html. Controls Discovery Web. Controls System. Win. Forms Design Protocols Component. Model System. Drawing Caching Security Drawing 2 D Printing Configuration Session. State Imaging Text System. Data System. Xml ADO SQL XSLT Design SQLTypes XPath Serialization System Runtime Interop. Services Collections IO Security Configuration Net Service. Process Diagnostics Reflection Text Remoting Globalization Resources Threading Serialization

네임스페이스(Base Framework) System Collections Security Configuration Service. Process Diagnostics Text Globalization Threading IO Runtime

네임스페이스(Base Framework) System Collections Security Configuration Service. Process Diagnostics Text Globalization Threading IO Runtime Interop. Services Net Reflection Remoting Resources Serialization

네임스페이스(Data And XML) System. Data ADO SQL Design SQLTypes System. Xml XSLT XPath Serialization

네임스페이스(Data And XML) System. Data ADO SQL Design SQLTypes System. Xml XSLT XPath Serialization

네임스페이스( Web Forms And Services) System. Web Services Description UI Html. Controls Discovery Web.

네임스페이스( Web Forms And Services) System. Web Services Description UI Html. Controls Discovery Web. Controls Protocols Caching Security Configuration Session. State

네임스페이스( Win Forms) System. Win. Forms Design Component. Model System. Drawing 2 D Printing

네임스페이스( Win Forms) System. Win. Forms Design Component. Model System. Drawing 2 D Printing Imaging Text

System. Array 클래스 n Array 클래스 l n 배열 관련 주요 속성 및 메서드

System. Array 클래스 n Array 클래스 l n 배열 관련 주요 속성 및 메서드 제공 주요 멤버 l Length l Rank l Get. Length() l Sort() l Reverse() l Clear() l 기타

System. Exception 클래스 : 예외 처리 n throw문을 사용한 사용자 정의 예외 처리 using

System. Exception 클래스 : 예외 처리 n throw문을 사용한 사용자 정의 예외 처리 using System; public class 예외처리{ public static void Main(){ Exception 예외내용; 예외내용 = new Exception("예외 발생!!!"); try{ throw(예외내용); } catch(Exception e){ Console. Write. Line(e. Message); } finally{ Console. Write. Line( "[예외 발생]이라는 예외가 발생했군요. "); } } }

System. String 클래스 : 중요 n String 클래스의 생성자 n String 클래스의 주요 멤버

System. String 클래스 : 중요 n String 클래스의 생성자 n String 클래스의 주요 멤버 string s = "Hello"; s[0] = 'c'; // Compile-time error

System. Object 클래스 n Synonym for System. Object n 모든 클래스의 기본 클래스(Base class

System. Object 클래스 n Synonym for System. Object n 모든 클래스의 기본 클래스(Base class for all classes) Object String Exception System. Exception My. Class

Unified type system n 모든 형은 궁극적으로 System. Object 을 상속 예약어 구조체 sbyte,

Unified type system n 모든 형은 궁극적으로 System. Object 을 상속 예약어 구조체 sbyte, byte System. SByte, System. Byte short, ushort System. Int 16, System. UInt 16 int, uint System. Int 32, System. UInt 32 long, ulong System. Int 64, System. UInt 64 char System. Char float System. Single double System. Double bool System. Boolean decimal System. Decimal

공통 메서드들(Common Methods) n Common methods for all reference types l To. String method

공통 메서드들(Common Methods) n Common methods for all reference types l To. String method l Equals method l Get. Type method l Finalize method

공통형 시스템(Common Type System; CTS) 개요 n CTS는 크게 값형과 참조형으로 구분한다. Type Value

공통형 시스템(Common Type System; CTS) 개요 n CTS는 크게 값형과 참조형으로 구분한다. Type Value Type Reference Type

내장형과 사용자정의형 비교 Value Types Built-in Type Examples of built-in value types: int float

내장형과 사용자정의형 비교 Value Types Built-in Type Examples of built-in value types: int float User-Defined Examples of user-defined value types: enum struct

is 연산자 n 만약에 변환이 가능하다면 true 값을 반환 Bird b; if (a is

is 연산자 n 만약에 변환이 가능하다면 true 값을 반환 Bird b; if (a is Bird) b = (Bird) a; // Safe else Console. Write. Line("Not a Bird");

as 연산자 n Reference-Type 간의 데이터 형 변환 n 잘못된 데이터 형 변환시 n

as 연산자 n Reference-Type 간의 데이터 형 변환 n 잘못된 데이터 형 변환시 n l null 을 반환 l 예외(Exception)을 발생하지 않음 as 는 is 연산자 + 캐스팅 Bird b = a as Bird; // Convert if (b == null) Console. Write. Line("Not a bird");

System. IO Namespace n 파일 처리관련 클래스 제공 l File, Directory l Stream. Reader,

System. IO Namespace n 파일 처리관련 클래스 제공 l File, Directory l Stream. Reader, Stream. Writer l File. Stream l Binary. Reader, Binary. Writer

System. Xml Namespace n XML/XSLT 관련 풍부한 클래스 제공

System. Xml Namespace n XML/XSLT 관련 풍부한 클래스 제공

System. Data Namespace n System. Data. Sql. Client l n SQL Server 관련된 주요

System. Data Namespace n System. Data. Sql. Client l n SQL Server 관련된 주요 클래스 제공 System. Data l 데이터베이스 처리 관련 클래스 및 네임스페이 스 제공