ترجمه مهدی محبیان c# گزیده کتاب ویژوال
139
ن ﺑ ﺮ ﻬ ﻣ ﺮ ﺘ ﺴ ﮔ ﺮ ﻬ ﻣ ﺪ ﻧ و ﺪ ﺧ م ﻧ ﻪ ﺑ ن ﺑ ﺮ ﻬ ﻣ ﺮ ﺘ ﺴ ﮔ ﺮ ﻬ ﻣ ﺪ ﻧ و ﺪ ﺧ م ﻧ ﻪ ﺑ ل و ﮋ ﯾ و ب ﺘ ﮐ ه ﺪ ﯾ ﺰ ﮔ C# www.ParsBook.org
-
Upload
bakhtiari1983 -
Category
Documents
-
view
232 -
download
1
description
another good reference for learning C#
Transcript of ترجمه مهدی محبیان c# گزیده کتاب ویژوال
#C
www.ParsBook.org
:
.
www.ParsBook.org
2...........................................................................................................................35
3............................................................................................................................56
4............................................................................................................................76
5.............................................................................................................................96
6............................................................................................................................115
www.ParsBook.org
#C .
C# .C# Microsoft .NET
. )UNIX( C
#C Java++C C
.
#C
. !
. #C I
)-( (+)
. .
while if
#C .
#C .
.
www.ParsBook.org
-
. #C
.
.
.
2008
:
All Programs Start
. Microsoft Visual Studio 2008
Microsoft Visual Studio 2008Microsoft Visual Studio 2008
.
www.ParsBook.org
. .
)IDE(
.Visual C# Development Settings
IDE . Start Visual Studio
.
Visual C# 2008 Express Edition
Express Edition Microsoft Visual C# 2008 All Programs Start
.
"
. "
. #C
.
New . Project File New
Project .
.
.
Console Templates . #Visual C Project types
Application .
Location
C:\Users\YourName \Documents\Microsoft Press\Visual CSharp Step By
Step\Chapter XP . 1
www.ParsBook.org
Press\Visual CSharp Step by Step\Chapter 1 .
“C:\
Users\ YourName \Documents”
“C:\Documents and Settings\ YourName \My Documents”
.
.
. TextHello Name
Create directory for solution
. OK
Visual C# 2008 Express Edition New
Project
\ C:\Users\ YourName AppData\Local\Temporary Projects .
:
. Options Tools
Projects and Show All Settings Options
Solutions .
Microsoft Press\Visual Visual Studio projects location
CSharp Step By Step\Chapter 1
. Documents
Visual C# 2008 Express Edition
. Console Application New Project
. TextHello Name
OK .
:
.
.
.
IDE
.
.
Solution Explorer.
.
Solution Explorer
.
Solution Explorer
:
Solution ‘TextHello’ solution
Documents\Microsoft .
Press\Visual CSharp Step by Step\Chapter 1\TextHello
solution .TextHello.sln
.
TextHello C# .
.
TextHello.csproj .
My Documents\Microsoft Press\Visual CSharp Step by Step\Chapter\
1\TextHello\TextHello .
Properties TextHello .
AssemblyInfo.cs .AssemblyInfo.cs
... .
.
.
References
.
.
.
.
Program.cs C#
.
.
.
Program Program.cs
. Main
" . "
--- Main.
Main.
. Main M
.
Hello World
Hello World
IntelliSense
Program.cs Main
Enter ---}---
. Console .
C Console IntelliSense
. #C
. Console
IntelliSense Con .
Tab Console
Enter .
static void Main(string[ ] args)
}
Console
.
Console . IntelliSense
. Console
WriteLine Enter .
, W r , i , t , e, L WriteLine
Enter .IntelliSense
WriteLine
: Main.
www.ParsBook.org
}
---(--- . IntelliSense .
WriteLine
WriteLine .
overloaded Console WriteLine
. ---
WriteLine
. (
overloaded Main.)
:
}
IntelliSense
overloaded . WriteLine
---(--- (;)-.
Main :
static void Main(string[] args)
}
“Hello World”
WriteLine Main.
}
{and})and(
.
.
IntelliSense
IntelliSense
. .
:
IntelliSense
.
/)(/
.)
: .
Console.ReadLine(); // Wait for the user to press the Enter key
.
(/*) . (*/)
.
.
.
. #C
.
View
Output
.
) Output(
.
Compile complete -- 0 errors, 0 warnings
TextHello -> C:\Documents and Settings\John\My Documents\Microsoft Press\…
www.ParsBook.org
========== Build: 1 succeeded or up-to-date, 0 failed, 0 skipped ========
. ) Error List(
Hello World
WriteLine .
.
Error List
.
.
.
www.ParsBook.org
. Build Solution
Visual Studio 2008 Professional Edition Visual Studio 2008 Standard Edition
.
Visual C# 2008 Express Edition
File Save All
Visual C# 2008 Express Edition
Options
. Start Without Debugging Debug
Hello World.
:
”. . . Press any key to continue“
.
Start Debugging Debug
.
Enter
.
.
Show All ) solution(TextHello Solution Explorer
Files Solution Explorer ---
.Solution Explorer
.
Microsoft Press\Visual CSharp Step by) obj bin Step\Chapter
1\TextHello\TextHello).
Solution Explorer (+) bin .
Debug .
. Release
. Debug (+) Solution Explorer
TextHello.exe TextHello.pdb TextHello.vshost.exeTextHello.vshost.exe.manifest
:
Visual C# 2008 Express Edition
.
Start Without TextHello.exe
Debugging Debug .
.
)Namespace(
.
.
.
.
.
(
).
)(
.
) (
.
)Namespace(
.
.
: TextHello
namespace TextHello
.
.
NET.) SDK( NET.
. System Console .
. System.Console
SystemConsole .
. using
www.ParsBook.org
TextHello Program.cs
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
.) ( using
.
. using using
.
.
using Program.cs
:
//using System;
Build . Build Solution
: ) Error List(
The name ’Console’ does not exist in the current context.
. ) Error List(
. Program.cs
System.Console Main
: Main.
static void Main(string[] args)
System
IntelliSense
. Build Solution Build
.
Main
.
Start Without Debugging
Debug
using ) (
. . ) assemblies(
dll.
exe. .
NET. .
System.Console
. NET.
.
. (
!)
NET.
.
System.Console
...
.
.
using
) (
.
)1:1(
www.ParsBook.org
.
.
Solution Explorer
. References (+) TextHello
System System.Core
System.Data System.Xml .
--- Add Reference References
.
.
. :
. ) Design View(
.
: .
.
Windows PresentationWPF. NET Framework version 1.0.
Foundation NET Framework version 3.0.
. Windows Forms
2008
www.ParsBook.org
Visual Studio 2008 Standard Edition Visual Studio 2008
Professional Edition :
. Project File New
. New Project
. #Visual C Project Types
Step\Chapter 1
TextHello
.
OK .
Visual C# 2008 Express Edition
.
. New Project File
TextHello Save New Project
Microsoft Press\Visual CSharp Location Save Project .
Step By Step\Chapter 1 Documents Save
.
WPF
XAML
) Error List( ) Output(
. ) Design View(
XAMLExtensible Application Markup Language
WPF
.
XML
XAML
XMLPad
MSDN
.
- #C
.
. Toolbox
.
www.ParsBook.org
. WPF
.
.
(
) . ) Toolbox(
( Toolbox Auto Hide
Toolbox. )
)Design View (
(.
Auto Hide .)
)drag).
).
XAML
Margin .
Margin
.
Margin
. Margin
Windows Presentation " WPF Width Height
Foundation" .
Properties
) Design View(
) Properties( )( grid .
)grid( WPF
.
Text ) Properties( . .
.
. 20 FontSize
.
.
. Design View
. Design View
XAML
.
<Label>
(“Label”)
Please enter your name
.
www.ParsBook.org
.
Design View )Toolbox ( .
) Toolbox( Design View
“There are no usable controls in this group.”
.
. TextBox Toolbox
. .
)Drag(
- .
.
Name ) Properties(
. userName
www.ParsBook.org
"
.
. Button ) Toolbox(
)Drag(
.
. ok Name
XAML
. . OKButton
. Design View Window1.xaml
Title Hello .
)grid( .
.
OK
Margin
.
OK .
.
:
www.ParsBook.org
Build
Build Solution .
. Start Without Debugging Debug
.
. OK
Click OK .
Close )X )
.
.
) (
.
Window1.xaml.cs .
Window1.xaml.cs .
:
{
using ) (
WPF
Window1 . Window1
InitializeComponent
constructor/ (.
.
.) .
XAML
.
.
OK
.
)Design View ( View Code
C# WPF
.
App.xaml
.
/Design View
“Intentionally Left Blank,”
Solution ExplorerApp.xaml (+)
. Application.xaml.cs
:
{
}
}
using
Main .
Main . Main
App.xaml StartupUri Main
. App.xaml .
!
OK
Window1 Window1.xaml
.
Window1.xaml.cs
ok_Click
.
. OK
.
)BOLD( ok_Click :
void ok_Click(object sender, RoutedEventArgs e)
{
OK
.
(
MessageBox.Show . )
www.ParsBook.org
"Hello" .
)username( .
Window1 Window1.xaml
. ) Design View(
XAML
Button
ok_Click Click Button .
Debug
.
. OK
.
OK
.
File
Exit . Save Yes
) (
Save (
. )Visual C# 2008 Express Edition
www.ParsBook.org
:
.
.
.
. –)( (+)
. ) (
" #C"
Windows Presentation Foundation
)WPF( / .
C# .
C#
.
(
#C )
.
www.ParsBook.org
.
" " .
#C . .Main
.
. () syntax(/
(.
#C/
. );(
:
Console.WriteLine(“Hello World”);
) ”free format“ ( # C
.
/
.
(
: / #C).
) ( .
( ).
result _score footballTeam plan9 result%
footballTeam$ 9plan .
www.ParsBook.org
FootballTeam footballTeam: #C
.
#C
.
.
class namespace using .
.
.
true protected in do abstract
try public int double as
typeof readonly interface else base
uint ref internal enum bool
ulong return is event break
unchecked sbyte lock explicit byte
unsafe sealed long extern case
ushort short namespace false catch
using sizeof new finally char
virtual stackalloc null fixed checked
void static object float class
volatile string operator for const
while struct out foreach continue
switch override goto decimal
this params if default
throw private implicit delegate
.
.
C#
.
yield select join from
set let get
value orderby group
where partial into
.
.
.
.
cost .
cost
.
.
:
.
.
MyVariable myVariable
.
.
.
. (
) forPrintOfText:.
. )(
!) )(
www.ParsBook.org
)CLS( .
NET.
.
#C.
)integer( ---
.
.
. )integer/ ( int age
- .
int age;
#C int
integer/
. (
. )
#C
.
.
.
. . age
age = 42;
(=)
age .
:
)ScreenTip(
.
C# )primitive data types ( .
#C
int
31 2
324 100.5
coin = 0.42M;
char grill;
grill = ‘x’;
teeth = false;
www.ParsBook.org
++CC .
. #C.
:
PrimitiveDataTypes #C
.
Visual Studio 2008 Standard Edition Visual Studio 2008 Professional Edition
Microsoft Press\Visual CSharp Step by Step\Chapter 2\PrimitiveDataTypes\
Documents
.
PrimitiveDataTypes
. Open
Solution Explorer ) ( PrimitiveDataTypes
.
.PrimitiveDataTypes.sln sln.
.csproj. .
.
Debug
:
Sample value ”forty two“
.
. )(int
Sample value
. int
booldouble .
.
Quit .
. Solution Explorer (Window1.xaml(
.) Design View( WPF
Window1.xaml ) Design View(
. View Code
. Window1.xaml.cs
) Solution Explorer(
(+)
Window1.xaml Window1.xaml.cs .
showFloatValue .
Edit
Find
and Replace Quick Find .
.
. Find Next
.
Find Options +
. Match Case
.
F ( Ctrl+F
Quick Find ) Edit
.
.
. showFloatValue float
:
float variable;
variable=0.42F;
value.Text = “0.42F”;
. float variable
variable 0.42F
0.42 F . float 0.42
double
#C---
.)
. value
.
. Text
. Console.WriteLine. (
Text )
. ) Text
”0.42F“ .
.
variable )string( Text
C#
Microsoft .NET
Framework .
: . showIntValue
www.ParsBook.org
}
. showIntValue int
. Members
(
(
.
.
showIntValue
:
private void showIntValue()
}
value.Text = variable;
variable
#C .
string variable
int
.
.
.
42 . Choose a data type int
Sample value .
Quit .
.
showDoubleValue
:
showBoolValue .
private void showBoolValue()
. Start Without Debugging Debug
. bool int double Choose a data type
Sample value
C#
:
(*)
/ *– +. (/)
: ) ()
long moneyPaidToConsultant;
.
.
.
. decimal char int long float double
(+)
. bool string
string
(
):
// compile-time error
. +
( ”431“ .
) ”44“
:
Int32.Parse NET.
.
. 5.0/2.0 2.5 double )C#
double
double ) float
. 5/2
.
#C. int int
. .
5/2.0
#C.
. double int
).2.5( double
.
.0.0/0.0
NaN . NaN0 *
: #C
2 % 9 . y x x % y. (%)
.1 42 9 1
C
C++
double
float #C.
.
.2.22.4 % 7.0 .
int
.MathsOperators #C
2\MathsOperators Documents .
.
.
. right operand 13
.
. Calculate Subtraction –
www.ParsBook.org
– 54 Expression Result 41 13
:
. Calculate Division /
Result
4.15384654/13 .
#C
.
. Calculate Remainder %
Result 2 13%54 Expression
. 54
.213
(54 – ((54/13) * 13))
---
13 * (54/13)
!54
. Quit
.
.MathsOperators
MathsOperators
Window1.xaml
).
Document . Document Outline Other Windows View
Outline
.
. Document Outline
Design Document Outline
View
.
TextBox
. Document
Outline lhsOperand rhsOperand . (
.)
Text
.
TextBox
expression TextBox
result .
. Document Outline
. Window1.xaml.cs
subtractValues . :
private void subtractValues()
}
lhs int
lhsOperand
)string(
int . int
int.Parse
.
int rhs
rhsOperand
. int
outcome int
.
www.ParsBook.org
+ ) (
. expression.Text
. expression
Text
result
int )string( Text .
( int Text
string( . ToString int
.
ToString
NET.
ToString .ToString
. outcome ToString
. outcome
. Text--- result Text
ToString
" " .
.
. ) Precedence(
:*+
2 + 3 * 4
3
*
:
(3 + 2)
20.
(4 * 3)
.1412 + 2 +
www.ParsBook.org
)-( + % )/ ( * #C
. 4 * 3 + 2
.144 * 3 + 2
.
32 .
* )5 ( +
20
( )) round brackets( ) parentheses(
.
) (
*/ .
.
) (
:
* (4/2)
(4/2) * 6
*.
- /
.12 6 4/2 .
. (=) #C
.
.
myInt 10
:
myInt = 10; //value of assignment expression is 10
:
int myInt;
int myInt2;
. myInt myInt2
.
.
:
myInt5 = myInt4 = myInt3 = myInt2 = myInt = 10;
.
.
.
(
)
: + 1
count = count + 1;
#C 1
++ .: count
:
count++;
– #C 1 – :
count--;
– ++ ) unary( –
. !
.
– ++ –
.
.
:
count++; // postfix increment
++count; // prefix increment
count--; // postfix decrement
--count; // prefix decrement
– –
count 1 ++count .
. 1 count++
.
– ++ –
++count .
count ++count
count .
:
int x;
x = 42;
www.ParsBook.org
(
) .x++
x
x
.
x x++
.
--++ ) ++x( .
:
int myInt;
.
:
int myInt = 99;
: myOtherInt
int myInt = myOtherInt * 99;
. int int .
C#
. C#
: var
var myVariable = 99;
. myOtherVariable myVariable
var
.
Variant
.
Variant .
Variant var
var #C .
.
var .
). (
var C#
.
.
.
Yes Save . Exit File
( Save ) (
. )Visual C# 2008 Express Edition
www.ParsBook.org
:
.
.
.
.
.
" "
.
.
.
.)(
.
.
C
.
.
(
CalculateIncomeTax (.
.
.
www.ParsBook.org
/
: #C /
returnType methodName (parameterList(
}
returnType
int
string.
void
.
methodName .
. add$Values
.displayCustomer ---
parameterList
.
.
.
.
#C ++C C
.
.
addValues int
int
{
// ...
// ...
}
.
. var
showResult
void showResult(int answer)
{
// ...
}
. void
C#
) (
( )
void
.
. return ) return
. .
int return
. :
int addValues(int leftHandSide, int rightHandSide)
{
// ...
. return )
return
).
void (
)
. return
: . return
void showResult(int answer)
}
.
.
. MathsOperators
.
.
Microsoft Press\Visual CSharp Step by Step\Chapter 3\Methods\ Methods
Documents
.
. Quit
. Window1.xaml.cs
addValues .
:
{
addValues . expression
rightHandSide leftHandSide .
rightHandSide leftHandSide +
int .
int
. int addValues
subtractValues multiplyValues divideValues
remainderValues
showResult
:
}
result answer
.
.
.
.
.
!
( .
. )
( )
.
/
: #C /
www.ParsBook.org
.
#C
) (void .
result = .
argumentList
.
.
.
.
: addValues
int addValues(int leftHandSide, int rightHandSide)
{
// ...
}
int int addValues
. int
:
int arg1 = 99;
int arg2 = 1;
addValues
:
www.ParsBook.org
addValues(); // compile-time error, not enough arguments
addValues(39); // compile-time error, not enough arguments
addValues(“39”, “3”); // compile-time error, wrong types
: .
int result = addValues(39, 3); // on right-hand side of an assignment
showResult(addValues(39, 3)); // as argument to another method call
Methods
.
.
. (Methods
. \Microsoft Press\Visual CSharp Step by
Step\Chapter 3\Methods Documents .(
. Window1.xaml.cs
calculateClick try .
( try " " .
:
Parse
Int32
.) int &n