tag:blogger.com,1999:blog-72592129091213184242024-02-18T17:55:54.007-08:00Mighty C#Dineshhttp://www.blogger.com/profile/00223829952726243429noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-7259212909121318424.post-28925717695919381862011-06-26T23:19:00.001-07:002011-06-27T00:37:33.987-07:00Custom Events in C# - Basic Example<span style="font-family: verdana;font-size:85%;" >Creating custom events are very tough for me until i have started writing few custom events for my application. Here i wanted to share the way i have learned writing Custom Events. I hope, it will help the others too.<br /><br />Before we go into the example, let me list out the concepts we need to know.<br /><br />1. delegates, also called as function pointers<br /><br />2. </span><span style="color: rgb(51, 102, 255); font-family: verdana;font-size:85%;" >event </span><span style="font-family: verdana;font-size:85%;" >keyword<br /><br />3. Event arguments<br /><br />4. += and -= operators.<br /><br />Thats it. We should know what are these and what functionality they provide.<br /><br />A custom event can be implemented in 7 steps<br />1. Define event arguments , using which we can pass some information along with the event. Normally we have to create a class extending </span><span style="font-weight: bold; font-family: verdana;font-size:85%;" >EventArgs</span><span style="font-family: verdana;font-size:85%;" > with required members. One of the best practice to suffix our class name with 'EventArgs', so that readability and maintainability will be more.<br /><br />2. Create the Class on which you wanted to raise the event.<br /><br />3. Define a delegate which will accept the event handler from the class which is intended to receive and handle this event. basically this delegate defines the signature of the event handler method that target application has to use.<br /><br />4. create event member by using </span><span style="font-weight: bold; font-family: verdana;font-size:85%;" >event </span><span style="font-family: verdana;font-size:85%;" >keyword and specify the event name. Whatever the name we uses here, will be visible to clients while adding the event handler.<br /><br />5. Include the logic to call the event. Basically this part provides the link between event and it's handler. Based some condition(that's where event is raised) we will call event handler.<br /><br />6. In client application, Add event handler using += operator. we can use -= operator to unregister the event.<br /><br />7. Implement the event handler method to provide the logic that ehat should happen if the event fires.<br /><br />Here is the Example,<br />Car Speed Controller Example. This is all about, if the car exceeds 60KMPH then warn the user saying Car speed is exceeding 60KMPH.<br /><br />#Step1<br />Creating a class for passing event arguments.</span><br /> <pre style=" background: white;font-family:Consolas;font-size:13;color:black;"><span style="color:green;">//CarSpeedEventArgs.cs - Step 1</span><br /><span style="color:blue;">namespace</span> CarSpeedControllerEvents<br />{<br /> <span style="color:blue;">public</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">CarSpeedEventArgs</span><br /> {<br /> <span style="color:blue;">private</span> <span style="color:blue;">string</span> _message;<br /><br /> <span style="color:blue;">public</span> CarSpeedEventArgs(<span style="color:blue;">string</span> strMessage)<br /> {<br /> _message = strMessage;<br /> }<br /><br /> <span style="color:blue;">public</span> <span style="color:blue;">string</span> Message<br /> {<br /> <span style="color:blue;">get</span><br /> {<br /> <span style="color:blue;">return</span> _message;<br /> }<br /> }<br /> }<br />}</pre><br /> <pre style=" background: white;font-family:Consolas;font-size:13;color:black;"><span style="color:green;">//Car.cs</span><br /><span style="color:blue;">namespace</span> CarSpeedControllerEvents<br />{<br /> <span style="color:green;">//Step 2</span><br /> <span style="color:blue;">public</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">Car</span><br /> {<br /> <span style="color:blue;">private</span> <span style="color:#2b91af;">Int32</span> _CarSpeed;<br /><br /> <span style="color:blue;">public</span> Car()<br /> {<br /> _CarSpeed = 0;<br /> }<br /> <br /> <span style="color:green;">//Step 3</span><br /> <span style="color:blue;">public</span> <span style="color:blue;">delegate</span> <span style="color:blue;">void</span> <span style="color:#2b91af;">CarSpeedController</span>(<span style="color:blue;">object</span> sender, <span style="color:#2b91af;">CarSpeedEventArgs</span> data);<br /><br /> <span style="color:green;">//Step 4</span><br /> <span style="color:blue;">public</span> <span style="color:blue;">event</span> <span style="color:#2b91af;">CarSpeedController</span> SpeedController;<br /><br /> <span style="color:blue;">public</span> <span style="color:#2b91af;">Int32</span> CarSpeed<br /> {<br /> <span style="color:blue;">set</span><br /> {<br /> _CarSpeed = <span style="color:blue;">value</span>;<br /><br /> <span style="color:green;">//Step 5</span><br /> <span style="color:blue;">if</span> (_CarSpeed > 60)<br /> {<br /> <span style="color:blue;">try</span><br /> {<br /> <span style="color:#2b91af;">CarSpeedEventArgs</span> objArgs = <span style="color:blue;">new</span> <span style="color:#2b91af;">CarSpeedEventArgs</span>(<span style="color:#a31515;">"Warning : Car speed exceeded 60KMPH"</span>);<br /> SpeedController(<span style="color:blue;">this</span>, objArgs);<br /> }<br /> <span style="color:blue;">catch</span> { }<br /> }<br /> }<br /> }<br /> <br /> }<br />}<br /></pre><br /> <pre style=" background: white;font-family:Consolas;font-size:13;color:black;"><span style="color:blue;">namespace</span> CarSpeedControllerEvents<br />{<br /> <span style="color:blue;">class</span> <span style="color:#2b91af;">Program</span><br /> {<br /> <span style="color:blue;">static</span> <span style="color:blue;">void</span> Main(<span style="color:blue;">string</span>[] args)<br /> {<br /> <span style="color:#2b91af;">Car</span> myCar = <span style="color:blue;">new</span> <span style="color:#2b91af;">Car</span>();<br /> <br /> <span style="color:green;">//Step 6 - Registering with event</span><br /> myCar.SpeedController += ControlCalSpeed;<br /><br /> <span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">" Car Current speed : 75KMPH"</span>);<br /> myCar.CarSpeed = 75;<br /> <span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">" Car Current speed : 25KMPH"</span>);<br /> myCar.CarSpeed = 25;<br /> <span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">" Car Current speed : 61KMPH"</span>);<br /> myCar.CarSpeed = 61;<br /><br /> <span style="color:green;">//Unregistering with event</span><br /> myCar.SpeedController -= ControlCalSpeed;<br /> <span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">" Car Current speed : 81KMPH"</span>);<br /> myCar.CarSpeed = 81;<span style="color:green;">//This statement won't fire an event</span><br /> <br /> <span style="color:#2b91af;">Console</span>.ReadLine();<br /><br /> }<br /><br /> <span style="color:green;">//Step 7</span><br /> <span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:blue;">void</span> ControlCalSpeed(<span style="color:blue;">object</span> sender, <span style="color:#2b91af;">CarSpeedEventArgs</span> data)<br /> {<br /> <span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">"\t ==> {0}"</span>, data.Message.ToString());<br /> }<br /> }<br />}<br /></pre><br /> <pre style="font-family: Consolas; font-size: 13; color: black; background: white;"> </pre>Dineshhttp://www.blogger.com/profile/00223829952726243429noreply@blogger.com0tag:blogger.com,1999:blog-7259212909121318424.post-64875692511872601452011-06-01T22:16:00.001-07:002011-06-01T23:01:05.165-07:00Managed Code vs Unmanaged Code<div style="text-align: justify; font-family: verdana;"><span style="font-size:85%;"><span style="font-weight: bold;">Managed code</span> is not compiled to machine code but to an intermediate language which is interpreted and executed by some service on a machine and is therefore operating within a secure framework which handles very dangerous like memory and threads for you. In modern usage this frequently means .NET but does not have to.</span><br /><br /><span style="font-size:85%;">In terms of .net, The code which is written using .net dlls and .net compliant languages such as C#, VB is called as Managed Code. Also we can say that, the code developed using .net framework is called as Managed Code. This code will be executed directly by CLR(Common Language Run time).</span><br /><br /><span style="font-size:85%;">Managed code is what created using C#, VB and other .net compliant languages. Whatever the code you write, it will be converted an intermediate language after compilation. This intermediate code is called MSIL(Microsoft Intermediate Language) code. This compiled IL code will be copied to an assembly, kept ready for usage. You can't run this assembly with out the help of CLR. Because at run time CLR's JIT compilers will convert this code into Native Code, which can be executed by Operating System. You can simple drop this assembly in to any .net installed machine and start using it. The target machine's OS be anything, but it should have .net installed. So, that's why we say, .net can create portable applications.</span><br /><br /><span style="font-size:85%;"><span style="font-weight: bold;">Unmanaged code</span> is compiled to machine code and therefore executed by the OS directly. It therefore has the ability to do damaging/powerful things Managed code does not. This is how everything used to work, so typically it's associated with old stuff like dlls</span><br /><br /><span style="font-size:85%;">A programmer needs to write many common functions in an unmanaged code. So functions like garbage collecting, exception handling, etc have to be handled explicitly.The code, which is developed outside .NET Framework is known as unmanaged code.The code written in C/C++ etc is called as unmanaged code.it can't be run directly by CLR. But we can write some wrapper classes to run unmanaged code in CLR. These wrapper classes are 2 types CCW (COM Callable Wrapper) and RCW (Runtime Callabale Wrapper).</span><br /><br /><span style="font-size:85%;">COM Callable Wrapper</span><br /><span style="font-size:85%;"><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgm5Z2VcbM02j1p98h8M1uJWzKMmh8dXvKbEi2PwXjO5PTysr9juOwyFyRVUFA-b5xBW1mabQN6aVZ7urZ4Y6-Q7sPzYlRNj4MP1AWCvLE2nrzsSg2Wvf8uQCg1FKHhI-D7xJDclNvc20w/s1600/unmanaged_code_COM.gif"><img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 304px; height: 320px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgm5Z2VcbM02j1p98h8M1uJWzKMmh8dXvKbEi2PwXjO5PTysr9juOwyFyRVUFA-b5xBW1mabQN6aVZ7urZ4Y6-Q7sPzYlRNj4MP1AWCvLE2nrzsSg2Wvf8uQCg1FKHhI-D7xJDclNvc20w/s320/unmanaged_code_COM.gif" alt="" id="BLOGGER_PHOTO_ID_5613496384977724066" border="0" /></a></span><br /><br /><span style="font-size:85%;">Runtime Callable Wrapper</span><br /><span style="font-size:85%;"><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhAfHmxYfNyAQZKTIrzVtgGdlsPQ8qYadqjT2Unh2BiORenZvsi59PtczUR7_InVVB77vWsCnWRI8BnVrDGgROUeiO433gkVseKQsy2yKJZFTb2QoJrFT6UbpUSyTw1VzMz5x3eMx2-UN4/s1600/unmanaged_code_RCW.gif"><img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 304px; height: 320px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhAfHmxYfNyAQZKTIrzVtgGdlsPQ8qYadqjT2Unh2BiORenZvsi59PtczUR7_InVVB77vWsCnWRI8BnVrDGgROUeiO433gkVseKQsy2yKJZFTb2QoJrFT6UbpUSyTw1VzMz5x3eMx2-UN4/s320/unmanaged_code_RCW.gif" alt="" id="BLOGGER_PHOTO_ID_5613496387662135202" border="0" /></a><br />For more details on the image, please refer this <a href="http://www.c-sharpcorner.com/UploadFile/puranindia/ManagedcodeUnmanagedcodeNET06252009005013AM/ManagedcodeUnmanagedcodeNET.aspx">post.</a><br /></span></div>Dineshhttp://www.blogger.com/profile/00223829952726243429noreply@blogger.com0tag:blogger.com,1999:blog-7259212909121318424.post-46329177314902921472011-05-29T21:36:00.000-07:002011-06-01T22:07:36.524-07:00Why doesn't C# support multiple inheritance?<span style="font-family:verdana;font-size:85%;">This question is the one of the most frequently asked interview question. So lets see, what it is. Here is the answer i like the most found in Stack Overflow.<br /><br /></span><blockquote style="font-family:verdana;"> <ul><li><span style="font-size:85%;">.NET was designed to support multiple languages, but not all languages can effectively support multiple inheritance. Or technically they could, but the complexities added in language semantics would make some of those languages more difficult to use (and less similar to their roots, like VB, and for backward compatibility reasons) and not worth the trade-off of being able to reuse code in the manner of multiple inheritance</span></li><li><span style="font-size:85%;">It would also make cross-language library interoperability (via CLS compliance) less of a reality than it is today, which is one of the most compelling features of .NET. There are over 50 languages supported on .NET in over 70 implementations today</span></li><li><span style="font-size:85%;">The most visible factor is language semantics complexity. In C++ we needed to add explicit language features in order to address ambiguities (such as the classic diamond problem) caused by multiple inheritance, such as the "virtual" keyword to support virtual inheritance to help the compiler resolve inheritance paths (and we had to use it correctly too)</span></li><li><span style="font-size:85%;">As we know code is written 20% of the time, but read 80% of the time. Thus advocates on simplicity side prefer not to add language features for the sake of keeping semantics simple. In comparison C# code is significantly simpler to read than C++ code, and arguably easier to write</span></li></ul> </blockquote><span style="font-family:verdana;font-size:85%;"><br /><br /></span>Dineshhttp://www.blogger.com/profile/00223829952726243429noreply@blogger.com0