<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-3985264170283763322</id><updated>2024-11-08T21:12:06.663+05:30</updated><category term="c#"/><category term="asp.net"/><category term="code"/><category term="WCF"/><category term="developer"/><category term="REST"/><category term="beginner tips"/><category term="best practise"/><category term="HTML5"/><category term="IO"/><category term="Productivity"/><category term="SOAP"/><category term="Web service"/><category term="code review"/><category term="development"/><category term="etc"/><category term="visual studio"/><category term="ADO.NET"/><category term="CORS"/><category term="CSS3"/><category term="Code design"/><category term="Connection"/><category term="Download"/><category term="JQUERY"/><category term="Mobile"/><category term="OLEDB"/><category term="PhoneGap"/><category term="WebGet"/><category term="Xamarin"/><category term="bdotnet"/><category term="captcha"/><category term="conversion"/><category term="currency"/><category term="file size"/><category term="generation"/><category term="generics"/><category term="impersonate"/><category term="number"/><category term="password"/><category term="permission"/><category term="random string"/><category term="string"/><title type='text'>Exploring .NET world !!</title><subtitle type='html'>C#, ASP.NET, HTML5, JQUERY, CSS3, SQL Server / T-SQL, Coding challenges, Latest updates etc. &#xa;&#xa;It will be awesome :)</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://pavancsharp.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3985264170283763322/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://pavancsharp.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Pavan N</name><uri>http://www.blogger.com/profile/16386797942234627578</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuxNeqFLB46wZIpHiomJ0jZ-rU9wu3NHzydxMkp0VjmkxePzjAhrm3Hjiq8dLiW8DvTXCmUUMBqgbA2MYUeJASd11n7BkbTOlfEvaIt8tW66S0xItZtDw7Rh5b2WX9DA/s220/IMG_20170223_100350.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>15</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-3985264170283763322.post-6930478454249448801</id><published>2018-04-30T15:53:00.000+05:30</published><updated>2018-04-30T15:53:49.214+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="beginner tips"/><category scheme="http://www.blogger.com/atom/ns#" term="best practise"/><category scheme="http://www.blogger.com/atom/ns#" term="Code design"/><category scheme="http://www.blogger.com/atom/ns#" term="developer"/><category scheme="http://www.blogger.com/atom/ns#" term="Productivity"/><title type='text'>Code Design - Choosing Abstract or Interface - Level 100</title><content type='html'>More often we wont get a chance to work on these aspects in early stage of your career. Even at the time of mid career getting a work related to API designing/Development is bit difficult. Unless you have a very good mentor in your initial career you will never get to know the real time. And you will end up in theoretical knowledge if you are computers academics. Others need to witness nightmares :-)&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Always try to implement good design in your day to day work. Never try to concentrate on these concepts for the matter of career growth. Only the passion to write a good code lets you to remember things. Design patterns are not meant for only senior programmers. You need to have a little bit knowledge on nitty-gritty of OOP/OOD concepts.&lt;br /&gt;&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
And I need to say that continuous practice only lets you to excel further in OOP/OOD concepts irrespective of your experience levels.&lt;br /&gt;&lt;br /&gt;Here are some basic recommendations to help you to decide whether to use an interface or an abstract class to provide polymorphism for your components.&lt;br /&gt;&lt;br /&gt;&lt;ol&gt;
&lt;li&gt;If you anticipate creating multiple versions of your component, create an abstract class. Abstract classes provide a simple and easy way to version your components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface.&lt;/li&gt;
&lt;li&gt;If the functionality you are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes.&lt;/li&gt;
&lt;li&gt;If you are designing small, concise bits of functionality, use interfaces. If you are designing large functional units, use an abstract class.&lt;/li&gt;
&lt;li&gt;If you want to provide common, implemented functionality among all implementations of your component, use an abstract class. Abstract classes allow you to partially implement your class, whereas interfaces contain no implementation for any members.&lt;/li&gt;
&lt;/ol&gt;
&lt;br /&gt;&lt;div&gt;
Remember there is nothing like ideal code base design. There will be always a scope of improvisations and caveats as well.&amp;nbsp;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://pavancsharp.blogspot.com/feeds/6930478454249448801/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://pavancsharp.blogspot.com/2018/04/code-design-choosing-abstract-or.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3985264170283763322/posts/default/6930478454249448801'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3985264170283763322/posts/default/6930478454249448801'/><link rel='alternate' type='text/html' href='http://pavancsharp.blogspot.com/2018/04/code-design-choosing-abstract-or.html' title='Code Design - Choosing Abstract or Interface - Level 100'/><author><name>Pavan N</name><uri>http://www.blogger.com/profile/16386797942234627578</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuxNeqFLB46wZIpHiomJ0jZ-rU9wu3NHzydxMkp0VjmkxePzjAhrm3Hjiq8dLiW8DvTXCmUUMBqgbA2MYUeJASd11n7BkbTOlfEvaIt8tW66S0xItZtDw7Rh5b2WX9DA/s220/IMG_20170223_100350.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3985264170283763322.post-4060241636556066405</id><published>2018-04-19T17:00:00.000+05:30</published><updated>2018-04-19T17:01:39.654+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="ADO.NET"/><category scheme="http://www.blogger.com/atom/ns#" term="c#"/><category scheme="http://www.blogger.com/atom/ns#" term="Connection"/><category scheme="http://www.blogger.com/atom/ns#" term="OLEDB"/><title type='text'>C# - Finding connection string - Easy way</title><content type='html'>&lt;span style=&quot;font-family: inherit;&quot;&gt;Most of the times we used to face difficulty while creating connection strings in our C# applications and especially when you are working in data intensive apps.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;I used to do lots of permutations and combinations (due to drivers installed) to find out the proper connection string in my initial career. Here I would like to explain easiest way and proper manner to test the connection ahead to actual usage.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;For database connections in C# apps you often need to supply an ADO Connection String. Here is a tip how to generate such string very easily.&lt;/span&gt;&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;(Windows only) &lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;Go to your desktop&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;Right click and choose &lt;b&gt;NEW/Microsoft Data Link&lt;/b&gt; - if you don&#39;t have this option on your menu, just choose &lt;b&gt;NEW/Text Document&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;If you have make a new text file - change the file extension from .txt to &lt;b&gt;.udl&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;Double click on the new file - you will see now the &lt;b&gt;Microsoft Connection wizard (Data Link Properties)&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;Choose your database driver and connection properties&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;Test the connection&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;Choose OK to save the file&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;Now click on the Start Menu (button) and choose &lt;b&gt;Run...&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;Type &lt;b&gt;WRITE &lt;/b&gt;(or Notepad)&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;Drag the file from your desktop just behind the command that you type on step 9 - you will see the file name and path appear just after WRITE&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;Run the command - you will see now the text editor started with your connection file open in it. It is all text&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;Select and copy the line after the comment &quot;; Everything after this line is ...&quot;&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;This you your ADO connection string! You can use this in your APP now! Just paste it into wherever connection dialog/string is asked for.&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;The same way you can do all the connection’s with OLEDB. I used to do same with Access/Excel files as well.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;This is all because of &lt;b&gt;Microsoft Connection wizard (Data Link Properties).&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;BTW, UDL stands for universal data links. In the wizard you can see all the drivers installed or supported in your machine.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;That&#39;s it - have fun!&lt;/span&gt;</content><link rel='replies' type='application/atom+xml' href='http://pavancsharp.blogspot.com/feeds/4060241636556066405/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://pavancsharp.blogspot.com/2018/04/c-finding-connection-string-easy-way.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3985264170283763322/posts/default/4060241636556066405'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3985264170283763322/posts/default/4060241636556066405'/><link rel='alternate' type='text/html' href='http://pavancsharp.blogspot.com/2018/04/c-finding-connection-string-easy-way.html' title='C# - Finding connection string - Easy way'/><author><name>Pavan N</name><uri>http://www.blogger.com/profile/16386797942234627578</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuxNeqFLB46wZIpHiomJ0jZ-rU9wu3NHzydxMkp0VjmkxePzjAhrm3Hjiq8dLiW8DvTXCmUUMBqgbA2MYUeJASd11n7BkbTOlfEvaIt8tW66S0xItZtDw7Rh5b2WX9DA/s220/IMG_20170223_100350.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3985264170283763322.post-3151013552884156122</id><published>2018-04-17T20:12:00.002+05:30</published><updated>2018-04-17T20:37:31.941+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="c#"/><category scheme="http://www.blogger.com/atom/ns#" term="generics"/><title type='text'>Generics in C# - Straight</title><content type='html'>&lt;br /&gt;In programming, generics are also known as &lt;b&gt;parametrized types &lt;/b&gt;or &lt;b&gt;parametric polymorphism&lt;/b&gt;. Generics have been added to the .Net framework since version 2.0. They introduce the type parameter T to methods, classes (or types) and interfaces.&lt;br /&gt;&lt;br /&gt;There is a documented &lt;a href=&quot;http://www.artima.com/intv/generics.html&quot;&gt;conversation about Generics in C#, Java and C++ with Anders Hejlsberg&lt;/a&gt;(The original author of &lt;a href=&quot;https://en.wikipedia.org/wiki/Turbo_Pascal&quot;&gt;Turbo Pascal&lt;/a&gt; , the chief architect of &lt;a href=&quot;https://en.wikipedia.org/wiki/Embarcadero_Delphi&quot;&gt;Delphi&lt;/a&gt; and the current lead architect of C#).&lt;br /&gt;&lt;br /&gt;With using generics, your code will become reusable, type safe (and strongly-typed) and will have a better performance at run-time since, when used properly, there will be zero cost for type casting or boxing/unboxing which in result will not introduce type conversion errors at runtime. The compiler of C# will be able to detect any problems of invalid casting at compile time.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Reusability &lt;/b&gt;with generics means that you will be able to create a method or a class that can be reused with different types in several places.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Now with generics you will be writing only one method, that accepts parameter of type T, and inside this method it will do the needed implementation, and it can either return void, or a concrete value (String, int, Employee, …etc) or even T itself.&lt;br /&gt;&lt;b&gt;&lt;u&gt;&lt;br /&gt;Boxing / Unboxing&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Boxing &lt;/b&gt;is the process of converting a type to object (or as the name suggests wrapping it inside the object container.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Unboxing &lt;/b&gt;means converting the object to a type. or unwrapping the type from the object container.&lt;br /&gt;&lt;br /&gt;Boxing/Unboxing are costly operations, and it is always better to not rely on them heavily in your code.&lt;br /&gt;&lt;br /&gt;With Generics you avoid doing boxing/unboxing since you are dealing with the parameter type T, which is a natural parameter that the compiler will replace it with the concrete type at compilation time without doing the boxing operation at runtime.&lt;br /&gt;&lt;br /&gt;By the way, if you would ask why we always use the letter T it is because by convention it refers to the word Type. You can use whatever valid letter or word you like for generics. The naming rules is no different than for naming classes.&lt;br /&gt;&lt;br /&gt;And a last note, you are not tied to only use one parameter, you can specify multiple parameters separating them with comma , like  or whatever valid naming you like. and then you can use the parameter in the place you want to use it.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;u&gt;Generic Methods&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;So our first example will be the generic method, let’s take a look at the following method, that accepts 2 parameters of type object and compares the value of the 2 objects.&lt;br /&gt;&lt;br /&gt;&lt;div&gt;
&lt;pre style=&quot;background: #1e1e1e; color: gainsboro; font-family: Consolas; font-size: 12;&quot;&gt;&lt;span style=&quot;color: #569cd6;&quot;&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;static&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;bool&lt;/span&gt;&amp;nbsp;Compare(&lt;span style=&quot;color: #569cd6;&quot;&gt;object&lt;/span&gt;&amp;nbsp;value,&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;object&lt;/span&gt;&amp;nbsp;value2)
&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;return&lt;/span&gt;&amp;nbsp;value&lt;span style=&quot;color: #b4b4b4;&quot;&gt;.&lt;/span&gt;Equals(value2);
&amp;nbsp;}
&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;Now this might serve well in terms of code reusability with the use of boxing/unboxing feature with object types. However, you will risk getting runtime errors due to unsafe casting and there will be cost for boxing/unboxing.&lt;br /&gt;&lt;br /&gt;Now we will rewrite the method above to make it generic, see the below:&lt;br /&gt;&lt;br /&gt;&lt;pre style=&quot;background: #1e1e1e; color: gainsboro; font-family: Consolas; font-size: 12;&quot;&gt;&lt;span style=&quot;color: #569cd6;&quot;&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;static&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;bool&lt;/span&gt;&amp;nbsp;Compare(T&amp;nbsp;value,&amp;nbsp;T&amp;nbsp;value2)
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;return&lt;/span&gt;&amp;nbsp;value&lt;span style=&quot;color: #b4b4b4;&quot;&gt;.&lt;/span&gt;Equals(value2);
}
&lt;/pre&gt;
&lt;br /&gt;The above method is the simplest implementation for generics method. I just wanted you to understand the structure of method when it has the parameter T.&lt;br /&gt;&lt;br /&gt;Now to call the above generic method, you will do the normal method call specifying the 2 arguments, the JIT (just-in-time) compiler will tell the type of the passed arguments and will deal with them naturally as if you are passing the concrete types.&lt;br /&gt;&lt;br /&gt;&lt;pre style=&quot;background: #1e1e1e; color: gainsboro; font-family: Consolas; font-size: 12;&quot;&gt;Assert&lt;span style=&quot;color: #b4b4b4;&quot;&gt;.&lt;/span&gt;IsTrue(GenericsMethods&lt;span style=&quot;color: #b4b4b4;&quot;&gt;.&lt;/span&gt;Compare(&lt;span style=&quot;color: #b5cea8;&quot;&gt;4&lt;/span&gt;,&amp;nbsp;&lt;span style=&quot;color: #b5cea8;&quot;&gt;4&lt;/span&gt;));
Assert&lt;span style=&quot;color: #b4b4b4;&quot;&gt;.&lt;/span&gt;IsTrue(GenericsMethods&lt;span style=&quot;color: #b4b4b4;&quot;&gt;.&lt;/span&gt;Compare(&lt;span style=&quot;color: #d69d85;&quot;&gt;&quot;abc&quot;&lt;/span&gt;,&amp;nbsp;&lt;span style=&quot;color: #d69d85;&quot;&gt;&quot;abc&quot;&lt;/span&gt;));
Assert&lt;span style=&quot;color: #b4b4b4;&quot;&gt;.&lt;/span&gt;IsTrue(GenericsMethods&lt;span style=&quot;color: #b4b4b4;&quot;&gt;.&lt;/span&gt;Compare(&lt;span style=&quot;color: #b5cea8;&quot;&gt;5.4d&lt;/span&gt;,&amp;nbsp;&lt;span style=&quot;color: #b5cea8;&quot;&gt;5.4d&lt;/span&gt;));
Assert&lt;span style=&quot;color: #b4b4b4;&quot;&gt;.&lt;/span&gt;IsTrue(GenericsMethods&lt;span style=&quot;color: #b4b4b4;&quot;&gt;.&lt;/span&gt;Compare(&lt;span style=&quot;color: #b5cea8;&quot;&gt;2.3m&lt;/span&gt;,&amp;nbsp;&lt;span style=&quot;color: #b5cea8;&quot;&gt;2.3m&lt;/span&gt;));
Assert&lt;span style=&quot;color: #b4b4b4;&quot;&gt;.&lt;/span&gt;IsTrue(GenericsMethods&lt;span style=&quot;color: #b4b4b4;&quot;&gt;.&lt;/span&gt;Compare(&lt;span style=&quot;color: #b5cea8;&quot;&gt;7f&lt;/span&gt;,&amp;nbsp;&lt;span style=&quot;color: #b5cea8;&quot;&gt;7f&lt;/span&gt;));&amp;nbsp;&lt;/pre&gt;
&lt;br /&gt;&lt;b&gt;&lt;u&gt;Generic Classes&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;Now we will be creating a generic class. You create it the same way you create a normal class, but you just add the parameter T at the end of the class name. You can define the parameter T everywhere inside the class, with members and methods.&lt;br /&gt;&lt;br /&gt;&lt;pre style=&quot;background: #1e1e1e; color: gainsboro; font-family: Consolas; font-size: 12;&quot;&gt;&lt;span style=&quot;color: #569cd6;&quot;&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: #4ec9b0;&quot;&gt;Lesson&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;private&lt;/span&gt;&amp;nbsp;T&amp;nbsp;t;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;void&lt;/span&gt;&amp;nbsp;Set(T&amp;nbsp;t)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;color: #b4b4b4;&quot;&gt;.&lt;/span&gt;t&amp;nbsp;&lt;span style=&quot;color: #b4b4b4;&quot;&gt;=&lt;/span&gt;&amp;nbsp;t;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;public&lt;/span&gt;&amp;nbsp;T&amp;nbsp;Get()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;return&lt;/span&gt;&amp;nbsp;t;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
}
&lt;/pre&gt;
Now to call the above class with the concrete type, you will need to initialize the object with the tags &amp;lt; &amp;gt; , and inside it you should specify the concrete type you want to create your Lesson class with:&lt;br /&gt;&lt;br /&gt;&lt;div&gt;
&lt;div&gt;
&lt;pre style=&quot;background: #1e1e1e; color: gainsboro; font-family: Consolas; font-size: 12;&quot;&gt;[TestMethod]
&lt;span style=&quot;color: #569cd6;&quot;&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;void&lt;/span&gt;&amp;nbsp;TestLessonClass()
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Lesson&amp;nbsp;integerLesson&amp;nbsp;&lt;span style=&quot;color: #b4b4b4;&quot;&gt;=&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;new&lt;/span&gt;&amp;nbsp;Lesson();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;integerLesson&lt;span style=&quot;color: #b4b4b4;&quot;&gt;.&lt;/span&gt;Set(&lt;span style=&quot;color: #b5cea8;&quot;&gt;1&lt;/span&gt;);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Assert&lt;span style=&quot;color: #b4b4b4;&quot;&gt;.&lt;/span&gt;AreEqual(integerLesson&lt;span style=&quot;color: #b4b4b4;&quot;&gt;.&lt;/span&gt;Get(),&amp;nbsp;&lt;span style=&quot;color: #b5cea8;&quot;&gt;1&lt;/span&gt;);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Lesson&amp;nbsp;stringLesson&amp;nbsp;&lt;span style=&quot;color: #b4b4b4;&quot;&gt;=&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;new&lt;/span&gt;&amp;nbsp;Lesson();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;stringLesson&lt;span style=&quot;color: #b4b4b4;&quot;&gt;.&lt;/span&gt;Set(&lt;span style=&quot;color: #d69d85;&quot;&gt;&quot;C#&amp;nbsp;Generics&quot;&lt;/span&gt;);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Assert&lt;span style=&quot;color: #b4b4b4;&quot;&gt;.&lt;/span&gt;AreEqual(&lt;span style=&quot;color: #d69d85;&quot;&gt;&quot;C#&amp;nbsp;Generics&quot;&lt;/span&gt;,&amp;nbsp;stringLesson&lt;span style=&quot;color: #b4b4b4;&quot;&gt;.&lt;/span&gt;Get());
}&lt;/pre&gt;
&lt;br /&gt;&lt;b&gt;&lt;i&gt;Can we have a generic constructor? &lt;/i&gt;&lt;/b&gt;No, generic constructors are not allowed. Which means that you cannot define the parameter T on the constructor itself.&lt;br /&gt;&lt;br /&gt;&lt;a href=&quot;https://stackoverflow.com/a/3606951/2797154&quot;&gt;Read Jon Skeet’s answer on stackoverflow regarding generic constructors&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;You can still use a factory creator static method and define to it the parameter T, but still I don’t think you really need that in most of the cases.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;u&gt;Generics Constraints&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;Constraints are like rules or instructions to define how to interact with a generic class or method. They can restrict the parameter that will be replaced with T to some certain type or class or have some properties, like to be new instance of class.&lt;br /&gt;&lt;br /&gt;If the consumer of the generic method or class didn’t comply with the constraint set on the parameter T, it will result in a compilation error.&lt;br /&gt;&lt;br /&gt;With constraints you can for example, specify a generic parameter to only accept reference types (class), or value types (struct), or to be a class that implements an interface.&lt;br /&gt;&lt;br /&gt;Constraints can be defined using the keyword where&lt;br /&gt;&lt;br /&gt;Let’s see the below example that includes having a constraint on the above Lesson class, we will put a constraint that the parameter T should only be of a reference type, value types are not allowed. so it will be defined as where T: class&lt;br /&gt;&lt;br /&gt;&lt;pre style=&quot;background: #1e1e1e; color: gainsboro; font-family: Consolas; font-size: 12;&quot;&gt;&lt;span style=&quot;color: #569cd6;&quot;&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: #4ec9b0;&quot;&gt;LessonWithConstraint&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;where&lt;/span&gt;&amp;nbsp;T&amp;nbsp;:&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;class&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;private&lt;/span&gt;&amp;nbsp;T&amp;nbsp;t;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;void&lt;/span&gt;&amp;nbsp;Set(T&amp;nbsp;t)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;color: #b4b4b4;&quot;&gt;.&lt;/span&gt;t&amp;nbsp;&lt;span style=&quot;color: #b4b4b4;&quot;&gt;=&lt;/span&gt;&amp;nbsp;t;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;public&lt;/span&gt;&amp;nbsp;T&amp;nbsp;Get()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;return&lt;/span&gt;&amp;nbsp;t;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
}&lt;/pre&gt;
&lt;br /&gt;Now if you try to initialize the above Lesson class using a non-reference type as its parameter, like int , you will get a compilation error:&lt;br /&gt;&lt;br /&gt;The type ‘int’ must be a reference type in order to use it as parameter ‘T’ in the generic type or method ‘Lesson’&lt;br /&gt;&lt;br /&gt;You can only call it with a reference type, like string or any class:&lt;br /&gt;&lt;br /&gt;&lt;pre style=&quot;background: #1e1e1e; color: gainsboro; font-family: Consolas; font-size: 12;&quot;&gt;&lt;span style=&quot;color: #4ec9b0;&quot;&gt;Lesson&lt;/span&gt;&amp;nbsp;stringLesson&amp;nbsp;&lt;span style=&quot;color: #b4b4b4;&quot;&gt;=&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: #4ec9b0;&quot;&gt;Lesson&lt;/span&gt;();
&lt;/pre&gt;
&lt;br /&gt;You can define constraint that a parameter must be implementing some interface. Let’s take this example:&lt;br /&gt;&lt;pre style=&quot;background: #1e1e1e; color: gainsboro; font-family: Consolas; font-size: 12;&quot;&gt;&lt;span style=&quot;color: #569cd6;&quot;&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: #4ec9b0;&quot;&gt;Container&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;where&lt;/span&gt;&amp;nbsp;T&amp;nbsp;:&amp;nbsp;IItem
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;private&lt;/span&gt;&amp;nbsp;T&amp;nbsp;t;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;void&lt;/span&gt;&amp;nbsp;Set(T&amp;nbsp;t)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;color: #b4b4b4;&quot;&gt;.&lt;/span&gt;t&amp;nbsp;&lt;span style=&quot;color: #b4b4b4;&quot;&gt;=&lt;/span&gt;&amp;nbsp;t;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;public&lt;/span&gt;&amp;nbsp;T&amp;nbsp;Get()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;return&lt;/span&gt;&amp;nbsp;t;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
}&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;Here you have a class Container that defines a generic parameter T, with a constraint that anyone wants to use this class, must pass an argument that implements the IItem Interface.&lt;br /&gt;&lt;br /&gt;So here we added the IItem Interface, that has a method showName():&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;pre style=&quot;background: #1e1e1e; color: gainsboro; font-family: Consolas; font-size: 12;&quot;&gt;&lt;span style=&quot;color: #569cd6;&quot;&gt;interface&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: #b8d7a3;&quot;&gt;IItem&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;string&lt;/span&gt;&amp;nbsp;showName();
}
&lt;/pre&gt;
We also added a class Item that will implement IItem Interface:&lt;br /&gt;&lt;br /&gt;&lt;pre style=&quot;background: #1e1e1e; color: gainsboro; font-family: Consolas; font-size: 12;&quot;&gt;&lt;span style=&quot;color: #569cd6;&quot;&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: #4ec9b0;&quot;&gt;Item&lt;/span&gt;&amp;nbsp;:&amp;nbsp;IItem
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;private&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;string&lt;/span&gt;&amp;nbsp;name;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;public&lt;/span&gt;&amp;nbsp;Item(&lt;span style=&quot;color: #4ec9b0;&quot;&gt;String&lt;/span&gt;&amp;nbsp;name)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;color: #b4b4b4;&quot;&gt;.&lt;/span&gt;name&amp;nbsp;&lt;span style=&quot;color: #b4b4b4;&quot;&gt;=&lt;/span&gt;&amp;nbsp;name;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;string&lt;/span&gt;&amp;nbsp;showName()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;return&lt;/span&gt;&amp;nbsp;name;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/pre&gt;
&lt;br /&gt;Now in the caller part, what will happen is that we will initialize Item object, and then initialize Container while providing it and Item Type, and then calling the set method of container to set the item that was initialized. Check the below:&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;pre style=&quot;background: #1e1e1e; color: gainsboro; font-family: Consolas; font-size: 12;&quot;&gt;[TestMethod]
&lt;span style=&quot;color: #569cd6;&quot;&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;void&lt;/span&gt;&amp;nbsp;TestItemClass()
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Item&amp;nbsp;pencil&amp;nbsp;&lt;span style=&quot;color: #b4b4b4;&quot;&gt;=&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;new&lt;/span&gt;&amp;nbsp;Item(&lt;span style=&quot;color: #d69d85;&quot;&gt;&quot;Pencil&quot;&lt;/span&gt;);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Container&amp;nbsp;container&amp;nbsp;&lt;span style=&quot;color: #b4b4b4;&quot;&gt;=&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;new&lt;/span&gt;&amp;nbsp;Container();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;container&lt;span style=&quot;color: #b4b4b4;&quot;&gt;.&lt;/span&gt;Set(pencil);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Assert&lt;span style=&quot;color: #b4b4b4;&quot;&gt;.&lt;/span&gt;AreEqual(pencil,&amp;nbsp;container&lt;span style=&quot;color: #b4b4b4;&quot;&gt;.&lt;/span&gt;Get());
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Assert&lt;span style=&quot;color: #b4b4b4;&quot;&gt;.&lt;/span&gt;AreEqual(&lt;span style=&quot;color: #d69d85;&quot;&gt;&quot;Pencil&quot;&lt;/span&gt;,&amp;nbsp;container&lt;span style=&quot;color: #b4b4b4;&quot;&gt;.&lt;/span&gt;Get()&lt;span style=&quot;color: #b4b4b4;&quot;&gt;.&lt;/span&gt;showName());
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Item&amp;nbsp;pen&amp;nbsp;&lt;span style=&quot;color: #b4b4b4;&quot;&gt;=&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;new&lt;/span&gt;&amp;nbsp;Item(&lt;span style=&quot;color: #d69d85;&quot;&gt;&quot;Pen&quot;&lt;/span&gt;);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Container&amp;nbsp;container2&amp;nbsp;&lt;span style=&quot;color: #b4b4b4;&quot;&gt;=&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;new&lt;/span&gt;&amp;nbsp;Container();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;container2&lt;span style=&quot;color: #b4b4b4;&quot;&gt;.&lt;/span&gt;Set(pen);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Assert&lt;span style=&quot;color: #b4b4b4;&quot;&gt;.&lt;/span&gt;AreEqual(pen,&amp;nbsp;container2&lt;span style=&quot;color: #b4b4b4;&quot;&gt;.&lt;/span&gt;Get());
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Assert&lt;span style=&quot;color: #b4b4b4;&quot;&gt;.&lt;/span&gt;AreEqual(&lt;span style=&quot;color: #d69d85;&quot;&gt;&quot;Pen&quot;&lt;/span&gt;,&amp;nbsp;container2&lt;span style=&quot;color: #b4b4b4;&quot;&gt;.&lt;/span&gt;Get()&lt;span style=&quot;color: #b4b4b4;&quot;&gt;.&lt;/span&gt;showName());
}&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;&lt;u&gt;Generic Interfaces&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;This is similar to generic classes, you define a parameter T on the interface level, and your methods can use this parameter in their prototype, so any class that will be implementing this interface will naturally implement the parameter T within its own methods. You can also add constraints to generic interfaces. Note that the class that will be implementing the generic interface must define parameter T.&lt;br /&gt;&lt;br /&gt;The example below will illustrate the use of a generic interface:&lt;br /&gt;&lt;pre style=&quot;background: #1e1e1e; color: gainsboro; font-family: Consolas; font-size: 12;&quot;&gt;&lt;span style=&quot;color: #569cd6;&quot;&gt;interface&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: #b8d7a3;&quot;&gt;IBook&lt;/span&gt;
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;void&lt;/span&gt;&amp;nbsp;add(T&amp;nbsp;book);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;void&lt;/span&gt;&amp;nbsp;delete();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;T&amp;nbsp;get();
}&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;br /&gt;And here is the code for the implementer Book:&lt;/div&gt;
&lt;div&gt;
&lt;pre style=&quot;background: #1e1e1e; color: gainsboro; font-family: Consolas; font-size: 12;&quot;&gt;&lt;span style=&quot;color: #569cd6;&quot;&gt;class&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: #4ec9b0;&quot;&gt;Book&lt;/span&gt;&amp;nbsp;:&amp;nbsp;IBook
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;T&amp;nbsp;book;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;void&lt;/span&gt;&amp;nbsp;add(T&amp;nbsp;book)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;color: #b4b4b4;&quot;&gt;.&lt;/span&gt;book&amp;nbsp;&lt;span style=&quot;color: #b4b4b4;&quot;&gt;=&lt;/span&gt;&amp;nbsp;book;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;void&lt;/span&gt;&amp;nbsp;delete()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;color: #b4b4b4;&quot;&gt;.&lt;/span&gt;book&amp;nbsp;&lt;span style=&quot;color: #b4b4b4;&quot;&gt;=&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;default&lt;/span&gt;(T);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;public&lt;/span&gt;&amp;nbsp;T&amp;nbsp;get()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;return&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;color: #b4b4b4;&quot;&gt;.&lt;/span&gt;book;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}
}
&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;
And to test the above implementation, we will be creating an object from Book with parameter String, and then we will use the add method to add the string value to the Book class. See below:&lt;br /&gt;&lt;br /&gt;&lt;pre style=&quot;background: #1e1e1e; color: gainsboro; font-family: Consolas; font-size: 12;&quot;&gt;[TestMethod]
&lt;span style=&quot;color: #569cd6;&quot;&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;void&lt;/span&gt;&amp;nbsp;TestIBookInterface()
{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Book&amp;nbsp;stringBook&amp;nbsp;&lt;span style=&quot;color: #b4b4b4;&quot;&gt;=&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;new&lt;/span&gt;&amp;nbsp;Book();
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;stringBook&lt;span style=&quot;color: #b4b4b4;&quot;&gt;.&lt;/span&gt;add(&lt;span style=&quot;color: #d69d85;&quot;&gt;&quot;Clean&amp;nbsp;Code&quot;&lt;/span&gt;);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Assert&lt;span style=&quot;color: #b4b4b4;&quot;&gt;.&lt;/span&gt;AreEqual(&lt;span style=&quot;color: #d69d85;&quot;&gt;&quot;Clean&amp;nbsp;Code&quot;&lt;/span&gt;,&amp;nbsp;stringBook&lt;span style=&quot;color: #b4b4b4;&quot;&gt;.&lt;/span&gt;get());
}&lt;/pre&gt;
&lt;br /&gt;&lt;b&gt;&lt;u&gt;System.Collections and System.Collections.Generic namespaces&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;In .Net framework the collections namespace includes classes that represent collection of objects that include lists, queues, bit arrays, hash tables and dictionaries.&lt;br /&gt;&lt;br /&gt;The namespace System.Collections.Generic is the generic version of System.Collection namespace. Using it, you can define strongly typed collections while gaining all benefits of generics (code reusability, type safety and better performance).&lt;br /&gt;&lt;br /&gt;Let’s take the most commonly used Collection type which is the  List. I will not be explaining about Collections now because it is outside the scope of this article.&lt;br /&gt;&lt;br /&gt;So with generics, the List will have its parameter T, that you can pass to it whatever type you like and you will have a list of that type.&lt;br /&gt;&lt;br /&gt;Check the below example, we are defining a list of float points:&lt;br /&gt;&lt;pre style=&quot;background: #1e1e1e; color: gainsboro; font-family: Consolas; font-size: 12;&quot;&gt;[TestMethod]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;void&lt;/span&gt;&amp;nbsp;TestGenericList()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #4ec9b0;&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #569cd6;&quot;&gt;float&lt;/span&gt;&amp;gt;&amp;nbsp;floatingPoints&amp;nbsp;&lt;span style=&quot;color: #b4b4b4;&quot;&gt;=&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: #569cd6;&quot;&gt;new&lt;/span&gt;&amp;nbsp;&lt;span style=&quot;color: #4ec9b0;&quot;&gt;List&lt;/span&gt;&amp;lt;&lt;span style=&quot;color: #569cd6;&quot;&gt;float&lt;/span&gt;&amp;gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #b5cea8;&quot;&gt;1.3f&lt;/span&gt;,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #b5cea8;&quot;&gt;2.6f&lt;/span&gt;,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #b5cea8;&quot;&gt;99.8f&lt;/span&gt;,
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style=&quot;color: #b5cea8;&quot;&gt;34f&lt;/span&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;};
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Assert&lt;span style=&quot;color: #b4b4b4;&quot;&gt;.&lt;/span&gt;AreEqual(&lt;span style=&quot;color: #b5cea8;&quot;&gt;4&lt;/span&gt;,&amp;nbsp;floatingPoints&lt;span style=&quot;color: #b4b4b4;&quot;&gt;.&lt;/span&gt;Count);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Assert&lt;span style=&quot;color: #b4b4b4;&quot;&gt;.&lt;/span&gt;AreEqual(&lt;span style=&quot;color: #b5cea8;&quot;&gt;1.3f&lt;/span&gt;,&amp;nbsp;floatingPoints[&lt;span style=&quot;color: #b5cea8;&quot;&gt;0&lt;/span&gt;]);
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;&lt;u&gt;Conclusion&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Generics are great abstraction and extensiblity feature that provides the benefit of code reuse, type-safety and performance gain. You just need to know where you should use it and the places where you can refactor your code to make use of generics.&lt;br /&gt;&lt;br /&gt;I hope that I was able to demystify the important topic in C# for you, I tried to cover most aspects of generics in C#, there might be other details regarding each section I discussed above, just let me know if you have any question or comment , or if you are having any issue using generics in your application.&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://pavancsharp.blogspot.com/feeds/3151013552884156122/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://pavancsharp.blogspot.com/2018/04/generics-in-c-straight.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3985264170283763322/posts/default/3151013552884156122'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3985264170283763322/posts/default/3151013552884156122'/><link rel='alternate' type='text/html' href='http://pavancsharp.blogspot.com/2018/04/generics-in-c-straight.html' title='Generics in C# - Straight'/><author><name>Pavan N</name><uri>http://www.blogger.com/profile/16386797942234627578</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuxNeqFLB46wZIpHiomJ0jZ-rU9wu3NHzydxMkp0VjmkxePzjAhrm3Hjiq8dLiW8DvTXCmUUMBqgbA2MYUeJASd11n7BkbTOlfEvaIt8tW66S0xItZtDw7Rh5b2WX9DA/s220/IMG_20170223_100350.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3985264170283763322.post-8663003749854786932</id><published>2014-09-23T11:24:00.001+05:30</published><updated>2014-09-23T11:33:48.365+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="asp.net"/><category scheme="http://www.blogger.com/atom/ns#" term="bdotnet"/><category scheme="http://www.blogger.com/atom/ns#" term="code"/><category scheme="http://www.blogger.com/atom/ns#" term="developer"/><category scheme="http://www.blogger.com/atom/ns#" term="Productivity"/><category scheme="http://www.blogger.com/atom/ns#" term="visual studio"/><title type='text'>A quick Utilities, Tips &amp; Tricks for web dev only</title><content type='html'>&lt;div&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;​Hi,​&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;While doing some validatioons you want to validate character count. Then here is the link&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;&lt;a href=&quot;http://www.javascriptkit.com/script/script2/charcount.shtml&quot;&gt;http://www.javascriptkit.com/script/script2/charcount.shtml&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;Do you want to validate the CSS you wrote? Then here is the link&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;&lt;a href=&quot;http://jigsaw.w3.org/css-validator/#validate_by_input&quot;&gt;http://jigsaw.w3.org/css-validator/#validate_by_input&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;You want to decode or encode a character. Then here is the link&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;&lt;a href=&quot;http://www.base64decode.org/&quot;&gt;http://www.base64decode.org/&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;Do you want to add days, months or years to a DATE? Then here is the link&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;&lt;a href=&quot;http://www.timeanddate.com/date/dateadd.html&quot;&gt;http://www.timeanddate.com/date/dateadd.html&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;​Tired of writing SQl and Do you love to work on LINQ but don&#39;t have a proper IDE? Then here is the link&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;&lt;a href=&quot;http://www.linqpad.net/&quot;&gt;http://www.linqpad.net/&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;Do you want to format the CSS you wrote in your own standards? Then here is the llink&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;&lt;a href=&quot;http://www.lonniebest.com/FormatCSS/&quot;&gt;http://www.lonniebest.com/FormatCSS/&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;​Do you want to format XML, JSON, HTML, HTML5, RegEx or SQl or else any? Here is the link&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;&lt;a href=&quot;http://www.freeformatter.com/html-formatter.html%E2%80%8B&quot;&gt;http://www.freeformatter.com/html-formatter.html​&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;Do you want to quickly write html, javascript and css and see how it behaves in browser? Then here is the link&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;&lt;a href=&quot;http://jsbin.com/&quot;&gt;http://jsbin.com/&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;Want to make your JSON looks pretty? Then here is the link&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;&lt;a href=&quot;http://jsonformatter.curiousconcept.com/&quot;&gt;http://jsonformatter.curiousconcept.com/&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;Do you want to make your javascript looks pretty? Then here is the link&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;&lt;a href=&quot;http://www.jspretty.com/&quot;&gt;http://www.jspretty.com/&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;Do you want to write regex and test at the same time? Then here is the link&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;&lt;a href=&quot;https://www.blogger.com/%E2%80%8Bhttp://regexpal.com/&quot;&gt;​http://regexpal.com/&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;​​Want to play with SQL online? Then here is the link&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;&lt;a href=&quot;http://sqlfiddle.com/&quot;&gt;http://sqlfiddle.com/&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;​​Finally a BIG one its called EMMET coding in previous days and now it is called as ZEN Coding which is incorporated by many IDE&#39;s really helpful while designing a web page. Imagine how much time does it takes to write 20 DIV&#39;s with proper ID name and class along with a table inside of it. Using ZEN Coding you can make it as less a 5-10 seconds isn&#39;t it great?! :)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;here is the link for the quick cheatsheet&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;&lt;a href=&quot;http://docs.emmet.io/cheat-sheet/&quot;&gt;http://docs.emmet.io/cheat-sheet/&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;Hope the Tips, Tricks, Utilities mentioned above is helpful in boosting your productivity. Happy Coding... :)&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;Regards,&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Segoe UI, Segoe UI, Segoe, Tahoma, Helvetica, Arial, sans-serif;&quot;&gt;&lt;span style=&quot;line-height: 20.7999992370605px;&quot;&gt;Pavan N&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://pavancsharp.blogspot.com/feeds/8663003749854786932/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://pavancsharp.blogspot.com/2014/09/a-quick-utilities-tips-tricks-for-web.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3985264170283763322/posts/default/8663003749854786932'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3985264170283763322/posts/default/8663003749854786932'/><link rel='alternate' type='text/html' href='http://pavancsharp.blogspot.com/2014/09/a-quick-utilities-tips-tricks-for-web.html' title='A quick Utilities, Tips &amp; Tricks for web dev only'/><author><name>Pavan N</name><uri>http://www.blogger.com/profile/16386797942234627578</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuxNeqFLB46wZIpHiomJ0jZ-rU9wu3NHzydxMkp0VjmkxePzjAhrm3Hjiq8dLiW8DvTXCmUUMBqgbA2MYUeJASd11n7BkbTOlfEvaIt8tW66S0xItZtDw7Rh5b2WX9DA/s220/IMG_20170223_100350.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3985264170283763322.post-4019899178891394653</id><published>2014-03-21T08:51:00.001+05:30</published><updated>2023-05-29T16:24:57.267+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="asp.net"/><category scheme="http://www.blogger.com/atom/ns#" term="c#"/><category scheme="http://www.blogger.com/atom/ns#" term="CORS"/><category scheme="http://www.blogger.com/atom/ns#" term="HTML5"/><category scheme="http://www.blogger.com/atom/ns#" term="REST"/><category scheme="http://www.blogger.com/atom/ns#" term="SOAP"/><category scheme="http://www.blogger.com/atom/ns#" term="WCF"/><category scheme="http://www.blogger.com/atom/ns#" term="Web service"/><title type='text'>How to avoid a CORS problem?</title><content type='html'>&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;&amp;quot; , serif , &amp;quot;&amp;quot; , serif; font-size: 12pt;&quot;&gt;Hi All,&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;&amp;quot; , serif , &amp;quot;&amp;quot; , serif; font-size: 12pt;&quot;&gt;As many
of you already moved in to mobility space development. you might be aware of
problem with CORS.&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;&amp;quot; , serif , &amp;quot;&amp;quot; , serif; font-size: 12pt;&quot;&gt;If you
are not then quick rollback based on my understandings...&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-left: 0.5in; text-indent: -0.25in;&quot;&gt;
&lt;!--[if !supportLists]--&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;1.&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot;; font-size: 7pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;/span&gt;&lt;/span&gt;&lt;!--[endif]--&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 12pt;&quot;&gt;First of all here is the
reference : &amp;nbsp;&lt;a href=&quot;http://stackoverflow.com/questions/9559947/cross-origin-authorization-header-with-jquery-ajax&quot;&gt;&lt;span style=&quot;color: blue;&quot;&gt;http://stackoverflow.com/questions/9559947/cross-origin-authorization-header-with-jquery-ajax&lt;/span&gt;&lt;/a&gt;&amp;nbsp;&amp;nbsp;This
gives you more insight&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;span style=&quot;color: blue; font-size: 12pt;&quot;&gt;&lt;a href=&quot;http://www.html5rocks.com/en/tutorials/cors/&quot;&gt;http://www.html5rocks.com/en/tutorials/cors/&lt;/a&gt;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif;&quot;&gt;And even my stack question&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;text-indent: -0.25in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif;&quot;&gt;&lt;a href=&quot;http://stackoverflow.com/questions/20610433/sending-plain-ajax-http-request-with-custom-header&quot;&gt;http://stackoverflow.com/questions/20610433/sending-plain-ajax-http-request-with-custom-header&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;text-indent: -0.25in;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: medium;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-left: 0.5in; text-indent: -0.25in;&quot;&gt;
&lt;!--[if !supportLists]--&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;2.&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot;; font-size: 7pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;/span&gt;&lt;/span&gt;&lt;!--[endif]--&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 12pt;&quot;&gt;Generally it happens for
cross domain request. Means while you are trying to request some webservice
from your HTML/JavaScript/JQuery related AJAX calls. But of course you can
access those services using devices like ipad/iphone/android apps and even with
browser extensions like HTTP client or REST client plugins( because these calls
are done via sandbox itself)&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-left: 0.5in; text-indent: -0.25in;&quot;&gt;
&lt;!--[if !supportLists]--&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;3.&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot;; font-size: 7pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;/span&gt;&lt;/span&gt;&lt;!--[endif]--&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 12pt;&quot;&gt;It’s a W3C security
implementation enforced on browsers. So that concerned web services can
restrict the cross domain browser based requests. It can be achieved by adding
the&amp;nbsp;&lt;/span&gt;&lt;i&gt;&lt;span style=&quot;color: red; font-family: &amp;quot;times new roman&amp;quot; , &amp;quot;serif&amp;quot;; font-size: 12pt;&quot;&gt;&#39;Access-Control-Allow-Origin&#39;&lt;/span&gt;&lt;/i&gt;&lt;span style=&quot;color: red; font-family: &amp;quot;times new roman&amp;quot; , &amp;quot;serif&amp;quot;; font-size: 12pt;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 12pt;&quot;&gt;header on server side only. Otherwise you will come across the
error like following:&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot;; font-size: 10pt;&quot;&gt;&lt;br /&gt;
&lt;!--[if !supportLineBreakNewLine]--&gt;&lt;br /&gt;
&lt;!--[endif]--&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;pre formatcs=&quot;1&quot;&gt; XMLHttpRequest cannot load https://www.abcxyz.com/API/XXX. No
&#39;Access-Control-Allow-Origin&#39; header is present on the requested resource.
Origin &#39;http://localhost&#39; is therefore
not allowed access.
&lt;/pre&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;&amp;quot; , serif , &amp;quot;&amp;quot; , serif; font-size: 12pt;&quot;&gt;So how to
overcome this situation if concerned tag is not possible to add? And what are
the different ways???&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;&amp;quot; , serif , &amp;quot;&amp;quot; , serif; font-size: 12pt;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;&amp;quot; , serif , &amp;quot;&amp;quot; , serif; font-size: 12pt;&quot;&gt;It can be
done via your own web service requests. Means you should write your own service
and using which you can call the targeted services. I came up with some sample
code using GET and POST request types along with sending custom headers for
authentication &amp;amp; passing data via POST request.&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;times new roman&amp;quot; , serif; font-size: 13.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot; style=&quot;margin-bottom: 0.0001pt;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
Using POST request by sending some data
&lt;br /&gt;

&lt;pre class=&quot;pavanCSharpCode&quot;&gt;public string TestDataPOST()
        {
            // Create a request using a URL that can receive a post. 
            WebRequest request = WebRequest.Create(&quot;http://abc.xyz.com/WebServices/check.asmx/abc&quot;);
            // Set the Method property of the request to POST.
            request.Method = &quot;POST&quot;;
            // Create POST data and convert it to a byte array.
            string postData = &quot;check1=HELLO&amp;amp;check2=WORLD&quot;;
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            // Set the ContentType property of the WebRequest.
            request.ContentType = &quot;application/x-www-form-urlencoded&quot;;
            // Set the ContentLength property of the WebRequest.
            request.ContentLength = byteArray.Length;
            // Get the request stream.
            Stream dataStream = request.GetRequestStream();
            // Write the data to the request stream.
            dataStream.Write(byteArray, 0, byteArray.Length);
            // Close the Stream object.
            dataStream.Close();
            // Get the response.
            WebResponse response = request.GetResponse();                      
            // Get the stream containing content returned by the server.
            dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd();
            

            // Clean up the streams.
            reader.Close();
            dataStream.Close();
            response.Close();
            return responseFromServer;
        }

       
&lt;/pre&gt;

&lt;br /&gt;
Now GET request along with sending some custom headers too
&lt;br /&gt;

&lt;pre class=&quot;pavanCSharpCode&quot;&gt;public string TestDataGET()
        {
            string sURL = &quot;https://www.ABC.com/API/123&quot;;

            // Create a request using a URL that can receive a post.            
            WebRequest request = WebRequest.Create(sURL);
            request.Method = &quot;GET&quot;;
            request.ContentType = &quot;application/json&quot;;
            request.Headers.Add(&quot;CUSTOM&quot;, &quot;abcdefghjklmn&quot;);
            using (WebResponse response = request.GetResponse())
            {
                using (Stream stream = response.GetResponseStream())
                {                       
                    // Open the stream using a StreamReader for easy access.
                    StreamReader reader = new StreamReader(stream);
                    // Read the content.
                    string responseFromServer = reader.ReadToEnd();
                    return responseFromServer;
                }
            }
        }
&lt;/pre&gt;

Hope it helps you !! :)


</content><link rel='replies' type='application/atom+xml' href='http://pavancsharp.blogspot.com/feeds/4019899178891394653/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://pavancsharp.blogspot.com/2014/01/how-to-avoid-cors-problem.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3985264170283763322/posts/default/4019899178891394653'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3985264170283763322/posts/default/4019899178891394653'/><link rel='alternate' type='text/html' href='http://pavancsharp.blogspot.com/2014/01/how-to-avoid-cors-problem.html' title='How to avoid a CORS problem?'/><author><name>Pavan N</name><uri>http://www.blogger.com/profile/16386797942234627578</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuxNeqFLB46wZIpHiomJ0jZ-rU9wu3NHzydxMkp0VjmkxePzjAhrm3Hjiq8dLiW8DvTXCmUUMBqgbA2MYUeJASd11n7BkbTOlfEvaIt8tW66S0xItZtDw7Rh5b2WX9DA/s220/IMG_20170223_100350.jpg'/></author><thr:total>0</thr:total><georss:featurename>Bangalore, Karnataka, India</georss:featurename><georss:point>12.9715987 77.594562699999983</georss:point><georss:box>12.4764182 76.949115699999979 13.4667792 78.240009699999987</georss:box></entry><entry><id>tag:blogger.com,1999:blog-3985264170283763322.post-3820527443141075353</id><published>2014-01-03T11:17:00.000+05:30</published><updated>2014-01-03T11:17:29.360+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="CSS3"/><category scheme="http://www.blogger.com/atom/ns#" term="HTML5"/><category scheme="http://www.blogger.com/atom/ns#" term="JQUERY"/><category scheme="http://www.blogger.com/atom/ns#" term="Mobile"/><category scheme="http://www.blogger.com/atom/ns#" term="PhoneGap"/><category scheme="http://www.blogger.com/atom/ns#" term="REST"/><category scheme="http://www.blogger.com/atom/ns#" term="SOAP"/><category scheme="http://www.blogger.com/atom/ns#" term="visual studio"/><category scheme="http://www.blogger.com/atom/ns#" term="WCF"/><category scheme="http://www.blogger.com/atom/ns#" term="Web service"/><category scheme="http://www.blogger.com/atom/ns#" term="Xamarin"/><title type='text'>My Learnings with current Mobility/Web market trend</title><content type='html'>Hi,&lt;br /&gt;
&lt;br /&gt;
First of all a big happy new year to all my visitors. Today I would like to share my learning about the current market trend as the winter is going to complete and all the market will start to bounce back in next 30-45 days.&lt;br /&gt;
&lt;br /&gt;
At the starting of my career I used to be in a big misconception like any fresher. I always think ASPX, PHP, JSP, CFM etc are far better than the plain HTML pages. I really felt painful to write plain prototype based html with javascrript.&lt;br /&gt;
&lt;br /&gt;
But now after seeing the market trends and mobility boom I realized the true strength &amp;amp; can confidently say it like HTML is far far away from any other above mentioned server based web technologies. In the coming future the only UI we will see with is developed only with HTML5.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;i&gt;&lt;u&gt;Question :&lt;/u&gt;&lt;/i&gt;&lt;/b&gt; So WHY previously we didn&#39;t concentrated on these things, Because HTML already there and which works only via http protocol means we have web services etc to work on.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;i&gt;&lt;u&gt;Answer &lt;/u&gt;&lt;/i&gt;&lt;/b&gt;: Previously the handshakes will be based on SOAP requests with XML and AJAX calls with difficulty of CORS(cross origin - means HTML page should be on same domain of services--&amp;gt; No separation of concerns). Which is too heavy and eats your network speed etc.&lt;br /&gt;
&lt;br /&gt;
Now we have HTML5 with XmlHttpRequest based on JSON along with JQUERY and CSS3 .... BINGO!!! :) .... Now we can opt for these technologies to develop a mobile webapps which need a tremendous network and data quality.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;i&gt;&lt;u&gt;Conclusion :&lt;/u&gt;&lt;/i&gt;&lt;/b&gt; If you know HTML5 you have many options in the market to explore your career. Like a HTML developer can be used for developing outstanding website, web applications, Responsive websites, Hybrid apps, Windows8 apps, Phone gap and many more. Also if you are aware of one server side technology (web services) along with a pinch of DB &amp;nbsp;knowledge. Then you are a complete package :) .&lt;br /&gt;
&lt;br /&gt;
Even companies also need to concentrate on these sections to have a tremendous future . Cloud computing, mobility, web services and DB(Hadoop, Teradata, etc.) .&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;i&gt;&lt;u&gt;Just FYI :&lt;/u&gt;&lt;/i&gt;&lt;/b&gt; Also a tool called XAMARIN getting more and more popular these days. Many MNC&#39;s are tieing up with Xamarin and many more bigger MNCs are migrating their apps to xamarin and encouraging their developers to move on. You just need a design knowledge of IOS, basics of android SDK, visual studio, C#..&lt;br /&gt;
&lt;br /&gt;
Its all just because of its powerful IDE. with which you can develop all kinds of mobile native apps with single code base. Isn&#39;t that cool ?! :) .&lt;br /&gt;
&lt;br /&gt;
Regards,&lt;br /&gt;
Pavan N&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://pavancsharp.blogspot.com/feeds/3820527443141075353/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://pavancsharp.blogspot.com/2014/01/my-learnings-with-current-mobilityweb.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3985264170283763322/posts/default/3820527443141075353'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3985264170283763322/posts/default/3820527443141075353'/><link rel='alternate' type='text/html' href='http://pavancsharp.blogspot.com/2014/01/my-learnings-with-current-mobilityweb.html' title='My Learnings with current Mobility/Web market trend'/><author><name>Pavan N</name><uri>http://www.blogger.com/profile/16386797942234627578</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuxNeqFLB46wZIpHiomJ0jZ-rU9wu3NHzydxMkp0VjmkxePzjAhrm3Hjiq8dLiW8DvTXCmUUMBqgbA2MYUeJASd11n7BkbTOlfEvaIt8tW66S0xItZtDw7Rh5b2WX9DA/s220/IMG_20170223_100350.jpg'/></author><thr:total>0</thr:total><georss:featurename>Bangalore, Karnataka, India</georss:featurename><georss:point>12.9715987 77.594562699999983</georss:point><georss:box>12.4764182 76.949115699999979 13.4667792 78.240009699999987</georss:box></entry><entry><id>tag:blogger.com,1999:blog-3985264170283763322.post-7329414751312539370</id><published>2013-05-30T19:27:00.001+05:30</published><updated>2013-05-30T19:28:42.854+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="asp.net"/><category scheme="http://www.blogger.com/atom/ns#" term="c#"/><category scheme="http://www.blogger.com/atom/ns#" term="code"/><category scheme="http://www.blogger.com/atom/ns#" term="impersonate"/><category scheme="http://www.blogger.com/atom/ns#" term="permission"/><title type='text'>Implementing impersonation in c#</title><content type='html'>&lt;div class=&quot;MsoNormal&quot;&gt;
Hi,&lt;br /&gt;
Today am going to explain about the implementation of impersonation. This code we can use in many contexts like web, windows, services etc. The main things to be a pr requisite are user context should have a proper permissions and necessary references were made.( Please see the ‘usings’ here in the snippet)&lt;br /&gt;
&lt;br /&gt;
This impersonation is needed mainly in few cases like to do some server side functionality means printing, network printing accessing, checking available printers, IO operations, etc all comes under particular user context.&lt;br /&gt;
&lt;br /&gt;
This thing needs to be done in many cases as the IIS application runs under very less privileges. So the IIS_User need to be impersonated before doing few operations.&lt;br /&gt;
Here am attaching the complete class for making it more simpler to benefit you people. You just need to copy this class in to your project and use a snippet as like this&lt;br /&gt;
&lt;br /&gt;
&lt;pre formatcs=&quot;1&quot;&gt;using (Impersonation impersonation = new Impersonation(impersonationUsername, impersonationDomainName, impersonationPWD))
{

}&lt;/pre&gt;
&lt;br /&gt;
And here is the code snippet required.&lt;br /&gt;
&lt;br /&gt;
&lt;pre formatcs=&quot;1&quot;&gt;
using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Security.Principal;


[PermissionSetAttribute(SecurityAction.Demand, Name = &quot;FullTrust&quot;)]
public class Impersonation : IDisposable
{

    /// &lt;summary&gt;
    /// Initializes a new instance of the &lt;see cref=&quot;Impersonator&quot;/&gt; class.
    /// &lt;/summary&gt;
    /// &lt;param name=&quot;userName&quot;&gt;Name of the user.&lt;/param&gt;
    /// &lt;param name=&quot;domainName&quot;&gt;Name of the domain.&lt;/param&gt;
    /// &lt;param name=&quot;password&quot;&gt;The password.&lt;/param&gt;
    public Impersonation(string userName, string domainName, string password)
    {
        ImpersonateValidUser(userName, domainName, password);
    }

    public void Dispose()
    {
        UndoImpersonation();
    }

    #region P/Invoke.
    [DllImport(&quot;advapi32.dll&quot;, SetLastError = true)]
    private static extern int LogonUser(string lpszUserName, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

    [DllImport(&quot;advapi32.dll&quot;, CharSet = CharSet.Auto, SetLastError = true)]
    private static extern int DuplicateToken(IntPtr hToken, int impersonationLevel, ref IntPtr hNewToken);

    [DllImport(&quot;advapi32.dll&quot;, CharSet = CharSet.Auto, SetLastError = true)]
    private static extern bool RevertToSelf();

    [DllImport(&quot;kernel32.dll&quot;, CharSet = CharSet.Auto)]
    private static extern bool CloseHandle(IntPtr handle);

    [DllImport(&quot;kernel32.dll&quot;, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
    private static extern int FormatMessage(int dwFlags, ref IntPtr lpSource,
        int dwMessageId, int dwLanguageId, ref String lpBuffer, int nSize, IntPtr Arguments);

    [DllImport(&quot;userenv.dll&quot;, SetLastError = true, CharSet = CharSet.Auto)]
    public static extern bool LoadUserProfile
    (IntPtr hToken, ref ProfileInfo lpProfileInfo);

    [DllImport(&quot;Userenv.dll&quot;, CallingConvention =
    CallingConvention.Winapi, SetLastError = true, CharSet = CharSet.Auto)]
    public static extern bool UnloadUserProfile
    (IntPtr hToken, IntPtr lpProfileInfo);

    private const int LOGON32_LOGON_INTERACTIVE = 2;
    private const int LOGON32_PROVIDER_DEFAULT = 0;

    #endregion

    #region Private member.
    // ------------------------------------------------------------------
    /// &lt;summary&gt;
    /// Impersonates the valid user.
    /// &lt;/summary&gt;
    /// &lt;param name=&quot;userName&quot;&gt;Name of the user.&lt;/param&gt;
    /// &lt;param name=&quot;domain&quot;&gt;The domain.&lt;/param&gt;
    /// &lt;param name=&quot;password&quot;&gt;The password.&lt;/param&gt;
    private void ImpersonateValidUser(string userName, string domain, string password)
    {
        WindowsIdentity tempWindowsIdentity = null;
        IntPtr token = IntPtr.Zero;
        IntPtr tokenDuplicate = IntPtr.Zero;

        try
        {
            if (RevertToSelf())
            {
                if (LogonUser(userName, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token) != 0)
                {
                    if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
                    {
                        tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
                        impersonationContext = tempWindowsIdentity.Impersonate();
                    }
                }
            }
        }
        catch (Exception ex)
        {
            EmdsLogger.AddToLog(ex);

        }
        finally
        {
            if (token != IntPtr.Zero)
            {
                CloseHandle(token);
            }
            if (tokenDuplicate != IntPtr.Zero)
            {
                CloseHandle(tokenDuplicate);
            }
        }
    }

    // GetErrorMessage formats and returns an error message
    // corresponding to the input errorCode.
    public static string GetErrorMessage(int errorCode)
    {
        int FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100;
        int FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200;
        int FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000;

        //int errorCode = 0x5; //ERROR_ACCESS_DENIED
        //throw new System.ComponentModel.Win32Exception(errorCode);

        int messageSize = 255;
        String lpMsgBuf = &quot;&quot;;
        int dwFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS;

        IntPtr ptrlpSource = IntPtr.Zero;
        IntPtr prtArguments = IntPtr.Zero;

        int retVal = FormatMessage(dwFlags, ref ptrlpSource, errorCode, 0, ref lpMsgBuf, messageSize, prtArguments);
        if (0 == retVal)
        {
            //throw new Exception(&quot;Failed to format message for error code &quot; + errorCode + &quot;. &quot;);
            //EmdsLogger.AddToLog(&quot;Impersonation&quot;, &quot;Failed to format message for error code &quot; + errorCode + &quot;. &quot;);
        }

        return lpMsgBuf;
    }

    /// &lt;summary&gt;
    /// Undoes the impersonation.
    /// &lt;/summary&gt;
    private void UndoImpersonation()
    {
        if (impersonationContext != null)
        {
            impersonationContext.Undo();
        }
    }

    private WindowsImpersonationContext impersonationContext = null;

    // ------------------------------------------------------------------
    #endregion
}

[StructLayout(LayoutKind.Sequential)]
public struct ProfileInfo
{
    ///
    /// Specifies the size of the structure, in bytes.
    ///
    public int dwSize;

    ///
    /// This member can be one of the following flags: 
    /// PI_NOUI or PI_APPLYPOLICY
    ///
    public int dwFlags;

    ///
    /// Pointer to the name of the user.
    /// This member is used as the base name of the directory 
    /// in which to store a new profile.
    ///
    public string lpUserName;

    ///
    /// Pointer to the roaming user profile path.
    /// If the user does not have a roaming profile, this member can be NULL.
    ///
    public string lpProfilePath;

    ///
    /// Pointer to the default user profile path. This member can be NULL.
    ///
    public string lpDefaultPath;

    ///
    /// Pointer to the name of the validating domain controller, in NetBIOS format.
    /// If this member is NULL, the Windows NT 4.0-style policy will not be applied.
    ///
    public string lpServerName;

    ///
    /// Pointer to the path of the Windows NT 4.0-style policy file. 
    /// This member can be NULL.
    ///
    public string lpPolicyPath;

    ///
    /// Handle to the HKEY_CURRENT_USER registry key.
    ///
    public IntPtr hProfile;
}
&lt;/pre&gt;
&lt;/br&gt;
I hope it helps you.
&lt;/br&gt;
Regards,&lt;/br&gt;
Pavan N
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://pavancsharp.blogspot.com/feeds/7329414751312539370/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://pavancsharp.blogspot.com/2013/05/implementing-impersonation-in-c.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3985264170283763322/posts/default/7329414751312539370'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3985264170283763322/posts/default/7329414751312539370'/><link rel='alternate' type='text/html' href='http://pavancsharp.blogspot.com/2013/05/implementing-impersonation-in-c.html' title='Implementing impersonation in c#'/><author><name>Pavan N</name><uri>http://www.blogger.com/profile/16386797942234627578</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuxNeqFLB46wZIpHiomJ0jZ-rU9wu3NHzydxMkp0VjmkxePzjAhrm3Hjiq8dLiW8DvTXCmUUMBqgbA2MYUeJASd11n7BkbTOlfEvaIt8tW66S0xItZtDw7Rh5b2WX9DA/s220/IMG_20170223_100350.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3985264170283763322.post-8206607076274149326</id><published>2013-05-22T18:17:00.000+05:30</published><updated>2013-05-22T18:17:11.218+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="asp.net"/><category scheme="http://www.blogger.com/atom/ns#" term="c#"/><category scheme="http://www.blogger.com/atom/ns#" term="captcha"/><category scheme="http://www.blogger.com/atom/ns#" term="code"/><category scheme="http://www.blogger.com/atom/ns#" term="generation"/><category scheme="http://www.blogger.com/atom/ns#" term="password"/><category scheme="http://www.blogger.com/atom/ns#" term="random string"/><category scheme="http://www.blogger.com/atom/ns#" term="string"/><category scheme="http://www.blogger.com/atom/ns#" term="WCF"/><title type='text'>Generate random string using c#</title><content type='html'>&lt;br /&gt;
Hi,&lt;br /&gt;
Today am blogging a simple concept related to generating a random sring. In realtime it will be used mainly for generating captcha, random password, Temporary objects etc.&lt;br /&gt;
&lt;br /&gt;
Here we can send the string length expected. Also You can modify he complexity. Its a simple code and easily understandble. Here is the code snippet.&lt;br /&gt;
&lt;br /&gt;
&lt;pre formatcs=&quot;1&quot;&gt;/// &lt;summary&gt;
/// Creates the random string based on the length provided
/// &lt;/summary&gt;
/// &lt;param name=&quot;PasswordLength&quot; /&gt;Length of the password.
/// &lt;returns&gt;&lt;/returns&gt;
public static string generateRandomString(int PasswordLength)
{
  string _allowedChars = &quot;abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789&quot;;
  Random randNum = new Random();
  char[] chars = new char[PasswordLength];
  int allowedCharCount = _allowedChars.Length;

  for (int i = 0; i &amp;lt; PasswordLength; i++)
  {
    chars[i] = _allowedChars[(int)((_allowedChars.Length) * randNum.NextDouble())];
  }
  return new string(chars);
}
&lt;/pre&gt;
&lt;br /&gt;
hope it helps you.&lt;br /&gt;
&lt;br /&gt;
Regards,&lt;br /&gt;
Pavan N</content><link rel='replies' type='application/atom+xml' href='http://pavancsharp.blogspot.com/feeds/8206607076274149326/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://pavancsharp.blogspot.com/2013/05/generate-random-string.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3985264170283763322/posts/default/8206607076274149326'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3985264170283763322/posts/default/8206607076274149326'/><link rel='alternate' type='text/html' href='http://pavancsharp.blogspot.com/2013/05/generate-random-string.html' title='Generate random string using c#'/><author><name>Pavan N</name><uri>http://www.blogger.com/profile/16386797942234627578</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuxNeqFLB46wZIpHiomJ0jZ-rU9wu3NHzydxMkp0VjmkxePzjAhrm3Hjiq8dLiW8DvTXCmUUMBqgbA2MYUeJASd11n7BkbTOlfEvaIt8tW66S0xItZtDw7Rh5b2WX9DA/s220/IMG_20170223_100350.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3985264170283763322.post-3740950792728107941</id><published>2013-05-21T20:05:00.000+05:30</published><updated>2013-05-21T20:05:32.156+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="asp.net"/><category scheme="http://www.blogger.com/atom/ns#" term="c#"/><category scheme="http://www.blogger.com/atom/ns#" term="etc"/><category scheme="http://www.blogger.com/atom/ns#" term="file size"/><category scheme="http://www.blogger.com/atom/ns#" term="IO"/><title type='text'>Finding file size in bytes on serverside using c#</title><content type='html'>Hi,&lt;br /&gt;
This requirement will come mainly while doing I/O operations (in my opinion it is obviously one of tedious taks carried by server). Here am showing you the snippet of code to find out the number of bytes of a file. here am using this code to validate the size of the file on server side. Which is a common requirement while doing file uploads.&lt;br /&gt;
&lt;br /&gt;
Here is the snippet.&lt;br /&gt;
&lt;br /&gt;
&lt;pre formatcs=&quot;1&quot;&gt;
private static long GetFileSizeOnDisk(string filelocation)
{
  FileInfo info = new FileInfo(file);
  uint dummy, sectorsPerCluster, bytesPerSector;
  int result = GetDiskFreeSpaceW(info.Directory.Root.FullName, out sectorsPerCluster, out bytesPerSector, out dummy, out dummy);
  if (result == 0) throw new Win32Exception();
  uint clusterSize = sectorsPerCluster * bytesPerSector;
  uint hosize;
  uint losize = GetCompressedFileSizeW(file, out hosize);
  long size;
  size = (long)hosize &lt;&lt; 32 | losize;
  return ((size + clusterSize - 1) / clusterSize) * clusterSize;
}

[DllImport(&quot;kernel32.dll&quot;)]
static extern uint GetCompressedFileSizeW([In, MarshalAs(UnmanagedType.LPWStr)] string lpFileName,
   [Out, MarshalAs(UnmanagedType.U4)] out uint lpFileSizeHigh);

[DllImport(&quot;kernel32.dll&quot;, SetLastError = true, PreserveSig = true)]
static extern int GetDiskFreeSpaceW([In, MarshalAs(UnmanagedType.LPWStr)] string lpRootPathName,
   out uint lpSectorsPerCluster, out uint lpBytesPerSector, out uint lpNumberOfFreeClusters,
   out uint lpTotalNumberOfClusters);
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
Thanks &amp;amp; Regards,&lt;br /&gt;
Pavan N</content><link rel='replies' type='application/atom+xml' href='http://pavancsharp.blogspot.com/feeds/3740950792728107941/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://pavancsharp.blogspot.com/2013/05/finding-file-size-in-bytes-on.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3985264170283763322/posts/default/3740950792728107941'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3985264170283763322/posts/default/3740950792728107941'/><link rel='alternate' type='text/html' href='http://pavancsharp.blogspot.com/2013/05/finding-file-size-in-bytes-on.html' title='Finding file size in bytes on serverside using c#'/><author><name>Pavan N</name><uri>http://www.blogger.com/profile/16386797942234627578</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuxNeqFLB46wZIpHiomJ0jZ-rU9wu3NHzydxMkp0VjmkxePzjAhrm3Hjiq8dLiW8DvTXCmUUMBqgbA2MYUeJASd11n7BkbTOlfEvaIt8tW66S0xItZtDw7Rh5b2WX9DA/s220/IMG_20170223_100350.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3985264170283763322.post-1989720189849047496</id><published>2013-05-21T19:53:00.002+05:30</published><updated>2013-05-21T19:57:25.363+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="asp.net"/><category scheme="http://www.blogger.com/atom/ns#" term="c#"/><category scheme="http://www.blogger.com/atom/ns#" term="conversion"/><category scheme="http://www.blogger.com/atom/ns#" term="currency"/><category scheme="http://www.blogger.com/atom/ns#" term="number"/><title type='text'>Number to String conversion</title><content type='html'>Hi,&lt;br /&gt;
Generally in finance or banking or this kind of requirement is needed. Its more or less a common thing to convert a number to a string. I used this to convert currency to words format.&lt;br /&gt;
&lt;br /&gt;
Here is the snippet.&lt;br /&gt;
&lt;br /&gt;
&lt;pre formatcs=&quot;1&quot;&gt;/// &lt;summary&gt;
/// Numbers to text.
/// &lt;/summary&gt;
/// &lt;param name=&quot;value&quot; /&gt;The value.
/// &lt;returns&gt;&lt;/returns&gt;
public string NumberToText(int value)
{
 string[] num0to10 = new string[] { &quot;Zero&quot;, &quot;One&quot;, &quot;Two&quot;, &quot;Three&quot;, &quot;Four&quot;, &quot;Five&quot;, &quot;Six&quot;, &quot;Seven&quot;, &quot;Eight&quot;, &quot;Nine&quot;, &quot;Ten&quot; };
 string[] num11to20 = new string[] { &quot;Eleven&quot;, &quot;Twelve&quot;, &quot;Thirteen&quot;, &quot;Fourteen&quot;, &quot;Fifteen&quot;, &quot;Sixteen&quot;, &quot;Seventeen&quot;, &quot;Eighteen&quot;, &quot;Nineteen&quot;, &quot;Twenty&quot; };
 string[] num20to100 = new string[] { &quot;Ten&quot;, &quot;Twenty&quot;, &quot;Thirty&quot;, &quot;Forty&quot;, &quot;Fifty&quot;, &quot;Sixty&quot;, &quot;Seventy&quot;, &quot;Eighty&quot;, &quot;Ninety&quot;, &quot;Twenty&quot; };

 string result = string.Empty;

 int INumValue;
 int ThousValue;
 int HundsValue;
 int TensValue;
 int OnesValue;
 int TeenChk;

 OnesValue = 0;
 INumValue = value;

 if (INumValue == 0)
 {
  result = &quot;Zero&quot;;
 }
 else
 {
  ThousValue = (INumValue / 1000);
  if (ThousValue &amp;gt; 0)
   INumValue = INumValue - (ThousValue * 1000);

  HundsValue = (INumValue / 100);
  if (HundsValue &amp;gt; 0)
   INumValue = INumValue - (HundsValue * 100);

  TeenChk = INumValue;

  if (INumValue &amp;gt;= 20)
  {
   TensValue = (INumValue / 10);
   if (TensValue &amp;gt; 0)
   {
    INumValue = INumValue - (TensValue * 10);
   }
   OnesValue = INumValue;
  }
  else
  {
   TensValue = INumValue;
  }

  if (ThousValue &amp;gt; 0)
  {
   result = num0to10[ThousValue] + &quot; Thousand&quot;;
   if (HundsValue == 0)
    result = result + &quot; and &quot;;
  }

  if (HundsValue &amp;gt; 0)
  {
   result = result + &quot; &quot; + num0to10[HundsValue] + &quot; Hundred&quot;;
   if (TensValue &amp;gt; 0 || OnesValue &amp;gt; 0)
    result = result + &quot; and &quot;;
  }

  if (TensValue &amp;gt; 0)

   if (TeenChk &amp;lt; 20)

    if (TeenChk &amp;lt;= 10)

     result = result + num0to10[TeenChk];
    else
     result = result + num11to20[TeenChk - 11];
   else
    result = result + &#39; &#39; + num20to100[TensValue - 1];
  if (OnesValue &amp;gt; 0)
   result = result + &#39; &#39; + num0to10[OnesValue];
 }

 return result.Trim();
}&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
Hope it helps anyone of you.&lt;br /&gt;
&lt;br /&gt;
Regrads,&lt;br /&gt;
Pavan N</content><link rel='replies' type='application/atom+xml' href='http://pavancsharp.blogspot.com/feeds/1989720189849047496/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://pavancsharp.blogspot.com/2013/05/number-to-string-conversions.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3985264170283763322/posts/default/1989720189849047496'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3985264170283763322/posts/default/1989720189849047496'/><link rel='alternate' type='text/html' href='http://pavancsharp.blogspot.com/2013/05/number-to-string-conversions.html' title='Number to String conversion'/><author><name>Pavan N</name><uri>http://www.blogger.com/profile/16386797942234627578</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuxNeqFLB46wZIpHiomJ0jZ-rU9wu3NHzydxMkp0VjmkxePzjAhrm3Hjiq8dLiW8DvTXCmUUMBqgbA2MYUeJASd11n7BkbTOlfEvaIt8tW66S0xItZtDw7Rh5b2WX9DA/s220/IMG_20170223_100350.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3985264170283763322.post-3354269503772393143</id><published>2013-05-09T18:46:00.000+05:30</published><updated>2013-05-21T17:45:20.089+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="asp.net"/><category scheme="http://www.blogger.com/atom/ns#" term="c#"/><category scheme="http://www.blogger.com/atom/ns#" term="code"/><category scheme="http://www.blogger.com/atom/ns#" term="Download"/><category scheme="http://www.blogger.com/atom/ns#" term="IO"/><category scheme="http://www.blogger.com/atom/ns#" term="REST"/><category scheme="http://www.blogger.com/atom/ns#" term="WCF"/><category scheme="http://www.blogger.com/atom/ns#" term="WebGet"/><title type='text'>Download file using WCF REST services</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Hi,&lt;br /&gt;
Generally we used to download a file using a handlers and sending parameters as a querystring (lets say download.ashx). Here few cases we might need to impersonate the user also. It purely depends on environment.&lt;br /&gt;
&lt;br /&gt;
Here is a sample code snippet just for reference.&lt;br /&gt;
&lt;br /&gt;
&lt;pre formatcs=&quot;1&quot; style=&quot;border-style: groove&quot;&gt;if (extension.Trim() == &quot;.pdf&quot;)
{
  context.Response.Clear();
  context.Response.ContentType = &quot;application/pdf&quot;;                        
  context.Response.AddHeader(&quot;content-disposition&quot;, &quot;attachment;filename=&quot; + queryFile + &quot;.pdf&quot;);
  context.Response.BinaryWrite(System.IO.File.ReadAllBytes(file));      
}
&lt;/pre&gt;
&lt;br /&gt;
But instead of that we can download a file as a stream using a WCF REST service too.&lt;br /&gt;
Here is the code snippet for declaration in interface ..&lt;br /&gt;
&lt;pre formatcs=&quot;1&quot;&gt;/// &amp;lt;summary&amp;gt;
/// To fetch documents
/// &amp;lt;/summary&amp;gt;
/// &amp;lt;param name=&quot;filePath&quot;&amp;gt;Represent text of filepath&amp;lt;/param&amp;gt;
/// &amp;lt;param name=&quot;fileName&quot;&amp;gt;Represents text of filename&amp;lt;/param&amp;gt;
/// &amp;lt;returns&amp;gt;Fetch documents&amp;lt;/returns&amp;gt;
[OperationContract]
[WebGet(UriTemplate = &quot;DownloadFile/{filePath}/{fileName}&quot;)]
Stream DownloadFile(string filePath, string fileName);
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
and here is the code for Implementation part.
&lt;br /&gt;
&lt;pre formatcs=&quot;1&quot;&gt;/// &amp;lt;summary&amp;gt;
/// To fetch documents
/// &amp;lt;/summary&amp;gt;
/// &amp;lt;param name=&quot;filePath&quot;&amp;gt;Represent text of filepath&amp;lt;/param&amp;gt;
/// &amp;lt;param name=&quot;fileName&quot;&amp;gt;Represents text of filename&amp;lt;/param&amp;gt;
/// &amp;lt;returns&amp;gt;Fetch documents&amp;lt;/returns&amp;gt;
public Stream DownloadFile(string filePath, string fileName)
{
  string downloadFilePath = Path.Combine(HostingEnvironment.MapPath(&quot;~/FOLDERPATH/&quot;), filePath + @&quot;/&quot; + fileName);
  WebOperationContext.Current.OutgoingResponse.ContentType = &quot;application/octet-stream&quot;;
  return File.OpenRead(downloadFilePath);
}
&lt;/pre&gt;
Regards,

Pavan N

&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://pavancsharp.blogspot.com/feeds/3354269503772393143/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://pavancsharp.blogspot.com/2013/05/download-file-using-wcf-rest-services.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3985264170283763322/posts/default/3354269503772393143'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3985264170283763322/posts/default/3354269503772393143'/><link rel='alternate' type='text/html' href='http://pavancsharp.blogspot.com/2013/05/download-file-using-wcf-rest-services.html' title='Download file using WCF REST services'/><author><name>Pavan N</name><uri>http://www.blogger.com/profile/16386797942234627578</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuxNeqFLB46wZIpHiomJ0jZ-rU9wu3NHzydxMkp0VjmkxePzjAhrm3Hjiq8dLiW8DvTXCmUUMBqgbA2MYUeJASd11n7BkbTOlfEvaIt8tW66S0xItZtDw7Rh5b2WX9DA/s220/IMG_20170223_100350.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3985264170283763322.post-2360920845248197153</id><published>2013-04-24T19:00:00.000+05:30</published><updated>2013-05-21T17:04:43.935+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="asp.net"/><category scheme="http://www.blogger.com/atom/ns#" term="c#"/><title type='text'>Validate domain user using c#</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Hi,&lt;br /&gt;
Here am sharing code related to validate a domain user againsta active directory. This is well used for implementing authentication and authorization in asp.net or windows application.&lt;br /&gt;
&lt;br /&gt;
the call should be sent a [domainname]\[username]&lt;username&gt; and [password]&lt;/username&gt;&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre formatcs=&quot;1&quot;&gt;public int VerifyUser(string userName, string password)
{
  try
  {
    string domainName = string.Empty;
    string domainUserName = string.Empty;
    string[] tempString = null;
    string[] delimiter = new string[] { @&quot;\&quot; };

    tempString = userName.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
    if ((tempString.Length == 0) || (tempString.Length == 1))
    {
      return 1;
    }

    domainName = tempString[0];
    domainUserName = tempString[1];
    LDAP.Domain_Authentication imperson = new LDAP.Domain_Authentication(domainUserName, password, domainName);
    if (imperson.IsValid())
    {
      return 2;
    }
    return 3;
  }
  catch (Exception ex)
  {
    return -1;
  }
}
&lt;/pre&gt;
&lt;br /&gt;
And the LDAP class is as follows&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre formatcs=&quot;1&quot;&gt;namespace LDAP
{
  using System;
  using System.Collections.Generic;
  using System.Text;
  using System.Security;
  using System.DirectoryServices.AccountManagement;

  public struct Credentials
  {
    public string Username;
    public string Password;
  }
  public class Domain_Authentication
  {
    public Credentials Credentials;
    public string Domain;
    public Domain_Authentication(string Username, string Password, string SDomain)
    {
      Credentials.Username = Username;
      Credentials.Password = Password;
      Domain = SDomain;
    }

    public bool IsValid()
    {
      using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, Domain))
      {
        // validate the credentials
        return pc.ValidateCredentials(Credentials.Username, Credentials.Password);
      }
    }
  }
}
&lt;/pre&gt;
Hope this post helps you. Please do comment if any queries/suggestions.&lt;br /&gt;
&lt;br /&gt;
Regards,&lt;br /&gt;
Pavan N

&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://pavancsharp.blogspot.com/feeds/2360920845248197153/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://pavancsharp.blogspot.com/2013/04/validate-domain-user-using-c.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3985264170283763322/posts/default/2360920845248197153'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3985264170283763322/posts/default/2360920845248197153'/><link rel='alternate' type='text/html' href='http://pavancsharp.blogspot.com/2013/04/validate-domain-user-using-c.html' title='Validate domain user using c#'/><author><name>Pavan N</name><uri>http://www.blogger.com/profile/16386797942234627578</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuxNeqFLB46wZIpHiomJ0jZ-rU9wu3NHzydxMkp0VjmkxePzjAhrm3Hjiq8dLiW8DvTXCmUUMBqgbA2MYUeJASd11n7BkbTOlfEvaIt8tW66S0xItZtDw7Rh5b2WX9DA/s220/IMG_20170223_100350.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3985264170283763322.post-29330468978509268</id><published>2013-04-24T18:47:00.000+05:30</published><updated>2013-05-21T17:19:45.971+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="asp.net"/><category scheme="http://www.blogger.com/atom/ns#" term="c#"/><title type='text'>server side printing / PDF conversions using c# and asp.net/wcf service</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;Hi All,
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;I know its a tough task to search this kind of &amp;nbsp;requirement . As i did same and endup with a solution like using Crystal reports for printing on network printer. And a WKHTML library to convert html pages to either images or PDF. There inturn i can convert TIFF files to PDF using Ghostscript(If you need this code please drop a comment).&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;Now coming to this server side network printing. At first we need to have a following things&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;Visual studio&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;Crystal reports for Visual studio&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://code.google.com/p/wkhtmltopdf/&quot; target=&quot;_blank&quot;&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;WKHTMLTOPDF/WKHTMLTOIMAGE&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;A proper permissions on a folder where we are going to store our images or PDF files etc which need to be sent to a printer&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;And the code flow is first i will scale the image to a standard size with the following code&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;font-family: Arial, Helvetica, sans-serif;&quot;&gt;prepare a HTML file having image tag with SRC as the image file which needs to be printed then i will call WKHTMLTOIMAGE library &amp;nbsp;to convert&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Trebuchet MS, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;pre formatcs=&quot;1&quot;&gt;public static Image ScaleImage(Image image, int maxWidth, int maxHeight)
        {
            var ratioX = (double)maxWidth / image.Width;
            var ratioY = (double)maxHeight / image.Height;
            var ratio = Math.Min(ratioX, ratioY);

            var newWidth = (int)(image.Width * ratio);
            var newHeight = (int)(image.Height * ratio);

            var newImage = new Bitmap(newWidth, newHeight);
            Graphics.FromImage(newImage).DrawImage(image, 0, 0, newWidth, newHeight);
            return newImage;
        }&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;br /&gt;
Then we will prepare a html string and save it to a folder in our directory. The code is as follows&lt;br /&gt;
&lt;br /&gt;
&lt;pre formatcs=&quot;1&quot;&gt;if (ext == &quot;bmp&quot; || ext == &quot;png&quot; || ext == &quot;gif&quot; || ext == &quot;jpg&quot; || ext == &quot;jpeg&quot;)
{
filename = ImageDocId + &quot;_&quot; + DateTime.Now.ToString(&quot;MMddyyyhhmmssfffff&quot;);
filePath = localReportPath + @&quot;Reports\Savedfiles\&quot; + filename;
using (Impersonation impersonation = new Impersonation(impersonationUsername, impersonationDomain, impersonationPWD))
{
 using (Image img = Image.FromFile(file))
 {
  int height = 0;
  int width = 0;

  if (img.Width &amp;lt; 700)
  { width = img.Width; }
  else
  { width = 700; }

  if (img.Height &amp;lt; 700)
  { height = img.Height; }
  else
  { height = 900; }

  Image newImage = ScaleImage(img, width, height);
  newImage.Save(filePath + &quot;.jpg&quot;, ImageFormat.Jpeg);
  
 }
}

StringBuilder sb = new StringBuilder();
   sb.AppendLine(&quot;&amp;lt;html&amp;gt;&amp;lt;body&amp;gt;&quot;);
sb.AppendLine(&quot;&amp;lt;img alt=\&quot;toprint\&quot; src=\&quot;&quot; + filePath + &quot;.jpg&quot; + &quot;\&quot; width=\&quot;100%\&quot;/&amp;gt;&quot;);
sb.AppendLine(&quot;&amp;lt;/body&amp;gt;&amp;lt;/html&amp;gt;&quot;);
   
if (flag == &quot;3&quot;)
{
 ppdf.ImagePath = filePath + &quot;.jpg&quot;;
 printtype = &quot;ChartViewImage&quot;;// its a report name i used to send to switch case which is not mentioned here
 isSupported = true;
}
else
{                          
 htmlFile = filePath + &quot;.html&quot;;
 fs = new FileStream(htmlFile, FileMode.Create, FileAccess.Write);
 writer = new StreamWriter(fs, Encoding.UTF8);
 writer.Write(sb.ToString());
 writer.Close();
 fs.Close();
  

}                         

}
&lt;/pre&gt;
&lt;br /&gt;
Then to convert to PDF from HTML file generated we will use this code&lt;br /&gt;
&lt;br /&gt;
&lt;pre formatcs=&quot;1&quot;&gt;ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = AppDomain.CurrentDomain.BaseDirectory + @&quot;\bin\wkhtmltopdf.exe&quot;;
pdfFile = localReportPath + &quot;\\Reports\\Savedfiles\\&quot; + filename + &quot;.pdf&quot;;
//Ref: http://madalgo.au.dk/~jakobt/wkhtmltoxdoc/wkhtmltopdf-0.9.9-doc.html
if (ext == &quot;xml&quot; &amp;amp;&amp;amp; filetype == &quot;Visit Summary (CCD)&quot;)
{
   startInfo.Arguments = &quot; --minimum-font-size 16 --margin-left 10mm --margin-right 10mm --zoom 3 &quot; + htmlFile + &quot; &quot; + pdfFile;
}
else
{
   startInfo.Arguments = &quot; --margin-left 10mm --margin-right 10mm &quot; + htmlFile + &quot; &quot; + pdfFile;
}
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process p = Process.Start(startInfo);
p.WaitForExit();
p.Dispose();
p.Close();
&lt;/pre&gt;
&lt;br /&gt;
But for printing Image file we need to use crystal report as follows&lt;br /&gt;
&lt;br /&gt;
&lt;pre formatcs=&quot;1&quot;&gt;if (reportName == &quot;ChartViewImage&quot;)
{
#region ChartViewImage

DataTable tempdataTable = new DataTable();
tempdataTable.TableName = &quot;Table&quot;;
tempdataTable.Columns.Add(&quot;ID&quot;, typeof(String));
tempdataTable.Columns.Add(&quot;Label&quot;, typeof(String));
tempdataTable.Columns.Add(&quot;image_path&quot;, typeof(String));
tempdataTable.Columns.Add(&quot;image_stream&quot;, typeof(System.Byte[]));
DataRow dr = tempdataTable.NewRow();

FileStream fs = new FileStream(ImagePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] Image = new byte[fs.Length];
fs.Read(Image, 0, Convert.ToInt32(fs.Length));
fs.Close();
dr[0] = &quot;1&quot;;
dr[1] = &quot;Hello&quot;;
dr[2] = ImagePath;
dr[3] = Image;
fs.Close();
tempdataTable.Rows.Add(dr);
string filename = &quot;CVImage&quot; + &quot;_&quot; + DateTime.Now.ToString(&quot;MMddyyyhhmmssfffff&quot;);

myreport.Load(localReportPath + &quot;\\Reports\\&quot; + &quot;ChartViewImage.rpt&quot;);
myreport.Database.Tables[0].ApplyLogOnInfo(GetLogonInfo());

myreport.SetDataSource(tempdataTable);
myreport.Refresh();
myreport.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, localReportPath + &quot;\\Reports\\Savedfiles\\&quot; + filename + &quot;.pdf&quot;);
using (Impersonation imp = new Impersonation(impersonationUsername, impersonationDomain, impersonationPWD))
{
 if (!string.IsNullOrEmpty(printerName))
 {
  
  myreport.PrintOptions.PrinterName = printerName;
  
 }
 myreport.PrintToPrinter(copies, false, 0, 0);
}

isSucces = true;
#endregion
}
&lt;/pre&gt;
And here the chartviewimage.rpt need to be prepared by just creating a blob object using a xsd file with dummy table in it. If you need this part too please drop a message.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Thanks for going through my post.&lt;br /&gt;
&lt;br /&gt;
Regards,&lt;br /&gt;
Pavan N

&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://pavancsharp.blogspot.com/feeds/29330468978509268/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://pavancsharp.blogspot.com/2013/04/server-side-printing-pdf-conversions.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3985264170283763322/posts/default/29330468978509268'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3985264170283763322/posts/default/29330468978509268'/><link rel='alternate' type='text/html' href='http://pavancsharp.blogspot.com/2013/04/server-side-printing-pdf-conversions.html' title='server side printing / PDF conversions using c# and asp.net/wcf service'/><author><name>Pavan N</name><uri>http://www.blogger.com/profile/16386797942234627578</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuxNeqFLB46wZIpHiomJ0jZ-rU9wu3NHzydxMkp0VjmkxePzjAhrm3Hjiq8dLiW8DvTXCmUUMBqgbA2MYUeJASd11n7BkbTOlfEvaIt8tW66S0xItZtDw7Rh5b2WX9DA/s220/IMG_20170223_100350.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3985264170283763322.post-4137380515404322985</id><published>2012-11-13T15:00:00.004+05:30</published><updated>2013-05-21T17:18:21.175+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="beginner tips"/><category scheme="http://www.blogger.com/atom/ns#" term="best practise"/><category scheme="http://www.blogger.com/atom/ns#" term="code"/><category scheme="http://www.blogger.com/atom/ns#" term="code review"/><category scheme="http://www.blogger.com/atom/ns#" term="developer"/><category scheme="http://www.blogger.com/atom/ns#" term="development"/><title type='text'>Part– II : Coding standards</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
These are the main checklist I used to follow in day to day code development and maintenance. Hope this might help some of you new people. If there is any mistakes/typo please do not hesitate to comment. Coz these are my personal views and opinions&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
1.&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;	&lt;/span&gt;Need to avoid unnecessary comments especially while releasing a build&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
2.&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;	&lt;/span&gt;Reduce as much lines of code as possible&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
3.&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;	&lt;/span&gt;if a variable is used only once then declare it locally if possible&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
4.&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;	&lt;/span&gt;Follow the naming conventions especially in DB layer. This helps a lot I promise :)&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
5.&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;	&lt;/span&gt;Method name should tell what it does. Do not use miss-leading names. If the method name is obvious, there is no need of documentation explaining what the method does&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
6.&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;	&lt;/span&gt;Make a note or summary at top of the page if the code was developed my multiple persons. it helps to track when and which kind of development started&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
7.&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;	&lt;/span&gt;Make sure that the required input values gets logged while logging the error message in catch block&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
8.&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;	&lt;/span&gt;Avoid unnecessary references and using’s&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
9.&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;	&lt;/span&gt;Dispose the objects properly and completely especially while COM programming&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
10.&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;	&lt;/span&gt;Prepare the project structure like so you need to reuse it for other projects where ever necessary. Hope you guys will be smart enough to change the names as of need. And enjoy the slag times to improve your logic.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
11.&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;	&lt;/span&gt;Avoid a ladder of IF loops. make sure your process flows as if – else&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
12.&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;	&lt;/span&gt;Your code looks beautiful and reflects your cleanliness &amp;amp; discipline. if there is a need of huge method just either split it in to different sub-methods or put a #region’s accordingly for hectic scrolling while debugging&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
13.&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;	&lt;/span&gt;For beginners, while typing every fragments of code question yourself why we need it. Don’t believe in Google for while at least :)&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
14.&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;	&lt;/span&gt;Your project structure should be very perfect. and folder structures&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
15.&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;	&lt;/span&gt;if there is any need of hard coding. try using them as a variable or an enum. so that while reviewing/code release it will be easier&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
16.&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;	&lt;/span&gt;Never assume that your code will run from drive &quot;C:” You may never know, some users may run it from network or from a &quot;Z:”&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
17.&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;	&lt;/span&gt;Don’t take it as granted that end-users will understand what ever pop-up’s you show. make sure that meaningful and helpful messages were shown. it must be short and simple&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
18.&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;	&lt;/span&gt;Also make sure that if you learn something new try to implement it where ever it is possible and acceptable in your logics&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
19.&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;	&lt;/span&gt;And unit testing; Read about the business logic of the thing you developed and behave as you are an end-user of it.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;1.&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;	&lt;/span&gt;Blindly don’t write code for 1+2=3. it will work for you in development. But will it work for an end user while giving a huge values?&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
20.&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;	&lt;/span&gt;:) This is for smart geeks…&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;1.&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;	&lt;/span&gt;what ever you learn it should be one of the weapon in your armory.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;2.&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;	&lt;/span&gt;Do not compare with other programmers and disrespect yourselves. Every programmer will have their own pro’s &amp;amp; con’s&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;3.&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;	&lt;/span&gt;Improve your presentation skills.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;4.&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;	&lt;/span&gt;get/create an opportunity to interact with customers and the sales professionals, analysts within &amp;nbsp; &amp;nbsp;your industry. You will get to know the current market trends…. who knows it might create an new opportunity.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;5.&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;	&lt;/span&gt;In the IT world higher managements wont allow you to do coding after some time. you must lead people later on. Do not hesitate or have a valid reason and try to overcome it before accepting driver seatRegularly attend for interviews or interact with the people who are in other companies or in other projects&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;6.&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;	&lt;/span&gt;Make sure you are preparing for interviews daily. Love your job not the company you work for&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
21.&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;	&lt;/span&gt;Try to get a clear picture while doing any career shift. whether it might be an onsite opportunity within your company or a new job&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
22.&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;	&lt;/span&gt;if you are going for an onsite don’t think like you are working for the same company. the dealings will be completely different and money matter&amp;nbsp;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
23.&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;	&lt;/span&gt;Last but not the least every other developer must be your friend. And none of the KT process is perfect. That’s a fact and that’s a major risk. So make sure that your code talks and helps the other developer to carry on from where you had left it.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
So happy coding guys.. &amp;nbsp;:) :)&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
Regards,&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
P@’1’&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://pavancsharp.blogspot.com/feeds/4137380515404322985/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://pavancsharp.blogspot.com/2012/11/part-ii-coding-standards.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3985264170283763322/posts/default/4137380515404322985'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3985264170283763322/posts/default/4137380515404322985'/><link rel='alternate' type='text/html' href='http://pavancsharp.blogspot.com/2012/11/part-ii-coding-standards.html' title='Part– II : Coding standards'/><author><name>Pavan N</name><uri>http://www.blogger.com/profile/16386797942234627578</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuxNeqFLB46wZIpHiomJ0jZ-rU9wu3NHzydxMkp0VjmkxePzjAhrm3Hjiq8dLiW8DvTXCmUUMBqgbA2MYUeJASd11n7BkbTOlfEvaIt8tW66S0xItZtDw7Rh5b2WX9DA/s220/IMG_20170223_100350.jpg'/></author><thr:total>0</thr:total><georss:featurename>BTM Layout 1st Stage, Bangalore, Karnataka, India</georss:featurename><georss:point>12.9128118 77.6092188</georss:point><georss:box>12.8973348 77.5894778 12.9282888 77.62895979999999</georss:box></entry><entry><id>tag:blogger.com,1999:blog-3985264170283763322.post-870590588582705490</id><published>2012-01-06T11:22:00.000+05:30</published><updated>2013-05-21T16:57:50.547+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="beginner tips"/><category scheme="http://www.blogger.com/atom/ns#" term="best practise"/><category scheme="http://www.blogger.com/atom/ns#" term="c#"/><category scheme="http://www.blogger.com/atom/ns#" term="code"/><category scheme="http://www.blogger.com/atom/ns#" term="code review"/><category scheme="http://www.blogger.com/atom/ns#" term="developer"/><category scheme="http://www.blogger.com/atom/ns#" term="development"/><category scheme="http://www.blogger.com/atom/ns#" term="etc"/><title type='text'>Part– I : Coding standards</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
Coding practise defines the developer awareness of writing a good code. but not his technical expertise..... I mean a little :)&lt;/blockquote&gt;
As of my experience with different projects across the domains and technologies &amp;amp; tools I got &amp;nbsp;a chance to work on enhancements, bug fixings, code reviewing, code development from the scratch level, enhancing my own code which was written years back by myself &amp;nbsp;:) , reading and preparing documentation on reviewing the code base from the client..... and many more. I got realised few points which i would like to blog.&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
1. Every developer should be friend of another developer (a stranger might be :) )&lt;br /&gt;
&lt;br /&gt;
2. Every developer must know what he is developing. Like he needs to get a bigger picture which we get a chance more in smaller companies compared to bigger MNC&#39;s&lt;br /&gt;
&lt;br /&gt;
3. Do coding like with in hours a developers must know what is there in the particular code base. (whether it is you or some one else who ever sitting for new changes)&lt;br /&gt;
&lt;br /&gt;
4. Everything can&#39;t be covered within few hours of knowledge transistion while leaving a company. So make sure that your code should talk about itself.&lt;br /&gt;
&lt;br /&gt;
5. Follow Do&#39;s and Dont&#39;s&lt;br /&gt;
&lt;br /&gt;
Still more to write... Just hang on :)&lt;br /&gt;
&lt;br /&gt;
Regards,&lt;br /&gt;
Pavan N&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;script type=&quot;text/javascript&quot;&gt;

  var _gaq = _gaq || [];
  _gaq.push([&#39;_setAccount&#39;, &#39;UA-28130071-1&#39;]);
  _gaq.push([&#39;_trackPageview&#39;]);

  (function() {
    var ga = document.createElement(&#39;script&#39;); ga.type = &#39;text/javascript&#39;; ga.async = true;
    ga.src = (&#39;https:&#39; == document.location.protocol ? &#39;https://ssl&#39; : &#39;http://www&#39;) + &#39;.google-analytics.com/ga.js&#39;;
    var s = document.getElementsByTagName(&#39;script&#39;)[0]; s.parentNode.insertBefore(ga, s);
  })();

&lt;/script&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://pavancsharp.blogspot.com/feeds/870590588582705490/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://pavancsharp.blogspot.com/2012/01/part-i-ifmy-code-my-rules.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3985264170283763322/posts/default/870590588582705490'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3985264170283763322/posts/default/870590588582705490'/><link rel='alternate' type='text/html' href='http://pavancsharp.blogspot.com/2012/01/part-i-ifmy-code-my-rules.html' title='Part– I : Coding standards'/><author><name>Pavan N</name><uri>http://www.blogger.com/profile/16386797942234627578</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='32' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuxNeqFLB46wZIpHiomJ0jZ-rU9wu3NHzydxMkp0VjmkxePzjAhrm3Hjiq8dLiW8DvTXCmUUMBqgbA2MYUeJASd11n7BkbTOlfEvaIt8tW66S0xItZtDw7Rh5b2WX9DA/s220/IMG_20170223_100350.jpg'/></author><thr:total>0</thr:total><georss:featurename>Bengaluru, Karnataka, India</georss:featurename><georss:point>12.9715987 77.5945627</georss:point><georss:box>12.724026199999999 77.2787057 13.2191712 77.910419699999991</georss:box></entry></feed>