Sei sulla pagina 1di 35

값형식 : 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# 프로그래밍 1


참조형식 : Reference Type

class
class
string , Exception
참조형식 interface
Reference
array
Type

delegate

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


• Using Reference-Type Variables

 Comparing Value Types to Reference Types


 Declaring and Releasing Reference Variables
 Invalid References
 Comparing Values and Comparing References
 Multiple References to the Same Object
 Using References as Method Parameters

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


Comparing Value Type to Reference Types

 Value Types  Reference Types


• The variable contains • The variable contains a
the value directly reference to the data
• Examples : • Data is stored in a
char, int separate memory area

int mo1 ; string mo1 ;


mo1 = 42 ; mo1 = "Hello" ;

mo1

42 Hello

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


Declaring and Releasing Reference Variables

 Declaring Reference Variables


coordinate c1 ; c1
c1 = new coordinate( ) ;
c1.x = 6.12 ;
c1.y = 4.2 ; 6.12 4.2

 Releasing Reference Variables

c1 = null ; c1

6.12 4.2

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


Invalid References

 If You Have Invalid References


• You cannot access members or variables
 Invalid References at Compile Time
• Compiler detects use of uninitialized references
 Invalid References at Run Time
• System will generate an exception error
• - NullReferenceException

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


Comparing Values and Comparing References

 Comparing Value Types ( string 포함 )


• == and != compare values
 Comparing Reference Types ( string 예외 )
• == and != compare the reference, not the
values

O 6.12 4.2

Different

O 6.12 4.2

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


Multiple References to the Same Object

 Two References Can Refer to the Same Object


• Two ways to access the same object for
read/write
c1
6.12 4.2
c2

coordinate c1 = new coordinate( ) ;


coordinate c2 ;
c1.x = 2.3 ; c1.y = 7.6 ;
c2 = c1 ;
Console.WriteLine (c1.x + " , " + c1.y) :
Console.WriteLine (c2.x + " , " + c2.y);
2021년 11월 2일 C# 프로그래밍 8
Using References as Method Parameters

 References Can Be Used as Parameters


• When passed by value, data being referenced
may be changed
2 3 3 4
c
static void PassCoordinateByValue (coordinate c)
{
c.x++ ; c.y++ ;
}

loc.x = 2 ; loc.y = 3 ;
PassCoordinateByValue (loc) ; loc
Console.WriteLine(loc.x + " , " + loc.y) ;

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


class coordinate {
public double x = 0.0; public double y = 0.0; }
static coordinate Example ( coordinate ca,
ref coordinate cb,
out coordinate cc ) { … }

static void PassCoordinateByValue (coordinate c) {


{ c.x++; c.y++; }

coordinate loc = new coordinate( );


loc.x = 2; loc.y = 3;
PassCoordinateByValue ( loc );
Console.WriteLine( loc.x, loc.y);

static void PassCoordinateByValue (coordinate c) {


{ c = new coordinate ( );
c.x++; c.y++; }

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


• Using Common Reference Types

 Exception Class
 String Class
 Common String Methods, Operators, and
Properties
 String Comparisons
 String Comparison Operators

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


Exception Class

 Exception is a Class
 Exception Objects Are Used to Raise Exceptions
• Create an Exception object by using new
• Throw the object by using throw
 Exception Types Are Subclasses of Exception
• InvalidCastException, DivideByZeroException..

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


String Class

 Multiple Character Unicode Data


 Shorthand for System.String
 Immutable ( 변경되지 않는… )

string s = “Hello” ;
s[0] = ‘c’ : // Compile-time error

 StringBuilder (mutable : can modify…)

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


Common String Methods, Operators, and Properties

 Brackets
 Insert Methods
 Length Property
 Copy Method
 Concat Method
 Trim Method
 ToUpper and ToLower Methods

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


String Comparisons

 Equals Method
• Value comparison
• if (s1.Equals(s2)) //instance method
• if (String.Equals(s1,s2)) //static method
 Compare Method
• More comparison
• Case-insensitive option
• Dictionary ordering
 Locale-Specific Compare Options

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


String Comparison Operators

 The == and != Operators Are Overloaded for


Strings
 They Are Equivalent to String.Equals and !
String.Equals

string a = “Test” ;
string b = “Test” ;
if ( a == b ) …. // Returns true

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


• The Object Hierarchy

 The Object Type


 Common Methods
 Reflection

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


The object Type

 Synonym for System.Object


 Base Class for All Classes

Object

String Exception MyClass

SystemException

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


Common Methods

 Common Methods for All Reference Types


• ToString method
• Equals method
• GetType method
• Finalize method

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


Reflection

 You Can Query the Type of an Object


 System.Reflection Namespace
 The typeof Operator Returns a Type Object
• Compile-time classes only
 GetType Method in System.Object
• Run-time class information

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


• Namespace in the .NET Framework

 System.IO Namespace
 System.Xml Namespace
 System.Data Namespace
 Other Useful Namespace

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


System.IO Namespace

 Access to File System Input/Output


• File, Directory
• StreamReader, StreamWriter – bytes,
characters
• FileStream – random access to files
• BinaryReader, BinaryWriter – binary types

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


System.Xml Namespace

 XML Support
 Various XML-Related Standards

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


System.Data Namespace

 System.Data.SqlClient
• SQL Server .NET Data Provider
 System.Data
• Consists mostly of the classes that constitute
the ADO.NET architecture

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


Other Usefule Namespaces

 System Namespace
 System.Net Namespace
 System.Net.Sockets Namespace
 System.Windows.Forms Namespace

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


• Data Conversions

 Converting Value Types


 Parent/Child Conversions
 The is Operator
 The as Operator
 Conversions and the object Type
 Conversions and Interfaces
 Boxing and Unboxing

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


Converting Value Types

 Impilcit Conversions
 Explicit Conversions
• Cast operator
 Exceptions
 System.Convert Class
• Handles the conversions internally

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


Reference Type Data Conversion

Parent - General

Child - Specific

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


Parent/Child Conversions

 Conversion to Parent Class Reference


• Implicit or explicit
• Always succeeds
• Can always assign to object
 Conversion to Child Class Reference
• Explicit casting required
• Will check that the reference is of the correct
type
• Will raise InvalidCastException if not

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


The is Operator

 Returns true If a Conversion Can Be Made

Bird b ;
if ( a is Bird )
b = ( Bird ) a ; // Safe
else
Console.WriteLine (“Not a Bird”) ;

 InvalidCastException

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


The as Operator

 Converts Between References Types, Like Cast


 On Error
• Returns null
• Does not raise an exception

Bird b = a as Bird ;
if ( b == null )
Console.WriteLine (“Not a Bird”) ;

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


Conversions and the object Type

 The object Type Is the Base for All Classes


 Any Reflection Can Be Assigned to object
 Any object Variable Can Be Assigned to Any
Reference
• With appropriate type conversion and checks
 The object Type and is Operator

object ox ;
ox = a ; b = (Bird) ox ;
ox = (object) a ; b = ox as Bird ;
ox = a as object ;

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


Conversion and Interfaces

 An Interface Can Only Be Used to Access Its Own


Members
 Other Methods and Variables of the Class Are Not
Accessible Through the Interface

Rectangle r = new Rectangle ( ) ;


r.Move ( ) ;
r.Paint ( ) ;

IVisual v = ( IVisual ) r ;
v.Move ( ) ; //not valid
v.Paint ( ) ; // ok

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


Boxing and Unboxing

 Unified Type System


 Boxing (implicit : always success)
 Unboxing
 Calling Object Methods on Value Types

int p = 123 ;
object box ; 123 p = ( int ) box ;
box = p ;

box 123

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


Data Conversions

implicit conversion
Value = Value
explicit conversion

parent = child : implicit


Reference = Reference
child = parent : explicit

Reference = Value boxing

Value = Reference unboxing

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

Potrebbero piacerti anche