Professional Documents
Culture Documents
hu
1. TARTALOMJEGYZÉK
1. Tartalomjegyzék ............................................................................................................. 2
2. A C# bemutatása ............................................................................................................. 4
2.1. A C# .......................................................................................................................................... 4
2.2. JITting ....................................................................................................................................... 4
2.3. CLS ........................................................................................................................................... 4
2.4. A “Hello World” Kód ............................................................................................................... 5
2.5. Típusok ..................................................................................................................................... 6
2.5.1. Megszámlálható Típusok ............................................................................................................ 6
Sbyte ............................................................................................................................................................................. 6
Byte ............................................................................................................................................................................... 6
Short.............................................................................................................................................................................. 6
Ushort............................................................................................................................................................................ 6
Int .................................................................................................................................................................................. 6
Uint ............................................................................................................................................................................... 6
Long .............................................................................................................................................................................. 6
Ulong ............................................................................................................................................................................ 6
2.5.2. Bool Típus .................................................................................................................................. 6
2.5.3. Char Típus .................................................................................................................................. 6
2.5.4. Lebegőpontos Típus .................................................................................................................... 6
Float .............................................................................................................................................................................. 6
Double........................................................................................................................................................................... 6
2.5.5. Decimal Típus............................................................................................................................. 6
Decimal ......................................................................................................................................................................... 6
2.5.6. Struct Típus................................................................................................................................. 6
2.5.7. Enum Típus ................................................................................................................................. 7
2.5.8. Object Típus ............................................................................................................................... 7
2.5.9. Class Típus ................................................................................................................................. 7
2.5.10. Interface ..................................................................................................................................... 7
2.5.11. Delegates .................................................................................................................................... 8
2.5.12. String Típus ................................................................................................................................ 8
2.5.13. Tömbök ....................................................................................................................................... 8
2.5.14. Boxing és Unboxing.................................................................................................................... 8
2.6. Class .......................................................................................................................................... 8
2.6.1. Constructor és destructor ........................................................................................................... 8
2.6.2. Metódusok .................................................................................................................................. 9
In Paraméterek .............................................................................................................................................................. 9
Ref Paraméterek ............................................................................................................................................................ 9
Out Paraméterek ............................................................................................................................................................ 9
Override ........................................................................................................................................................................ 9
Hide............................................................................................................................................................................... 9
2.6.3. Példa Class ............................................................................................................................... 10
2.6.4. Class Property-k és Indexer-k .................................................................................................. 10
2.6.5. Attribútumok ............................................................................................................................. 11
2.6.6. Event ......................................................................................................................................... 11
2.6.7. Modifikátorok alkalmazása ...................................................................................................... 12
Class Modifikátorok .................................................................................................................................................... 12
Member Modifikátorok ............................................................................................................................................... 12
Access Modifikátorok ................................................................................................................................................. 12
2.7. Vezérlő Kulcsszavak............................................................................................................... 13
2.7.1. Szelekciós Kulcsszavak ............................................................................................................. 13
If Kulcsszó .................................................................................................................................................................. 13
Switch Kulcsszó .......................................................................................................................................................... 13
2.7.2. Iterációs Kulcsszavak ............................................................................................................... 14
For Kulcsszó ............................................................................................................................................................... 14
Foreach Kulcsszó ....................................................................................................................................................... 14
While Kulcsszó............................................................................................................................................................ 14
Do Kulcsszó ................................................................................................................................................................ 14
Forrás: http://www.doksi.hu
2. A C# BEMUTATÁSA
2.1. A C#
• Egyszerű Nincs pointer
Nincs ::, ., ->
Egységes típus rendszer
Boxing/Unboxing támogatás
If csak Boolean lehet
Ismétlődések kizárása (const <-> #define, stb.)
• Verziók Komponensek
Verzió számozás
2.2. JITting
• JIT Erősen optimalizált IL kód, lassú fordítás
• EconoJIT Gyors konverzió, kód cache
• PreJIT Ellenőrzött natív kód
2.3. CLS
• Primitív Típusok Bool, char, byte, short, int, long, float, double, string, object
• Interface-k Öröklődés
Class HelloWorld
{
public static int Main(string[] args)
{
Console.WriteLine(“Hello Világ!”);
}
}
Using System;
Class InputOutput
{
public static void Main()
{
Console.Write(“Név ?”);
String strName = Console.ReadLine();
Console.Write(“Hello ” + strName);
Console.Write(“Hello {0}”, strName);
}
}
Részletes, futó kódok a Komponensek építése részben találhatóak (3.1 és 3.2)
Forrás: http://www.doksi.hu
\’ ’
\” ”
\\ \
\0 Null
\a Alert
\b Backspace
\f Form feed
\n New line
\r Carriage return
\t Horizontal tab
\v Vertical tab
class Test
{
Forrás: http://www.doksi.hu
struct Point
{
double X;
double Y;
void MoveBy(double dX, double dY)
{ X+=dX; Y+=dY; }
}
2.5.10. Interface
Interface Iface
{
void ShowMyFace();
int a {get;set}
}
class Cface:Iface
{
public void ShowMyFace()
{
// Implementation
}
int a
{
// Implementation
}
}
class Test
Forrás: http://www.doksi.hu
2.5.11. Delegates
Lásd Events (2.6.6)
2.5.13. Tömbök
String[] arrArray = { "C", "C++", "C#" };
int[] SingleDim;
int[,] TwoDim;
int [][] Jagged;
// Boxing
object BoxedValue = Value;
// Unboxing
Value = (double)BoxedValue;
2.6. Class
class TestClass
{
public TestClass(): base {} // default
}
Forrás: http://www.doksi.hu
class TestClass
{
public TestClass(string strName, int nAge) { ... }
public ~TestClass() { Release(); } // destructor, called by GC !
public Release() { ... } // call it by hand
}
2.6.2. Metódusok
In Paraméterek
public int Square(int n) { ... }
Console.WriteLine(Square(25));
// object always pass a reference by value
Ref Paraméterek
void Square(ref int n) { ... }
int n = 20; // init
Square(ref n);
Console.WriteLine(n);
Out Paraméterek
void Square(int n, out int nn) { ... }
int n; // noninit
Square(20, out n);
Console.WriteLine(n);
Override
class Triangle
{
public virtual double Area(int a, int b, int c)
{
}
}
class RightTriangle:Triangle
{
public override double Area(int a, int b, int c)
{
return(base.Area(a, b, c));
}
}
class Test
{
public static void Main()
{
RightTriangle Instance = new RightTriangle();
Console.WriteLine(((Triangle)Instance).Area(3,4,7));
}
}
Hide
class Triangle
{
public double Area(int a, int b, int c)
{
}
}
class RightTriangle:Triangle
{
new public double Area(int a, int b, int c)
Forrás: http://www.doksi.hu
class Test
{
public static void Main()
{
RightTriangle Instance = new RightTriangle();
Console.WriteLine(((Triangle)Instance).Area(3,4,7));
}
}
public Person()
{
}
class Test
{
public static void Main()
{
Forrás: http://www.doksi.hu
2.6.5. Attribútumok
// Transaction logic
[Transaction(TransactionOption.Required)]
class MyBusinessComponent { ... }
// custom attributes
// Typeof(<object-expr>).GetCustomAttributes();
2.6.6. Event
// forward declaration with delegation
public delegate void EventHandler(string strText);
class EventSource
{
public event EventHandler TextOut;
class TestApp
{
public static void Main()
{
EventSource evsrc = new EventSource();
Access Modifikátorok
• Public Mindenhonnan elérhető
Forrás: http://www.doksi.hu
chLetter = Char.FromString(args[0]);
if (Char.IsDigit((chLetter = args[0][0])))
Console.WriteLine('{0} is digit",chLetter);
return 0;
}
}
Switch Kulcsszó
switch (nInt)
{
case 6:
// something
// won't drop to case 1, coz there is code at case 6
case 1:
break;
default:
// code
case 7:
case 2:
// here 7 will drop to 2, coz there is no code at case 7
goto case 3;
// this line is unreachable
case 4:
goto default;
}
String is használható !
Forrás: http://www.doksi.hu
// code
if (condition2)
{
break;
// terminate loop
}
// code
}
Foreach Kulcsszó
// foreach (Type identifier in expression) { ... }
class EnviromentDumpApp
{
public static void Main()
{
Idictionary envvars = Environment.GetEnvironmentVariables();
unchecked { ... }
try
{
checked
{
for (;nCurDig <= nComputeTo; nCurDig++)
nFactorial *= nCurDig;
}
bAllFine = true;
}
finally
{
if (!bAllFine)
Console.WriteLine("{0} caused", nComputeTo, nFactorial);
Else
Console.WriteLine("{0}! is {1}", nComputeTo, nFactorial);
}
Catching : try-catch-finally
class CatchIT
{
public static void Main()
{
try
{
int x = 10/0;
}
catch(DivideByZeroException divEx)
{
}
Forrás: http://www.doksi.hu
public RequestWebPage() { }
if (m_strURL == "")
throw new ArgumentException("URL must be provided");
WebRequest theRequest =
(WebRequest) WebRequestFactory.Create(m_strURL);
WebResponse theResponse = theRequest.GetResponse();
int BytesRead = 0;
Byte[] Buffer = new Byte[BUFFER_SIZE];
strContent = strResponse.ToString();
}
}
string strResult;
try
{
wrq.GetContent(out strResult);
}
catch (Exception e)
{
Console.WriteLine(e);
return;
}
Console.WriteLine(strResult);
}
}
3.2. Namespace
namespace CSharp
{
public class WhoisLookup
{
public static bool Query(string strDomain, out string
strWhoisInfo)
{
const int BUFFER_SIZE = 128;
if (strDomain == "")
throw new ArgumentException(
"Domain must be provided");
if (tcpc.Connect("whois.networksolutions.com", 43) != 0)
return false;
Stream s = tcpc.GetStream();
strDomain += "\r\n"
Byte[] bDomArr =
Encoding.ASCII.GetBytes(strDomain.ToCharArray());
s.Write(bDomArr, 0, strDomain.Length);
tcpc.Close();
strWhoisInfo = strWhoisResponse.ToString();
return true;
}
}
}
class TestWhois
{
public static void Main(string[] args)
{
string strResult;
bool bReturnValue;
try
{
bReturnValue = WhoisLookup.Query(args[0], out strResult);
}
catch (Exception e)
{
Console.WriteLine(e);
return;
}
if (bReturnValue)
Console.WriteLine(strResult);
else
Console.WriteLine("No Info");
}
}
#if RELEASE
// code1
#warning Warning
#error Error
#else
// code3
#endif
Kondícionális fordítás
#define DEBUG
using System;
class Info
{
[conditional("DEBUG")]
public static void Trace(string strMessage)
{
Console.WriteLine(strMessage);
}
[conditional("DEBUG")]
public static void TraceX(string strFormat, params object[] list)
{
Console.WriteLine(strFormat, list);
}
}
class TestConditional
{
public static void Main()
{
Info.Trace("Coool!");
Info.TraceX("{0} {1} {2}", "C", "U", 2001);
}
}
3.3.2. Dokumentáció
• <summary> Egyszerű leírás
• <para> Paragrafus
• <see cref/> Referencia másik elemre
• <seealso cref/> Lásd még
• <list> Lista
• <item> Elem
• <remarks> Megjegyzés
• <paramref> Referencia paraméterre
• <example> Leírás és példa
• <code> Példa kód
• <param> Paraméter
• <returns> Visszatérési paraméter
• <value> Property leírása
class TouchFile
{
public static void Main()
{
TouchIt ti = new TouchIt();
Try
{
bResult = ti.SetToCurrentTime("asptouch.cs");
}
catch(Exception e)
{
Console.WriteLine(e);
}
}
}
Late-bound object hívása
using System;
using System.Reflection;
class TestLateBound
{
public static void Main()
{
Type tTouch;
tTouch = Type.GetTypeFromProgID("AspTouch.TouchIt");
Object objTouch;
objTouch = Activator.CreateInstance(tTouch);
try
{
bResult = (bool)tTouch.InvokeMember("SetToCurrentTime",
BindingFlags.InvokeMethod, null, objTouch, parameters);
}
catch(Exception e)
{
Console.WriteLine(e);
}
if (bResult)
Console.WriteLine("Success !");
}
}
class TestPInvoke
{
[sysimport(dll="user32.dll", name="MessageBoxA"]
public static extern int PopupBox(int h, string m,
string c, int type);
Forrás: http://www.doksi.hu
class TestUnsafe
{
public static void Main()
{
int nResult = 0;
unsafe
{
fixed (int * pResult = &nResult)
{
SquareSampleUnsafe sqsu = new SquareSampleUnsafe();
sqsu.CalcSquare(15, pResult);
Console.WriteLine(nResult);
}
}
}
}
Forrás: http://www.doksi.hu
4.1.1. Funkciói
• Adminisztrátorok létrehozhatnak biztonsági beállításokat, amik bizonyos jogokat adhatnak bizonyos kód-
csoportoknak
• A kód megkövetelheti, hogy a hívó bizonyos jogokkal rendelkezzen
• Kód végrehajtás a futtató rendszer által korlátozható. Ellenőrzések történnek, hogy a hívó rendelkezik-e a
szükséges jogokkal a végrehajtáshoz
• Kód megadhatja, mik azok a jogok, amik szükségesek a futásához, mik azok, amik jók lennének, és mik
azok a jogok, amikkel soha nem szabad rendelkeznie
• A kód alapú biztonság a jogokat a komponens betöltésekor adja. Ez a folyamat a kód követelésein alapszik,
figyelembe véve a biztonsági beállításokat
4.1.2. Jogok
• Required Jogok, melyek szükségesek a kód futásához
• Refused Jogok, melyekről biztosítani szeretnénk, hogy a kód soha ne juthasson hozzájuk — még akkor
is, ha a biztonsági beállítások engedélyeznék. Lehetséges biztonsági lyukak kiküszöböléséhez
használatos
• SiteIdentityPermission Jogok, melyek alapjául az a web oldal szolgál, ahonnan a futatott kód
származik