Sei sulla pagina 1di 31

C# 기본사항

 대소문자 구별 (Case Sensitive)


 Semicolon ( ; ) 사용
 { } 으로 블록 구분
 // 주석문 한줄
 /* 주석문 여러 줄 */
 using System ;
 namespace projectname ;
 class Class1
{ public static void Main(string[ ] args)
{ // TODO: Add code to start application here
}
}
2021년 11월 2일 C# 프로그래밍 1
Overview

 Structure of a C# Program
 Basic Input/Output Operations
 Recommended Practices
 Compiling, Running, and Debugging

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


• Structure of a C# Program

 Hello, World
 The Class
 The Main Method
 The using Directive and the System
Namespace
 Demonstration : Using Visual Studio
to Create a C# Program

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


Hello, World

using System ;
class Hello
{
public static void Main( )
{
Console.WriteLine("Hello, World");
}
}

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


The Class

 A C# Application is a Collection of Classes,


Structures, and Types
 A Class is a Set of Data and Methods
 Syntax
class name
{ ….. }
 A C# Application Can Consist of Many Files
 A Class Cannot Span Multiple Files

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


The Main Method

 When Writing Main, You Should :


• Use an uppercase "M" as in "Main"
• Designate one Main as the entry point to the
program
• Declare Main as public static void Main
 Multiple Classes Can Have a Main
 When Main Finishes, or Returns, the Application
Quits

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


The using Directive and the System Namespace

 The .NET Framework Provides Many Utility Classes


• Organized into namespaces
 System is the Most Commonly Used Namespace
 Refer to Classes by Their Namespace

System.Console.WriteLine ("Hello, World");

 The using Directive


using System;
....
Console.WriteLine ("Hello, World");

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


Demonstration using Visual Studio

// 기본 폼 – 콘솔응용 프로그램

using System ;
namespace helloworld
{
class Class1
{ static void Main (string[ ] args)
{ //
// TODO: Add code to start application here
//
}
}
}

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


• Basic Input/Output Operations

 The Console Class


 Write and WriteLine Methods
 Read and ReadLine Methods

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


The Console Class

 Provides Access to the Standard Input, Output,


and Error Streams
 Only Meaningful for Console Applications
• Standard Input - Keyboard
• Standard Output - Screen
• Standard Error - Screen
 All Streams May Be Redirected
• prog1 > progout.txt

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


Write and WriteLine Methods

 Console.Write and Console.WriteLine Display


Information on the Console Screen
• WriteLine outputs a line feed/carriage return
 Both Methods Are Overloaded
 A Format String and Parameters Can Be Used
• Text formatting
• Numeric formatting

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


Text formatting

 { N, M }
• N : the Parameter Number
• M : Field Width and Justification

 Console.WriteLine("{0,10}", 99);
" 99"
 Console.WriteLine("{0,-10}", 99);
"99 "

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


Text Formatting

Console.WriteLine(99);
Console.WriteLine("99");
Console.WriteLine("The sum of {0} and {1} is {2}", 100,
200, 100+200);
Console.WriteLine("Left justified in a field of width 10:
{0,-10}", 99);
Console.WriteLine("Right justified in a field of width 10:
{0,10}", 99);
Note : use backward slash (\) to turn off the special
meaning of the character.
"\{" = {, "\\" = \, "@\\a\b" = \\a\b

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


Numeric formatting

 { N : Format M }
• N : the Parameter Number
• M : Field Width and Justification
 Formatting
• C : currency
• D : decimal integer
• E : exponential notation
• G : either Fixed point or Integer
• N : the number with embedded commas
• X : the number by hexadecimal notation

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


Numeric Formatting

Display the number

C as currency

D as a decimal integer

E by using exponential notation

F as a fixed-point value

G as either fixed-point or integer

N with embeded commas

X by using hexadecimal notation

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


Console.WriteLine("Currency formatting - {0:C} {1:C4}", 8.8, Ê-888.8);
Console.WriteLine("Integer formatting - {0:D5}", 88);
Console.WriteLine("Exponential formatting - {0:E}", 888.8);
Console.WriteLine("Fixed-point formatting - {0:F3}", Ê888.8888);
Console.WriteLine("General formatting - {0:G}", 888.8888);
Console.WriteLine("Number formatting - {0:N}", 8888888.8);
Console.WriteLine("Hexadecimal formatting - {0:X4}", 88);

When the previous code is run, it displays the following:

Currency formatting - $88.80 ($888.8000)


Integer formatting - 00088
Exponential formatting - 8.888000E+002
Fixed-point formatting - 888.889
General formatting - 888.8888
Number formatting - 8,888,888.80
Hexadecimal formatting – 0058

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


Read and ReadLine Methods

 Console.Read & Console.ReadLine read User


Input

 Read reads the next character


 ReadLine reads the entire line input

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


• Recommended Practices

 Commenting Applications
 Generating XML Documentation
 Demonstration : Generating and view XML
documentation
 Exception Handling

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


Commenting Applications
 Comments Are Important
• A well-commented application permits a
developer to fully understand the structure of
the application
 Single-Line Comments
// Get the user's name
Console.WriteLine(" What is your name ?");
name = Console.ReadLine( );
 Multiple-Line Comments
/* Find the higher root of the
quadratic equation */
x = (……) :
2021년 11월 2일 C# 프로그래밍 19
Generating XML Documentation

/// <summary> The Hello class prints a greeting


/// on the screen
/// </summary>
class Hello
{ /// <remarks>
/// ….
/// </remarks>
public static void Main( )
{
Console.WriteLine("Hello, World");
}
}

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


Exception Handling

using System;
public class Hello
{
public static void Main( string [ ] args)
{
try { Console.WriteLine (args[0]) ; }
catch (Exception e) {
Console.WriteLine("Exception at {0}",
e.StackTrace);
}}}

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


• Compling, Running, and Debugging

 Invoking the Compiler


 Running the Application
 Demonstration : Compiling and Running
 Debugging
 Demonstration : using Visual Studio
 The SDK Tools
 Demonstration : Using ILDASM

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


Invoking the Compiler

 Common Compiler Switches


 Compiling from the Command Line
 Compiling from Visual Studio
 Locating Errors

/? Or /help /warnaserror
/out /target
/main /checked
/optimize /doc
/warn /debug

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


Compiler Options
 > csc week2.cs
 > csc /t:exe week2.cs
 > csc /out:week2.exe /t:exe week2.cs
 > csc /r:assemb1.dll,assemb2.dll /out:result2.exe
week2.cs
 /t: target (exe, library, module)
 /out:
 /r: reference

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


Running the Application

 Running from the Command Line


• Type the name of the application

 Running from Visual Studio


• Click Start Without Debugging on the Debug
menu

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


Demonstration : Hello, World !

Console.WriteLine( "Hello World !");

Console.WriteLine("Type your name and press


Enter");
string name = Console.ReadLine( );

// Write text to the console


Console.WriteLine("Hello " + name + "!");
// Control + F5

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


Debugging

 Exceptions and JIT Debugging


 The Visual Studio Debugger
• Setting breakpoints and watches
• Stepping through code
• Examining and modifying variables

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


The SDK Tools

 General Tools and Utilities

 Windows Forms Design Tools and Utilities

 Security Tools and Utilities

 Configuration and Deployment Tools and Utilities

 Demonstration : Using ILDASM

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


The Process of EXE/DLL Source
Managed Execution (MSIL and Compiler Code
metadata)
Class
Libraries
(MSIL and
Class Loader
Metadata)
JIT Compiler
With optional
verification

Trusted, Managed Call to an


Pre-JITed Native Uncompiled
Code only Code method

Execution

Runtime Engine
Security Checks
2021년 11월 2일 C# 프로그래밍 29
C# Keywords

 Keywords Are Reserved Identifiers


• abstract, base, bool, default, if, finally
 Do Not Use Keywords As Variable
Names
• Results in compile-time error
 Avoid Using Keywords by Changing
Their Case Sensitivity
• Int INT; // poor style

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


Keywords in C#
abstract as base bool break byte
case catch char checked class const
continue decimal default delegate do double
else enum event explicit extern false
finally fixed float for foreach goto
if implicit in int interface internal
is lock long namespace new null
object operator out override params private
protected public readonly ref return sbyte
sealed short sizeof stackalloc static string
struct switch this throw true try
typeof uint ulong unchecked unsafe ushort
using virtual void while
2021년 11월 2일 C# 프로그래밍 31

Potrebbero piacerti anche