NET
C# өң қ.NET- .
C# ққ
1) C# Microsoft.NET Framework- ұ ү ғ ә ғғ.
2) C# ғғ .
.NET- ғғ ә .
.NET қңқң қ ө Comman Language Runtime(CLR) .NET runtime . CLR қ қ . , CLR- ү қ .
.NET 2 қ ұ:
1) Microsoft Intermediate Language(IL)-ғ қ
2) CLR қ IL ң ү қ
ұ қ ө ң. Ө Microsoft Intermediate Language(IL) .NET- ө ққ .
Microsoft Intermediate Language (MSIL) ң ққ.
Microsoft Intermediate Language Java-ң қ ө ө. ұ қ қғ ә ң ө ққ . :
1) ә
ұ - ұққ ұ
2) ұ өң ғғ
IL әқ ә Java-ғ қғ қ
3) ққ қ
IL қ ә қ қ қ ққ қ қ .
2. ә ә . өң ә .
C# ә ң 2 : ә , ѳ
ә ѳ ң ғ ә ә ө қ , ѳ ә қ.
ұ ң ә ү қ: ә қғ қ, ѳ қ ү қ.ұ ө ң, ғ ә ә ү ., int ә ,ғ ә ң 2 ғ құ,ң әқ 20 ә қ:
|
|
// i & j int
i-20;
j-20;
ққ. Vector ғ қғ . Vector int Value ғ .
Vector x,y;
X=new Vector();
x.Value=30;
y=x;
Console.Writeline(y.Value);
y.Value=50;
Console.Writeline(x.Value);
ұ ғ ң Vector ғ құ, x y қғ қ ө.
x y ғқ,ң әқң қ Vector ә ң құ, қ. ғ құ. құ ү ө new ү ө қ . x y , ө ө.қ 30 50 ғ.
, қ ғ ,ң ә null ң.
Y=null;
null ң, ғ қ қ қ ә қ қғ ү .
Attribute қ ұғ қғ .
ң ә . , ө, қ / ә құ қ қғ қң . ө-ң ғ қ, -ң ә-ң ә , ә қ . , ң - -ң ә -ң ө , / -ң қ-ң ғ ғ ү қ.
3. # ң . . ң ө .
C # - ү :
ә ;
,
int ;
ұ int ұ. ұ ә ғ қғ ү .
ғ ң,ғ ә = қ :
=10;
ә ғ .
int i=10;
ә , .
int i=10,y=20; // x ә y int
Әү ү ө-ө ғ .ө ә ү ә .
int x=10;
bool y=true; // true false қ құ.
|
|
int x=10,bool y=true; // ұ ө!
// ң ү(). // ө ұғ ә ү .ұ ғ ұ ү ү қ ү қ.Ө ң ө .
C # - қ қ ө.
Ққ қ,C # - - қ ғ қ ұ,қ ә ғ .
C # - қ ұ ү 2 ә қ.
Ø ң қ , ң
Ø Ә қ - ә қ ң қ .
, C # - ү қ:
public static int Main()
{ int d;
Console.WriteLine(d); //ұ !
//d қ ұ .
return 0;}
ұ ң Main() ә void int қ ұ. қ ғ ң,қ ө .
4. C#-ң қғ ә . Main() ә.
C#- ң :
ә
ң ғ: ә қғ , ә ә қ.
ә ң ғ қ.
C#-ң қғ ә
ʳ ү ә ү , қ ә қ ө.
ү
C# 8 - қғ ү қ:
CTS | |||
Sbyte | System.SByte | ң 8- ү | -128:127 (-2: 27-1) |
Short | System.Int16 | ң 16- ү | -32 768:32 767 (-215: 215-1) |
Int | System.Int32 | ң 32- ү | -20 147 483 648:2 147 483 647 (-231: 231-1) |
Long | System.Int64 | ң 64- ү | -9 223 372 036 854 775 808: 9 223 372 036 854 775 807 (-263: 263-1) |
Byte | System.Byte | ң 8- ү | 0:255 (0: 28-1) |
Ushort | System.Uint16 | ң 16- ү | 0:65 535 (0: 216-1) |
Uint | System.Uint32 | ң 32- ү | 0:4 294 967 295 (0: 232-1) |
Ulong | System.Uint64 | ң 64- ү | 0:18 446 744 073 709 551 615 (0: 264-1) |
ү
CTS | ң | |||
Float | System.Single | ә ү 32- | 1.5×10-45- 3.4×1038- | |
double | System.Double | ә ү 64- | 15/16 | 5.0×10-324- 1.7×10308- |
Float ү ә ғ.double ә Float қғ ә. ә Float ,ғ F қ :
5. -ғ ү System.Console- . .
|
|
-ғ ү System.Console-
қ ү Console.ReadLine() ә қ. ғ , қ қ . ң ә . :
Ø Console. Write() ә ;
Ø Console. WriteLine() , қ ә ң ң қ.
Console. WriteLine() қ C-ғ printf() ә ғ ү ө ұқ . Console. WriteLine()- қ ү .Ә ө ө ұ ғ {0} ғқ ө.:
Int i=10;
Int j =20;
Console.WriteLine({0} қ{1} ң{2}, i, j, i+j);
ұ ғ:
10 қ 20 ң 30
.
қ ө, қ ң ә . қ қ қ қғ ә ұ қғ .
қ # .
қң ққ қ ө:
# define ә #undef
# define #define DEBUG ү қ ә ұ ғ ғ (ң ғ DEBUG) .
#undef қ қ - ө:
#undef DEBUG
# define ә #undef C# ғқ ң ғ .
#if, #elif, #else ә #endif
ұ ғ ғ ғ қ .
#warning ә #error
ә ғ қ .
#region ә #endregion
#region ә #endregion ғ ұ қ ө
6. / / . ә ң ө қ/. ү.
.
ө ң , ғ . қ құ ү ө ү . new қ құ.
: Demo a = new Demo(); // Demo // Demo b = new Demo(); // Demo ң // қ әң , әң ө , қ. ѳ ә ү ( - heep) қ. a b ң қ. қғ ө ө, new OutOfMemory-Exception ғ . Ү ө ғ, ғ өң ә қ .
|
|
ә
b = c
ѳ қ ә- ұ,ң (b == c, қ a!= b ң ә ң ә null- ң ғ ғ ).
.
# ә ң қ ә қ ғ () : ә (value types) ә . ә ғ ө ғ .
ѳ әң ө , ң қ, ұғ ә -ң қ ғ қ (ү ғ) қ . ә қ , қ , құ ә () . ѳ , ө () ә .
ә ң ө қ/.
ә ұ. құғ ә қ. ғ, .
ү: , , ә . қ: -қ , ұ қ құ; ң қ ұ, ғ ; ұ қ , ұң құ; ғ қ ә .
7. ә ә . Әң ү. Ә
ә ә ғ қ ө . ғ қ ү ү ө ә ғ , құ, , ү . ғ , , , , ә ү құ . ү ө, , .
Ә
Ә әү ұ, қ ғ , ә , қң , қ ә .
() - қ ә ()
Ә
Ә ң ә ә ғ ұ. әң ғ қ , қ ғң қ ғ ү қғ . ғ,
Public bool IsSguare (Rectangle rect)
Return (rect.Heightrect.Width)
ұ қ ң қ ұ. NET,System,Drawing,Rectangle. ҳұ ө.
ә қ , қ void ө. қ ә өң ү, ө қ . ұ ә қ қ , ә қ қ ө ү қ. , ә әү ғ .
Public bool IsPositive (int value)
If (value <0)
Return false
Return true
Ә қ
, MathTest құ ә қ , қ ә қ ә қ.
Using System
Namespace Wrox
Class mainEntryPoint
Static void Main ()
қ қ ү
Console.WritLine(Pi +MathTest.Get.Pi());
|
|
Int x =MathTest.GetSguareOf (5)
Console.Write.Line (5 +x)
MathTest құ
MathTest =new MathTest ()
қ ә қ
Math.value=30
Console.writeLine (value ө math+math.value)
Console.WriteLIne(30 +math.GetSguare)
MathTest қ, class MathTest ә қ
Public int value
Public int GetSguare ()
Return value * value
Public static int GetSguareOf (int X)
Return x*x
Public static double GetPi () Return 3.14159
8. ә қ . қғ ұқ ө.
.
ң ә ү . new қ қ. .
қ:
қ ә ( void ) қ.
ң, ң ә ү ү әү , ү.
- ө ө , ә ө ө, ө null ә .
қ (ү) ғ .
using System;
namespace Listing5_6 {
class Demo
{
public Demo(int a, double y) //
{
this.a = a;
this.y = y;
}
int a;
double y;
}
class Class1
{ static void Main()
{
Demo a = new Demo(300, 0.002); // қ
Console.WriteLine(a.Gety()); // ә: 0,002
Demo b = new Demo(1, 5.71); // қ
Console.WriteLine(b.Gety()); // ә: 5,71
} } }
қ .
, қ ғ қ қ, ү ә ң ..қ ң ң , ң қ құң , ғ ғ ..NET қ қ , қ қ , ө ғ . қ ғ , қ қ ә ү .қ ө ә - ң қ. # қ ң қ ү .
қ қ ғ ү .
9. . қ . Object .
Partial () ө ,құ, қғ ү .
Partial ө ң, құң, ң .
ң ө ө қ:
-public
-private
-protected
-internal
-abstract
-sealed
-new
-
ө , Partial ө class ө ә .
қ
ңғ ө қ ң қ үң қ ғ ұқ ққ. қ ә қ ғ ұ , ө қ . ұ ң ө ү . static ө ғ ө қ, ұ қ қ қ үң қ . ғ қ . ұ қ, ң өң . қ ү :
Static class StaticUtilities
Ø {Public static void HelperMethod(){}}
HelperMethod() қ ү StaticUtilities ң қ . қ ң :
StaticUtilities.HelperMethod();
Object
ө, System.Object .NET ұ. ң қ ө, ү ң Object ұ ұ . ұ ө ұ қ , ө ұғ қ Object ұ ұ. (ғ ө құ ү ұ ү . Құ System.ValueType қ ұ ұ, ө System.Object қ ұ ұ.)
қ ә: қ ә қ ө, Object қғ ә қғғ ә қ . ұ ә қ қғ қ.
10. ұ. ұ . () ұ.
ғғ ұң ү : ұ ә ұ.
1. ұ қ қ ү ү ұ ү. ұ қ ң ә . ұң ұ қ қ қ ү қ ғ . қ, қ қ қ ө ө ұғ қғ қ.
2. ұ. ұ ұ. қ қ ұ. ұң ұ құғ қ ү қғ қ .
# ұ ұ ғ . ұ ң # құғ қ .
Құ system.ValueType қ ұ. ө ұ .
әқ ңғ ұ. қ ө ұ .
ұ. қ ұ ү қ:
Class ұғ :қ
{ // ә-ү ә -ү }
ұ, қ ң ә ң ү қ :
Public class MyDerivedClass: MyBaseClass, IInterface1, IInterface2
{// ә ..}
қ қ ө , C# қ System.Object . ө :
1) Class Myclass: Object // System.Object қ ұ.
{// ә ..}
2) Class Myclass: Object // System.Object қ ұ.
{// ә ..}
Object ғ ү Object ө қң, Visual Studio.NET қ.
11. ұ.ұқ . .
public ә private ә ө қ . қ ң virtual ә abstract қ . C# ң ө ү қғ.
ұқ .
Public | ө. |
Protected | ө. |
Internal | құ ө. |
Private | ө ө. |
Protected interval | құ ә ө |
қғ , қ үң ә ұқ .
қ қ
New | ұ ү ұғ ү ө |
Static | ұ ү қ |
Virtual | ү ұ қ ү. |
Abstract | қғ қ ү |
Override | қ ң ұғ ү қ қ. |
Sealed | қ ғ ұғ . |
Extern | ұ ү қ ,қ ү ғ |
ғғ қ ғ # ққ. IDisposable ққ.
Dispose() ә ғ , IDisposable , C# IDisposable ұ .
қ ә .
ү ү I ә .
12. . ғ . ғ құ.
ұ . ө -ң ө:
class _<_-ң_> { //...
қ ө:
_<_-ң_> _ =
new _< _-ң_ > (_-ң_);
:
namespace ConsoleApplication1
// құқ
class MyObj<T>
{
T obj,
public MyObj(T obj)
{
this.obj = obj;
}
public void objectType()
{
Console.WriteLine(" : " + typeof(T));
}
}
//
class MyObjects<T, V, E>
{
T obj1;
V obj2;
E obj3;
public MyObjects(T obj1, V obj2, E obj3)
{
this.obj1 = obj1;
this.obj2 = obj2;
this.obj3 = obj3;
}
public void objectsType()
{
Console.WriteLine("\n 1: " + typeof(T)+
"\n 2: " + typeof(V) +
"\n 3: " + typeof(E));
}
}
class Program
{
static void Main()
{
// int ғ -ң
MyObj<int> obj1 = new MyObj<int>(25);
obj1.objectType();
MyObjects<string, byte, decimal> obj2 = new MyObjects<string, byte, decimal>("Alex",26,12.333m);
obj2.objectsType();
Console.ReadLine();
}
}
}
MyObj ү ө, /, int / string, #- құғ() .. құ. .қ., MyObj<int> қ құғ . /, , MyObj<T> қ ғ -. қ құғ , /ғ MyObj<int>, қ құ. MyObj<T> қ , #- қ құғ, / int әә қ , ө.
қ / ө қ / ғ . қ қ . Құғ - -ң қ ө ғ .
ғ ғ. ұқ ү: MyObj -ң ә қ ү- -қ ғ , ғ - ө, қ. MyObj ? ү MyObj қ қ -ң қ қ .
13. ғ құ. ғ .
ғ құ
ғ құ C#-ң қ ө қ ү. , ғ null ә ү . ұ ғ default ө қғ . ғ Object ң құ қ , қ қ ә қ , қғ .
Құ қ ә ү қ. ң DocumentManager құ ә DocumentManager<T> қ.
using System;
using System.Collections.Generic;
namespace Wrox.ProCSharp.Generics
{ public class DocumentManager<T>
{ private readonly Queue<T> documentQueue=new Queue<T>();
public void AddDocument (T doc)
{ look (this)
{ documentQueue.Enqueue(doc); }
}
public bool IsDocumentAvaiable
{ get {return documentQueue.Count>0;}
}
}
}
Ү ә
DocumentManager<T> GetDocument() ә қ . ұ әң null- . қ ғ null- . Ө, ғ әң құғ ү , null ғ . ʳ ө қ null ә , ә 0.
public T GetDocument()
{T doc=default(T);
lock (this)
{ doc=documentQueue.Decueue();}
return doc;}
ғ қ ғ ә қ , қ . DocumentManager<T> ғ қ құң қ DisplayAllDocuments() ә ө . Document Title ә Content құ Idocument ү .
public interface Idocument
{ string Title {get; set;}
string Content{get; set;}
}
public class Document: Idocument{
public Document()
{
}
public Document (string title, string content)
{this.title=title;
this.content=content;}
public string Title{get;set;}
public string Content{get; set;}
}
14. ғ . ғ ә.
ғ ғ ғ ө ұқ .қ ұ қ .
Nullable<T>. Net Framework ғ ң . қ ә әү қ null .C # null .
Nullable<T> ғ қ. ғ қ ,қғ Nullable<T>,hasvalue қ ұ, ғ ә ә null ғ ң қ. ғ hasvalue ә value қ ү қ қ.ә қ. Nullable<T> - ғ ң ү қ, қ hasvalue falseқ ң ,өң .Nullable<T> - ғ ң ү қ , әқ ғ:
public struct Nullable<T> where T:struct
{ public Nullable(T value)
{ this.hasvalue=True;
this.value=value;}
private bool hasvalue;
public bool Hasvalue
{get
{return hasvalue;}}
private T vaue;
public T Value{
get if(!hasvalue)
{throw new InvalidOperation (no value);}
return value;}}
public static explicit operator T(Nullable<T> value)
{return value.Value;}
public static implicit operator Nullable<T>(T value)
{return new Nullable<T>(value);}
Public overridge string ToString()
{if (!HasValue)
return String.Empty;
return this.value.Tostring();}}
ұ , Nullable<T> ө Nullable<int> құғ. int қғ .ғ ғ ғ қғ .ұ ә Nullable<T> қ ү .
ғ ә.
ғ ә ғ, ғ қ. ғ ә ғ қ .
Swap <T> ә - ғ қ:
void swap <T>(ref T x,ref T y)
{t temp,temp=x;
temp =x;
x=y;
y=temp;}
ғ ә қ ң ғң қ ү:
int i=4;
int=5;
Swap <int >
(ref I,ref j))};
15. ә . . өө .
ұ ү ә қғ . C # ғ ә қ ү қғ. ұ ң ә қ ә ө Array қ. Қ (enoumerator) ө ң қ ү . қ,.NET 4- ү қ Tuple () ң .
Қ
ұ қғ .
ң құ ә құ.
ң
- ң қ қ ң қ . , қ ү .
int [] myArray;
ң
ң қ қ . , ү ү . ұ ө new ө . ө ү ғa :
myArray = new int [4];
myArray ү 4 ә . ә , ө ә ғ :
Int [] myArray = new int [4];
ө ң ә ә . ғ , қ.
Int []myArray = new int [4] {4, 7, 11, 2};
ә қ қ ң, ө ө , ө қ.
Int []myArray = new int {4, 7, 11, 2);
C # қ ққ . ә ү қ қ ғ . ә ғ .
Int []myArray = {4, 7, 11, 2};
ұқ
ғ , ң (indexer) қғ . қ ү қ. ұқ ү ғ . ү ә 0 . ғ ң ү ң қ 1- қғ ң, , ғ. myArray ө ү ә .
16. ҳ . Array .
ҳ . ө ұ . ҳ ө қғ ұ. ұ ә ө ө ү. ө 33 ө 1 2, 2 6, 3 3 ұ ө.
ҳ қ қ ө ө. ғ / қ қ қ, ң ң әү ү. ,
Int [] [] jagged = new int [3] [];
jagged [0] = new int [2] {1, 2};
jagged [1] = new int [6] {3, 4, 5, 6, 7, 8};
jagged [2] = new int [3] {9, 10, 11};
Array . құғ, ө, ә ұғ ғ ә , ғ CLR қ қ қ қ.
ұ
System.Object
System.Array
қ System.Array ң ұ . System.Array қ ұ: ICloneable, IList, ICollection, Enumerable, ә ң қ қ ә қ. Object ө ң қ ә қ, өң қ ә .
17. . ҳ .
ә ә ә ғ .ң қ ү қ қ . GetPersons() ә ө:
Static Person[ ] GetPersons ()
{
Return new Person [ ] {
New Person { FirstName=Damon, LastName=Hill},
New Person { FirstName=Niki, LastName=Lauda},
New Person { FirstName=Ayrton, LastName=Senna},
New Person { FirstName=Graham, LastName=Hill}
};
}
ң ү қ, DisplayPersons ():
Static void DispayPersons (Person [ ] persons)
{
//
ң ғ
ң ғ ұ ң қ қ ә ң .:
Static void DisplayArray (object [ ] data)
{
//
}
ң ғ ү ғ ү.
ң қ ә ә ә қ. қ ү қ , ө GetPersons():
staticPerson[]GetPersons()
returnnewPerson[](
newPerson(FirstName=Damon,LastName="Hill),newPerson(FirstName=Niki,LastName=Lauda),newPerson(FirstName=Ayrton",LastName=Senna},newPerson(FirstName="Graham",LastName="Hill"}
,ң ә DisplayPersons():
staticvoidDisplayPersons(Person[]persons)
ҳ
Foreach ң ө ң ң ң қ ғ . Loreach ө қ(enumerator).6.7 Loreach қғ ә ғ қ қ ө.GetEnumerator()-ңө IEnumerable қ құ.
IEnumerator To reach ә IEnumerator ә ү қң қ қ. ұ ң қ ұқ IEnumerator <T>, IDisposable ң ғ,қ ө ү ө ө ү Dispose ә қ.
қ ә foreach ң ү қ:
Public void HelloWorld()
{var helloCollection=new HelloCollection();
Foreach(string s in helloCollection)
{Console.WriteLine(s);
} }
ғ yield ү,ң қ, ң қ ө.Yield ғ Inumerator ә IDisposable әң құ. yield ң Enuterator ө.GetEnuterator ә құ ә yield қ. Yield ң қ state қ ә әқ ө, MoveNext() ә қғ . MoveNext() Ә ғ ң ң ғ ә ң құ қ,қғ ә .
ң ү ә қ қ
ө ә қ ғ yield қ ң ә қғ ә ңқ. MusicTitles GetEnumerator() ә қ қ, Reverse () ә қ ә Subset () ә ө ғ ө.
18. . Құқ .
ү, ә ү ү ..NET 4- ң қ қ Tuple ә Tuple қ қғ. Ә ү ө қ Tuple ә ү : Tuple<T1> , Tuple<T1, T2> ұ.
Divide() ң үң қ ө - Tuple<int,int>. ң үң қ, ң ғ ү . Tuple қ ң Create() қ ғ. Create() ң ң қ ң қ. Қ ғ ө ә қ ү result ә reminder .
Public static<int,int> Divide(int dividend, int divisor)
{
Int result=dividend/divisor;
Int reminder=dividend%divisor;
Return Tuple.Create<int,int>(result,reminder);
}
Divide() қ ө. Item1 ә Item2 қ ғ қ.
Var result=Divide(5,2);
Console.WriteLine(resultat delenia: {0}, ostatok:{1},
Result.Item1, result.Item2);
ө ғ ғ Tuple ң қ қғ . ң ө ңғ TRest . ғ .
ұ қ ө ү :
Public class Tuple<T1,T2,T3,T5,T6,T7,TRest>
ұ ң ңғ ң , ө ғ :
Var tuple=Tuple.create<string,string,string,int,int,int,double,
Tule<int,int>>(Stephanie,Alina,Nagel,2009,6,2,1,37,
Tuple.create<int,int>(52,3490));
Құқ
қ IStructuralEquatable ә IStructuralComparable ү . ұ .NET 4- ә ғ , қ ң ғ ү . қ ү , қ қ ң . IstructuralEquatable ң қ, IstructuralComparable ү . e IstructuralEquatableң қ ө, lEquatable Person құғ. ұ FirstName ә LastNAme Equals() ғ қ:
19. C#-ғ . ә ә ұ .
C# :
қ | + - * / % |
қ | & | ^ ~ && ||! |
Ө | + |
ә | ++ -- |
<<>> | |
==!=<><= => | |
= += -= *= /= %= &= |= ^= <<= >>= | |
ү қ | . |
[] | |
() | |
құ | New |
қ | Size of tupe as |
C# қғ ғ . C# қ қ (=) ә (==) . ү ң :
x=3; ғң , қ ң == қң :
If (x==3)
{
}
C# ұ ң қ ққ қ:
(?:) , ifelse ң ққғ ү. ү ұ . ұ ғ, ң. ұ ұ қ.
Chiced unChiced . ұ қң қ ғ .
Is ә .
as ѳ қ ү ғ .
20. әң қ ө ө. Қ(boxing) құ ә ө.
Қ ұ ә- ө. ө int object - қ:
1.int x = 9;
2.
3.object obj = x;// int -ң қ
ң ү ң. Console.WriteLine() қғ start object- , / WriteLine() ү 2- object . ѳ (ғ қ) ұ ө ң, / object қғ ү қ . ғ object қ ө .
, ә- қ, - Point ү ң "қ" құ, Point ң құ қ / ғ Point-ң ә ө. Қ ү(heap) . / object қ ұ . ұ WriteLine()-ғ , Point қғ ү ToString() қ / ғ ғ:
Start: (5, 5)
ә- object ү қ ү.
Қғ - ә- ә ү ( (unboxing)) қ.
int v = 123;
object o = v; // int -ң
int v2 = (int) o; // ә- int-
v ә ү ә ү қ. int қ . ұ ө:.
ғ - қғ ә . қ ө ү ( ү ):
object o = 15;
short s = (short) o; // қ: short құ қ
short t = (short)(int) o; // ұ ұ
21. ѳ қ ө. қ ү ү.
ѳ қ ө
C#- қ ққ, ғ ғ қ қғ