<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>www.der-wirtschaftsingenieur.de &#187; C#</title>
	<atom:link href="http://www.der-wirtschaftsingenieur.de/index.php/category/ingenieursdisziplinen/informatik/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.der-wirtschaftsingenieur.de</link>
	<description>Portal für Wirtschaftsingenieure</description>
	<lastBuildDate>Tue, 31 Jan 2012 13:34:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>.NET-Code, unfreiwilliger Open Source?</title>
		<link>http://www.der-wirtschaftsingenieur.de/index.php/net-code-unfreiwilliger-open-source/</link>
		<comments>http://www.der-wirtschaftsingenieur.de/index.php/net-code-unfreiwilliger-open-source/#comments</comments>
		<pubDate>Sat, 01 Nov 2008 12:26:36 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Informatik]]></category>

		<guid isPermaLink="false">http://firedaxx.com/projectioneering/?p=94</guid>
		<description><![CDATA[Es ist schon ein wenig gewöhnungsbedürftig, der Gedanke, dass der gesamte Code eines mühevoll entwickeltes Programms quasi von jedem Nutzer eingesehen werden kann. &#8220;Open Source&#8221; ist an dieser Stelle sicherlich zu viel gesagt, denn nur weil der Quellcode (mit Hilfsmitteln) einsehbar ist, heißt dies nicht, dass dieser auch für andere Zwecke als die Programmausführung genutzt [...]]]></description>
			<content:encoded><![CDATA[<p>Es ist schon ein wenig gewöhnungsbedürftig, der Gedanke, dass der gesamte Code eines mühevoll entwickeltes Programms quasi von jedem Nutzer eingesehen werden kann. &#8220;Open Source&#8221; ist an dieser Stelle sicherlich zu viel gesagt, denn nur weil der Quellcode (mit Hilfsmitteln) einsehbar ist, heißt dies nicht, dass dieser auch für andere Zwecke als die Programmausführung genutzt werden darf.</p>
<p>Aber ein einsehbarer Quellcode erleichtert die Spionage, Piraterie und sonstigen Missbrauch enorm.<br />
Aber warum kann der Quellcode eines .Net Programms so einfach eingesehen werden? Das Problem existiert bei .NET Sprachen (und auch Java) zum einen, weil die Quellcode in eine Zwischensprache (IL, Intermediate Language) übersetzt wird und zum anderen an <a title="C# | Reflections" href="http://www.der-wirtschaftsingenieur.de/index.php/reflections-in-c-sharp/" target="_blank">Reflections</a>.</p>
<p>Die Zwischensprache ist bereits für den Menschen lesbarer als Assemblercode und lässt sich verhältnismäßig einfach wieder zurück übersetzen, denn die Zwischensprache spricht anders als Assemblercode nicht einzelne Register an (kann sie auch nicht, denn sie ist eine hardware-universelle Sprache), sondern liest sich auf einem viel höheren Abstraktionslevel.<span id="more-3292"></span></p>
<p><strong>Mit welchen Programmen kann man an den .NET Quellcode nur über eine ausführbare Datei gelangen?</strong><br />
Dafür gibt es das kostenlose Programm <em>Reflector for .NET</em> sowie das kostenpflichtige Programm <em>Remotesoft .NET Explorer</em>.</p>
<p>Auch ohne diese Programme ist es Fakt, dass der Quellcode von in .NET Sprachen geschriebenen Programmen mit genügend Aufwand zu dekompilieren ist. Wie kann sich ein Unternehmer davor schützen? Es ist ein ganz natürliches Interesse eines Unternehmers, dass ein teuer entwickeltes Programm mit neuesten Algorithmen nicht durch Quellcodespionage einfach nachgebaut werden kann.<br />
Ein wichtiger Aspekt ist hierbei allerdings die Usability, welche nicht (oder nur im unwesentlichen Umfang) unter dem Schutz vor Spionage leiden sollte. Der Programmbenutzer sollte von den Schutzmaßnahmen nichts merken und sich wegen diesen nicht einschränken müssen.</p>
<p>Das einfachste Verfahren zur Erschwerung des wirksamen Quellcodeverständnisses ist die <strong>Obfuskation</strong>. Hierfür gibt es bestimmte Programme. Die Entwickler können aber auch selbst eine gewisse (und vielleicht sehr effektive) Verwirrung hervorrufen, indem sie Methoden gezielt falsch benennen und falsche Fährten legen. Dies sollte aber erst nach kompletter Fertigstellung geschehen und erfordert eine gute Eigendokumentation.</p>
<p>Die weitergehende Variante ist die Kompilierung von .NET Quellcode in Maschinencode. Dies ist beispielsweise mit dem <strong>Salamander .NET Native Compiler</strong> der Firma Remotesoft möglich und hat den positiven Nebeneffekt, dass kein .NET Framework zur Ausführungszeit benötigt (aber dafür auf die Plattformunabhängigkeit verzichtet) wird.<br />
So ist der Quellcode dann etwa vergleichbar gut geschützt, wie z. B. in C geschriebener und kompilierter Code.</p>
<p>Weiterführende Webseiten:<br />
http://www.remotesoft.com &#8211; Remotesoft<br />
http://www.red-gate.com/products/reflector/ &#8211; .NET Reflector (kostenlos)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.der-wirtschaftsingenieur.de/index.php/net-code-unfreiwilliger-open-source/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Reflections in C#</title>
		<link>http://www.der-wirtschaftsingenieur.de/index.php/reflections-in-c-sharp/</link>
		<comments>http://www.der-wirtschaftsingenieur.de/index.php/reflections-in-c-sharp/#comments</comments>
		<pubDate>Sat, 11 Oct 2008 19:43:02 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.der-wirtschaftsingenieur.de/?p=167</guid>
		<description><![CDATA[Es kann zur Laufzeit eines Programms notwendig sein, dass das Programm sich selbst analysieren und ggf. verändern kann. In C# kann dies mir der Funktionalität von Reflections erreicht werden. Reflection „reflektiert" zur Laufzeit aufrufbare Assemblys, Module und Typen und bietet so dynamischen Zugriff auf Objekte, Methoden und Metadaten, welche erst zur Laufzeit bekannt werden. Eine [...]]]></description>
			<content:encoded><![CDATA[<p>Es kann zur Laufzeit eines Programms notwendig sein, dass das Programm sich selbst analysieren und ggf. verändern kann. In C# kann dies mir der Funktionalität von <strong>Reflections</strong> erreicht werden.</p>
<p><strong>Reflection</strong> „reflektiert" zur Laufzeit aufrufbare Assemblys, Module und Typen und bietet so <strong>dynamischen Zugriff auf Objekte, Methoden und Metadaten</strong>, welche erst <strong>zur Laufzeit</strong> bekannt werden.<br />
Eine Programmiersprache, welche diese Möglichkeit zur "Reflektion" der eigenen Programminhalte hat, kann ihre eigene Struktur selbst zur Laufzeit analysieren und auf sie Einfluss nehmen.</p>
<p><strong>Reflection</strong> als Methode zum dynamischen Aufruf von Klassen, Methoden, Variablen, Ressourcen usw. ist abgesehen von C# (und anderen .NET-Programmiersprachen) auch in Java bekannt und in noch einigen weiteren Programmiersprachen in ähnlicher Form verbreitet. Aber auch die <strong>Reflection-Funktionalität von Java und C# sind</strong> <strong>nicht absolut identisch</strong>. Während Java auf Klassenebene „reflektiert" und generell auch Klassen als Informationsempfänger verwendet, kommt C# nicht über Klassen, sondern über Assemblies an die gewünschten Informationen heran.</p>
<p>Grundsätzlich sollte der Einsatz von <strong>Reflections</strong> vermieden werden, denn der dynamische Aufruf ist wesentlich langsamer, als der direkte Aufruf. Insgesamt ist der Einsatz dieser Funktionalität mit übermäßigen Performance-Einbußen verbunden. Mit der Anwendung von <strong>Reflections</strong> erkauft sich der Programmierer Flexibilität zur Laufzeit, nimmt damit aber Leistungseinbußen in Kauf.<br />
Nur wenn ein dynamischer Aufruf unbedingt notwendig ist, gibt es für <strong>Reflections</strong> grundsätzlich keine Alternative. Beispiele für Anwendungsgebiete sind Schnittstellen für Plugins, der Debugger und auch die automatische Textvervollständigung in Entwicklungsumgebungen (z. B. Microsofts IntelliSense).<span id="more-167"></span></p>
<p>Um <strong>Reflections</strong> in C# nutzen zu können, sind Klassen aus dem Namespace <strong>System<span style="color: #000000;">.</span><span style="color: #000000;">Reflection</span></strong><span style="color: #33cccc;"> </span>notwendig, welcher daher in das Projekt eingebunden werden muss.</p>
<div class="igBar"><span id="lcsharp-14"><a href="#" onclick="javascript:showPlainTxt('csharp-14'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-14">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">using</span> <span style="color: #000000;">System</span>.<span style="color: #000000;">Reflection</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Auch die Klasse <strong>System.<span style="color: #33cccc;">Type</span></strong> ist für Reflections von ausschlaggebender Bedeutung. Sie steht im Namespace <strong>System</strong> zur Verfügung. <strong></strong></p>
<p><strong>System.<span style="color: #33cccc;">Type</span> </strong>repräsentiert u.a. Klassen, Interfaces und Wertetypen.</p>
<p>Um das Objekt eines Typen nach <strong>System.<span style="color: #33cccc;">Type</span></strong> zu erhalten, dienen der <strong><span style="color: #0000ff;">typeof()</span></strong> Operator bzw. zur Laufzeit eines Objekts die Instanzmethode [object]<strong>.GetType()</strong> als Typlieferanten. (Siehe hierzu <a title="typeof() vs getType()" href="http://www.der-wirtschaftsingenieur.de/index.php/2008/08/20/typeof-vs-gettype-in-csharp/">typeof() vs getType()</a>)</p>
<p>Das <strong>Type</strong>-Objekt stellt wichtige Informationen und Zugriffsmöglichkeiten, u.a. auf Methoden, Konstruktoren und Variablen bereit.</p>
<p>Von Interesse könnte für den Programmier beispielweise sein, wie die eigene Assembly heißt. Der Name der Assembly wird mit der Methode <em>myAssemblyInfo()</em> ausgegeben.</p>
<div class="igBar"><span id="lcsharp-15"><a href="#" onclick="javascript:showPlainTxt('csharp-15'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-15">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> myAssemblyInfo<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;Assembly myAssembly = <span style="color: #000000;">System</span>.<span style="color: #000000;">Reflection</span>.<span style="color: #000000;">Assembly</span>.<span style="color: #000000;">GetExecutingAssembly</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;Console.<span style="color: #000000;">Writeline</span><span style="color: #000000;">&#40;</span>myAssembly.<span style="color: #000000;">GetName</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #000000;">FullName</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Diese Methode kann in der neuen Klasse (z.B. im Konstruktor) aufgerufen werden.</p>
<div class="igBar"><span id="lcsharp-16"><a href="#" onclick="javascript:showPlainTxt('csharp-16'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-16">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">myAssemblyInfo<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p><strong>Ausgabe:</strong></p>
<blockquote><p>ReflectionExample, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null</p></blockquote>
<p>Ausgegeben wurden alle Informationen über den Namen dieser Assembly (Fullname). Die Assembly selbst heißt <em>ReflectionExample </em>(dies ist der Name des Projekts).</p>
<h2><strong>Reflections in C# an Hand eines Beispiels:</strong></h2>
<p>Die nachfolgende Klasse <em>Calculator</em> im Namensraum <em>Reflection_Test</em> soll mit Reflections analysiert werden.</p>
<p>Die Klasse <em>Calculator </em>hat eine Konstante und Eigenschaft (Property) sowie einige Methoden.</p>
<div class="igBar"><span id="lcsharp-17"><a href="#" onclick="javascript:showPlainTxt('csharp-17'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-17">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">using</span> <span style="color: #000000;">System</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">using</span> <span style="color: #000000;">System</span>.<span style="color: #000000;">Collections</span>.<span style="color: #000000;">Generic</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">using</span> <span style="color: #000000;">System</span>.<span style="color: #000000;">Text</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">using</span> <span style="color: #000000;">System</span>.<span style="color: #000000;">IO</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">using</span> <span style="color: #000000;">System</span>.<span style="color: #000000;">Windows</span>.<span style="color: #000000;">Forms</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">using</span> <span style="color: #000000;">System</span>.<span style="color: #000000;">Reflection</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">namespace</span> Reflection_Test</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">class</span> Calculator</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">const</span> <span style="color: #0600FF;">float</span> pi = <span style="color: #FF0000;color:#800000;">3</span>.14f;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">float</span> PI</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;get</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">return</span> pi;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> Calculator<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">int</span> DivideAndWrite<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">double</span> d<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">double</span> result = d / <span style="color: #FF0000;color:#800000;">2</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">int</span><span style="color: #000000;">&#41;</span>result;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> WriteText<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">string</span> text<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Console.<span style="color: #000000;">WriteLine</span><span style="color: #000000;">&#40;</span>text<span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> WriteText2<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">string</span> text<span style="color: #000000;">&#41;</span> <span style="color: #008200; font-style: italic;">// invisible, not able to reflect</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Console.<span style="color: #000000;">WriteLine</span><span style="color: #000000;">&#40;</span>text<span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> doThis<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008200; font-style: italic;">// do anything possible...</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Console.<span style="color: #000000;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">"We do this!"</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> doThat<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008200; font-style: italic;">// do anything (else) possible</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Console.<span style="color: #000000;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">"We do that!"</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Dazu wird eine neue Klasse aufgestellt, in welche je nach Bedarf nachfolgenden Methoden implementiert und aufgerufen werden können.</p>
<p>Wissenswert ist auch, welche Datei (und in welchem Pfad sie liegt) zu der Klasse Reflection_Class gehört.<br />
Die Methode myAssemblyFiles() gibt alle Dateien namentlich aus, welche zu dieser Klasse gehören.</p>
<div class="igBar"><span id="lcsharp-18"><a href="#" onclick="javascript:showPlainTxt('csharp-18'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-18">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> myAssemblyFiles<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;Assembly asm = Assembly.<span style="color: #000000;">GetAssembly</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #0600FF;">typeof</span></a><span style="color: #000000;">&#40;</span>Reflection_Class<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;FileStream<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> files = asm.<span style="color: #000000;">GetFiles</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>FileStream file <span style="color: #0600FF;">in</span> files<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; Console.<span style="color: #000000;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">"Filename: "</span> + file.<span style="color: #000000;">Name</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Nach Aufruf der Methode myAssembyFiles() in der neuen Klasse (z.B. im Konstruktor)...</p>
<div class="igBar"><span id="lcsharp-19"><a href="#" onclick="javascript:showPlainTxt('csharp-19'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-19">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">myAssemblyFiles<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>erfolgt folgende Ausgabe:</p>
<blockquote><p>Filename: C:\Dokument...daten\Temporary Projects\Reflection\bin\Debug\Reflection.exe</p></blockquote>
<p>Die Ausgabe zeigt den Namen der ausführbaren Datei mit absoluter Pfadangabe.</p>
<p>Eine weitere Methode namens getMetaDataByTypeName() bekommt einen Typennamen (Name einer Klasse) als Parameter und gibt die Namen aller Methoden aus.</p>
<div class="igBar"><span id="lcsharp-20"><a href="#" onclick="javascript:showPlainTxt('csharp-20'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-20">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> getMetaDataByTypename<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">string</span> typeName<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;Type ctype = Type.<span style="color: #000000;">GetType</span><span style="color: #000000;">&#40;</span>typeName<span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;MethodInfo<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> mis = ctype.<span style="color: #000000;">GetMethods</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>MethodInfo mi <span style="color: #0600FF;">in</span> mis<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; Console.<span style="color: #000000;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">"Methodename: '"</span> + mi.<span style="color: #000000;">Name</span>.<span style="color: #000000;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> + <span style="color: #000000;">"'"</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Diese Methode kann wie folgt aufgerufen werden:</p>
<div class="igBar"><span id="lcsharp-21"><a href="#" onclick="javascript:showPlainTxt('csharp-21'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-21">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">this</span>.<span style="color: #000000;">getMetaDataByTypeName</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">"Reflection_Test.Calculator"</span><span style="color: #000000;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Ausgabe:</p>
<blockquote><p>Methodename: 'get_PI'<br />
Methodename: 'DivideAndWrite'<br />
Methodename: 'WriteText'<br />
Methodename: 'doThis'<br />
Methodename: 'doThat'<br />
Methodename: 'GetType'<br />
Methodename: 'ToString'<br />
Methodename: 'Equals'<br />
Methodename: 'GetHashCode'</p></blockquote>
<p>Zu beachten ist hier, dass die private Methode writeText2() nicht gefunden wurde. Die get()-Methode der Eigenschaft PI wurde mitgezählt.</p>
<p>Eine andere Methode reflectProperty() greift auf den Wert der Eigenschaft PI zu und zeigt diesen Wert an.<br />
Dabei wird vor dem Zugriff mit der Eigenschaft <strong>CanRead</strong> der Instanz vom Typ <span style="color: #33cccc;"><strong>PropertyInfo</strong></span> auch geprüft, ob die Eigenschaft PI Lesezugriff bietet.</p>
<p>Die Methode reflectProperty() verlang hierzu die Klasse als Parameter, welche die Eigenschaft PI hat.</p>
<div class="igBar"><span id="lcsharp-22"><a href="#" onclick="javascript:showPlainTxt('csharp-22'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-22">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> reflectProperty<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">object</span> rClass<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;PropertyInfo propi = rClass.<span style="color: #000000;">GetType</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #000000;">GetProperty</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">"PI"</span>, <a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #0600FF;">typeof</span></a><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">float</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">null</span> != propi &amp;&amp; propi.<span style="color: #000000;">CanRead</span><span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; Console.<span style="color: #000000;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">"PI: "</span> + propi.<span style="color: #000000;">GetValue</span><span style="color: #000000;">&#40;</span>rClass, <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0600FF;">new</span></a> <span style="color: #0600FF;">object</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Aufgerufen wird diese Methode beispielsweise mit einer flüchtigen Instanz der Klasse Calculator.</p>
<div class="igBar"><span id="lcsharp-23"><a href="#" onclick="javascript:showPlainTxt('csharp-23'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-23">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">reflectProperty<span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0600FF;">new</span></a> Reflection_Test.<span style="color: #000000;">Calculator</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Ausgabe:</p>
<blockquote><p>PI: 3,14</p></blockquote>
<h3><strong>Reflektierte Methoden aufrufen</strong></h3>
<p>Häufiger Wunsch ist es, nicht nur Metadaten einer Assembly abzufragen, sondern auch bestimmte Methoden aufzurufen.</p>
<p>Dies geschieht mit der Instanzmethode des Objekts vom Typ <em>MethodInfo</em>.<br />
Die Methode <em>reflectAndInvokeMethode() </em>sucht nach Methoden nach ihren Namen und ruft sie nacheinander auf.</p>
<div class="igBar"><span id="lcsharp-24"><a href="#" onclick="javascript:showPlainTxt('csharp-24'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-24">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> reflectAndInvokeMethod<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">object</span> rClass, <span style="color: #0600FF;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> methodnames<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">string</span> methodname <span style="color: #0600FF;">in</span> methodnames<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; MethodInfo mi = rClass.<span style="color: #000000;">GetType</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #000000;">GetMethod</span><span style="color: #000000;">&#40;</span>methodname<span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">null</span> != mi<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mi.<span style="color: #000000;">Invoke</span><span style="color: #000000;">&#40;</span>rClass, <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0600FF;">new</span></a> <span style="color: #0600FF;">Object</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Aufgerufen wird die Methode beispielsweise wie folgt, dabei wird ihr eine flüchtige Instanz der Klasse <em>Calculator</em> sowie zwei Namen von Methoden übergeben.</p>
<div class="igBar"><span id="lcsharp-25"><a href="#" onclick="javascript:showPlainTxt('csharp-25'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-25">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">reflectAndInvokeMethod<span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0600FF;">new</span></a> Reflection_Test.<span style="color: #000000;">Calculator</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>, <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0600FF;">new</span></a> <span style="color: #0600FF;">String</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span> <span style="color: #000000;">"doThis"</span>, <span style="color: #000000;">"doThat"</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p><strong>Ausgabe:</strong></p>
<blockquote><p>We do this!<br />
We do that!</p></blockquote>
<h3><strong>Aufruf einer reflektierten Methode mit Rückgabewert</strong></h3>
<p>Eine "reflektierte" Methode liefert ihren Rückgabewert direkt von der Invoke-Methode zurück und kann einer Variable zugewiesen werden.</p>
<div class="igBar"><span id="lcsharp-26"><a href="#" onclick="javascript:showPlainTxt('csharp-26'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-26">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Type ctype = <a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #0600FF;">typeof</span></a><span style="color: #000000;">&#40;</span>Reflection_Test.<span style="color: #000000;">Calculator</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">MethodInfo<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> mi3 = ctype.<span style="color: #000000;">GetMethods</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>MethodInfo mi <span style="color: #0600FF;">in</span> mi3<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;Reflection_Test.<span style="color: #000000;">Calculator</span> Calculator = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0600FF;">new</span></a> Reflection_Test.<span style="color: #000000;">Calculator</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>mi.<span style="color: #000000;">Name</span>.<span style="color: #000000;">Equals</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">"DivideAndWrite"</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #008200; font-style: italic;">// Rückgabewert von reflektierter Methode empfangen</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">double</span> returnvalue = Convert.<span style="color: #000000;">ToDouble</span><span style="color: #000000;">&#40;</span>mi.<span style="color: #000000;">Invoke</span><span style="color: #000000;">&#40;</span>Calculator, <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0600FF;">new</span></a> <span style="color: #0600FF;">Object</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span> <span style="color: #FF0000;color:#800000;">5</span>.<span style="color: #FF0000;color:#800000;">0</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; Console.<span style="color: #000000;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">"Value: "</span> + returnvalue.<span style="color: #000000;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p><strong>Ausgabe:</strong></p>
<blockquote><p>Value: 2</p></blockquote>
<p><br /><br />
<center><script type="text/javascript"><!--
google_ad_client = "pub-4204488675913141";
/* der-wirtschaftsingenieur5 (horizontale Reihe) */
google_ad_slot = "6242293900";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></center></p>
]]></content:encoded>
			<wfw:commentRss>http://www.der-wirtschaftsingenieur.de/index.php/reflections-in-c-sharp/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Überladen von Methoden</title>
		<link>http://www.der-wirtschaftsingenieur.de/index.php/uberladen-von-methoden-in-c-sharp/</link>
		<comments>http://www.der-wirtschaftsingenieur.de/index.php/uberladen-von-methoden-in-c-sharp/#comments</comments>
		<pubDate>Tue, 26 Aug 2008 19:46:26 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.der-wirtschaftsingenieur.de/?p=191</guid>
		<description><![CDATA[Im Laufe eines Software-Projekts sind Algorithmen zu entwerfen und in Methoden zu schachteln, welche zwar prinzipiell das gleiche tun, aber mit Werten von unterschiedlichen Datentypen oder einer verschiedenen Anzahl an Werten. Für solche Fälle bietet C# die Möglichkeit zur Überladung von Methoden. Die meisten höheren Programmiersprachen erlauben überladene Methoden, neben C# beispielsweise auch C++ und [...]]]></description>
			<content:encoded><![CDATA[<p>Im Laufe eines Software-Projekts sind Algorithmen zu entwerfen und in Methoden zu schachteln, welche zwar prinzipiell das gleiche tun, aber mit Werten von unterschiedlichen Datentypen oder einer verschiedenen Anzahl an Werten. Für solche Fälle bietet C# die Möglichkeit zur Überladung von Methoden.</p>
<p>Die meisten höheren Programmiersprachen erlauben überladene Methoden, neben C# beispielsweise auch C++ und Java.<br />
Überladene Methoden haben zwei oder mehr Signaturen. Die Signatur ist die Kopfzeile einer Methode, welche die Zugriffsmöglichkeit, den Rückgabewert, den Methodennamen und die Parameter (Namen, Anzahl und Typ) beschreibt.<br />
Die Überladung bezieht sich in C# (und den meisten anderen Programmiersprachen auch) jedoch ausschließlich auf die Parameteranzahl und die Datentypen der Parameter, denn nur diese sind bei einem Methodenaufruf maßgebend für eine klare Abgrenzung der verschiedenen Ausführungen einer überladenen Methode.<span id="more-191"></span></p>
<p>Die Methode <strong><em>sumTwoValues()</em></strong> (tatsächlich wird hier für gewöhnlich von nur einer Methode gesprochen, obwohl es sich strukturell um zwei Methoden handelt) ist <strong>überladen</strong>.<br />
Werden der Methode zwei Werte des Typs Integer (<span style="color: #0000ff;"><strong>int</strong></span>) übergeben, wird entsprechend ein Wert vom Typ Integer zurückgeliefert.<br />
Sind die Parameter andernfalls aber Werte vom Typ <strong><span style="color: #0000ff;">double</span></strong>, liefert die Methode einen Wert vom Typ <strong><span style="color: #0000ff;">double</span></strong> zurück. Der Rückgabedatentyp und die Datentypen der Parameter entsprechen dem in der Signatur der Methode vorgegebenen Schema.</p>
<div class="igBar"><span id="lcsharp-32"><a href="#" onclick="javascript:showPlainTxt('csharp-32'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-32">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">int</span> sumTwoValues<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">int</span> a, <span style="color: #0600FF;">int</span> b<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">return</span> a + b;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">double</span> sumTwoValues<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">double</span> a, <span style="color: #0600FF;">double</span> b<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">return</span> a + b;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Da sich eine überladene Methode in ihren Ausführungen immer durch die Anzahl an Parametern oder den Datentypen der Parameter klar unterscheiden lassen muss, ist eine weitere Ausführung der Überladung der Methode <em><strong>sumTwoVariables()</strong></em> in beispielsweise folgender Form nicht möglich:</p>
<div class="igBar"><span id="lcsharp-33"><a href="#" onclick="javascript:showPlainTxt('csharp-33'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-33">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008200; font-style: italic;">// nicht möglich</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">int</span> sumTwoValues<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">double</span> a, <span style="color: #0600FF;">double</span> b<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">int</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#40;</span>a + b<span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Die überladene Methode <strong><em>pythagoras()</em></strong> unterscheidet zwei Signaturen, welche eine unterschiedliche Parameteranzahl haben. Die erste Ausführung der Methode <strong><em>pythagoras()</em></strong> hat gegenüber der zweiten einen dritten Parameter, welcher mit dem Schlüsselwort <a title="Referenzparameter (ref und out)" href="http://www.der-wirtschaftsingenieur.de/index.php/2008/03/25/wertetyp-vs-refernztyp/" target="_self"><strong><span style="color: #0000ff;">out</span></strong> als nicht initialisierter Refernezparameter</a> vorgegeben wird.<br />
Die zweite Ausführung von <em><strong>pythagoras()</strong></em> hat nur zwei Parameter und einen Rückgabewert. Diese Ausführung greift auf die erste Ausführung zurück und gibt das (durch die Referenz erworbene) Ergebnis zurück.</p>
<div class="igBar"><span id="lcsharp-34"><a href="#" onclick="javascript:showPlainTxt('csharp-34'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-34">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> pythagoras<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">double</span> a, <span style="color: #0600FF;">double</span> b, <span style="color: #0600FF;">out</span> <span style="color: #0600FF;">double</span> c<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;c = Math.<span style="color: #000000;">Sqrt</span><span style="color: #000000;">&#40;</span>a * a + b * b<span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">double</span> pythagoras<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">double</span> a, <span style="color: #0600FF;">double</span> b<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">double</span> c;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;pythagoras<span style="color: #000000;">&#40;</span>a, b, <span style="color: #0600FF;">out</span> c<span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">return</span> c;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Das Überladen von Methoden kann leider nicht immer angewandt werden, wie das folgende Beispiel zeigt:</p>
<p>Die Methode <em><strong>multiplyValues()</strong></em> multipliziert beliebig viele Parameter des Typs <strong><span style="color: #0000ff;">double</span></strong> und liefert das Ergebnis ebenfalls als <strong><span style="color: #0000ff;">double</span></strong> zurück.<br />
Das Schlüsselwort <span style="color: #0000ff;"><strong>params</strong></span> sorgt für eine beliebige Anzahl (ein Parameterarray) an Wertetypen im vorgegeben Datentypen (hier: <span style="color: #0000ff;"><strong>double</strong></span>). Das Parameterarray darf auch "leer" sein, wie der Aufruf von <em><strong>multiplyValues()</strong></em> (Rückgabewert: 1.0) beweist.<br />
Eine Methode kann nur einen Parameterarray haben und somit auch nur einmal das Schlüsselwort <span style="color: #0000ff;"><strong>params</strong></span> aufnehmen.<br />
Das Schlüsselwort <span style="color: #0000ff;"><strong>params</strong></span> ist nicht mit den Schlüsselwörtern <a title="Referenzparameter (ref und out)" href="http://www.der-wirtschaftsingenieur.de/index.php/2008/03/25/wertetyp-vs-refernztyp/" target="_self"><strong><span style="color: #0000ff;">ref</span></strong> und <span style="color: #0000ff;"><strong>out</strong></span></a> kombinierbar, ein Parameterarray ist somit immer ein Wertetyp.</p>
<div class="igBar"><span id="lcsharp-35"><a href="#" onclick="javascript:showPlainTxt('csharp-35'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-35">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">double</span> multiplyValues<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">params</span> <span style="color: #0600FF;">double</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> numbers<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">double</span> product = <span style="color: #FF0000;color:#800000;">1</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">double</span> number <span style="color: #0600FF;">in</span> numbers<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; product *= number;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">return</span> product;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p><strong>Überladung (weitere Ausführung hinzufügen):</strong><br />
Soll nun noch eine Ausführung der Methode <strong><em>multiplyValues()</em></strong> aufgestellt werden, welche nur genau zwei Werte vom Typ <strong><span style="color: #0000ff;">double</span></strong> als Parameter annehmen soll, dann steht diese, <span style="text-decoration: underline;">wenn die Methode mit genau zwei Parametern aufgerufen wird</span>, mit der Ausführung der Methode mit einer beliebigen Anzahl an Parametern in Konflikt.<br />
Es bestünde in diesem Fall keine Klarheit, welche Methode aufzurufen ist.</p>
<p>Aus diesem Grund müssen die Methoden verschieden benannt und somit die Überladung umgangen werden. Beispielsweise könnte die zweite Ausführung als eigene Methode wie folgt aussehen:</p>
<div class="igBar"><span id="lcsharp-36"><a href="#" onclick="javascript:showPlainTxt('csharp-36'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-36">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">double</span> multiplyTwoValues<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">double</span> a, <span style="color: #0600FF;">double</span> c<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">return</span> multiplyValues<span style="color: #000000;">&#40;</span>a, c<span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p><br /><br />
<center><script type="text/javascript"><!--
google_ad_client = "pub-4204488675913141";
/* der-wirtschaftsingenieur5 (horizontale Reihe) */
google_ad_slot = "6242293900";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></center></p>
]]></content:encoded>
			<wfw:commentRss>http://www.der-wirtschaftsingenieur.de/index.php/uberladen-von-methoden-in-c-sharp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>typeof() vs .GetType()</title>
		<link>http://www.der-wirtschaftsingenieur.de/index.php/typeof-vs-gettype-in-csharp/</link>
		<comments>http://www.der-wirtschaftsingenieur.de/index.php/typeof-vs-gettype-in-csharp/#comments</comments>
		<pubDate>Wed, 20 Aug 2008 17:59:57 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.der-wirtschaftsingenieur.de/?p=162</guid>
		<description><![CDATA[Um den Typen nach System.Type bestimmen zu können, werden entweder der Operator typeof() oder die Instanzmethode getType() verwendet. Diese beiden Möglichkeiten liefern ein Objekt vom Typ System.Type zurück. Oft wird die Frage gestellt, welcher Unterschied zwischen diesen beiden Möglichkeiten der Typ-Bestimmung besteht. Der typeof()-Operator kann nur das Objekt von System.Type eines Typen (eine Klasse) bestimmen. [...]]]></description>
			<content:encoded><![CDATA[<p>Um den Typen nach System.Type bestimmen zu können, werden entweder der Operator <span style="color: #000080;"><strong>typeof()</strong></span> oder die Instanzmethode <span style="color: #000000;"><strong>getType()</strong></span> verwendet. Diese beiden Möglichkeiten liefern ein Objekt vom Typ <strong>System.<span style="color: #008080;">Type</span></strong> zurück.</p>
<p>Oft wird die Frage gestellt, welcher Unterschied zwischen diesen beiden Möglichkeiten der Typ-Bestimmung besteht.</p>
<p>Der <span style="color: #000080;"><strong>typeof()</strong></span>-Operator kann nur das Objekt von <strong>System.<span style="color: #008080;">Type</span></strong> eines Typen (eine Klasse) bestimmen.</p>
<p>(Achtung: In C# sind auch die oft als <em>primitiven Typen</em> bezeichneten Wertetypen, z. B. Integer (int) oder Double (double) Klassen!)</p>
<p>Dabei kann der <strong><span style="color: #000080;">typeof()</span></strong>-Operator auch generische Typen bestimmen. Überladen werden, kann dieser Operator jedoch nicht.</p>
<div class="igBar"><span id="lcsharp-42"><a href="#" onclick="javascript:showPlainTxt('csharp-42'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-42">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">System</span>.<span style="color: #000000;">Type</span> type = <a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #0600FF;">typeof</span></a><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">double</span><span style="color: #000000;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p><strong>getType()</strong> ist eine Methode einer Instanz (eines Objekts). <strong>getType()</strong> kann daher nicht frei verwendet werden, wie der <span style="color: #000080;"><strong>typeof()</strong></span>-Operator, sondern ist an eine Instanz gebunden. <strong>getType()</strong> liefert den Typen, der eigenen Instanz zurück.</p>
<div class="igBar"><span id="lcsharp-43"><a href="#" onclick="javascript:showPlainTxt('csharp-43'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-43">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">double</span> d = <span style="color: #FF0000;color:#800000;">5</span>.<span style="color: #FF0000;color:#800000;">4</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">System</span>.<span style="color: #000000;">Type</span> type = d.<span style="color: #000000;">GetType</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p><strong>getType() </strong>kann im Gegensatz zu <span style="color: #000080;"><strong>typeof()</strong></span> auch überladen werden. <strong>GetType()</strong> kann allerdings nicht (<span style="color: #000080;"><strong>typeof()</strong></span> natürlich auch nicht) überschrieben werden.</p>
<p>Auf den Punkt gebracht: Zur Bestimmung von Typen von Klassen wird der Operator <span style="color: #000080;"><strong>typeof()</strong></span> benutzt. Die Bestimmung von Typen von Objekten ist hingegen die Methode <strong>getType()</strong> zu benutzen.<br />
<span id="more-162"></span><br />
Nachfolgend eine kleine, vergleichende Veranschauung. Es soll der Typ <strong><span style="color: #008080;">Int32</span></strong> festgestellt werden.</p>
<div class="igBar"><span id="lcsharp-44"><a href="#" onclick="javascript:showPlainTxt('csharp-44'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-44">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">int</span> number = <span style="color: #FF0000;color:#800000;">4</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Type<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> types = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0600FF;">new</span></a> Type<span style="color: #000000;">&#91;</span><span style="color: #FF0000;color:#800000;">3</span><span style="color: #000000;">&#93;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">types<span style="color: #000000;">&#91;</span><span style="color: #FF0000;color:#800000;">0</span><span style="color: #000000;">&#93;</span> = <a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #0600FF;">typeof</span></a><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">int</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008200; font-style: italic;">// types[1] = typeof(number);&nbsp; &nbsp; // Not possible!</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">types<span style="color: #000000;">&#91;</span><span style="color: #FF0000;color:#800000;">2</span><span style="color: #000000;">&#93;</span> = number.<span style="color: #000000;">GetType</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">int</span> i = <span style="color: #FF0000;color:#800000;">0</span>; i &lt;types.<span style="color: #000000;">Length</span>; i++<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">null</span> != types<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; Console.<span style="color: #000000;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">"Type["</span> + i.<span style="color: #000000;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> + <span style="color: #000000;">"]: "</span> + types<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #000000;">Name</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p><strong>Ausgabe:</strong></p>
<blockquote><p>Type[0]: Int32<br />
Type[2]: Int32</p></blockquote>
<p>Es wurde mit <span style="color: #000080;"><strong>typeof()</strong></span> genauso wie mit <strong>getType()</strong> der <strong>System.<span style="color: #008080;">Type Int32</span></strong> festgestellt.</p>
<p>Ein anderes Beispiel mit einer eigenen Klasse, <span style="color: #008080;"><strong><em>TestClass</em></strong></span>:</p>
<div class="igBar"><span id="lcsharp-45"><a href="#" onclick="javascript:showPlainTxt('csharp-45'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-45">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">class</span> TestClass</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> TestClass<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Es wird versucht, das Objekt von System.Type für diese Klasse <em><strong><span style="color: #008080;">TestClass</span></strong></em> zu ermitteln:</p>
<div class="igBar"><span id="lcsharp-46"><a href="#" onclick="javascript:showPlainTxt('csharp-46'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-46">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">TestClass tc = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0600FF;">new</span></a> TestClass<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Type<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> types = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0600FF;">new</span></a> Type<span style="color: #000000;">&#91;</span><span style="color: #FF0000;color:#800000;">3</span><span style="color: #000000;">&#93;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008200; font-style: italic;">// types[0] = typeof(tc);&nbsp; &nbsp; &nbsp; &nbsp; // Not possible!</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">types<span style="color: #000000;">&#91;</span><span style="color: #FF0000;color:#800000;">1</span><span style="color: #000000;">&#93;</span> = <a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #0600FF;">typeof</span></a><span style="color: #000000;">&#40;</span>TestClass<span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">types<span style="color: #000000;">&#91;</span><span style="color: #FF0000;color:#800000;">2</span><span style="color: #000000;">&#93;</span> = tc.<span style="color: #000000;">GetType</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">int</span> i = <span style="color: #FF0000;color:#800000;">0</span>; i &lt;types.<span style="color: #000000;">Length</span>; i++<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">null</span> != types<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp;Console.<span style="color: #000000;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">"Type["</span> + i.<span style="color: #000000;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> + <span style="color: #000000;">"]: "</span> + types<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>.<span style="color: #000000;">Name</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p><strong>Ausgabe:</strong></p>
<blockquote><p>Type[1]: TestClass<br />
Type[2]: TestClass</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.der-wirtschaftsingenieur.de/index.php/typeof-vs-gettype-in-csharp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Ausnahme-Behandlung in C# (Exception-Handling)</title>
		<link>http://www.der-wirtschaftsingenieur.de/index.php/exceptions-in-c-sharp/</link>
		<comments>http://www.der-wirtschaftsingenieur.de/index.php/exceptions-in-c-sharp/#comments</comments>
		<pubDate>Fri, 25 Apr 2008 18:49:11 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.der-wirtschaftsingenieur.de/?p=108</guid>
		<description><![CDATA[Exception-Handling in C#]]></description>
			<content:encoded><![CDATA[<p>Ein Programm, welches mit einer höheren Programmiersprache entwickelt wurde, interagiert mit seiner Umwelt, neben anderen Programmen vor allem mit dem Betriebssystem, über dieses auch mit der Hardware.</p>
<p>Während das Programm bis in das kleinste Detail durchdacht und nach unzähligen Tests auch (scheinbar) völlig fehlerfrei funktioniert, kann es bei der Interaktion zwischen dem Programm mit seiner Umwelt zu Problemen kommen.</p>
<p>Beispielsweise könnte es beim Öffnen einer Datei zu einem Fehler kommen, die Datei lässt sich nicht öffnen (obwohl das Programm vorher zumindest die Existenz der Datei festgestellt hatte). Möglicherweise lässt sich eine Datei auch nicht speichern, da der Speicherplatz nicht ausreicht, das Speichermedium entfernt wurde oder defekt ist. Nur einige von sehr vielen Beispielen von sogenannten Exceptions.<br />
Einige Exceptions lassen sich bis zu einem bestimmten Grad vorbeugen; Beispielsweise lässt sich unmittelbar vor der Beanspruchung von Speicherplatz prüfen, ob der Speicherplatz ausreicht. Ob der Speicherplatz dann aber bei der Speicherung tatsächlich vorhanden ist und nicht schon von einem anderen Vorgang belegt wurde, ist erst bei der Speicherung feststellbar.</p>
<p>Mit einem bestimmten System lassen sich solche Fehler zwar nicht verhindern, jedoch behandeln.<br />
Software-Entwickler sprechen hierbei von <strong>Fehlerbehandlung</strong> oder (besser:) <strong>Ausnahmebehandlungen</strong>, also die Behandlung von Ausnahmen (<strong>Exceptions</strong>).</p>
<p class="MsoNormal">Mit einer Ausnahmebehandlung, wie sie z.B. in C++, Java und C# möglich ist, können diese Fehler umgangen werden, so dass Programm nur die fehlerträchtige Funktion beendet, aber dennoch nicht komplett abstürzt. Das .NET Framework von Microsoft bietet beispielsweise ein einheitliches Fehlermanagement mit der Möglichkeit zur Ausnahmebehandlung.<span id="more-108"></span></p>
<p class="MsoNormal"><!--[if !supportEmptyParas]--> <!--[endif]--></p>
<h3><strong>Was kann mit der Ausnahmebehandlung erreicht werden?</strong></h3>
<p class="MsoNormal">Im Grund bezweckt die Fehlerbehandlung, dass ein Programm immer stabil läuft. Frühere Programme, welche über keine ausgereifte Fehlerbehandlung verfügen, stürzten häufig komplett ab, wenn eine Funktionalität (z.B. das Schreiben auf einen Datenträger) einen Fehler erzeugte. Dies ist besonders ärgerlich, wenn im Programm (bzw. im Arbeitsspeicher) noch nicht gesicherte Daten liegen, welche beim Programmabsturz komplett verloren gehen.</p>
<p class="MsoNormal"><!--[if !supportEmptyParas]--> <!--[endif]--></p>
<h4><strong>Wo können diese Fehler entstehen?</strong></h4>
<p class="Default"><!--[if !supportEmptyParas]--> <!--[endif]--></p>
<p class="MsoNormal">•<strong> Im Laufzeitsystem</strong>: z.B. wenn ein Stack „gesprengt“ wurde (StackOverflow) oder auch sogenannte Nullreferenzen (Arrayfeld mit bestimmten Index nicht vorhanden etc.)</p>
<p class="MsoNormal">•<strong> Bei der Benutzung von Bibliotheksklassen</strong>: z.B. wenn das Lesen aus einer Datei über eine Methode aus einer Bibliotheksklasse fehlschlägt</p>
<p class="MsoNormal">•<strong> Im eigenen Code</strong>: Es ist möglich Exceptions nach eigenem Wunsch auszulösen und auch eigene Exceptions zu definieren und diese auszulösen</p>
<p class="MsoNormal"><span style="text-decoration: underline;"><strong>Hinweis:</strong></span> Mit Exception-Behandlung sollen keine Programmfehler "ausgebügelt" werden, Programmfehler bzw. Fehler im Programmalgorithmus sollen behoben, nicht mit einer Exception-Behandlung umgangen werden.</p>
<h3><strong>Beispiel: Exception in C#</strong></h3>
<p class="MsoNormal"><!--[if !supportEmptyParas]--> <!--[endif]--></p>
<p class="MsoNormal">
<p class="MsoNormal"><!--[if !supportEmptyParas]--> <!--[endif]--></p>
<p class="MsoNormal">
<p class="MsoNormal">Eine <strong>Klasse Exception</strong> definiert die Exception, eine Exception ist eine Instanz dieser Klasse.</p>
<p class="MsoNormal">Neben der allgemeinen Exception gibt es spezielle Exceptions, welchen nur einen besonderen Fehler abfangen.</p>
<p class="MsoNormal">Eine spezielle Exception ist immer mit Exception benannt, Beispiele:</p>
<ul style="margin-top: 0cm;" type="disc">
<li class="MsoNormal">FileNotFound<strong>Exception</strong></li>
<li class="MsoNormal">IndexOutOfRange<strong>Exception</strong></li>
<li class="MsoNormal">OutOfMemory<strong>Exception</strong></li>
</ul>
<p class="MsoNormal"><!--[if !supportEmptyParas]--> <!--[endif]--></p>
<h4><strong>Aufbau der Ausnahmebehandlung:</strong></h4>
<p>Die Ausnahmebehandlung wird mit nur drei Schlüsselwörtern gegliedert: <span style="color: #0000ff;"><strong>try</strong></span>, <span style="color: #0000ff;"><strong>catch</strong></span> und <span style="color: #0000ff;"><strong>finally</strong></span></p>
<p class="MsoNormal"><span style="text-decoration: underline;"><strong><span style="color: #0000ff;">Try</span> („Versuch“ bzw. „versuche“)</strong></span></p>
<p class="MsoNormal"><!--[if !supportEmptyParas]--> <!--[endif]--></p>
<p class="MsoNormal">Im Try-Block wird der Quellcode eingeschleust, welcher in Verdacht steht, Exceptions auszulösen.</p>
<div class="igBar"><span id="lcsharp-47"><a href="#" onclick="javascript:showPlainTxt('csharp-47'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-47">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">try</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #008200; font-style: italic;">//...&nbsp; der Code, welche Ausnahmen erzeugen könnte</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p class="MsoNormal"><!--[if !supportEmptyParas]--> <!--[endif]--></p>
<p class="MsoNormal"><span style="text-decoration: underline;"><strong><span style="color: #0000ff;">Catch</span> („Abfangen“)</strong></span></p>
<p class="MsoNormal"><!--[if !supportEmptyParas]--> <!--[endif]--></p>
<p class="MsoNormal">Im Catch-Block wird eine Exception behandelt.</p>
<div class="igBar"><span id="lcsharp-48"><a href="#" onclick="javascript:showPlainTxt('csharp-48'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-48">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>Exception e<span style="color: #000000;">&#41;</span> <span style="color: #008200; font-style: italic;">// allgemeine Exception (irgend eine!)</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #008200; font-style: italic;">// Behandlung der Ausnahme, z.B. Ausgabe der Fehlermeldung</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p class="MsoNormal"><!--[if !supportEmptyParas]--> <!--[endif]--></p>
<p class="MsoNormal">Der Catch-Block kann jede Exception abfangen oder sich auf eine spezielle Exception beschränken</p>
<div class="igBar"><span id="lcsharp-49"><a href="#" onclick="javascript:showPlainTxt('csharp-49'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-49">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>FileNotFoundException fnfe<span style="color: #000000;">&#41;</span> <span style="color: #008200; font-style: italic;">// Behandlung der FileNotFoundException (nur diese!)</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #008200; font-style: italic;">// Behandlung der speziellen Ausnahme, z.B. Ausgabe an den User, was schief gelaufen sein könnte</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p class="MsoNormal"><!--[if !supportEmptyParas]--> <!--[endif]--></p>
<p class="MsoNormal">Wenn jedoch nur bestimmte Exceptions behandelt werden, jedoch eine Exception auftritt, welche durch keinen Catch-Block abgefangen wird, wird die Exception in das nächst höhere Level weitergegeben (bis hin zum Betriebssystem) und zumindest die Methode, wenn nicht das gesamte Programm, abgebrochen.</p>
<p class="MsoNormal"><!--[if !supportEmptyParas]--> <!--[endif]--></p>
<p class="MsoNormal"><span style="text-decoration: underline;"><strong><span style="color: #0000ff;">Finally</span> („abschließend“)</strong></span></p>
<p class="MsoNormal"><!--[if !supportEmptyParas]--> <!--[endif]--></p>
<p class="MsoNormal">Ein Programmierer hat für die Sicherheit seines Programms Sorge zu tragen und so einige Feinheiten bei der Programmierung zu beachten.</p>
<p class="MsoNormal">So sind häufig Anweisungen auszuführen, die unbedingt ausgeführt werden müssen, „komme was wolle“.</p>
<p class="MsoNormal">Beispielweise sollte ein Stream (z.B. zu einer Datei) immer geschlossen werden, nach der Benutzung.</p>
<div class="igBar"><span id="lcsharp-50"><a href="#" onclick="javascript:showPlainTxt('csharp-50'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-50">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">StreamReader sr;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008200; font-style: italic;">// Benutzung</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">sr.<span style="color: #000000;">close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p class="MsoNormal">Der Stream soll immer geschlossen werden, unbedingt aber auch, wenn etwas bei der Benutzung des Streams schief läuft.<br />
<!--[endif]--></p>
<p class="MsoNormal">Hierfür gibt es den Finally-Block, welcher dazu dient, eine sichere Zone für kritische Aufgabe zu bilden. Anweisungen, welche in jedem Fall auszuführen sind, werden in den Finally-Block platziert.</p>
<div class="igBar"><span id="lcsharp-51"><a href="#" onclick="javascript:showPlainTxt('csharp-51'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-51">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">finally</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>sr != <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; sr.<span style="color: #000000;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p class="MsoNormal">Der Zweck von <strong>Finally</strong> ist sicher leicht nach zu vollziehen, dennoch kommt schnell die Frage auf, warum der Stream nicht einfach hinter/unter der <strong>Catch</strong>-Anweisung platziert wird; dann würde der Code ebenfalls ausgeführt werden, egal ob eine <em>FileNotFoundException</em> ausgelöst wurde oder nicht.</p>
<p class="MsoNormal">Das ist soweit richtig, allerdings ist der <strong>Finally</strong>-Block eine speziell sichere Zone, welcher unbedingt vor Verlassen der Methode ausgeführt wird.<br />
Wenn beispielsweise eine andere als die <em>FileNotFoundException</em> auftritt, würde die Methode sofort stoppen und der Stream nicht geschlossen werden. Der <strong>Finally</strong>-Block würde aber in jedem Fall ausgeführt werden.</p>
<p class="MsoNormal"><!--[if !supportEmptyParas]--> <!--[endif]--></p>
<p class="MsoNormal"><!--[if !supportEmptyParas]--> <!--[endif]--></p>
<h4><strong>Weitergabe einer Exception:</strong></h4>
<p class="MsoNormal"><!--[if !supportEmptyParas]--> <!--[endif]--></p>
<p class="MsoNormal">Es kann auch notwendig sein, dass eine Methode abgefangen und behandelt wird, dann aber doch trotzdem an die nächst höhere Ebene im Programm oder gar an das Betriebssystem weitergereicht wird, damit die Exception auch auf höherer Ebene behandelt werden kann. Dies ist mit dem Schlüsselwort <span style="color: #0000ff;"><strong>throw</strong></span> möglich.</p>
<div class="igBar"><span id="lcsharp-52"><a href="#" onclick="javascript:showPlainTxt('csharp-52'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-52">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>Exception e<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #008200; font-style: italic;">// Fehlerbehandlung</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;MessageBox.<span style="color: #000000;">Show</span><span style="color: #000000;">&#40;</span>e.<span style="color: #000000;">Message</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">throw</span> e;&nbsp; &nbsp; <span style="color: #008200; font-style: italic;">// Weiterreichung der Exception</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Mit dem Schlüsselwort <span style="color: #0000ff;"><strong>throw</strong></span> wird die Exception neu geworfen und dadurch die Weiterleitung nach der erstmaligen Behandlung erreicht.</p>
<p>Auf diese Art lässt sich auch irgend eine definierte Exception auslösen, zu jeder Zeit.</p>
<div class="igBar"><span id="lcsharp-53"><a href="#" onclick="javascript:showPlainTxt('csharp-53'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-53">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> GiveException<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">throw</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0600FF;">new</span></a> NoNullAllowedException<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<h3><strong>Komplette Beispiele:</strong></h3>
<h4><strong>1. Beispiel - Datei lesen und die FileNotFoundException</strong></h4>
<p>Die Methode <em>FileIsReady()</em> prüft die Zugriffsbereitschaft genau, indem sie ausgelesen wird.</p>
<div class="igBar"><span id="lcsharp-54"><a href="#" onclick="javascript:showPlainTxt('csharp-54'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-54">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">bool</span> FileIsReady<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">string</span> path<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">bool</span> isReady = <span style="color: #0600FF;">true</span>; <span style="color: #008200; font-style: italic;">// zugriffsbereit (optimistisch)</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">try</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; sr = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0600FF;">new</span></a> StreamReader<span style="color: #000000;">&#40;</span>path<span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">string</span> line;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>line = sr.<span style="color: #000000;">ReadLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> != <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Console.<span style="color: #000000;">WriteLine</span><span style="color: #000000;">&#40;</span>line<span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>FileNotFoundException<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; isReady = <span style="color: #0600FF;">false</span>;&nbsp; &nbsp; <span style="color: #008200; font-style: italic;">// doch nicht zugriffsbereit</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; Console.<span style="color: #000000;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">"File does not exist !?"</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">finally</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>sr != <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sr.<span style="color: #000000;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">return</span> isReady;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p><span style="text-decoration: underline;"><strong>Hinweis:</strong></span><strong> </strong>In dem Beispiel wird nur die <em>FileNotFoundException</em> behandelt. Ein Dateizugriff bzw. der Versuch eine Datei zu öffnen, kann jedoch auch zu weiteren Exceptions führen.</p>
<p>Der Test, ob eine Datei zugriffsbereit ist, ist in der Praxis eher unnötig, da sich die Datei, wenn sie wirklich erst benötigt wird, schon wieder in einem anderen Status befinden könnte. Daher wird i.d.R. auf die Datei zugegriffen wenn es nötig wird und genau dann auf eventuell auftretende Ausnahmen eingegangen.</p>
<h4><strong>2. Beispiel - Zugriff auf ein Array und die IndexOutOfRangeException</strong></h4>
<p>Die <em>TestClass</em> stellt die Methode<em> SetNull()</em> zur Verfügung, welche ein eigenes Array anspricht. Die Funktion setzt alle Felder im Array von (Parameter) <em>start</em> bis <em>end</em> auf den Wert <strong>0</strong>.</p>
<div class="igBar"><span id="lcsharp-55"><a href="#" onclick="javascript:showPlainTxt('csharp-55'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-55">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008200; font-style: italic;">///</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008200; font-style: italic;">/// Diese Klasse dient der Produktion von Exceptions</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008200; font-style: italic;">///</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">class</span> TestClass</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">public</span> TestClass<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #008200; font-style: italic;">/// Setzt alle Werte eines eigenen Arrays vom Start-Index bis Ziel-Index</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #008200; font-style: italic;">/// auf den Wert 0</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #008200; font-style: italic;">/// Bei zu hohem Index wird so die Exception &quot;IndexOutOfRangeException&quot;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #008200; font-style: italic;">/// ausgelöst</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> SetNull<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">int</span> start, <span style="color: #0600FF;">int</span> end<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">int</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> numbers = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0600FF;">new</span></a> <span style="color: #0600FF;">int</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;color:#800000;">100</span><span style="color: #000000;">&#93;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">int</span> i = start; i &lt;= end; i++<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;numbers<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = <span style="color: #FF0000;color:#800000;">0</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Console.<span style="color: #000000;">WriteLine</span><span style="color: #000000;">&#40;</span>i<span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Die Klasse <em>Form1</em> instanziiert die Klasse <em>TestClass</em> und führt deren Methode <em>SetNull()</em> aus. Dabei wird der Wert 500 als Parameter <em>end</em> übergeben, welcher viel zu groß ist und so eine <em>IndexOutOfRangeException</em> hervorruft.</p>
<div class="igBar"><span id="lcsharp-56"><a href="#" onclick="javascript:showPlainTxt('csharp-56'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-56">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> partial <span style="color: #0600FF;">class</span> Form1 : Form</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">public</span> Form1<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #008200; font-style: italic;">// Testklasse instanziieren</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;TestClass tc = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0600FF;">new</span></a> TestClass<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #008200; font-style: italic;">// versuche.....</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">try</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#123;</span>&nbsp; <span style="color: #008200; font-style: italic;">// ... die Methode auszuführen</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; tc.<span style="color: #000000;">SetNull</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;color:#800000;">80</span>, <span style="color: #FF0000;color:#800000;">500</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #008200; font-style: italic;">// Falls Fehlerfall 1 (Index zu hoch)...</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>IndexOutOfRangeException iore<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #008200; font-style: italic;">// ...gebe eine Fehlermeldung aus</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; Console.<span style="color: #000000;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">"Index Out of Range <img src='http://www.der-wirtschaftsingenieur.de/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> "</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #008200; font-style: italic;">// Falls Fehlerfall 2 (irgend ein Fehler)...</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">catch</span> <span style="color: #000000;">&#40;</span>Exception e<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #008200; font-style: italic;">// ...gebe die Fehlermeldung aus</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; Console.<span style="color: #000000;">WriteLine</span><span style="color: #000000;">&#40;</span>e.<span style="color: #000000;">Message</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #008200; font-style: italic;">// .... weiter geht`s mit anderen Aufgaben...</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p><span style="text-decoration: underline;"><strong>Hinweise:</strong></span> In der Praxis sollte die Methode <em>SetNull()</em> natürlich mit eine If-Bedingung feststellen, ob die Größe des Array ausreicht. So ließe sich die <em>IndexOutOfRangeException</em> <span style="text-decoration: underline;">vermeiden</span>!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.der-wirtschaftsingenieur.de/index.php/exceptions-in-c-sharp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Wertetyp vs Refernztyp</title>
		<link>http://www.der-wirtschaftsingenieur.de/index.php/wertetyp-vs-refernztyp/</link>
		<comments>http://www.der-wirtschaftsingenieur.de/index.php/wertetyp-vs-refernztyp/#comments</comments>
		<pubDate>Tue, 25 Mar 2008 18:44:29 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Informatik]]></category>

		<guid isPermaLink="false">http://www.der-wirtschaftsingenieur.de/index.php/2008/03/25/wertetyp-vs-refernztyp/</guid>
		<description><![CDATA[In der prozeduralen und objektorientierten Programmierung wird zwischen Wertetypen und Referenztypen unterschieden. Beispielsweise sind in den Programmiersprachen C# und Java primitive Datentypen, Enumeratoren und Strukturen Wertetypen. Klassenobjekte sind hingegen Referenztypen. In C/C++ dienen Pointer (Zeiger) als Refernztyp, welche die Adresse speichert, die auf einen veränderbaren Wert verweist. Über den Pointer kann die Adresse dann von [...]]]></description>
			<content:encoded><![CDATA[<p>In der prozeduralen und objektorientierten Programmierung wird zwischen <strong>Wertetypen</strong> und <strong>Referenztypen</strong> unterschieden.<br />
Beispielsweise sind in den Programmiersprachen C# und Java <strong>primitive Datentypen</strong>, Enumeratoren und Strukturen <strong>Wertetypen.</strong> Klassenobjekte sind hingegen <strong>Referenztypen</strong>.<br />
In C/C++ dienen Pointer (Zeiger) als Refernztyp, welche die Adresse speichert, die auf einen veränderbaren Wert verweist. Über den Pointer kann die Adresse dann von Funktion zu Funktion überreicht werden.<br />
So kann aus jeder Funktion heraus, die den Zeiger zur Verfügung hat, der Wert verändert werden, auf den die Adresse zeigt. Der geänderte Wert ist über diese Adresse wieder abrufbar, von allen Funktionen aus, die die Refernz bekommen haben. Ein Zeiger in C ist also eine <strong>Referenz</strong>.</p>
<p>Ähnlich verhält es sich bei Klassenobjekten in C# und Java. Für diese wird ein Speicherplatz im <strong>Heap</strong> des Arbeitsspeichers angefordert, welcher sich über die Speicheradresse ansprechen und verändern (bzw. überschreiben) lässt. Auch die Klassenobjekte in C++ werden im <strong>Heap</strong> gespeichert.<br />
Der <strong>Heap</strong> kann beliebig angesprochen werden, jedoch muss der Speicher bei Bedarf geordert und spätestens bei Programmende wieder freigegeben werden.<br />
In C++ muss sich darum der Programmierer selbst kümmern; C#, Java und die meisten objektorientierten Skriptsprachen haben hierfür eine <strong>automatische Speicherbereinigung</strong> (<strong>Garbage Collector</strong>).</p>
<p><strong>Wertetypen</strong> werden nicht über eine Speicheradresse an eine Funktion übergeben, sondern als Wert. Diese Werte werden kopiert und auf den <strong>Stack</strong> im Arbeitsspeicher gelegt.<br />
Je mehr die Funktionsaufrufe verschachtelt sind, um so größer wird auch der <strong>Stack</strong>.<br />
Diese Vorgehensweise ist durchaus sinnvoll, denn auf den <strong>Stack</strong> kann zwar nicht beliebig, sondern nur über das aktuellste (oberste) Stack-Element zugegriffen werden, da jedoch auch vor allem die<br />
Funktionsvariablen (Parameter) benötigt werden, erfolgt der Zugriff daher i.d.R. sehr schnell.<br />
Bei jeder Parameterübergabe mit Wertetypen werden die Werte daher kopiert und der Stack wächst. Die Werte sind (zumindest noch bei Funktionsaufruf) redundant auf dem Stack.<br />
Werden die Parameter in der Funktion verändert, hat dies keine Auswirkungen auf die Variablen, welche außerhalb der Funktion als Parameter übergeben wurden.</p>
<p>In einigen Fällen ist es vom Programmierer erwünscht, dass auch Wertetypen innerhalb der Funktion geändert werden können. <strong> Wertetypen</strong> können als <strong>Referenz</strong> übergeben werden. Dann sind diese in C/C++ als Pointer zu übergeben, in C# und Java mit dem Schlüsselwort <em><font color="#0000ff"><strong>ref</strong></font></em>.</p>
<p>Ein Beispiel-Programm mit Funktionsaufrufen und Parameterübergaben als Werte- und Referenztyp:</p>
<div class="igBar"><span id="lcsharp-59"><a href="#" onclick="javascript:showPlainTxt('csharp-59'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-59">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">class</span> Program</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> args<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; Program program = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0600FF;">new</span></a> Program<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> Program<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">int</span> a = <span style="color: #FF0000;color:#800000;">45</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">int</span> b = <span style="color: #FF0000;color:#800000;">34</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">this</span>.<span style="color: #000000;">Sum</span><span style="color: #000000;">&#40;</span>a, b<span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; Console.<span style="color: #000000;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">"a = "</span> + a.<span style="color: #000000;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> + <span style="color: #000000;">"; b = "</span> + b.<span style="color: #000000;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">this</span>.<span style="color: #000000;">Sum2</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">ref</span> a, <span style="color: #0600FF;">ref</span> b<span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; Console.<span style="color: #000000;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">"a = "</span> + a.<span style="color: #000000;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> + <span style="color: #000000;">"; b = "</span> + b.<span style="color: #000000;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #008200; font-style: italic;">// Normalfall: By Value - die Variable a kann nicht überschrieben werden</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> Sum<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">int</span> param1, <span style="color: #0600FF;">int</span> param2<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; param1 = param1 + param2;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #008200; font-style: italic;">// Sonderfall: By Reference - die Variable a wird überschrieben</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> Sum2<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">ref</span> <span style="color: #0600FF;">int</span> param1, <span style="color: #0600FF;">ref</span> <span style="color: #0600FF;">int</span> param2<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; param1 = param1 + param2;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Ausgabe:</p>
<blockquote><p> a = 45; b = 34<br />
a = 79; b = 34</p></blockquote>
<p>Die Ausgabe des Programms zeigt, dass nur die Methode mit der als Referenz übergebenen Parameter die Variablen im Konstruktor verändert.</p>
<p>Alternativ zum Schlüsselwort <font color="#0000ff"><strong>ref</strong></font> kann auch das Schlüsselwort <font color="#0000ff"><strong>out</strong></font> verwendet werden.<br />
Während ein mit <font color="#0000ff"><strong>ref</strong></font> bezeichneter Parameter vor Paramterübergabe definitiv initialisiert werden muss damit er verwendet werden kann, initialisiert sich ein mit <font color="#0000ff"><strong>out</strong></font> bezeichneter Parameter bei Methodenaufruf selber und muss daher nicht im Vorfeld initialisiert worden sein.<br />
Hiervon abgesehen besteht kein wesentlicher Unterschied zwischen den Schlüsselwörtern <font color="#0000ff"><strong>ref</strong></font> und <font color="#0000ff"><strong>out</strong></font>, daher kann auch keine <a title="Referenzparameter (ref und out)" href="http://www.der-wirtschaftsingenieur.de/index.php/2008/08/26/uberladen-von-methoden-in-c-sharp/" target="_self">Überladung</a> mit Unterscheidung dieser beiden Schlüsselwörter geschehen.</p>
<h3><strong>Boxing / Unboxing </strong></h3>
<p>Eine <strong>Konvertierung</strong> eines <strong>Wertetyp</strong>s zu einem <strong>Referenztyp</strong> wird als <strong>Boxing</strong> bezeichnet. <strong>Unboxing</strong> ist die entgegengesetzte <strong>Konvertierung</strong>, also einer Referenz zu einem Wert. Dabei ist in C# und Java i.d.R. explizites Casting notwendig (Vorbestimmung des Datentyps zwischen einfachen Klammern).</p>
<div class="igBar"><span id="lcsharp-60"><a href="#" onclick="javascript:showPlainTxt('csharp-60'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-60">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">int</span> a = <span style="color: #FF0000;color:#800000;">1548</span>;&nbsp; &nbsp; <span style="color: #008200; font-style: italic;">// Wertetyp</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">object</span> o = a;&nbsp; &nbsp; <span style="color: #008200; font-style: italic;">// boxing</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">int</span> b = <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">int</span><span style="color: #000000;">&#41;</span>o;&nbsp; <span style="color: #008200; font-style: italic;">// unboxing (mit Casting) </span></div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://www.der-wirtschaftsingenieur.de/index.php/wertetyp-vs-refernztyp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Queue Datenstruktur in C#</title>
		<link>http://www.der-wirtschaftsingenieur.de/index.php/queue-datenstruktur-in-c/</link>
		<comments>http://www.der-wirtschaftsingenieur.de/index.php/queue-datenstruktur-in-c/#comments</comments>
		<pubDate>Wed, 19 Mar 2008 22:06:15 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.der-wirtschaftsingenieur.de/index.php/2008/03/19/queue-datenstruktur-in-c/</guid>
		<description><![CDATA[Eine Queue (Warteschlange) ist eine First-in-First-out-(FIFO) Datenstruktur. Die Datenstruktur Queue ist ein Stapel, auf welchem nach und nach Daten abgelegt werden. Allerdings wird auf Elemente der Queue nur von unten zugegriffen. Das unterste Element ist das mit höchster Priorität, nur auf dieses als erstes hinzugefügte Element kann zugegriffen werden. Ein Pointer (Zeiger) zeigt intern immer [...]]]></description>
			<content:encoded><![CDATA[<p>Eine <strong>Queue</strong> (Warteschlange) ist eine First-in-First-out-(FIFO) <strong>Datenstruktur.</strong><br />
Die Datenstruktur <strong>Queue</strong> ist ein Stapel, auf welchem nach und nach Daten abgelegt werden.<br />
Allerdings wird auf Elemente der Queue nur von unten zugegriffen. Das unterste Element ist  das mit höchster Priorität, nur auf dieses als erstes hinzugefügte Element kann zugegriffen werden.</p>
<p><img src="http://www.der-wirtschaftsingenieur.de/bilder/queue.PNG" alt="Stack" height="" width="" /></p>
<p>Ein <strong>Pointer</strong> (Zeiger) zeigt intern immer auf das Element, welches (relativ zu den anderen Elementen) als erstes in die Queue aufgenommen wurde.<br />
Ein einfaches "Lösche aus der Queue" gibt es nicht, ein Element kann aus der Queue mit <strong>Peek()</strong> herausgelesen und mit <strong>Dequeue()</strong> herausgelesen und zugleich entfernt werden.<br />
An das Warteschlangenende angereiht werden Elemente mit <strong>Enqueue()</strong>;</p>
<p><strong>Funktionsweise einer Queue an Hand eines praktischen Beispiels:</strong></p>
<p>Stellen Sie sich vor, Sie müssten eine Software für ein Theater schreiben. Die Software soll Ticket-Bestellungen für<br />
Vorführungen bearbeiten. Nun sind die Vorstellungen aber sehr schnell ausgebucht, so dass viele Tickets nicht mehr ausgestellt werden können. Bei der Ticket-Bestellung gilt jedoch "wer zu erst kommt, mal zu erst".<br />
Die Daten aller eingehenden Bestellungen legen Sie in eine Queue (Warteschlange). Die Daten in der Queue werden vom ersten bis zum letzten Eintrag abgearbeitet, bis die Queue vollständig abgearbeitet wurde oder keine Plätze mehr verfügbar sind.</p>
<p><span id="more-90"></span></p>
<h3><strong>Beispielhafte Verwendung einer Queue in C#</strong></h3>
<p>Eine <strong>Queue</strong> wird mit der Klasse <em>Queue</em> angelegt. Die Queue benötigt jedoch die Information, von welchem Typ die zu speichernden Daten sein werden, diese werden mit spitzen Klammern angegeben.<br />
Im Beispiel wird eine Queue angelegt, welche Strings speichern soll. Dabei wird seine Kapazität auf 500 Elemente beschränkt.</p>
<p>Diese <strong>Queue</strong> soll Bewerber auf Tickets für eine Theateraufführung speichern.</p>
<div class="igBar"><span id="lcsharp-66"><a href="#" onclick="javascript:showPlainTxt('csharp-66'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-66">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Queue&lt;string&gt; queue = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0600FF;">new</span></a> Queue&lt;string&gt;<span style="color: #000000;">&#40;</span><span style="color: #FF0000;color:#800000;">500</span><span style="color: #000000;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>In einer ArrayList werden dafür 7 Beispiel-Bewerber eingetragen. Die Klasse ArrayList setzt <em>using System.Collections</em>  vorraus.</p>
<div class="igBar"><span id="lcsharp-67"><a href="#" onclick="javascript:showPlainTxt('csharp-67'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-67">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">ArrayList applicants = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0600FF;">new</span></a> ArrayList<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">applicants.<span style="color: #000000;">AddRange</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0600FF;">new</span></a> <span style="color: #0600FF;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">"Thomas"</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">"Daniela"</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">"David"</span> ,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">"Sarah"</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">"Marion"</span>,</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">"Friedarike"</span>,</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">"Benjamin"</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Die Bewerber werden in die Queue (Warteschlange) eingereiht:</p>
<div class="igBar"><span id="lcsharp-68"><a href="#" onclick="javascript:showPlainTxt('csharp-68'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-68">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">string</span> applicant <span style="color: #0600FF;">in</span> applicants<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp;queue.<span style="color: #000000;">Enqueue</span><span style="color: #000000;">&#40;</span>applicant<span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Die Plätze sind jedoch begrenzt, nur 5 Tickets können vergeben werden.</p>
<div class="igBar"><span id="lcsharp-69"><a href="#" onclick="javascript:showPlainTxt('csharp-69'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-69">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">this</span>.<span style="color: #000000;">guests</span> = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0600FF;">new</span></a> <span style="color: #0600FF;">string</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;color:#800000;">5</span><span style="color: #000000;">&#93;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Eine Methode vergibt die Plätze nach dem Prinzip "Wer zu erst kommt, malt zu erst". Die Elemente der Queue werden in das Array <em>guests </em>übertragen.</p>
<div class="igBar"><span id="lcsharp-70"><a href="#" onclick="javascript:showPlainTxt('csharp-70'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-70">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> EnterShow<span style="color: #000000;">&#40;</span>Queue&lt;string&gt; queue<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">int</span> i = <span style="color: #FF0000;color:#800000;">0</span>; i &lt; <span style="color: #0600FF;">this</span>.<span style="color: #000000;">guests</span>.<span style="color: #000000;">Length</span>; i++<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">this</span>.<span style="color: #000000;">guests</span><span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = queue.<span style="color: #000000;">Dequeue</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>In dem Beispiel können nicht alle Plätze vergeben werden, so dass die Personen  "Friedarike" und "Benjamin" die Vorführung nicht sehen können und vorerst in der Warteschlange verbleiben müssen.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.der-wirtschaftsingenieur.de/index.php/queue-datenstruktur-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Stack Datenstruktur in C#</title>
		<link>http://www.der-wirtschaftsingenieur.de/index.php/stack-datenstruktur-in-c/</link>
		<comments>http://www.der-wirtschaftsingenieur.de/index.php/stack-datenstruktur-in-c/#comments</comments>
		<pubDate>Wed, 19 Mar 2008 21:27:42 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.der-wirtschaftsingenieur.de/index.php/2008/03/19/stack-datenstruktur-in-c/</guid>
		<description><![CDATA[Ein Stack (Stapel) ist eine Last-in-First-out-(LIFO) Datenstruktur. Die Datenstruktur Stack sollte dann verwendet werden, wenn Daten gesammelt werden, jedoch immer nur ein Datenelement aktuell bearbeitet werden darf. Das aktuelle Element ist dann das einzige, auf welches zugegriffen werden kann. Alle anderen Elemente bleiben im Hintergrund und werden erst (und dann auch jeweils einzeln) bearbeitet, wenn [...]]]></description>
			<content:encoded><![CDATA[<p>Ein <strong>Stack</strong> (Stapel) ist eine Last-in-First-out-(LIFO) <strong>Datenstruktur</strong>.<br />
Die Datenstruktur <strong>Stack</strong> sollte dann verwendet werden, wenn Daten gesammelt werden, jedoch immer nur ein Datenelement aktuell bearbeitet werden darf.</p>
<p>Das aktuelle Element ist dann das einzige, auf welches zugegriffen werden kann. Alle anderen Elemente bleiben im<br />
Hintergrund und werden erst (und dann auch jeweils einzeln) bearbeitet, wenn das aktuelle Element abgearbeitet und vom Stapel entfernt wurde.</p>
<p>Ein <strong>Pointer</strong> (Zeiger) zeigt intern immer auf das Element, welches (relativ zu den anderen Elementen) als letztes auf den Stapel gelegt wurde.<br />
Ein einfaches "Lösche aus dem Stack" gibt es nicht, ein Element kann aus dem Stack mit <em><strong>Peek() </strong></em>herausgelesen und mit <strong><em>Pop()</em></strong> herausgelesen und zugleich entfernt werden.<br />
Ein Element kann mit <strong><em>Push()</em></strong> auf den Stack gelegt werden.</p>
<p><img src="http://www.der-wirtschaftsingenieur.de/bilder/stack.PNG" alt="Stack" height="239" width="333" /></p>
<p><strong>Die Funktionsweise des Stack an Hand eines praktischen Beispiels:</strong><br />
Stellen Sie sich einen Stapel Teller vor, welchen sie abwaschen möchten. Sie würden sicherlich beim obersten Teller anfangen, diesen vom Stapel nehmen und abwaschen. Erst dann können Sie den nächsten Teller vom Stapel nehmen.</p>
<p><span id="more-89"></span></p>
<h3><strong>Beispielhafte Verwendung eines Stack in C#</strong></h3>
<p>Ein Stack wird mit der Klasse <em>Stack</em> angelegt. Der Stack benötigt jedoch die Information, von welchem Typ die zu speichernden Daten sein werden, diese werden mit spitzen Klammern angegeben.<br />
Im Beispiel wird ein Stack angelegt, welcher Integer-Werte speichern soll. Dabei wird seine Kapazität auf 15 Elemente beschränkt.</p>
<p>Im Beispiel werden dann die Werte 3, 4, 5 und 6 auf den <strong>Stack</strong> abgelegt.</p>
<div class="igBar"><span id="lcsharp-74"><a href="#" onclick="javascript:showPlainTxt('csharp-74'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-74">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Stack&lt;int&gt; stack = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0600FF;">new</span></a> Stack&lt;int&gt;<span style="color: #000000;">&#40;</span><span style="color: #FF0000;color:#800000;">15</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">stack.<span style="color: #000000;">Push</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;color:#800000;">3</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">stack.<span style="color: #000000;">Push</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;color:#800000;">4</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">stack.<span style="color: #000000;">Push</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;color:#800000;">5</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">stack.<span style="color: #000000;">Push</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;color:#800000;">6</span><span style="color: #000000;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Die Anweisung zum Abruf des obersten Segments...</p>
<div class="igBar"><span id="lcsharp-75"><a href="#" onclick="javascript:showPlainTxt('csharp-75'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-75">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">stack.<span style="color: #000000;">Peek</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>...hat folgende Ausgabe zur Folge:</p>
<blockquote><p>6</p></blockquote>
<p>Mit der Methode <em>Contains()</em> kann geprüft werden, ob der Stack ein Element enthält (dieses muss nicht das oberste sein). Die Stack-Klasse bietet eine Methode <em>ToArray() </em>an, mit der sich alle Elemente des Stack ausgeben lassen.</p>
<p>Eigentlich soll der Stack die Elemente jedoch nicht alle ausgeben können, sondern nur das oberste (bzw. zuletzt eingefügte) Element!</p>
<p>Dennoch ein Beispiel für eine Methode, welche alle Elemente des Stack ausgibt, in dem jedes Element ausgegeben und entfernt und dann auf einen anderen Stack gelegt wird, um dann wieder (nach der Ausgabe aller Elemente) wieder in den ursprünglichen Stack abgelegt zu werden.</p>
<div class="igBar"><span id="lcsharp-76"><a href="#" onclick="javascript:showPlainTxt('csharp-76'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-76">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> showStackItems<span style="color: #000000;">&#40;</span>Stack&lt;int&gt; stack<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; Stack&lt;int&gt; tempStack = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0600FF;">new</span></a> Stack&lt;int&gt;<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span>stack.<span style="color: #000000;">Count</span>&gt; <span style="color: #FF0000;color:#800000;">0</span><span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.<span style="color: #000000;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">"Oberster Stackinhalt: "</span> + stack.<span style="color: #000000;">Peek</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #000000;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tempStack.<span style="color: #000000;">Push</span><span style="color: #000000;">&#40;</span>stack.<span style="color: #000000;">Pop</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span>tempStack.<span style="color: #000000;">Count</span>&gt; <span style="color: #FF0000;color:#800000;">0</span><span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stack.<span style="color: #000000;">Push</span><span style="color: #000000;">&#40;</span>tempStack.<span style="color: #000000;">Pop</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://www.der-wirtschaftsingenieur.de/index.php/stack-datenstruktur-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hashtable in C#</title>
		<link>http://www.der-wirtschaftsingenieur.de/index.php/hashtable-in-c/</link>
		<comments>http://www.der-wirtschaftsingenieur.de/index.php/hashtable-in-c/#comments</comments>
		<pubDate>Mon, 17 Mar 2008 23:48:27 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.der-wirtschaftsingenieur.de/index.php/2008/03/18/hashtable-in-c/</guid>
		<description><![CDATA[Hashtables (Hash-Tabellen) sind Datenstrukturen, die den Arrays sehr ähnlich sind. Hashtables können in C# ähnlich wie eine ArrayList verwendet werden, der Zugriff erfolgt über einen Index. Der Index ist ein spezieller Key, welcher eine Zahl (z.B. vom Typ Float oder Integer) genauso wie ein String sein darf. PLAIN TEXT C#: // Beispiele, Syntax: hashtablename[key] = [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Hashtables</strong> (Hash-Tabellen) sind <strong>Datenstrukturen</strong>, die den Arrays sehr ähnlich sind.<br />
Hashtables können in C# ähnlich wie eine ArrayList verwendet werden, der Zugriff erfolgt über einen <strong>Index</strong>.<br />
Der Index ist ein spezieller <strong>Key</strong>, welcher eine Zahl (z.B. vom Typ <em>Float</em> oder <em>Integer</em>) genauso wie ein <em>String</em> sein darf.</p>
<div class="igBar"><span id="lcsharp-91"><a href="#" onclick="javascript:showPlainTxt('csharp-91'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-91">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008200; font-style: italic;">// Beispiele, Syntax: hashtablename[key] = value;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">ht_pass<span style="color: #000000;">&#91;</span><span style="color: #FF0000;color:#800000;">32</span>.<span style="color: #FF0000;color:#800000;">3</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000;">"juhu"</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">ht_pass<span style="color: #000000;">&#91;</span><span style="color: #FF0000;color:#800000;">5</span><span style="color: #000000;">&#93;</span> = <span style="color: #000000;">"tobias"</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">ht_pass<span style="color: #000000;">&#91;</span><span style="color: #000000;">"zoo"</span><span style="color: #000000;">&#93;</span> = <span style="color: #FF0000;color:#800000;">45</span>.<span style="color: #FF0000;color:#800000;">3</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<h3><strong>Wozu werden Hashtables benötigt?</strong></h3>
<p><strong> Hashtables</strong> sind sinnvoll, wenn sich Listen bzw. Arrays <strong>verknüpfen bzw. verschachteln</strong> lassen. So kann der Wert eines Listenfeldes der Schlüssel zu einem Wert eines anderen Feldes sein. Zwar ließe sich das auch mit einem normalen Array<br />
(und somit Integer-Werten als Index) realisieren, dann wären jedoch einige zusätzliche Listen/Tabellen nötig, um von einer<br />
Zahl auf einen speziellen Schlüssel und von diesem Schlüssel wieder auf eine anderen Zahlenschlüssel zu verweisen.</p>
<p><strong>C#</strong> bietet natürlich einen gewissen Komfort bei Benutzung von <strong>Hashtables</strong> (wie bei vielen anderen Dingen auch). In der Programmiersprache C haben Programmierer eigene Hash-Funktionen entwickelt, um z.B. einen Text als Zahl umzuschlüsseln (z.B. "Hallo" -&gt; "2342424", "Schule" -&gt; "792942" usw.), um diesen dann als Index (Schlüssel) für ein Array zu benuten. <strong>Vorteil:</strong> Um einen Wert zu suchen, musste nicht unbedingt das ganze Array durchlaufen und im<br />
schlimmsten Falle alle Felder iteriert werden. Man musste sich nur die Schlüssel für bestimmte Werte merken, diese in eine Zahl konvertieren, auf den Wert im Feld mit dem Schlüssel zugreifen und es auslesen. Daraus ergibt sich ein sehr großer <strong>Geschwindigkeitsvorteil</strong>.<br />
Der <strong>Nachteil</strong> liegt in der Größe des Arrays, dieses muss nämlich so groß sein bzw. so einen hohen Index haben, wie eine Zahl, die aus einem Text resultiert, werden kann. Außerdem könnte es vorkommen, dass Werte nicht abgespeichert werden können, da zwei oder mehrere unterschiedliche Texte trotz ausgereiften Algorithmus den selben Index ergeben. Ausweichmöglichkeiten sind zwar vorhanden (den Index verschieben, bis ein freies Feld gefunden wurde), macht die Sache jedoch trotzdem nicht viel unproblematischer.</p>
<p>Die Hashtables in C# sind weitaus dynamischer, denn intern ist ein Hahstable auch nur so groß, wie es notwendig ist. Der Zugriff ist jedoch absolut schnell, <strong>konstant schnell</strong>! <strong>Unabhängig von der Größe des Hashtable ist die Zugriffszeit konstant.</strong></p>
<p><span id="more-87"></span><strong>Verwendung von Hashtables</strong></p>
<p>Um die Klasse Hashtable verfügbar zu machen, muss der Namespace <em>Collections </em>benutzt werden.</p>
<div class="igBar"><span id="lcsharp-92"><a href="#" onclick="javascript:showPlainTxt('csharp-92'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-92">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">using</span> <span style="color: #000000;">System</span>.<span style="color: #000000;">Collections</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<h4>Ein Anwendungsbeispiel:</h4>
<p>Es soll ein Hashtable angelegt werden, welcher Benutzernamen als Key mit dazugehörigem Benutzerpasswort als Value speichert.</p>
<div class="igBar"><span id="lcsharp-93"><a href="#" onclick="javascript:showPlainTxt('csharp-93'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-93">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008200; font-style: italic;">// Deklaration</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Hashtable ht_pass;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008200; font-style: italic;">// Instanziierung des Hashtable ht_pass</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">ht_pass = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0600FF;">new</span></a> Hashtable<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008200; font-style: italic;">// Anlegen von Schlüssel-Werte-Paare, ähnlich einem normalen Array</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">ht_pass<span style="color: #000000;">&#91;</span><span style="color: #000000;">"benny"</span><span style="color: #000000;">&#93;</span>&nbsp; &nbsp; = <span style="color: #000000;">"porsche"</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">ht_pass<span style="color: #000000;">&#91;</span><span style="color: #000000;">"david"</span><span style="color: #000000;">&#93;</span>&nbsp; &nbsp; = <span style="color: #000000;">"programmierer"</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">ht_pass<span style="color: #000000;">&#91;</span><span style="color: #000000;">"thomas"</span><span style="color: #000000;">&#93;</span>&nbsp; &nbsp;= <span style="color: #000000;">"kaufmann"</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">ht_pass<span style="color: #000000;">&#91;</span><span style="color: #000000;">"daniela"</span><span style="color: #000000;">&#93;</span>&nbsp; = <span style="color: #000000;">"hamburg"</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">ht_pass<span style="color: #000000;">&#91;</span><span style="color: #000000;">"sarah"</span><span style="color: #000000;">&#93;</span>&nbsp; &nbsp; = <span style="color: #000000;">"universität"</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Nun können die Passwörter ausgegeben werden, beispielweise:</p>
<div class="igBar"><span id="lcsharp-94"><a href="#" onclick="javascript:showPlainTxt('csharp-94'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-94">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Console.<span style="color: #000000;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>.<span style="color: #000000;">ht_pass</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">"benny"</span><span style="color: #000000;">&#93;</span>.<span style="color: #000000;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Ausgabe:</p>
<blockquote><p>porsche</p></blockquote>
<p>Aber Vorsicht! Wird ein Schlüssel in den Hashtable eingesetzt, welcher nicht im Hashtable vorkommt, geschieht eine Exception! (Programmabsturz)</p>
<p>Daher wird eine Methode implementiert, welche vorher prüft, ob der Hashtable nicht <em>Null</em> ist und der gewünschte Key im Hashtable vorkommt und erst dann auf das Value zugreift bzw. es nicht versucht, auf einen Wert zuzugreifen.</p>
<div class="igBar"><span id="lcsharp-95"><a href="#" onclick="javascript:showPlainTxt('csharp-95'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-95">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">string</span> GetHashTableValueByIndey<span style="color: #000000;">&#40;</span>Hashtable ht, <span style="color: #0600FF;">string</span> key<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">string</span> return_string;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>ht != <span style="color: #0600FF;">null</span> &amp;&amp; ht.<span style="color: #000000;">ContainsKey</span><span style="color: #000000;">&#40;</span>key<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return_string = ht<span style="color: #000000;">&#91;</span>key<span style="color: #000000;">&#93;</span>.<span style="color: #000000;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">else</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return_string = <span style="color: #000000;">"[Wert-Schlüssel-Paar mit Schlüssel '"</span> + key + <span style="color: #000000;">"' existiert nicht]"</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">return</span> return_string;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Um den gesamten Inhalt des Hashtable zu illustrieren, wird eine weitere Methode <em>ShowAllItems()</em> implementiert:</p>
<div class="igBar"><span id="lcsharp-96"><a href="#" onclick="javascript:showPlainTxt('csharp-96'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-96">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> ShowAllItems<span style="color: #000000;">&#40;</span>Hashtable hashtable<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">object</span> key <span style="color: #0600FF;">in</span> hashtable.<span style="color: #000000;">Keys</span><span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Console.<span style="color: #000000;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">"Key: "</span> + key.<span style="color: #000000;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> + <span style="color: #000000;">"; Value: "</span> + hashtable<span style="color: #000000;">&#91;</span>key<span style="color: #000000;">&#93;</span>.<span style="color: #000000;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Wenn diese Methode mit dem Hashtable <em>ht_pass</em> als Parameter aufgerufen wird....</p>
<div class="igBar"><span id="lcsharp-97"><a href="#" onclick="javascript:showPlainTxt('csharp-97'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-97">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">this</span>.<span style="color: #000000;">ShowAllItems</span><span style="color: #000000;">&#40;</span>ht_pass<span style="color: #000000;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>... erfolgt die Ausgabe der Schlüssel-Werte-Paare.</p>
<p>Ausgabe:</p>
<blockquote><p> Key: daniela; Value: hamburg<br />
Key: benny; Value: porsche<br />
Key: sarah; Value: universität<br />
Key: thomas; Value: kaufmann<br />
Key: david; Value: programmierer</p></blockquote>
<h4>Verschachtelung von Hashtables</h4>
<p>Ein weiterer Hashtable wird deklariert, welcher eine Beschreibung (Value) zu einem Objekt (Key) bereithält. Die Beschreibung soll einen Hinweis/Tipp auf ein Passwort sein.</p>
<div class="igBar"><span id="lcsharp-98"><a href="#" onclick="javascript:showPlainTxt('csharp-98'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-98">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Hashtable ht_tipp; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Dazu wird eine neue Methode implementiert, die den Hashtable instanziiert und mit Werten füllt.</p>
<div class="igBar"><span id="lcsharp-99"><a href="#" onclick="javascript:showPlainTxt('csharp-99'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-99">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> InitializeHashtable2<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">this</span>.<span style="color: #000000;">ht_tipp</span> = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0600FF;">new</span></a> Hashtable<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">this</span>.<span style="color: #000000;">ht_tipp</span>.<span style="color: #000000;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">"hamburg"</span>, <span style="color: #000000;">"stadt"</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">this</span>.<span style="color: #000000;">ht_tipp</span>.<span style="color: #000000;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">"bmw"</span>, <span style="color: #000000;">"automarke"</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">this</span>.<span style="color: #000000;">ht_tipp</span>.<span style="color: #000000;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">"porsche"</span>, <span style="color: #000000;">"automarke"</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">this</span>.<span style="color: #000000;">ht_tipp</span>.<span style="color: #000000;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">"universität"</span>, <span style="color: #000000;">"öffentliche Einrichtung"</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Wie im Beispiel zu erkennen, können Schlüssel-Werte-Paare auch mit der Methode <em>Add() </em>hinzugefügt werden. Der erste Parameter der Methode <em>Add()</em> ist der Schlüssel, der zweite Parameter der Wert.</p>
<p>Diese Methode InitializeHashtable2() soll nun ausgeführt werden:</p>
<div class="igBar"><span id="lcsharp-100"><a href="#" onclick="javascript:showPlainTxt('csharp-100'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-100">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">InitializeHashtable2<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Nun können wir einem Dritten einen Tipp geben, welches Passwort <em>benny</em> benutzt.</p>
<div class="igBar"><span id="lcsharp-101"><a href="#" onclick="javascript:showPlainTxt('csharp-101'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-101">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">string</span> benutzer = <span style="color: #000000;">"benny"</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Console.<span style="color: #000000;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">"Ein Tipp für das Passwort von '"</span> + benutzer + <span style="color: #000000;">"': "</span> + <span style="color: #0600FF;">this</span>.<span style="color: #000000;">ht_tipp</span><span style="color: #000000;">&#91;</span><span style="color: #0600FF;">this</span>.<span style="color: #000000;">ht_pass</span><span style="color: #000000;">&#91;</span>benutzer<span style="color: #000000;">&#93;</span>.<span style="color: #000000;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>.<span style="color: #000000;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>oder besser:</p>
<div class="igBar"><span id="lcsharp-102"><a href="#" onclick="javascript:showPlainTxt('csharp-102'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-102">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">string</span> benutzer = <span style="color: #000000;">"benny"</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Console.<span style="color: #000000;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">"Ein Tipp für das Passwort von '"</span> + benutzer + <span style="color: #000000;">"': "</span> + <span style="color: #0600FF;">this</span>.<span style="color: #000000;">GetHashTableValueByIndey</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>.<span style="color: #000000;">ht_tipp</span>, <span style="color: #0600FF;">this</span>.<span style="color: #000000;">GetHashTableValueByIndey</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>.<span style="color: #000000;">ht_pass</span>, benutzer<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Ausgabe:</p>
<blockquote><p>Ein Tipp für das Passwort von 'benny': automarke</p></blockquote>
<p>Die beiden Hashtables <em>ht_pass </em>und <em>ht_tipp</em> wurden verschachtelt. Der Wert von <em>ht_pass</em> mit dem Schlüssel <em>benny</em> ist der Schlüssel für einen Wert des Hashtable <em>ht_tipp</em>.</p>
<p>Die Verschachtelung über die Methode <em>GetHashTableValueByIndey() </em>ist sinnvoller, da diese eigenimplementierte Methode auf nicht vorhandene Schlüssel eingeht und eine Exception verhindert.</p>
<p>Ein Beispiel für einen nicht vorhandenen Schlüssel:</p>
<div class="igBar"><span id="lcsharp-103"><a href="#" onclick="javascript:showPlainTxt('csharp-103'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-103">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">benutzer = <span style="color: #000000;">"jens"</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Console.<span style="color: #000000;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">"Ein Tipp für das Passwort von '"</span> + benutzer + <span style="color: #000000;">": "</span> + <span style="color: #0600FF;">this</span>.<span style="color: #000000;">GetHashTableValueByIndey</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>.<span style="color: #000000;">ht_tipp</span>, <span style="color: #0600FF;">this</span>.<span style="color: #000000;">GetHashTableValueByIndey</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>.<span style="color: #000000;">ht_pass</span>, benutzer<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Ausgabe:</p>
<blockquote><p>Ein Tipp für das Passwort von 'jens: [Wert-Schlüssel-Paar mit Schlüssel '[Wert-Schlüssel-Paar mit Schlüssel 'jens' existiert nicht]' existiert nicht]</p></blockquote>
<h4><!--more--><strong>Nachfolgend den gesamten Quelltext im Zusammenhang:</strong></h4>
<div class="igBar"><span id="lcsharp-104"><a href="#" onclick="javascript:showPlainTxt('csharp-104'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-104">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">using</span> <span style="color: #000000;">System</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">using</span> <span style="color: #000000;">System</span>.<span style="color: #000000;">Collections</span>.<span style="color: #000000;">Generic</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">using</span> <span style="color: #000000;">System</span>.<span style="color: #000000;">Collections</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">using</span> <span style="color: #000000;">System</span>.<span style="color: #000000;">ComponentModel</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">using</span> <span style="color: #000000;">System</span>.<span style="color: #000000;">Data</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">using</span> <span style="color: #000000;">System</span>.<span style="color: #000000;">Text</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">namespace</span> ConsoleApplication_stack</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">class</span> Program</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008200; font-style: italic;">// Deklaration</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; Hashtable ht_pass;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; Hashtable ht_tipp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">private</span> Program<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008200; font-style: italic;">// Instanziierung des Hashtable ht_pass</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ht_pass = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0600FF;">new</span></a> Hashtable<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008200; font-style: italic;">// Anlegen von Schlüssel-Werte-Paaren, ähnlich einem normalen Array</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ht_pass<span style="color: #000000;">&#91;</span><span style="color: #000000;">"benny"</span><span style="color: #000000;">&#93;</span>&nbsp; &nbsp; = <span style="color: #000000;">"porsche"</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ht_pass<span style="color: #000000;">&#91;</span><span style="color: #000000;">"david"</span><span style="color: #000000;">&#93;</span>&nbsp; &nbsp; = <span style="color: #000000;">"programmierer"</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ht_pass<span style="color: #000000;">&#91;</span><span style="color: #000000;">"thomas"</span><span style="color: #000000;">&#93;</span>&nbsp; &nbsp;= <span style="color: #000000;">"kaufmann"</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ht_pass<span style="color: #000000;">&#91;</span><span style="color: #000000;">"daniela"</span><span style="color: #000000;">&#93;</span>&nbsp; = <span style="color: #000000;">"hamburg"</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ht_pass<span style="color: #000000;">&#91;</span><span style="color: #000000;">"sarah"</span><span style="color: #000000;">&#93;</span>&nbsp; &nbsp; = <span style="color: #000000;">"universität"</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.<span style="color: #000000;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>.<span style="color: #000000;">ht_pass</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">"benny"</span><span style="color: #000000;">&#93;</span>.<span style="color: #000000;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.<span style="color: #000000;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>.<span style="color: #000000;">GetHashTableValueByIndey</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>.<span style="color: #000000;">ht_pass</span>,<span style="color: #000000;">"key1"</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.<span style="color: #000000;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>.<span style="color: #000000;">GetHashTableValueByIndey</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>.<span style="color: #000000;">ht_pass</span>, <span style="color: #000000;">"thomas"</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">this</span>.<span style="color: #000000;">ShowAllItems</span><span style="color: #000000;">&#40;</span>ht_pass<span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; InitializeHashtable2<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">string</span> benutzer = <span style="color: #000000;">"benny"</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.<span style="color: #000000;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">"Ein Tipp für das Passwort von '"</span> + benutzer + <span style="color: #000000;">"': "</span> + <span style="color: #0600FF;">this</span>.<span style="color: #000000;">ht_tipp</span><span style="color: #000000;">&#91;</span><span style="color: #0600FF;">this</span>.<span style="color: #000000;">ht_pass</span><span style="color: #000000;">&#91;</span>benutzer<span style="color: #000000;">&#93;</span>.<span style="color: #000000;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>.<span style="color: #000000;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.<span style="color: #000000;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">"Ein Tipp für das Passwort von '"</span> + benutzer + <span style="color: #000000;">"': "</span> + <span style="color: #0600FF;">this</span>.<span style="color: #000000;">GetHashTableValueByIndey</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>.<span style="color: #000000;">ht_tipp</span>, <span style="color: #0600FF;">this</span>.<span style="color: #000000;">GetHashTableValueByIndey</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>.<span style="color: #000000;">ht_pass</span>, benutzer<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; benutzer = <span style="color: #000000;">"jens"</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.<span style="color: #000000;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">"Ein Tipp für das Passwort von '"</span> + benutzer + <span style="color: #000000;">": "</span> + <span style="color: #0600FF;">this</span>.<span style="color: #000000;">GetHashTableValueByIndey</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>.<span style="color: #000000;">ht_tipp</span>, <span style="color: #0600FF;">this</span>.<span style="color: #000000;">GetHashTableValueByIndey</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>.<span style="color: #000000;">ht_pass</span>, benutzer<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008200; font-style: italic;">/// &lt;summary&gt;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008200; font-style: italic;">/// Liefert den Wert eines Hastables zurück</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008200; font-style: italic;">/// &lt;/summary&gt;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008200; font-style: italic;">/// &lt;param name=&quot;ht&quot;&gt;Referenz des Hashtable&lt;/param&gt;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008200; font-style: italic;">/// &lt;param name=&quot;key&quot;&gt;Schlüssel&lt;/param&gt;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008200; font-style: italic;">/// &lt;returns&gt;Wert&lt;/returns&gt;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">string</span> GetHashTableValueByIndey<span style="color: #000000;">&#40;</span>Hashtable ht, <span style="color: #0600FF;">string</span> key<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">string</span> return_string;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>ht != <span style="color: #0600FF;">null</span> &amp;&amp; ht.<span style="color: #000000;">ContainsKey</span><span style="color: #000000;">&#40;</span>key<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return_string = ht<span style="color: #000000;">&#91;</span>key<span style="color: #000000;">&#93;</span>.<span style="color: #000000;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">else</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return_string = <span style="color: #000000;">"[Wert-Schlüssel-Paar mit Schlüssel '"</span> + key + <span style="color: #000000;">"' existiert nicht]"</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">return</span> return_string;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> InitializeHashtable2<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">this</span>.<span style="color: #000000;">ht_tipp</span> = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0600FF;">new</span></a> Hashtable<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">this</span>.<span style="color: #000000;">ht_tipp</span>.<span style="color: #000000;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">"hamburg"</span>, <span style="color: #000000;">"stadt"</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">this</span>.<span style="color: #000000;">ht_tipp</span>.<span style="color: #000000;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">"bmw"</span>, <span style="color: #000000;">"automarke"</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">this</span>.<span style="color: #000000;">ht_tipp</span>.<span style="color: #000000;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">"porsche"</span>, <span style="color: #000000;">"automarke"</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">this</span>.<span style="color: #000000;">ht_tipp</span>.<span style="color: #000000;">Add</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">"universität"</span>, <span style="color: #000000;">"öffentliche Einrichtung"</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> ShowAllItems<span style="color: #000000;">&#40;</span>Hashtable hashtable<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">object</span> key <span style="color: #0600FF;">in</span> hashtable.<span style="color: #000000;">Keys</span><span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.<span style="color: #000000;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">"Key: "</span> + key.<span style="color: #000000;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> + <span style="color: #000000;">"; Value: "</span> + hashtable<span style="color: #000000;">&#91;</span>key<span style="color: #000000;">&#93;</span>.<span style="color: #000000;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Program programm = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0600FF;">new</span></a> Program<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://www.der-wirtschaftsingenieur.de/index.php/hashtable-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Events in C#</title>
		<link>http://www.der-wirtschaftsingenieur.de/index.php/events-in-c/</link>
		<comments>http://www.der-wirtschaftsingenieur.de/index.php/events-in-c/#comments</comments>
		<pubDate>Wed, 12 Mar 2008 23:33:42 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.der-wirtschaftsingenieur.de/index.php/2008/03/13/events-in-c/</guid>
		<description><![CDATA[Oft ist es notwendig, dass ein Quellcode ausgeführt wird, wenn ein Button geklickt, ein Programm gestartet oder ein Status erreicht wurde. In vielen Fällen liegt es hierbei an "fremden" Objekten, dass etwas passiert ist, dann soll jedoch entsprechend darauf reagiert werden. Eine Nachricht muss von diesem "fremden" Objekt empfangen werden, wenn das entsprechende Ereignis eintritt. [...]]]></description>
			<content:encoded><![CDATA[<p>Oft ist es notwendig, dass ein Quellcode ausgeführt wird, wenn ein <strong>Button</strong> geklickt, ein Programm gestartet oder ein Status erreicht wurde. In vielen Fällen liegt es hierbei an "fremden" Objekten, dass etwas passiert ist, dann soll jedoch entsprechend darauf reagiert werden. Eine Nachricht muss von diesem "fremden" Objekt empfangen werden, wenn das entsprechende Ereignis eintritt. Dieses <strong>Ereignis</strong>/<strong>Event</strong> ist der springende Punkt, es kann vom eigenen Programmcode (im Algorithmus oder durch Interaktion mit Benutzer oder Netzwerk) oder auch vom Betriebssystem ausgehen.</p>
<p>Das Betriebssystem und das Framework stellen sehr viele vorgefertigte <strong>Events</strong> zur Verfügung, welche nur <strong>abonniert</strong> werden müssen. Bei der Arbeit mit <em>Windows.Forms</em> wird das Ereignis des <em>Button.Click</em> beispielsweise häufig verwendet. Dieses wird z.B. von einem Button <em>button1</em> wie folgt von der eigenen Klasse (welche auf das Ereignis reagieren soll) <strong>abonniert</strong>:</p>
<div class="igBar"><span id="lcsharp-118"><a href="#" onclick="javascript:showPlainTxt('csharp-118'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-118">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">this</span>.<span style="color: #000000;">button1</span>.<span style="color: #000000;">Click</span> += <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0600FF;">new</span></a> <span style="color: #000000;">System</span>.<span style="color: #000000;">EventHandler</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>.<span style="color: #000000;">button1_Click</span><span style="color: #000000;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Dabei wird eine Methode einem <a href="http://www.der-wirtschaftsingenieur.de/index.php/delegate-in-c/" title="Delegate" target="_blank"><strong>Delegate</strong></a> (Funktionszeiger) zugewiesen. Diese Methode heißt im Beispiel <em>button1_Click</em>, sie soll ausgeführt werden, wenn der <em>button1</em> geklickt wird.</p>
<div class="igBar"><span id="lcsharp-119"><a href="#" onclick="javascript:showPlainTxt('csharp-119'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-119">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">void</span> button1_Click<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">object</span> sender, EventArgs e<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">\\ tu etwas</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Die Methode ist die <strong>Event</strong>-Reaktion. Wird das <strong>Event</strong> ausgeführt, also der Button geklickt, führt ein Funktionszeiger, der auf diese Methode zeigt, diese aus. Die Methode hat zwei Parameter (Pflichtparameter bei vom Framework vordefinierten Events), auf welche später weiter eingegangen wird.</p>
<p><a href="http://www.der-wirtschaftsingenieur.de/bilder/events.jpg" title="Eventhandling" target="_blank"><img src="http://www.der-wirtschaftsingenieur.de/bilder/events.jpg" alt="Eventhandling" height="336" width="425" /></a></p>
<p>Tritt ein Objekt ein <strong>Event</strong> los, werden alle abonnierenden Objekte benachrichtigt. Bei <em>Button.Click</em> ist der Button das publizierende Objekt. Die Abonnenten können nach der Benachrichtigung, dass ein <strong>Event</strong> eingetreten ist, entsprechend reagieren. Wie die empfangenden Objekte auf das Ereignis reagieren, interessiert den Event-Publizierer nicht. Dabei wird eine 1-zu-n Beziehung angewandt, <strong>denn ein Publizierer kann sehr viele Abonnenten benachrichten</strong>.</p>
<p><span id="more-79"></span></p>
<h3><strong> Ein eigenes Event definieren</strong></h3>
<p>Nachfolgend soll ein kleines Programm beschrieben werden, welches von einem Startwert zu einem Endwert zählt. Dabei wird über einen Parameter eine ArrayList an Zahlen übergeben, welche als kritisch eingestuft werden. Stößt die Zählschleife auf diese Zahlen, soll sie sofort (<strong>per Event</strong>) Alarm schlagen! Wie abonnierende Objekte darauf dann reagieren, ist der Zählschleife ganz egal, nur Alarmschlagen liegt in ihrem Aufgabenbereich.</p>
<div class="igBar"><span id="lcsharp-120"><a href="#" onclick="javascript:showPlainTxt('csharp-120'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-120">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Count<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">int</span> start, <span style="color: #0600FF;">int</span> end, ArrayList items<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">int</span> i = start; i &lt;= end; i++<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>items.<span style="color: #000000;">Contains</span><span style="color: #000000;">&#40;</span>i<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>&nbsp; &nbsp; &nbsp; <span style="color: #008200; font-style: italic;">// Ein Item erwischt?</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008200; font-style: italic;">// mach was, aber nicht hier!!</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Um ein <strong>Event-Handling</strong> (darf beruhigt als Ereignis-Behandlung bezeichnet werden) einzurichten, ist ein EventHandler notwendig. Dieser ist nichts weiter als ein <a href="http://www.der-wirtschaftsingenieur.de/index.php/2008/03/10/delegate-in-c/" title="Delegate" target="_blank">Delegate</a> (<strong>Funktionszeiger</strong>), welcher auf eine oder mehrere Methoden zeigt. Das Delegate überbrückt die Distanz zwischen Event-Sender und den entkoppelten Event-Empfängern. Im folgenden Beispiel wird ein <strong>EventHandler</strong> namens <em>CountEventHandler</em> definiert, er hat zwei Parameter vom Typ <strong><em>object</em></strong> bzw. <em><strong>InfoEventArgs</strong></em>.</p>
<div class="igBar"><span id="lcsharp-121"><a href="#" onclick="javascript:showPlainTxt('csharp-121'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-121">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008200; font-style: italic;">// Delegate für die Abonnenten</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">delegate</span> <span style="color: #0600FF;">void</span> CountEventHandler<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">object</span> Instanz, InfoEventArgs EventInfo<span style="color: #000000;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Die beiden Parameter sind nicht unbedingt für eine Event-Behandlung notwendig, haben jedoch ihren Zweck. Vordefinierte Events des Frameworks sind nach gleichem/ähnlichem Schema eingerichtet. Der erste Parameter vom Typ <em>object</em> verweist auf den Absender (Ereignisauslöser). So kann der Empfänger wissen, welches Objekt das Ereignis auslöste bzw. bei welchem es ausgelöst wurde (dies kann z.B. wichtig sein, wenn mehrere EventHandler auf die gleiche Methode verweisen, also eine n-zu-n Beziehung umgesetzt wurde).</p>
<p>Der zweite Parameter vom Typ <em>InfoEventArgs</em> ist ein spezieller Informationslieferant. Er soll verschiedene Auskünfte über das Event geben.<br />
I.d.R. (spezielle Events ausgenommen) wird statt des Parameters vom Typ <em>InfoEventArgs</em> ein Parameter vom Typ <strong><em>EventArgs</em></strong> übergeben. Dieser Parameter wird bei der Event-Reaktion eine wichtige Rolle spielen.</p>
<p>Das Delegate muss natürlich noch deklariert werden:</p>
<div class="igBar"><span id="lcsharp-122"><a href="#" onclick="javascript:showPlainTxt('csharp-122'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-122">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008200; font-style: italic;">// Deklaration des Delegates</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> CountEventHandler ItemMatch; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Nun existiert ein Delegate vom Typ <em>CountEventHandler</em> mit dem Instanz-Namen <em>ItemMatch</em>. Dieses soll Abonnenten benachrichtigen, wenn eine kritische Zahl erreicht wurde.</p>
<p><em>InfoEventArgs</em> ist eine, eigens für dieses Event neuverfasste, Definition von der Klasse <strong><em>EventArgs</em></strong> aus dem .NET-Framework. Dabei wird hier eine eigene Klasse geschrieben, <strong>die von der Klasse <em>EventArgs</em> erbt</strong>, denn die vorgegebene Klasse <em>EventArgs</em> stellt für dieses Beispiel eine Information zu wenig zur Verfügung.</p>
<div class="igBar"><span id="lcsharp-123"><a href="#" onclick="javascript:showPlainTxt('csharp-123'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-123">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">class</span> InfoEventArgs : EventArgs</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">readonly</span> <span style="color: #0600FF;">int</span> number;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">public</span> InfoEventArgs<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">int</span> number<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">this</span>.<span style="color: #000000;">number</span> = number;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Zweck dieser Eigendefinition ist die Bereitstellung der Zahl, welche für das Event (nämlich dem Alarm) ausschlaggebend war. Diese Zahl soll der Ereignisauslöser bereitstellen und der Ereignis-Abonnent erhalten können, jedoch wirklich nur erhalten, nicht verändern (daher <strong>readonly</strong>).</p>
<p>Sind keine maßgeschneiderten Event-Informationen notwendig, kann die vorgefertigte <strong><em>EventArgs</em></strong> Klasse verwendet werden (sie darf bei eigenen Events auch ganz weggelassen werden, wenn keine Event-Informationen benötigt werden und die Abonnenten-Benachrichtigung alleine ausreicht). Dann muss eine Instanz der Klasse <em>EventArgs</em> deklariert und instanziiert werden:</p>
<div class="igBar"><span id="lcsharp-124"><a href="#" onclick="javascript:showPlainTxt('csharp-124'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-124">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">System</span>.<span style="color: #000000;">EventArgs</span> eventInfo = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0600FF;">new</span></a> <span style="color: #000000;">System</span>.<span style="color: #000000;">EventArgs</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Da hier jedoch die eigene Definition benutzt wird, wird stattdessen die vorherig beschriebene <em>InfoEventArgs</em> Klasse deklariert.</p>
<div class="igBar"><span id="lcsharp-125"><a href="#" onclick="javascript:showPlainTxt('csharp-125'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-125">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008200; font-style: italic;">// Instanzierung der Klasse für die Event-Informationen</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">InfoEventArgs eventInfo; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Die eigentliche Zählschleife wird um einige Anweisungen erweitert. Wenn eine kritische Zahl erreicht wurde, also die Bedingung <em>items.Contains(i)</em> erfüllt ist, soll Alarm geschlagen werden. Dazu wird eine Instanz der InfoEventArgs erstellt und <strong>dieser über den Konstruktor-Parameter die kritische Zahl übergeben</strong>. Dann werden alle Abonnenten benachrichtigt. Gibt es keine Abonnenten, ist das Delegate mit dem Wert <em>Null</em> ausgewiesen, so dass (dank der if-Bedingung) die Benachrichtigung nicht versucht wird. Die Benachrichtigung geschieht durch das Delegate als Stellvertreter für die Methoden der Abonnenten. Diese Methoden, auf welche das Delegate zeigt, müssen zwei Parameter empfangen, wie es bereits bei der Delegate-Definition festgeschrieben wurde.</p>
<p>Zu einem den Parameter vom Typ <em>object</em>, welcher das eigene Objekt (referenziert durch das Schlüsselwort <strong><em>this</em></strong>), zum anderen den Parameter, welcher Auskunft über das Event selbst geben soll, das Objekt <em>eventInfo</em>.</p>
<div class="igBar"><span id="lcsharp-126"><a href="#" onclick="javascript:showPlainTxt('csharp-126'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-126">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008200; font-style: italic;">// Zählschleife</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Count<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">int</span> start, <span style="color: #0600FF;">int</span> end, ArrayList items<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">int</span> i = start; i &lt;= end; i++<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>items.<span style="color: #000000;">Contains</span><span style="color: #000000;">&#40;</span>i<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>&nbsp; &nbsp; &nbsp; <span style="color: #008200; font-style: italic;">// Ein Item erwischt?</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008200; font-style: italic;">// Event-Information bereitstellen</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;eventInfo = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0600FF;">new</span></a> InfoEventArgs<span style="color: #000000;">&#40;</span>i<span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>ItemMatch != <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008200; font-style: italic;">// Action! Benachrichtigung der Abonnenten!</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ItemMatch<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>, eventInfo<span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Das Event ist nun vollständig eingerichtet und wird vom Objekt publiziert, d.h. es kann von anderen Objekten (die Zugriff auf das Objekt haben) abonniert werden.</p>
<h4><strong>Einen Abonnenten einrichten: </strong></h4>
<p>Nun wird eine neue, eigene Klasse <em>Alarm</em> definiert, welche Alarm schlagen soll. Diese Klasse hat einen Konstruktor, der das Objekt als Parameter empfängt, welches das Event anbietet. In diesem Beispiel ist es das Objekt der Klasse <em>Counter</em>, das zur Laufzeit instanziiert werden soll.</p>
<div class="igBar"><span id="lcsharp-127"><a href="#" onclick="javascript:showPlainTxt('csharp-127'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-127">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">class</span> EventAlarm</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">public</span> EventAlarm<span style="color: #000000;">&#40;</span>Counter counter<span style="color: #000000;">&#41;</span>&nbsp; &nbsp; <span style="color: #008200; font-style: italic;">// Konstruktor</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Alarm<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">int</span> item<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; Console.<span style="color: #000000;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">"Die kritische Zahl "</span> + item.<span style="color: #000000;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> + <span style="color: #000000;">" wurde gefunden!"</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Wenn die Instanz der Klasse <em>Counter</em> das Event lostritt, soll die Methode <em>Alarm</em> der Klasse <em>EventAlarm</em> aufgerufen werden. Dazu wird eine weitere Methode implementiert:</p>
<div class="igBar"><span id="lcsharp-128"><a href="#" onclick="javascript:showPlainTxt('csharp-128'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-128">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> ItemMatched<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">object</span> sender, InfoEventArgs e<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #0600FF;">this</span>.<span style="color: #000000;">Alarm</span><span style="color: #000000;">&#40;</span>e.<span style="color: #000000;">number</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Diese Methode <em>ItemMatched</em> ruft die Methode <em>Alarm</em> auf. Nun spielen die Parameter vom Typ <em>object</em> bzw. <em>InfoEventArgs</em> wieder eine Rolle. Diese Parameter müssen in die Signatur der Methode, denn der EventHandler (Delegate) soll auf diese Methode zeigen. Der EventHandler setzt diese Parameter (wie in der Definition des EventHandlers festgelegt) voraus.</p>
<p>Der Parameter <em>e</em> vom Typ <em>InfoEventArgs</em> bietet Zugriff auf die Zahl, die zum Event führte (siehe dazu die Klassen-Definition InfoEventArgs). Der Parameter darf natürlich beliebig benannt werden, Events des .NET Frameworks nennen ihn standardmäßig jedoch auch <em>e</em>.</p>
<p>Damit diese Methode <em>ItemMatched</em> auch im Falle eines Events aufgerufen wird, muss das Objekt der Klasse <em>EventAlarm</em> das Event mit dieser Methode abonnieren, dies geschieht <strong>im Konstruktor</strong> wie folgt:</p>
<div class="igBar"><span id="lcsharp-129"><a href="#" onclick="javascript:showPlainTxt('csharp-129'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-129">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> EventAlarm<span style="color: #000000;">&#40;</span>Counter counter<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;<span style="color: #008200; font-style: italic;">// Der Funktionszeiger soll auch auf die Funktion &quot;ItemMatched&quot; zeigen</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp;counter.<span style="color: #000000;">ItemMatch</span> += <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0600FF;">new</span></a> Counter.<span style="color: #000000;">CountEventHandler</span><span style="color: #000000;">&#40;</span>ItemMatched<span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Das Objekt <em>counter</em> der Klasser <em>Counter</em>  stellt den EventHandler <em>ItemMatch</em> zur Verfügung. Eine neue Referenz auf eine Methode wird mittels <em>new</em>-Operator eingerichtet, der Methodenname wird als Parameter (als Referenz) übergeben. Da bekannt ist, dass es nur einen einzigen Abonnent gibt, dürfte auf die Zuweisung mit "+=" verzichtet werden. Sobald es jedoch weitere Abonnenten gibt, würden diese überschrieben werden (und als Abonnenten verloren gehen), daher sollte immer die "+=" Operatorkombination verwendet werden.</p>
<p>Das Event ist nun mit einem Abonnent vollständig eingerichtet.</p>
<p><!--more--><strong> Nachfolgend der gesamte Quellcode im Zusammenhang:</strong></p>
<div class="igBar"><span id="lcsharp-130"><a href="#" onclick="javascript:showPlainTxt('csharp-130'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-130">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008200; font-style: italic;">/// &lt;summary&gt;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #008200; font-style: italic;">/// Start-Klasse</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #008200; font-style: italic;">/// &lt;/summary&gt;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">class</span> Program</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Program program = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0600FF;">new</span></a> Program<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">private</span> Program<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Counter counter = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0600FF;">new</span></a> Counter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; EventAlarm eventAlarm = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0600FF;">new</span></a> EventAlarm<span style="color: #000000;">&#40;</span>counter<span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ArrayList items = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0600FF;">new</span></a> ArrayList<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; items.<span style="color: #000000;">AddRange</span><span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0600FF;">new</span></a> <span style="color: #0600FF;">object</span> <span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span> <span style="color: #FF0000;color:#800000;">2</span>, <span style="color: #FF0000;color:#800000;">50</span>, <span style="color: #FF0000;color:#800000;">88</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counter.<span style="color: #000000;">Count</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;color:#800000;">0</span>, <span style="color: #FF0000;color:#800000;">100</span>, items<span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #008200; font-style: italic;">/// &lt;summary&gt;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #008200; font-style: italic;">/// Zusätzliche Event-Information in Klasse lagern</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #008200; font-style: italic;">/// Erbt von EventArgs, welche Standard-Informationen zur Verfügung stellt</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #008200; font-style: italic;">/// &lt;/summary&gt;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">class</span> InfoEventArgs : EventArgs</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">readonly</span> <span style="color: #0600FF;">int</span> number;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> InfoEventArgs<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">int</span> number<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">this</span>.<span style="color: #000000;">number</span> = number;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #008200; font-style: italic;">/// &lt;summary&gt;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #008200; font-style: italic;">/// Event-Publizierer</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #008200; font-style: italic;">/// &lt;/summary&gt;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">class</span> Counter</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008200; font-style: italic;">// Delegate für die Abonnenten</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">delegate</span> <span style="color: #0600FF;">void</span> CountEventHandler<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">object</span> Instanz, InfoEventArgs EventInfo<span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008200; font-style: italic;">// Deklaration des Delegates</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> CountEventHandler ItemMatch;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008200; font-style: italic;">// Instanzierung der Klasse für die Event-Informationen</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; InfoEventArgs eventInfo;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> Counter<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>&nbsp; &nbsp; <span style="color: #008200; font-style: italic;">// Konstruktor</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008200; font-style: italic;">// Zählschleife</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Count<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">int</span> start, <span style="color: #0600FF;">int</span> end, ArrayList items<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">int</span> i = start; i &lt;= end; i++<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>items.<span style="color: #000000;">Contains</span><span style="color: #000000;">&#40;</span>i<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>&nbsp; &nbsp; &nbsp; <span style="color: #008200; font-style: italic;">// Ein Item erwischt?</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008200; font-style: italic;">// Event-Information bereitstellen</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; eventInfo = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0600FF;">new</span></a> InfoEventArgs<span style="color: #000000;">&#40;</span>i<span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>ItemMatch != <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008200; font-style: italic;">// Action! Benachrichtigung der Abonnenten!</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ItemMatch<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>, eventInfo<span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #008200; font-style: italic;">/// &lt;summary&gt;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #008200; font-style: italic;">/// Diese Klasse aboniert das Event und schlägt Alarm, wenn es eintritt</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #008200; font-style: italic;">/// &lt;/summary&gt;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">class</span> EventAlarm</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> EventAlarm<span style="color: #000000;">&#40;</span>Counter counter<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008200; font-style: italic;">// Der Funktionszeiger soll auch auf die Funktion &quot;ItemMatched&quot; zeigen</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counter.<span style="color: #000000;">ItemMatch</span> += <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #0600FF;">new</span></a> Counter.<span style="color: #000000;">CountEventHandler</span><span style="color: #000000;">&#40;</span>ItemMatched<span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> Alarm<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">int</span> item<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.<span style="color: #000000;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">"Die kritische Zahl "</span> + item.<span style="color: #000000;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> + <span style="color: #000000;">" wurde gefunden!"</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> ItemMatched<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">object</span> sender, InfoEventArgs e<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">this</span>.<span style="color: #000000;">Alarm</span><span style="color: #000000;">&#40;</span>e.<span style="color: #000000;">number</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; <span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Ausgabe:</p>
<p><img src="http://www.der-wirtschaftsingenieur.de/bilder/events_ergebnis.JPG" height="332" width="663" /><br /><br />
<center><script type="text/javascript"><!--
google_ad_client = "pub-4204488675913141";
/* der-wirtschaftsingenieur5 (horizontale Reihe) */
google_ad_slot = "6242293900";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></center></p>
]]></content:encoded>
			<wfw:commentRss>http://www.der-wirtschaftsingenieur.de/index.php/events-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

