tag:blogger.com,1999:blog-454683706053069462024-02-21T00:24:59.382-08:00.NET Interview for ExperiencedThis blog is providing interview questions and answers for experienced professionals in .NET technology. It covers most of technologies like .NET, C#, ASP.NET, MVC, OOPS Concept, Design Principles and Patterns, HTML, CSS, Java Script, jQuery, AngularJS, knockoutJS, SQL Server, IIS,Fluent Nhibernate etc.Bhanu Prataphttp://www.blogger.com/profile/17529341083467033319noreply@blogger.comBlogger28125tag:blogger.com,1999:blog-45468370605306946.post-72340405522810339282019-07-23T03:16:00.005-07:002020-01-14T05:12:52.627-08:00When to use an abstract class vs. interface in C#<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="line-height: 36.75pt; text-align: left;">
<b><span style="font-size: 30pt; letter-spacing: -0.4pt;"><span style="color: #0b5394; font-family: inherit;">When to use an abstract class vs.
interface in C#</span></span></b></h2>
<h3 style="line-height: normal; margin-bottom: 0.0001pt; text-align: left;">
<span style="font-size: 13.5pt;"><span style="color: #3d85c6; font-family: inherit;">Understanding the
differences between an abstract class and interface is key to designing loosely
coupled and extensible applications</span></span></h3>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: inherit;"><br /></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: inherit;"><br /></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: inherit;"><span style="font-size: 12pt;">When designing
applications, it is important to know when to use an abstract class and when to
use an interface. Although abstract classes and interfaces seem similar in some
ways, there are key differences that will determine which is the best choice for
what you’re trying to accomplish. In this blog post I’ll discuss those
differences and how to decide when to use which.</span><span style="font-size: 12pt;"><o:p></o:p></span></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: inherit;"><br /></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;">
<span style="font-family: inherit;"><span style="font-family: inherit; font-size: 12pt;">The short answer: An
abstract class allows you to create functionality that sub classes can
implement or override. An interface only allows you to define functionality,
not implement it. And whereas a class can extend only one abstract class, it
can take advantage of multiple interfaces. </span><span style="font-family: inherit; font-size: 12pt;"><o:p></o:p></span></span><br />
<span style="font-family: inherit;"><span style="font-size: 12.0pt;"><br /></span></span></div>
<h3 style="text-align: left;">
<span style="color: #3d85c6; font-family: inherit;">C# abstract class explained</span></h3>
<span style="font-family: inherit;">An abstract class is a special type of class that cannot be instantiated. An abstract class is designed to be inherited by sub classes that either implement or override its methods. In other words, abstract classes are either partially implemented or not implemented at all. You can have functionality in your abstract class—the methods in an abstract class can be both abstract and concrete. An abstract class can have constructors—this is one major difference between an abstract class and an interface. You can take advantage of abstract classes to design components and specify some level of common functionality that must be implemented by derived classes.</span><br />
<h3 style="text-align: left;">
<span style="color: #3d85c6; font-family: inherit;">C# interface explained</span></h3>
<span style="font-family: inherit;">An interface is basically a contract—it doesn’t have any implementation. An interface can contain only method declarations; it cannot contain method definitions. Nor can you have any member data in an interface. Whereas an abstract class may contain method definitions, fields, and constructors, an interface may only have declarations of events, methods, and properties. Methods declared in an interface must be implemented by the classes that implement the interface. Note that a class can implement more than one interface but extend only one class. The class that implements the interface should implement all its members. Like an abstract class, an interface cannot be instantiated.</span><br />
<span style="font-family: inherit;"><br /></span>
<br />
<h3 style="text-align: left;">
<span style="color: #3d85c6; font-family: inherit;"> Should I use an abstract class or an interface?</span></h3>
<span style="font-family: inherit;">Abstract classes provide you the flexibility to have certain concrete methods and some other methods that the derived classes should implement. By contrast, if you use interfaces, you would need to implement all the methods in the class that extends the interface. An abstract class is a good choice if you have plans for future expansion – i.e. if a future expansion is likely in the class hierarchy. If you would like to provide support for future expansion when using interfaces, you’ll need to extend the interface and create a new one.</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">On a different note, it is easy to add a new interface to the hierarchy if need be. However, if you already have an abstract class in your hierarchy, you can’t add another—i.e., you can add an abstract class only if none are available. You should use an interface if you want a contract on some behavior or functionality. You should not use an interface if you need to write the same code for the interface methods. In this case, you should use an abstract class, define the method once, and reuse it as needed. Do use interfaces to decouple your application’s code from specific implementations of it, or to restrict access to members of a certain type.</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">By using interfaces, you can, for example, include behavior from multiple sources in a class. That capability is important in C# because the language doesn’t support multiple inheritance of classes. In addition, you must use an interface if you want to simulate inheritance for structs, because they can’t inherit from another struct or class.</span><br />
<h3 style="text-align: left;">
<span style="color: #3d85c6; font-family: inherit;">Implicit and explicit interface implementations</span></h3>
<span style="font-family: inherit;">Interfaces can be implemented implicitly or explicitly. Let me explain how these two implementations differ. Consider an interface called IBusinessLogic.</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">public interface IBusinessLogic</span><br />
<span style="font-family: inherit;">{</span><br />
<span style="font-family: inherit;">void Initialize();</span><br />
<span style="font-family: inherit;">}</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">The following class named BusinessLogic implements the IBusinessLogic interface.</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">public class BusinessLogic : IBusinessLogic</span><br />
<span style="font-family: inherit;">{</span><br />
<span style="font-family: inherit;">public void Initialize()</span><br />
<span style="font-family: inherit;">{</span><br />
<span style="font-family: inherit;">//Some code</span><br />
<span style="font-family: inherit;">}</span><br />
<span style="font-family: inherit;">}</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">You can create an instance of the BusinessLogic class explicitly and then call the Initialize() method as shown below.</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">IBusinessLogic businessLogic = new BusinessLogic();</span><br />
<span style="font-family: inherit;">businessLogic.Initialize();</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">The following code snippet illustrates how you can implement the IBusinessLogic interface implicitly.</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">public class BusinessLogic : IBusinessLogic</span><br />
<span style="font-family: inherit;">{</span><br />
<span style="font-family: inherit;">void IBusinessLogic.Initialize()</span><br />
<span style="font-family: inherit;">{</span><br />
<span style="font-family: inherit;">}</span><br />
<span style="font-family: inherit;">}</span><br />
<span style="font-family: inherit;"><br /></span>
<span style="font-family: inherit;">You can now invoke the Initialize() method the same way using a reference to the IBusinessLogic interface. The difference in the two approaches is that when you implement the interface explicitly in your class, you are constrained to invoking a method of your interface using a reference to the interface only. Therefore, the following code snippet would not work, i.e. would not compile.</span><br />
<br />
<br />
<br />
<section class="bodee" style="clear: left; margin-bottom: 20px; position: relative;">
</section></div>
Bhanu Prataphttp://www.blogger.com/profile/17529341083467033319noreply@blogger.com0tag:blogger.com,1999:blog-45468370605306946.post-91057190593629378832019-01-25T23:17:00.001-08:002020-01-14T04:50:47.170-08:00pass parameters in RedirectToAction from one controller to another<div dir="ltr" style="text-align: left;" trbidi="on">
in First controller action method<br />
<br />
var res = new ResponseModel(); // with many properties<br />
<br />
//var routeValues = new RouteValueDictionary {<br />
// { "res", JsonConvert.SerializeObject(res) }<br />
//};<br />
<br />
//return RedirectToAction("Index", "Customer", routeValues);<br />
<br />
In Customer controller index method<br />
<br />
public ActionResult Index(string res)<br />
{<br />
if (!string.IsNullOrEmpty(res))<br />
{<br />
var paymentRes = JsonConvert.DeserializeObject<ResponseModel>(res);<br />
if (paymentRes.IsSuccessful)<br />
{<br />
ViewBag.paymentMessage = paymentRes.Message;<br />
}<br />
else<br />
{<br />
ViewBag.paymentMessage = paymentRes.ErrorMessage;<br />
}<br />
}<br />
return View();<br />
}<br />
</div>
Bhanu Prataphttp://www.blogger.com/profile/17529341083467033319noreply@blogger.com2tag:blogger.com,1999:blog-45468370605306946.post-49057159625999952132018-08-10T03:34:00.002-07:002020-01-14T04:50:36.516-08:00HTTP 404 Not Found Error with woff or woff2 Font Files<div dir="ltr" style="text-align: left;" trbidi="on">
<div>
<span style="color: #4f4c47; font-family: roboto slab, serif;"> For woff and woff2 fonts we get these below errors. I was getting for my Product <a href="https://www.qfles.com/" target="_blank">QFLES</a></span></div>
<div>
<span style="color: #4f4c47; font-family: roboto slab, serif;"><br /></span></div>
<ul style="background-color: white; box-sizing: border-box; color: #4f4c47; font-family: "roboto slab", serif; font-size: 16px; list-style-image: initial; list-style-position: inside; margin: 0px; outline: 0px !important; padding: 0px 15px 1em;">
<li style="box-sizing: border-box; outline: 0px !important;">HTTP/1.1 404 Not Found</li>
<li style="box-sizing: border-box; outline: 0px !important;">Failed to load resource: the server responded with a status of 404 (Not Found)</li>
</ul>
<div>
<span style="color: #4f4c47; font-family: roboto slab, serif;">even files are present in the directory.</span></div>
<div>
<span style="color: #4f4c47; font-family: roboto slab, serif;"><br /></span></div>
<div>
<h3 style="background-color: white; box-sizing: border-box; color: #4f4c47; font-family: lato, Arial, sans-serif; font-size: 24px; line-height: 1.1em; margin: 1.5em 0px 0.65em; outline: 0px !important;">
Cause</h3>
<div style="background-color: white; box-sizing: border-box; color: #4f4c47; font-family: "roboto slab", serif; font-size: 16px; margin-bottom: 10px; outline: 0px !important;">
On many server configurations, the WOFF or WOFF2 file is not expected. The default configuration for most web servers is to deny a request for any unexpected file type, including this one. This is why you see the HTTP 404 response, even though the file exists.</div>
<div style="background-color: white; box-sizing: border-box; color: #4f4c47; font-family: "roboto slab", serif; font-size: 16px; margin-bottom: 10px; outline: 0px !important;">
<br /></div>
<h3 style="background-color: white; box-sizing: border-box; color: #4f4c47; font-family: lato, Arial, sans-serif; font-size: 24px; line-height: 1.1em; margin: 1.5em 0px 0.65em; outline: 0px !important;">
Fix</h3>
</div>
<div>
<div style="background-color: white; box-sizing: border-box; color: #4f4c47; font-family: "roboto slab", serif; font-size: 16px; margin-bottom: 10px; outline: 0px !important;">
You simply need to update your web.config file to tell the web server (IIS) that this file is okay to serve, and how to serve it. Use your preferred or available method to edit the web.config file, including the built-in Configuration Manager module.</div>
<div style="background-color: white; box-sizing: border-box; color: #4f4c47; font-family: "roboto slab", serif; font-size: 16px; margin-bottom: 10px; outline: 0px !important;">
Find the <system.webServer> section in your web.config, and look to see if it already has a <staticContent> section. If it doesn't create it. Otherwise, you'll be editing it now.</div>
<div style="background-color: white; box-sizing: border-box; color: #4f4c47; font-family: "roboto slab", serif; font-size: 16px; margin-bottom: 10px; outline: 0px !important;">
In the example you see below, the <staticContent> section likely didn't exist already, and it was added to the top of the <system.webServer> section, but before the <modules> section. In your site, you don't need to have this exact placement. Just make sure that the <staticContent> section is found somewhere between the open and close <system.webServer> tags.</div>
<pre class=" language-xml" style="background: linear-gradient(transparent 50%, rgba(69, 142, 209, 0.04) 50%) 0% 0% / 3em 3em content-box content-box rgb(253, 253, 253); border-color: rgb(238, 238, 238) rgb(238, 238, 238) rgb(238, 238, 238) rgb(53, 140, 203); border-image: initial; border-radius: 3px; border-style: solid; border-width: 1px 1px 1px 10px; box-shadow: rgb(53, 140, 203) -1px 0px 0px 0px, rgb(223, 223, 223) 0px 0px 0px 1px; box-sizing: border-box; font-family: Consolas, Monaco, "Andale Mono", "Ubuntu Mono", monospace; font-size: 0.9em; hyphens: none; line-height: 1.5; margin-bottom: 1em; margin-top: 0.5em; outline: 0px !important; overflow: visible; padding: 0px; position: relative; tab-size: 4; word-break: normal; word-wrap: normal;"><code class=" language-xml" style="background: none; border-radius: 0px; box-sizing: border-box; display: block; font-family: Consolas, Monaco, "Andale Mono", "Ubuntu Mono", monospace; font-size: inherit; height: 208.667px; hyphens: none; line-height: 1.5; max-height: inherit; outline: 0px !important; overflow: auto; padding: 0px 1em; tab-size: 4; word-break: normal; word-spacing: normal; word-wrap: normal;"> <system.webServer>
<span class="token tag" style="box-sizing: border-box; color: #c92c2c; outline: 0px !important;"><span class="token tag" style="box-sizing: border-box; outline: 0px !important;"><span class="token punctuation" style="box-sizing: border-box; color: #5f6364; outline: 0px !important;"><</span>staticContent</span><span class="token punctuation" style="box-sizing: border-box; color: #5f6364; outline: 0px !important;">></span></span>
<span class="token tag" style="box-sizing: border-box; color: #c92c2c; outline: 0px !important;"><span class="token tag" style="box-sizing: border-box; outline: 0px !important;"><span class="token punctuation" style="box-sizing: border-box; color: #5f6364; outline: 0px !important;"><</span>clientCache</span> <span class="token attr-name" style="box-sizing: border-box; color: #2f9c0a; outline: 0px !important;">cacheControlCustom</span><span class="token attr-value" style="box-sizing: border-box; color: #1990b8; outline: 0px !important;"><span class="token punctuation" style="box-sizing: border-box; color: #5f6364; outline: 0px !important;">=</span><span class="token punctuation" style="box-sizing: border-box; color: #5f6364; outline: 0px !important;">"</span>public<span class="token punctuation" style="box-sizing: border-box; color: #5f6364; outline: 0px !important;">"</span></span> <span class="token attr-name" style="box-sizing: border-box; color: #2f9c0a; outline: 0px !important;">cacheControlMode</span><span class="token attr-value" style="box-sizing: border-box; color: #1990b8; outline: 0px !important;"><span class="token punctuation" style="box-sizing: border-box; color: #5f6364; outline: 0px !important;">=</span><span class="token punctuation" style="box-sizing: border-box; color: #5f6364; outline: 0px !important;">"</span>UseMaxAge<span class="token punctuation" style="box-sizing: border-box; color: #5f6364; outline: 0px !important;">"</span></span> <span class="token attr-name" style="box-sizing: border-box; color: #2f9c0a; outline: 0px !important;">cacheControlMaxAge</span><span class="token attr-value" style="box-sizing: border-box; color: #1990b8; outline: 0px !important;"><span class="token punctuation" style="box-sizing: border-box; color: #5f6364; outline: 0px !important;">=</span><span class="token punctuation" style="box-sizing: border-box; color: #5f6364; outline: 0px !important;">"</span>365.00:00:00<span class="token punctuation" style="box-sizing: border-box; color: #5f6364; outline: 0px !important;">"</span></span> <span class="token punctuation" style="box-sizing: border-box; color: #5f6364; outline: 0px !important;">/></span></span>
<span class="token tag" style="box-sizing: border-box; color: #c92c2c; outline: 0px !important;"><span class="token tag" style="box-sizing: border-box; outline: 0px !important;"><span class="token punctuation" style="box-sizing: border-box; color: #5f6364; outline: 0px !important;"><</span>remove</span> <span class="token attr-name" style="box-sizing: border-box; color: #2f9c0a; outline: 0px !important;">fileExtension</span><span class="token attr-value" style="box-sizing: border-box; color: #1990b8; outline: 0px !important;"><span class="token punctuation" style="box-sizing: border-box; color: #5f6364; outline: 0px !important;">=</span><span class="token punctuation" style="box-sizing: border-box; color: #5f6364; outline: 0px !important;">"</span>.woff<span class="token punctuation" style="box-sizing: border-box; color: #5f6364; outline: 0px !important;">"</span></span> <span class="token punctuation" style="box-sizing: border-box; color: #5f6364; outline: 0px !important;">/></span></span>
<span class="token tag" style="box-sizing: border-box; color: #c92c2c; outline: 0px !important;"><span class="token tag" style="box-sizing: border-box; outline: 0px !important;"><span class="token punctuation" style="box-sizing: border-box; color: #5f6364; outline: 0px !important;"><</span>remove</span> <span class="token attr-name" style="box-sizing: border-box; color: #2f9c0a; outline: 0px !important;">fileExtension</span><span class="token attr-value" style="box-sizing: border-box; color: #1990b8; outline: 0px !important;"><span class="token punctuation" style="box-sizing: border-box; color: #5f6364; outline: 0px !important;">=</span><span class="token punctuation" style="box-sizing: border-box; color: #5f6364; outline: 0px !important;">"</span>.woff2<span class="token punctuation" style="box-sizing: border-box; color: #5f6364; outline: 0px !important;">"</span></span> <span class="token punctuation" style="box-sizing: border-box; color: #5f6364; outline: 0px !important;">/></span></span>
<span class="token tag" style="box-sizing: border-box; color: #c92c2c; outline: 0px !important;"><span class="token tag" style="box-sizing: border-box; outline: 0px !important;"><span class="token punctuation" style="box-sizing: border-box; color: #5f6364; outline: 0px !important;"><</span>mimeMap</span> <span class="token attr-name" style="box-sizing: border-box; color: #2f9c0a; outline: 0px !important;">fileExtension</span><span class="token attr-value" style="box-sizing: border-box; color: #1990b8; outline: 0px !important;"><span class="token punctuation" style="box-sizing: border-box; color: #5f6364; outline: 0px !important;">=</span><span class="token punctuation" style="box-sizing: border-box; color: #5f6364; outline: 0px !important;">"</span>.woff<span class="token punctuation" style="box-sizing: border-box; color: #5f6364; outline: 0px !important;">"</span></span> <span class="token attr-name" style="box-sizing: border-box; color: #2f9c0a; outline: 0px !important;">mimeType</span><span class="token attr-value" style="box-sizing: border-box; color: #1990b8; outline: 0px !important;"><span class="token punctuation" style="box-sizing: border-box; color: #5f6364; outline: 0px !important;">=</span><span class="token punctuation" style="box-sizing: border-box; color: #5f6364; outline: 0px !important;">"</span>application/x-font-woff<span class="token punctuation" style="box-sizing: border-box; color: #5f6364; outline: 0px !important;">"</span></span> <span class="token punctuation" style="box-sizing: border-box; color: #5f6364; outline: 0px !important;">/></span></span>
<span class="token tag" style="box-sizing: border-box; color: #c92c2c; outline: 0px !important;"><span class="token tag" style="box-sizing: border-box; outline: 0px !important;"><span class="token punctuation" style="box-sizing: border-box; color: #5f6364; outline: 0px !important;"><</span>mimeMap</span> <span class="token attr-name" style="box-sizing: border-box; color: #2f9c0a; outline: 0px !important;">fileExtension</span><span class="token attr-value" style="box-sizing: border-box; color: #1990b8; outline: 0px !important;"><span class="token punctuation" style="box-sizing: border-box; color: #5f6364; outline: 0px !important;">=</span><span class="token punctuation" style="box-sizing: border-box; color: #5f6364; outline: 0px !important;">"</span>.woff2<span class="token punctuation" style="box-sizing: border-box; color: #5f6364; outline: 0px !important;">"</span></span> <span class="token attr-name" style="box-sizing: border-box; color: #2f9c0a; outline: 0px !important;">mimeType</span><span class="token attr-value" style="box-sizing: border-box; color: #1990b8; outline: 0px !important;"><span class="token punctuation" style="box-sizing: border-box; color: #5f6364; outline: 0px !important;">=</span><span class="token punctuation" style="box-sizing: border-box; color: #5f6364; outline: 0px !important;">"</span>application/font-woff2<span class="token punctuation" style="box-sizing: border-box; color: #5f6364; outline: 0px !important;">"</span></span> <span class="token punctuation" style="box-sizing: border-box; color: #5f6364; outline: 0px !important;">/></span></span>
<span class="token tag" style="box-sizing: border-box; color: #c92c2c; outline: 0px !important;"><span class="token tag" style="box-sizing: border-box; outline: 0px !important;"><span class="token punctuation" style="box-sizing: border-box; color: #5f6364; outline: 0px !important;"></</span>staticContent</span><span class="token punctuation" style="box-sizing: border-box; color: #5f6364; outline: 0px !important;">></span></span>
<span class="token tag" style="box-sizing: border-box; color: #c92c2c; outline: 0px !important;"><span class="token tag" style="box-sizing: border-box; outline: 0px !important;"><span class="token punctuation" style="box-sizing: border-box; color: #5f6364; outline: 0px !important;"><</span>modules</span><span class="token punctuation" style="box-sizing: border-box; color: #5f6364; outline: 0px !important;">></span></span></code></pre>
<div style="background-color: white; box-sizing: border-box; color: #4f4c47; font-family: "roboto slab", serif; font-size: 16px; margin-bottom: 10px; outline: 0px !important;">
The important parts of the example above are the <remove> and <mimeMap> tags for your font files. If you already have a <staticContent> section added, make sure those lines are added in your configuration file.</div>
</div>
</div>
Bhanu Prataphttp://www.blogger.com/profile/17529341083467033319noreply@blogger.com5tag:blogger.com,1999:blog-45468370605306946.post-66139785376665960752018-08-06T01:43:00.005-07:002021-02-14T23:46:48.754-08:00QFLES - Interview Questions, Tech Blogs and solutions<div dir="ltr" style="text-align: left;" trbidi="on">
Hello Guys,<br />
<br />
We are providing a Platform (<a href="https://www.qfles.com/" target="_blank">QFLES</a>) that provide interview questions for IT Experienced Professionals and freshers to clear job interviews.<br />
<br />
Thanks<br />
<a href="https://www.qfles.com/" target="_blank">QFLES </a>Team</div>
Bhanu Prataphttp://www.blogger.com/profile/17529341083467033319noreply@blogger.com2tag:blogger.com,1999:blog-45468370605306946.post-22804384711831715522018-01-31T23:27:00.002-08:002020-01-14T04:50:21.773-08:00Choose between an interface and abstract class<div dir="ltr" style="text-align: left;" trbidi="on">
<br />Choose between an interface and abstract class?<br /> I have been asking this question in interviews for some years now. I get to hear a lot of interesting answers from folks. All these answers have helped me make a great addition to my knowledge. If you google this topic you would find a lot of interesting articles. <a href="https://msdn.microsoft.com/en-gb/library/ms229013(v=vs.100).aspx">This article on MSDN</a> offers a good discussion on the topic. To summarise the recommendations from there and few other articles I have come across, here are the reasons in favour of abstract classes<br /> <br />Use abstract class if you have a default implementation of some behaviour that child classes do not have to implement <br />Prefer abstract classes if your contract has a possibility of changing over time. So if you are using an abstract class and need to add a new method to your abstract class, you can happily add that without breaking any code using that class. The same is not true for interfaces. <br /><br /><br />And these are the reasons in favour of interfaces <br />Since multiple inheritance is not supported in C#, you cannot inherit your class from two abstract classes. An interface is your only option in such situations. <br />If there is no default or common behaviour among all the classes that are inheriting from abstract class then interface may be a better choice. <br /><br /><br />I personally do not believe that these give a complete picture.<br /> What is wrong with this reasoning?<br /><br /><br />First, above recommendations are mostly around syntax a particular language supports and not around semantics (e.g. use interfaces if you need multiple inheritance). By semantics I mean, what definition of an interface fundamentally differentiates it from an abstract class?<br /><br /><br />Second, I feel the above criteria are too futuristic. By that I mean, they all depend on you knowing how your design is going to take shape in future. At times, I may have some idea of how my design is going to take shape in future but most of the times I do not have enough clarity to know in advance <br />Whether I am going to need to inherit from multiple abstract classes or not <br />Whether there is going to be a default implementation of some contract or not <br />Whether I would add a new method to a contract or define a new contract entirely in order to implement a change <br /><br /><br />So, if you do not know which way your software is going to go, there is no way you can base your decision of interface or abstract class on these reasons.<br /> So how do we decide?<br /><br /><br />Lately, I have been using below heuristic to determine when to use interfaces/abstract classes and I feel quite excited about it as it works most of the time.<br /><br /><br />Interfaces represent capabilities and abstract classes represent type<br /><br /><br />in other words<br /><br /><br />Implementing interfaces represents can-do relationship and inheriting from (abstract) class represents an is-a relationship<br /><br /><br />To elaborate this point, let’s consider following two classes<br /><br /><br />public class Camera
<br /><br /><br />{
<br /><br /><br /> public void Shoot()
<br /><br /><br /> {
<br /><br /><br /> //Take a picture here
<br /><br /><br /> }
<br /><br /><br />}
<br /><br /><br />public class Gun
<br /><br /><br />{
<br /><br /><br /> public void Shoot()
<br /><br /><br /> {
<br /><br /><br /> //Hit the target
<br /><br /><br /> }
<br /><br /><br />}
<br /><br /><br /><br />Both Camera and Gun can Shoot, that is their capability. But they are both not the same type of things, they are completely different. So an interface like below would make more sense here<br /><br /><br />public interface IShootable
<br /><br /><br />{
<br /><br /><br /> void Shoot();
<br /><br /><br />}
<br /><br /><br />public class Camera : IShootable
<br /><br /><br />{
<br /><br /><br /> public void Shoot()
<br /><br /><br /> {
<br /><br /><br /> //Take a picture here
<br /><br /><br /> }
<br /><br /><br />}
<br /><br /><br />public class Gun : IShootable
<br /><br /><br />{
<br /><br /><br /> public void Shoot()
<br /><br /><br /> {
<br /><br /><br /> //Hit the target
<br /><br /><br /> }
<br /><br /><br />}
<br /><br /><br /><br />If you have learned OO programming the same way I did, then you would remember the classic shape example as below<br /><br /><br />public abstract class Shape
<br /><br /><br />{
<br /><br /><br /> void Draw();
<br /><br /><br />}
<br /><br /><br />public class Rectangle : Shape
<br /><br /><br />{
<br /><br /><br /> public void Draw()
<br /><br /><br /> {
<br /><br /><br /> //Draw a rectangle here
<br /><br /><br /> }
<br /><br /><br />}
<br /><br /><br />public class Circle : Shape
<br /><br /><br />{
<br /><br /><br /> public void Draw()
<br /><br /><br /> {
<br /><br /><br /> //Draw a circle here
<br /><br /><br /> }
<br /><br /><br />}
<br /><br /><br /><br />Rectangle and Circle inheriting from Shape makes perfect sense here because Rectangle/Circle are a type of Shape.</div>
Bhanu Prataphttp://www.blogger.com/profile/17529341083467033319noreply@blogger.com0tag:blogger.com,1999:blog-45468370605306946.post-31047925011314596432017-01-30T20:00:00.003-08:002017-01-30T20:01:07.945-08:00Can you remove default View Engine in ASP.NET MVC? How?<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: Verdana, sans-serif; font-size: 13.3333px;">We can customize view engines in ASP.NET MVC application.</span><br />
<span style="font-family: Verdana, sans-serif; font-size: 13.3333px;"><br /></span>
<span style="font-family: Verdana, sans-serif; font-size: 13.3333px;">If you are not using any view engine like ASPX View Engine, better you remove it to improve the performance, it is one of the MVC performance tuning tips from the bunch.</span><br />
<br />Removing the Web Form view engine is easy in MVC. We can remove all the view engines and add only Razor view engine by using Application_Start event of Global.asax.cs file like below.<div>
<br /></div>
<div>
<ol class="linenums" style="box-sizing: border-box; color: #9d9d9d; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 13px; font-weight: bold; list-style-type: decimal-leading-zero; margin: 0px; padding: 0px; text-align: justify; white-space: pre-wrap;">
<li class="L0" style="background-color: #eeeeee; box-sizing: border-box; color: #3c3c3c; list-style-type: none; margin: 0px; padding: 0px;"><span class="pln" style="box-sizing: border-box; color: #393124; margin: 0px; padding: 0px; vertical-align: top;"> </span><span class="kwd" style="box-sizing: border-box; color: #ff0080; margin: 0px; padding: 0px; vertical-align: top;">protected</span><span class="pln" style="box-sizing: border-box; color: #393124; margin: 0px; padding: 0px; vertical-align: top;"> </span><span class="kwd" style="box-sizing: border-box; color: #ff0080; margin: 0px; padding: 0px; vertical-align: top;">void</span><span class="pln" style="box-sizing: border-box; color: #393124; margin: 0px; padding: 0px; vertical-align: top;"> </span><span class="typ" style="box-sizing: border-box; color: #5353a6; margin: 0px; padding: 0px; vertical-align: top;">Application_Start</span><span class="pun" style="box-sizing: border-box; color: #393124; margin: 0px; padding: 0px; vertical-align: top;">()</span><span class="pln" style="box-sizing: border-box; color: #393124; margin: 0px; padding: 0px; vertical-align: top;"> </span></li>
<li class="L1" style="background: rgb(238, 238, 238); box-sizing: border-box; color: #3c3c3c; list-style-type: none; margin: 0px; padding: 0px;"><span class="pun" style="box-sizing: border-box; color: #393124; margin: 0px; padding: 0px; vertical-align: top;">{</span><span class="pln" style="box-sizing: border-box; color: #393124; margin: 0px; padding: 0px; vertical-align: top;"> </span></li>
<li class="L2" style="background-color: #eeeeee; box-sizing: border-box; color: #3c3c3c; list-style-type: none; margin: 0px; padding: 0px;"><span class="com" style="box-sizing: border-box; color: #4e9b00; font-style: italic; margin: 0px; padding: 0px; vertical-align: top;">//Remove All Engine</span></li>
<li class="L3" style="background: rgb(238, 238, 238); box-sizing: border-box; color: #3c3c3c; list-style-type: none; margin: 0px; padding: 0px;"><span class="pln" style="box-sizing: border-box; color: #393124; margin: 0px; padding: 0px; vertical-align: top;"> </span><span class="typ" style="box-sizing: border-box; color: #5353a6; margin: 0px; padding: 0px; vertical-align: top;">ViewEngines</span><span class="pun" style="box-sizing: border-box; color: #393124; margin: 0px; padding: 0px; vertical-align: top;">.</span><span class="typ" style="box-sizing: border-box; color: #5353a6; margin: 0px; padding: 0px; vertical-align: top;">Engines</span><span class="pun" style="box-sizing: border-box; color: #393124; margin: 0px; padding: 0px; vertical-align: top;">.</span><span class="typ" style="box-sizing: border-box; color: #5353a6; margin: 0px; padding: 0px; vertical-align: top;">Clear</span><span class="pun" style="box-sizing: border-box; color: #393124; margin: 0px; padding: 0px; vertical-align: top;">();</span></li>
<li class="L4" style="background-color: #eeeeee; box-sizing: border-box; color: #3c3c3c; margin: 0px; padding: 0px;"><span class="pln" style="box-sizing: border-box; color: #393124; margin: 0px; padding: 0px; vertical-align: top;"> </span><span class="com" style="box-sizing: border-box; color: #4e9b00; font-style: italic; margin: 0px; padding: 0px; vertical-align: top;">//Add Razor Engine</span></li>
<li class="L5" style="background: rgb(238, 238, 238); box-sizing: border-box; color: #3c3c3c; list-style-type: none; margin: 0px; padding: 0px;"><span class="pln" style="box-sizing: border-box; color: #393124; margin: 0px; padding: 0px; vertical-align: top;"> </span><span class="typ" style="box-sizing: border-box; color: #5353a6; margin: 0px; padding: 0px; vertical-align: top;">ViewEngines</span><span class="pun" style="box-sizing: border-box; color: #393124; margin: 0px; padding: 0px; vertical-align: top;">.</span><span class="typ" style="box-sizing: border-box; color: #5353a6; margin: 0px; padding: 0px; vertical-align: top;">Engines</span><span class="pun" style="box-sizing: border-box; color: #393124; margin: 0px; padding: 0px; vertical-align: top;">.</span><span class="typ" style="box-sizing: border-box; color: #5353a6; margin: 0px; padding: 0px; vertical-align: top;">Add</span><span class="pun" style="box-sizing: border-box; color: #393124; margin: 0px; padding: 0px; vertical-align: top;">(</span><span class="kwd" style="box-sizing: border-box; color: #ff0080; margin: 0px; padding: 0px; vertical-align: top;">new</span><span class="pln" style="box-sizing: border-box; color: #393124; margin: 0px; padding: 0px; vertical-align: top;"> </span><span class="typ" style="box-sizing: border-box; color: #5353a6; margin: 0px; padding: 0px; vertical-align: top;">RazorViewEngine</span><span class="pun" style="box-sizing: border-box; color: #393124; margin: 0px; padding: 0px; vertical-align: top;">());</span></li>
<li class="L6" style="background-color: #eeeeee; box-sizing: border-box; color: #3c3c3c; list-style-type: none; margin: 0px; padding: 0px;"><span class="pln" style="box-sizing: border-box; color: #393124; margin: 0px; padding: 0px; vertical-align: top;"> </span><span class="pun" style="box-sizing: border-box; color: #393124; margin: 0px; padding: 0px; vertical-align: top;">...</span><span class="pln" style="box-sizing: border-box; color: #393124; margin: 0px; padding: 0px; vertical-align: top;"> </span></li>
<li class="L7" style="background: rgb(238, 238, 238); box-sizing: border-box; color: #3c3c3c; list-style-type: none; margin: 0px; padding: 0px;"><span class="pun" style="box-sizing: border-box; color: #393124; margin: 0px; padding: 0px; vertical-align: top;">}</span><span class="pln" style="box-sizing: border-box; color: #393124; margin: 0px; padding: 0px; vertical-align: top;"> </span></li>
</ol>
</div>
<div>
<br /></div>
<div>
NOTE:</div>
<div>
<ol class="notelist" style="background-color: #f3f3f3; border-left: 2px solid rgb(225, 114, 24); box-sizing: border-box; color: #161616; font-family: "Segoe UI", Arial, sans-serif; font-size: 14px; margin: 0px 0px 10px 2px; padding: 5px 5px 5px 35px;">
<li style="box-sizing: border-box; font-size: 20px; margin: 0px; padding: 1px 0px 1px 2px; text-align: justify;"><div style="box-sizing: border-box; font-size: 14px; padding: 0px 5px 0px 0px; vertical-align: top;">
After removing Web Form engine, Razor View Engine will be almost twice as fast with the Web Form engine.</div>
</li>
<li style="box-sizing: border-box; font-size: 20px; margin: 0px; padding: 1px 0px 1px 2px; text-align: justify;"><div style="box-sizing: border-box; font-size: 14px; padding: 0px 5px 0px 0px; vertical-align: top;">
Use this when you are sure that you will use only Razor views. It will be helpful to you.</div>
</li>
<li style="box-sizing: border-box; font-size: 20px; margin: 0px; padding: 1px 0px 1px 2px; text-align: justify;"><div style="box-sizing: border-box; font-size: 14px; padding: 0px 5px 0px 0px; vertical-align: top;">
If you are using both type of views (ASPX & Razor), don't implement this.</div>
</li>
</ol>
</div>
<div>
You can also customize the view engines. for more click </div>
<div>
<br /></div>
<div>
<a href="http://www.itorian.com/2013/03/removing-or-customizing-view-engines-in.html" target="_blank">Customizing View Engines in MVC</a></div>
</div>
Bhanu Prataphttp://www.blogger.com/profile/17529341083467033319noreply@blogger.com4tag:blogger.com,1999:blog-45468370605306946.post-31277550124679791352016-12-27T04:53:00.002-08:002016-12-27T05:23:11.834-08:00Different methods used to render the views and Partial views in ASP.Net MVC<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="MsoNormal">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj56qAMVgRwTchWRfWQy2NGLTGzI7IyQYSVpkZ4wv7cU8_H9vHF7Rk15vJSRpSB5emEAD7su_01dgMUA_Vs6AR6P6YleI2TjK1k1cdiItFtALbXeWJvgkdEFqLsWNFEOyGsEYTD3LuSmw/s1600/view.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="226" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj56qAMVgRwTchWRfWQy2NGLTGzI7IyQYSVpkZ4wv7cU8_H9vHF7Rk15vJSRpSB5emEAD7su_01dgMUA_Vs6AR6P6YleI2TjK1k1cdiItFtALbXeWJvgkdEFqLsWNFEOyGsEYTD3LuSmw/s400/view.png" width="400" /></a></div>
<b><br /></b>
<b>1. RenderPartial - </b>RenderPartial method is
useful when the displaying data in the partial view is already in the
corresponding view model.</div>
<div class="MsoNormal">
<b>@{Html.RenderPartial("_partialView");}</b></div>
<div class="MsoNormal">
This method returns void. And simple to use and no need to
create any action.<o:p></o:p></div>
<div class="MsoNormal">
This method is faster than Partial method since its result
is directly written to the response stream which makes it fast.<o:p></o:p></div>
<div class="MsoNormal">
<br />
<b>2. Partial - </b>Like RenderPartial method, Partial method is also useful
when the displaying data in the partial view is already in the corresponding
view model.</div>
<div class="MsoNormal">
<b>@Html.Partial("_partialView")</b></div>
<div class="MsoNormal">
Simple to use and no need to create any action.This method
result can be stored in a variable, since it returns string type value.<o:p></o:p></div>
<div class="MsoNormal">
Renders the partial view as an HTML-encoded string.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>3. RenderAction - </b>RenderAction method is useful
when the displaying data in the partial view is independent from corresponding
view model.</div>
<div class="MsoNormal">
<b>@{Html.RenderAction("Action_Name","Controller-Name");}</b></div>
<div class="MsoNormal">
For this method, we need to create a child action for the
rendering the partial view.<o:p></o:p></div>
<div class="MsoNormal">
This method is the best choice when you want to cache a
partial view.<o:p></o:p></div>
<div class="MsoNormal">
This method is faster than Action method since its result is
directly written to the HTTP response stream which makes it fast.<o:p></o:p></div>
<div class="MsoNormal">
<br />
<b>4. Action - </b>Action method is useful when the displaying data in the
partial view is independent from corresponding view model.</div>
<div class="MsoNormal">
<b>@Html.Action("Action_Name","Controller-Name");</b></div>
<div class="MsoNormal">
For this method, we need to create a child action for the
rendering the partial view.This method result can be stored in a variable,
since it returns string type value.<o:p></o:p></div>
<div class="MsoNormal">
Renders the partial view as an HtmlString .<o:p></o:p></div>
<br />
<div class="MsoNormal">
This method is also the best choice when you want to cache a
partial view.<o:p></o:p><br />
<br />
<b>5. Direct return from Controller </b><br />
<b><br /></b>
<br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">public</span><span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">ActionResult</span><span style="font-family: "consolas"; font-size: 9.5pt;"> Action(</span><span style="font-family: "consolas"; font-size: 9.5pt;">)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">
{</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">return</span><span style="font-family: "consolas"; font-size: 9.5pt;"> PartialView(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"PartialViewName"</span><span style="font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;">OR</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: "consolas"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "consolas"; font-size: 9.5pt;">return</span><span style="font-family: "consolas"; font-size: 9.5pt;"> View(</span><span style="color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"ViewName"</span><span style="font-family: "consolas"; font-size: 9.5pt;">);</span></div>
<br />
<div class="MsoNormal">
<span style="font-family: "consolas"; font-size: 9.5pt; line-height: 115%;"> }</span><o:p></o:p><br />
<span style="font-family: "consolas"; font-size: 9.5pt; line-height: 115%;"><br /></span>
<span style="font-family: "consolas"; line-height: 115%;">For More check <a href="https://docs.microsoft.com/en-us/aspnet/core/mvc/views/overview" target="_blank">Views</a> <a href="https://docs.microsoft.com/en-us/aspnet/core/mvc/views/partial" target="_blank">Partial Views</a></span></div>
</div>
</div>
Bhanu Prataphttp://www.blogger.com/profile/17529341083467033319noreply@blogger.com1tag:blogger.com,1999:blog-45468370605306946.post-29263855677941221782016-12-24T20:54:00.002-08:002016-12-24T20:55:14.053-08:00What is Caching? Explain Output Cache.<div dir="ltr" style="text-align: left;" trbidi="on">
<pre style="overflow: auto; padding: 5px; word-wrap: normal;"><span style="color: #2a2a2a; font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif;"><span style="white-space: normal;">Caching is a most important aspect of high-performance web application. Caching provides a way of storing frequently accessed data and reusing that data. Practically, this is an effective way for improving web application’s performance.</span></span></pre>
<pre style="overflow: auto; padding: 5px; word-wrap: normal;"><span style="color: #2a2a2a; font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif;"><span style="white-space: normal;">Output Cache can dramatically improve the performance of an ASP.NET MVC application. The output cache enables you to cache the content returned by a controller action. That way, the same content does not need to be generated each and every time the same controller action is invoked.</span></span></pre>
<pre style="overflow: auto; padding: 5px; word-wrap: normal;"><span style="color: #2a2a2a; font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif;"><span style="white-space: normal;">The OutputCache filter allow you to cache the data that is output of an action method. By default, this attribute filter cache the data till 60 seconds. After 60 sec, if a call was made to this action then ASP.NET MVC will cache the output again.</span></span></pre>
<pre style="overflow: auto; padding: 5px; word-wrap: normal;"><span style="font-size: large;">Enabling Output Caching</span></pre>
<pre style="overflow: auto; padding: 5px; word-wrap: normal;"><div style="border: 0px; color: #505050; font-family: "segoe ui", tahoma, arial, helvetica, sans-serif; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline; white-space: normal;">
You enable output caching by adding an [OutputCache] attribute to either an individual controller action or an entire controller class as below.</div>
<pre class="c#:nogutter:nocontrols prettyprint prettyprinted" name="code" style="background: rgb(239, 239, 239); border: 1px solid rgb(210, 210, 210); color: #505050; font-family: consolas, monospace; font-size: 13.008px; margin-bottom: 30px; margin-top: 18px; outline: 0px; overflow: auto; padding: 5px; vertical-align: baseline;"><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">[</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">OutputCache</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Duration</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="lit" style="background: transparent; border: 0px; color: red; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">10</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">VaryByParam</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="str" style="background: transparent; border: 0px; color: #a31515; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">"none"</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">)]</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="kwd" style="background: transparent; border: 0px; color: blue; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">public</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">ActionResult</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Index</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">()</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">{</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="kwd" style="background: transparent; border: 0px; color: blue; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">return</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">View</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">();</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">}</span></pre>
<div style="border: 0px; color: #505050; font-family: "segoe ui", tahoma, arial, helvetica, sans-serif; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline; white-space: normal;">
<b>Output Caching Location</b><br />
By default, content is cached in three locations: the web server, any proxy servers, and the user's browser. You can control the content's cached location by changing the location parameter of the OutputCache attribute to any of the following values: </div>
<ul style="background-color: white; border: 0px; color: #111111; font-family: "segoe ui", arial, sans-serif; font-size: 14px; margin: 10px 0px; padding: 0px 0px 0px 40px; white-space: normal;">
<li style="border: 0px; line-height: normal; margin: 0px; padding: 0px;"><code style="border: 0px; color: #990000; font-family: consolas, "courier new", courier, mono; font-size: 11pt; font-stretch: normal; line-height: normal; margin: 0px; padding: 0px;">Any</code> (Default): Content is cached in three locations: the web server, any proxy servers, and the web browser.</li>
<li style="border: 0px; line-height: normal; margin: 0px; padding: 0px;"><code style="border: 0px; color: #990000; font-family: consolas, "courier new", courier, mono; font-size: 11pt; font-stretch: normal; line-height: normal; margin: 0px; padding: 0px;">Client</code>: Content is cached on the web browser.</li>
<li style="border: 0px; line-height: normal; margin: 0px; padding: 0px;"><code style="border: 0px; color: #990000; font-family: consolas, "courier new", courier, mono; font-size: 11pt; font-stretch: normal; line-height: normal; margin: 0px; padding: 0px;">Server</code>: Content is cached on the web server.</li>
<li style="border: 0px; line-height: normal; margin: 0px; padding: 0px;"><code style="border: 0px; color: #990000; font-family: consolas, "courier new", courier, mono; font-size: 11pt; font-stretch: normal; line-height: normal; margin: 0px; padding: 0px;">ServerAndClient</code>: Content is cached on the web server and and the web browser.</li>
<li style="border: 0px; line-height: normal; margin: 0px; padding: 0px;"><code style="border: 0px; color: #990000; font-family: consolas, "courier new", courier, mono; font-size: 11pt; font-stretch: normal; line-height: normal; margin: 0px; padding: 0px;">None</code>: Content is not cached anywhere.</li>
</ul>
<pre class="c#:nogutter:nocontrols prettyprint prettyprinted" name="code" style="background: rgb(239, 239, 239); border: 1px solid rgb(210, 210, 210); color: #505050; font-family: consolas, monospace; font-size: 13.008px; margin-bottom: 30px; margin-top: 18px; outline: 0px; overflow: auto; padding: 5px; vertical-align: baseline;"><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">[</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">OutputCache</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Duration</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="lit" style="background: transparent; border: 0px; color: red; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">3600</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">VaryByParam</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="str" style="background: transparent; border: 0px; color: #a31515; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">"none"</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Location</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">OutputCacheLocation</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Client</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">NoStore</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="kwd" style="background: transparent; border: 0px; color: blue; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">true</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">)]</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="kwd" style="background: transparent; border: 0px; color: blue; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">public</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="background: transparent; border: 0px; color: blue; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">string</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">GetName</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">()</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">{</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="kwd" style="background: transparent; border: 0px; color: blue; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">return</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="str" style="background: transparent; border: 0px; color: #a31515; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">"Hi "</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">+</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">User</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Identity</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Name</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">;</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">}</span></pre>
<div style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;">
<span style="color: #505050; font-family: "segoe ui" , "tahoma" , "arial" , "helvetica" , sans-serif;"><span style="white-space: normal;"><b>Varying the Output Cache</b></span></span><br />
<span style="color: #505050; font-family: "segoe ui" , "tahoma" , "arial" , "helvetica" , sans-serif; white-space: normal;">In some situations, you might want different cached versions of the very same content, So for that we use "</span><span style="color: #2b91af; font-family: "consolas" , monospace;">VaryByParam</span><span style="color: #505050; font-family: "segoe ui" , "tahoma" , "arial" , "helvetica" , sans-serif; white-space: normal;">" as in above code.</span><br />
<span style="background-color: white; color: #111111; font-family: "segoe ui" , "arial" , sans-serif; white-space: normal;">In case we have multiple action methods across controllers needing the same caching behavior, we can put this caching values in the </span><code style="background-color: white; border: 0px; color: #990000; font-family: consolas, "courier new", courier, mono; font-stretch: normal; line-height: normal; margin: 0px; padding: 0px; white-space: normal;">web.config</code><span style="background-color: white; color: #111111; font-family: "segoe ui" , "arial" , sans-serif; white-space: normal;"> and create a </span><code style="background-color: white; border: 0px; color: #990000; font-family: consolas, "courier new", courier, mono; font-stretch: normal; line-height: normal; margin: 0px; padding: 0px; white-space: normal;">cacheprofile</code><span style="background-color: white; color: #111111; font-family: "segoe ui" , "arial" , sans-serif; white-space: normal;"> for it.</span></div>
<pre class="notranslate" id="pre329324" lang="xml" style="background-color: #fbedbb; border: 1px solid rgb(251, 237, 187); font-family: consolas, "courier new", courier, mono; font-size: 9pt; font-stretch: normal; line-height: normal; overflow: auto; padding: 6px; white-space: pre-wrap; word-wrap: break-word;"><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;"><</span><span class="code-leadattribute" style="border: 0px; color: maroon; margin: 0px; padding: 0px;">caching</span><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">></span>
<span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;"><</span><span class="code-leadattribute" style="border: 0px; color: maroon; margin: 0px; padding: 0px;">outputCacheSettings</span><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">></span>
<span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;"><</span><span class="code-leadattribute" style="border: 0px; color: maroon; margin: 0px; padding: 0px;">outputCacheProfiles</span><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">></span>
<span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;"><</span><span class="code-leadattribute" style="border: 0px; color: maroon; margin: 0px; padding: 0px;">add</span> <span class="code-attribute" style="border: 0px; color: red; margin: 0px; padding: 0px;">name</span><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">="</span><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">MyCacheProfile"</span>
<span class="code-attribute" style="border: 0px; color: red; margin: 0px; padding: 0px;">duration</span><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">="</span><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">30"</span>
<span class="code-attribute" style="border: 0px; color: red; margin: 0px; padding: 0px;">varyByParam</span><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">="</span><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">id"</span>
<span class="code-attribute" style="border: 0px; color: red; margin: 0px; padding: 0px;">location</span><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">="</span><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">Any"</span> <span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">/</span><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">></span>
<span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;"><</span><span class="code-leadattribute" style="border: 0px; color: maroon; margin: 0px; padding: 0px;">/outputCacheProfiles</span><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">></span>
<span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;"><</span><span class="code-leadattribute" style="border: 0px; color: maroon; margin: 0px; padding: 0px;">/outputCacheSettings</span><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">></span>
<span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;"><</span><span class="code-leadattribute" style="border: 0px; color: maroon; margin: 0px; padding: 0px;">/caching</span><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">></span></pre>
<div>
<span style="background-color: white; color: #111111; font-family: "segoe ui" , "arial" , sans-serif; font-size: 14px; white-space: normal;">And to use these values in the action methods we just need to specify the </span><code style="background-color: white; border: 0px; color: #990000; font-family: consolas, "courier new", courier, mono; font-size: 11pt; font-stretch: normal; line-height: normal; margin: 0px; padding: 0px; white-space: normal;">CacheProfile</code><span style="background-color: white; color: #111111; font-family: "segoe ui" , "arial" , sans-serif; font-size: 14px; white-space: normal;"> name in the action method.</span></div>
<div>
<pre class="notranslate" id="pre402527" lang="cs" style="background-color: #fbedbb; border: 1px solid rgb(251, 237, 187); font-family: consolas, "courier new", courier, mono; font-size: 9pt; font-stretch: normal; line-height: normal; overflow: auto; padding: 6px; white-space: pre-wrap; word-wrap: break-word;">[OutputCache(CacheProfile = <span class="code-string" style="border: 0px; color: purple; margin: 0px; padding: 0px;">"</span><span class="code-string" style="border: 0px; color: purple; margin: 0px; padding: 0px;">MyCacheProfile"</span>)]
<span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">public</span> ActionResult Dummy()
{
<span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">return</span> View();
}</pre>
</div>
<span style="font-size: large;">For more <a href="https://www.asp.net/mvc/overview/older-versions-1/controllers-and-routing/improving-performance-with-output-caching-cs">Cache MSDN</a> <a href="http://www.codeproject.com/Articles/757201/A-Beginners-Tutorial-for-Understanding-and-Imple">Cache</a></span></pre>
</div>
Bhanu Prataphttp://www.blogger.com/profile/17529341083467033319noreply@blogger.com0tag:blogger.com,1999:blog-45468370605306946.post-45593198148463457572016-12-19T19:48:00.003-08:002016-12-23T02:08:31.383-08:00Different techniques to handle Exceptions in ASP.NET MVC<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjaPmrhTy6bqBhjA4ICO2XI6lVZiKoKu7lYx1SpPXbwSQzR_QDKaZoR_xbZmvOMvVobJROvCRKSm5h0qMD3VrpZwqxTXHEeSHZd6tNk6V9W8xjBePg-xagf6neDjxRv2wGJceQKyW1KmQ/s1600/error.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="180" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjaPmrhTy6bqBhjA4ICO2XI6lVZiKoKu7lYx1SpPXbwSQzR_QDKaZoR_xbZmvOMvVobJROvCRKSm5h0qMD3VrpZwqxTXHEeSHZd6tNk6V9W8xjBePg-xagf6neDjxRv2wGJceQKyW1KmQ/s320/error.jpg" width="320" /></a></div>
<br />
There are many ways to handle exceptions. We will discuss here some of them.<br />
<br />
<b>1. Try - Catch Block</b><br />
<b><br /></b>
<br />
<pre class="notranslate" id="pre405065" lang="”html”" style="background-color: #fbedbb; border: 1px solid rgb(251, 237, 187); font-family: Consolas, "Courier New", Courier, mono; font-size: 9pt; font-stretch: normal; line-height: normal; overflow: auto; padding: 6px; tab-size: 4; white-space: pre-wrap; word-break: break-word; word-wrap: break-word;"><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">public</span> ActionResult SomeError()
{
<span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">try</span>
{}
<span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">catch</span>(Exception ex)
{<span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">return</span> View(<span class="code-string" style="border: 0px; color: purple; margin: 0px; padding: 0px;">"</span><span class="code-string" style="border: 0px; color: purple; margin: 0px; padding: 0px;">Error"</span>);}
}</pre>
<b><br /></b>
<b>2. By Overriding 'OnException' method.</b><br />
<b><br /></b>
In this we override the 'OnException' method of controller and specify error page.<br />
<pre class="notranslate" id="pre997133" lang="”html”" style="background-color: #fbedbb; border: 1px solid rgb(251, 237, 187); font-family: Consolas, "Courier New", Courier, mono; font-size: 9pt; font-stretch: normal; line-height: normal; overflow: auto; padding: 6px; tab-size: 4; white-space: pre-wrap; word-break: break-word; word-wrap: break-word;"><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">public</span> <span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">class</span> HomeController : Controller
{
<span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">protected</span> <span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">override</span> <span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">void</span> OnException(ExceptionContext filterContext)
{
Exception ex = filterContext.Exception;
filterContext.ExceptionHandled = <span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">true</span>;
<span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">var</span> model = <span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">new</span> HandleErrorInfo(filterContext.Exception, <span class="code-string" style="border: 0px; color: purple; margin: 0px; padding: 0px;">"</span><span class="code-string" style="border: 0px; color: purple; margin: 0px; padding: 0px;">Controller"</span>,<span class="code-string" style="border: 0px; color: purple; margin: 0px; padding: 0px;">"</span><span class="code-string" style="border: 0px; color: purple; margin: 0px; padding: 0px;">Action"</span>);
filterContext.Result = <span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">new</span> ViewResult()
{
ViewName = <span class="code-string" style="border: 0px; color: purple; margin: 0px; padding: 0px;">"</span><span class="code-string" style="border: 0px; color: purple; margin: 0px; padding: 0px;">Error"</span>,
ViewData = <span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">new</span> ViewDataDictionary(model)
};
}
}</pre>
and in view we have to use<br />
<pre class="notranslate" id="pre715532" lang="”html”" style="background-color: #fbedbb; border: 1px solid rgb(251, 237, 187); font-family: Consolas, "Courier New", Courier, mono; font-size: 9pt; font-stretch: normal; line-height: normal; overflow: auto; padding: 6px; tab-size: 4; white-space: pre-wrap; word-break: break-word; word-wrap: break-word;">@Model.Exception;</pre>
<b><br /></b>
but this way does not provide code re-usability across multiple controllers.<br />
<br />
<b>3. HandleError Attribute</b><br />
<b><br /></b>
1. we put this attribute on action in controller as below.<br />
<pre class="notranslate" id="pre194511" lang="”html”" style="background-color: #fbedbb; border: 1px solid rgb(251, 237, 187); font-family: Consolas, "Courier New", Courier, mono; font-size: 9pt; font-stretch: normal; line-height: normal; overflow: auto; padding: 6px; tab-size: 4; white-space: pre-wrap; word-break: break-word; word-wrap: break-word;"><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">public</span> <span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">class</span> HomeController : Controller
{
[HandleError()]
<span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">public</span> ActionResult SomeError()
{
<span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">throw</span> <span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">new</span> Exception(<span class="code-string" style="border: 0px; color: purple; margin: 0px; padding: 0px;">"</span><span class="code-string" style="border: 0px; color: purple; margin: 0px; padding: 0px;">test"</span>);
}
}</pre>
2. Add custom error code in web config.<br />
<pre class="notranslate" id="pre215481" style="background-color: #fbedbb; border: 1px solid rgb(251, 237, 187); font-family: Consolas, "Courier New", Courier, mono; font-size: 9pt; font-stretch: normal; line-height: normal; overflow: auto; padding: 6px; tab-size: 4; white-space: pre-wrap; word-break: break-word; word-wrap: break-word;"><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;"><</span><span class="code-leadattribute" style="border: 0px; color: maroon; margin: 0px; padding: 0px;">system.web</span><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">></span>
<span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;"><</span><span class="code-leadattribute" style="border: 0px; color: maroon; margin: 0px; padding: 0px;">customErrors</span> <span class="code-attribute" style="border: 0px; color: red; margin: 0px; padding: 0px;">defaultRedirect</span><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">="</span><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">Error.cshtml"</span> <span class="code-attribute" style="border: 0px; color: red; margin: 0px; padding: 0px;">mode</span><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">="</span><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">On"</span><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">></span>
<span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;"><</span><span class="code-leadattribute" style="border: 0px; color: maroon; margin: 0px; padding: 0px;">/customErrors</span><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">></span>
<span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;"><</span><span class="code-leadattribute" style="border: 0px; color: maroon; margin: 0px; padding: 0px;">/system.web</span><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">></span> </pre>
<br />
If you want different views for different exception types then modify 'HandleError' attribute as below.<br />
<pre class="notranslate" id="pre115966" lang="”html”" style="background-color: #fbedbb; border: 1px solid rgb(251, 237, 187); font-family: Consolas, "Courier New", Courier, mono; font-size: 9pt; font-stretch: normal; line-height: normal; overflow: auto; padding: 6px; tab-size: 4; white-space: pre-wrap; word-break: break-word; word-wrap: break-word;"><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">public</span> <span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">class</span> HomeController : Controller
{
[HandleError(ExceptionType=<span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">typeof</span>(ArithmeticException),View=<span class="code-string" style="border: 0px; color: purple; margin: 0px; padding: 0px;">"</span><span class="code-string" style="border: 0px; color: purple; margin: 0px; padding: 0px;">Arthimetic"</span>)]
[HandleError(ExceptionType = <span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">typeof</span>(NotImplementedException),View =<span class="code-string" style="border: 0px; color: purple; margin: 0px; padding: 0px;">"</span><span class="code-string" style="border: 0px; color: purple; margin: 0px; padding: 0px;">Error1"</span>)]
<span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">public</span> ActionResult SomeError()
{
}
}</pre>
<b><br /></b><b>4. Create separate class Inheriting from 'ErrorHandleAttribute'.</b><br />
<b><br /></b>
In this way we can use code re-usability concept across all controller. we create separate class that inherit from 'ErrorHandleAttribute' class and then we use this '<b>Errorclass</b>' as an <b>Attribute</b> for Actions in controllers.<br />
<pre class="notranslate" id="pre318169" lang="”html”" style="background-color: #fbedbb; border: 1px solid rgb(251, 237, 187); font-family: Consolas, "Courier New", Courier, mono; font-size: 9pt; font-stretch: normal; line-height: normal; overflow: auto; padding: 6px; tab-size: 4; white-space: pre-wrap; word-break: break-word; word-wrap: break-word;"><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">public</span> <span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">class</span> ErrorClass : HandleErrorAttribute
{
<span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">public</span> <span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">override</span> <span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">void</span> OnException(ExceptionContext filterContext)
{
Exception ex = filterContext.Exception;
filterContext.ExceptionHandled = <span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">true</span>;
<span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">var</span> model = <span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">new</span> HandleErrorInfo(filterContext.Exception, <span class="code-string" style="border: 0px; color: purple; margin: 0px; padding: 0px;">"</span><span class="code-string" style="border: 0px; color: purple; margin: 0px; padding: 0px;">Controller"</span>, <span class="code-string" style="border: 0px; color: purple; margin: 0px; padding: 0px;">"</span><span class="code-string" style="border: 0px; color: purple; margin: 0px; padding: 0px;">Action"</span>);
filterContext.Result = <span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">new</span> ViewResult()
{
ViewName = <span class="code-string" style="border: 0px; color: purple; margin: 0px; padding: 0px;">"</span><span class="code-string" style="border: 0px; color: purple; margin: 0px; padding: 0px;">Error1"</span>,
ViewData = <span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">new</span> ViewDataDictionary(model)
};
}
}</pre>
<br />
<b>5. Handling Http Errors</b><br />
<b><br /></b>
There are many error like 'FileNotFound', Http 500 error etc . for these we use customer error in web config.<br />
<pre class="notranslate" id="pre950565" style="background-color: #fbedbb; border: 1px solid rgb(251, 237, 187); font-family: Consolas, "Courier New", Courier, mono; font-size: 9pt; font-stretch: normal; line-height: normal; overflow: auto; padding: 6px; tab-size: 4; white-space: pre-wrap; word-break: break-word; word-wrap: break-word;"><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;"><</span><span class="code-leadattribute" style="border: 0px; color: maroon; margin: 0px; padding: 0px;">system.web</span><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">></span>
<span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;"><</span><span class="code-leadattribute" style="border: 0px; color: maroon; margin: 0px; padding: 0px;">customErrors</span>
<span class="code-attribute" style="border: 0px; color: red; margin: 0px; padding: 0px;">mode</span><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">="</span><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">On"</span> <span class="code-attribute" style="border: 0px; color: red; margin: 0px; padding: 0px;">defaultRedirect</span><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">="</span><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">Error1"</span><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">></span>
<span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;"><</span><span class="code-leadattribute" style="border: 0px; color: maroon; margin: 0px; padding: 0px;">error</span> <span class="code-attribute" style="border: 0px; color: red; margin: 0px; padding: 0px;">statusCode</span><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">="</span><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">404"</span> <span class="code-attribute" style="border: 0px; color: red; margin: 0px; padding: 0px;">redirect</span><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">="</span><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">~/Testing/NoPageFound"</span><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">/</span><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">></span>
<span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;"><</span><span class="code-leadattribute" style="border: 0px; color: maroon; margin: 0px; padding: 0px;">/customErrors</span><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">></span>
<span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;"><</span><span class="code-leadattribute" style="border: 0px; color: maroon; margin: 0px; padding: 0px;">/system.web</span><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">></span> </pre>
<b><br /></b><b>6. Global Error handling </b><br />
We write code in Global.asax file in Application_Error event.<br />
<pre class="notranslate" id="pre35879" lang="”html”" style="background-color: #fbedbb; border: 1px solid rgb(251, 237, 187); font-family: Consolas, "Courier New", Courier, mono; font-size: 9pt; font-stretch: normal; line-height: normal; overflow: auto; padding: 6px; tab-size: 4; white-space: pre-wrap; word-break: break-word; word-wrap: break-word;"><span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">protected</span> <span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">void</span> Application_Error(<span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">object</span> sender, EventArgs e)
{
Exception exception = Server.GetLastError();
Server.ClearError();
Response.Redirect(<span class="code-string" style="border: 0px; color: purple; margin: 0px; padding: 0px;">"</span><span class="code-string" style="border: 0px; color: purple; margin: 0px; padding: 0px;">/Home/Error"</span>);
}</pre>
<br />
<br /></div>
Bhanu Prataphttp://www.blogger.com/profile/17529341083467033319noreply@blogger.com1tag:blogger.com,1999:blog-45468370605306946.post-7074235696971175062016-12-19T07:06:00.001-08:002016-12-23T05:00:04.798-08:00Important links for .NET Core<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiNuyB1HaWdp2_XOQmKWZqYll5j4EdMAnWvmZ8xgaHjVJ62Bkz3bEOW2_RMjWR3tgRuvZ4lv9utOyVdm7ngzksbXsJpD9IAF9cv-wP2qJBHGLMxOFBtH3QwZsQXySluI8PSO1vxvWkt9g/s1600/AspNetCore.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="236" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiNuyB1HaWdp2_XOQmKWZqYll5j4EdMAnWvmZ8xgaHjVJ62Bkz3bEOW2_RMjWR3tgRuvZ4lv9utOyVdm7ngzksbXsJpD9IAF9cv-wP2qJBHGLMxOFBtH3QwZsQXySluI8PSO1vxvWkt9g/s400/AspNetCore.png" width="400" /></a></div>
<br />
I am providing some important links for .NET Core as below.<br />
<br />
.NET Core - <a href="https://docs.microsoft.com/en-us/dotnet/articles/core/" target="_blank">.NET Core</a><br />
<br />
Official release of .NET Core - <a href="https://www.microsoft.com/net/core#windowsvs2015" target="_blank">Official release</a><br />
<br />
GitHub Repository - <a href="https://github.com/dotnet/core" target="_blank">GitHub dotnet core</a><br />
<br />
ASP.NET Core - <a href="https://docs.microsoft.com/en-us/aspnet/index" target="_blank">ASP.NET Core</a><br />
<br />
ASP.NET Core Tutorial - <a href="https://docs.microsoft.com/en-us/aspnet/core/getting-started" target="_blank">Tutorial</a><br />
<br />
.NET Core Rad-map - <a href="https://github.com/dotnet/core/blob/master/roadmap.md" target="_blank">Road Map</a><br />
<br />
.NET Core Api - <a href="https://docs.microsoft.com/en-us/dotnet/core/api/" target="_blank">Core api</a></div>
Bhanu Prataphttp://www.blogger.com/profile/17529341083467033319noreply@blogger.com5tag:blogger.com,1999:blog-45468370605306946.post-73944046702873044672016-12-19T06:08:00.000-08:002016-12-19T07:15:36.935-08:00What is .NET Core and it's characteristics?<div dir="ltr" style="text-align: left;" trbidi="on">
<b>.NET Core - </b><br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhITF0GrgAE3QTBm5cfZpzXO5leDwQwvfx8rLtaoc9SACmGAJXLlaHdbo6Wub0cdKQXEqHL7T56gFWJScwGjwsI_loDrDjI3qFFzwVIrBe_cz9qkL1F-tCHhfMbQaR-POOjaJlDM0sidg/s1600/dotnet_core.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em; text-align: center;"><img border="0" height="229" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhITF0GrgAE3QTBm5cfZpzXO5leDwQwvfx8rLtaoc9SACmGAJXLlaHdbo6Wub0cdKQXEqHL7T56gFWJScwGjwsI_loDrDjI3qFFzwVIrBe_cz9qkL1F-tCHhfMbQaR-POOjaJlDM0sidg/s400/dotnet_core.jpg" width="500" /></a><b><br /></b>
<span style="color: #2a2a2a; font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif;"><span style="font-size: 13px;">.NET Core is a general purpose, modular, cross-platform and open source implementation of the .NET Platform. It contains many of the same APIs as the .NET Framework (but .NET Core is a smaller set) and includes run-time, framework, compiler and tools components that support a variety of operating systems and chip targets. The .NET Core implementation was primarily driven by the ASP.NET Core workloads but also by the need and desire to have a more modern run-time. It can be used in device, cloud and embedded/IoT scenarios.</span></span><br />
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<span style="color: #2a2a2a; font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif;"><span style="font-size: 13px;">.NET Core has following main <span style="font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif;">characteristics.</span></span></span><br />
<span style="color: #2a2a2a; font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif;"><span style="font-size: 13px;"><span style="font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif;"><b>1. Cross-Plateform - </b></span><span style="font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif;">.NET Core provides key functionality to implement the app features you need and reuse this code regardless of your platform target.</span></span></span><br />
<span style="color: #2a2a2a; font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif;"><span style="font-size: 13px;"><span style="font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif;"><b>2. Open Source - </b></span><span style="font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif;"> Having .NET Core as an open source project promotes a more transparent development process </span><span style="font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif;">and is available on </span><a href="https://github.com/" style="color: #00709f; font-family: "Segoe UI", "Lucida Grande", Verdana, Arial, Helvetica, sans-serif; text-decoration: none;">GitHub</a><span style="font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif;">.</span></span></span><br />
<span style="color: #2a2a2a; font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif;"><span style="font-size: 13px;"><span style="font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif;"><b>3. Flexible Deployment - </b></span><span style="font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif;">there are two main ways to deploy your app: framework-dependent deployment or self-contained deployment. With framework-dependent deployment, only your app and third-party dependencies are installed and your app depends on a system-wide version of .NET Core to be present. With self-contained deployment, the .NET Core version used to build your application is also deployed along with your app and third-party dependencies and can run side-by-side with other versions. For more information, see </span><a href="https://docs.microsoft.com/en-us/dotnet/articles/core/deploying/index" style="color: #00709f; font-family: "Segoe UI", "Lucida Grande", Verdana, Arial, Helvetica, sans-serif; text-decoration: none;">.NET Core Application Deployment</a><span style="font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif;">.</span></span></span><br />
<span style="color: #2a2a2a; font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif;"><span style="font-size: 13px;"><span style="font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif;"><b>4. Modular - </b>Modular means collection of small modules so </span><span style="font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif;">.NET Core is modular because it is released through NuGet in smaller assembly packages. Rather than one large assembly that contains most of the core functionality, .NET Core is made available as smaller feature-centric packages. This enables a more agile development model for us and allows you to optimize your app to include just the NuGet packages you need. The benefits of a smaller app surface area include tighter security, reduced servicing, improved performance, and decreased costs in a pay-for-what-you-use model.</span></span></span><br />
<span style="color: #2a2a2a; font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif;"><span style="font-size: 13px;"><span style="font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif;"><b>5. Compatible - </b></span></span></span><span style="background-color: white; color: #222222; font-family: segoe-ui_normal, "Segoe UI", Segoe, "Segoe WP", "Helvetica Neue", Helvetica, sans-serif;">.NET Core is compatible with .NET Framework, Xamarin and Mono, via the </span><a href="https://docs.microsoft.com/en-us/dotnet/articles/standard/library" style="backface-visibility: hidden; background-color: white; box-shadow: transparent 0px 0px 1px; color: #007bb8; cursor: pointer; font-family: segoe-ui_normal, "Segoe UI", Segoe, "Segoe WP", "Helvetica Neue", Helvetica, sans-serif; text-decoration: none; transition-duration: 0.1s; transition-property: transform, color, background-color, -webkit-transform; transition-timing-function: ease-out;">.NET Standard Library</a><span style="background-color: white; color: #222222; font-family: segoe-ui_normal, "Segoe UI", Segoe, "Segoe WP", "Helvetica Neue", Helvetica, sans-serif;">.</span><br />
<span style="color: #2a2a2a; font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif;"><span style="font-size: 13px;"><span style="font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif;"><br /></span></span></span>
<span style="color: #2a2a2a; font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif;"><span style="font-size: 13px;"><span style="font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif;">For More refer <a href="https://docs.microsoft.com/en-us/dotnet/articles/core/index" target="_blank">DotNet Core</a> <a href="https://msdn.microsoft.com/en-us/library/dn878908(v=vs.110).aspx" target="_blank">.NET Core</a></span></span></span>
<span style="color: #2a2a2a; font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif;"><span style="font-size: 13px;"><br /></span></span>
<span style="color: #2a2a2a; font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif;"><span style="font-size: 13px;"><br /></span></span>
<span style="color: #2a2a2a; font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif;"><span style="font-size: 13px;"><br /></span></span></div>
Bhanu Prataphttp://www.blogger.com/profile/17529341083467033319noreply@blogger.com1tag:blogger.com,1999:blog-45468370605306946.post-11341527308571028562016-12-12T23:28:00.001-08:002016-12-13T04:32:53.744-08:00 $apply, $watch, $digest methods in AngularJS<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="background-color: white;">
There are many methods in AngularJs some of them are give below.<br />
<b>$watch - </b><span style="color: #333333; font-family: "helvetica neue" , "helvetica" , "arial" , sans-serif; font-size: 14px;">Registers a </span><code style="background: rgb(244, 244, 244); border-radius: 5px; box-sizing: border-box; color: maroon; font-family: menlo, monaco, consolas, "courier new", monospace; padding: 2px 5px; white-space: nowrap;"><span class="pln" style="box-sizing: border-box; color: #333333;">listener</span></code><span style="color: #333333; font-family: "helvetica neue" , "helvetica" , "arial" , sans-serif; font-size: 14px;"> callback to be executed whenever the </span><code style="background: rgb(244, 244, 244); border-radius: 5px; box-sizing: border-box; color: maroon; font-family: menlo, monaco, consolas, "courier new", monospace; padding: 2px 5px; white-space: nowrap;"><span class="pln" style="box-sizing: border-box; color: #333333;">watchExpression</span></code><span style="color: #333333; font-family: "helvetica neue" , "helvetica" , "arial" , sans-serif; font-size: 14px;"> changes.</span><br />
<span style="color: #333333; font-family: "helvetica neue" , "helvetica" , "arial" , sans-serif; font-size: 14px;">The </span><code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: menlo, monaco, consolas, "courier new", monospace; padding: 2px 4px; white-space: nowrap;"><span class="pln" style="box-sizing: border-box; color: #333333;">watchExpression</span></code><span style="color: #333333; font-family: "helvetica neue" , "helvetica" , "arial" , sans-serif; font-size: 14px;"> is called on every call to </span><a href="https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$digest" style="background-position: 0px 0px; box-sizing: border-box; color: #428bca; font-family: "helvetica neue", helvetica, arial, sans-serif; text-decoration: none;">$digest()</a><span style="color: #333333; font-family: "helvetica neue" , "helvetica" , "arial" , sans-serif; font-size: 14px;"> and should return the value that will be watched. (</span><code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: menlo, monaco, consolas, "courier new", monospace; padding: 2px 4px; white-space: nowrap;"><span class="pln" style="box-sizing: border-box; color: #333333;">watchExpression</span></code><span style="color: #333333; font-family: "helvetica neue" , "helvetica" , "arial" , sans-serif; font-size: 14px;">should not change its value when executed multiple times with the same input because it may be executed multiple times by </span><a href="https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$digest" style="background-position: 0px 0px; box-sizing: border-box; color: #428bca; font-family: "helvetica neue", helvetica, arial, sans-serif; text-decoration: none;">$digest()</a><span style="color: #333333; font-family: "helvetica neue" , "helvetica" , "arial" , sans-serif; font-size: 14px;">.</span><br />
<span style="color: #333333; font-family: "helvetica neue" , "helvetica" , "arial" , sans-serif; font-size: 14px;">The </span><code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: menlo, monaco, consolas, "courier new", monospace; padding: 2px 4px; white-space: nowrap;"><span class="pln" style="box-sizing: border-box; color: #333333;">listener</span></code><span style="color: #333333; font-family: "helvetica neue" , "helvetica" , "arial" , sans-serif; font-size: 14px;"> is called only when the value from the current </span><code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: menlo, monaco, consolas, "courier new", monospace; padding: 2px 4px; white-space: nowrap;"><span class="pln" style="box-sizing: border-box; color: #333333;">watchExpression</span></code><span style="color: #333333; font-family: "helvetica neue" , "helvetica" , "arial" , sans-serif; font-size: 14px;"> and the previous call to </span><code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: menlo, monaco, consolas, "courier new", monospace; padding: 2px 4px; white-space: nowrap;"><span class="pln" style="box-sizing: border-box; color: #333333;">watchExpression</span></code><span style="color: #333333; font-family: "helvetica neue" , "helvetica" , "arial" , sans-serif; font-size: 14px;"> are not equal. example - </span><br />
<pre style="background-color: whitesmoke; border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: menlo, monaco, consolas, "courier new", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 10px; overflow: auto; padding: 9.5px; white-space: pre-wrap; word-break: normal; word-wrap: break-word;"><code class="lang-js" style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: inherit; padding: 0px;"><span class="pln" style="box-sizing: border-box;">scope</span><span class="pun" style="box-sizing: border-box;">.</span><span class="pln" style="box-sizing: border-box;">$watch</span><span class="pun" style="box-sizing: border-box;">(</span><span class="str" style="box-sizing: border-box; color: #dd1144;">'name'</span><span class="pun" style="box-sizing: border-box;">,</span><span class="pln" style="box-sizing: border-box;"> </span><span class="kwd" style="box-sizing: border-box;">function</span><span class="pun" style="box-sizing: border-box;">(</span><span class="pln" style="box-sizing: border-box;">newValue</span><span class="pun" style="box-sizing: border-box;">,</span><span class="pln" style="box-sizing: border-box;"> oldValue</span><span class="pun" style="box-sizing: border-box;">)</span><span class="pln" style="box-sizing: border-box;"> </span><span class="pun" style="box-sizing: border-box;">{</span><span class="pln" style="box-sizing: border-box;">
scope</span><span class="pun" style="box-sizing: border-box;">.</span><span class="pln" style="box-sizing: border-box;">counter </span><span class="pun" style="box-sizing: border-box;">=</span><span class="pln" style="box-sizing: border-box;"> scope</span><span class="pun" style="box-sizing: border-box;">.</span><span class="pln" style="box-sizing: border-box;">counter </span><span class="pun" style="box-sizing: border-box;">+</span><span class="pln" style="box-sizing: border-box;"> </span><span class="lit" style="box-sizing: border-box; color: #445588;">1</span><span class="pun" style="box-sizing: border-box;">;</span><span class="pln" style="box-sizing: border-box;">
</span><span class="pun" style="box-sizing: border-box;">});</span></code></pre>
<b>$watchgroup - </b><span style="color: #333333; font-family: "helvetica neue" , "helvetica" , "arial" , sans-serif; font-size: 14px;">A variant of </span><a href="https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$watch" style="background-position: 0px 0px; box-sizing: border-box; color: #428bca; font-family: "helvetica neue", helvetica, arial, sans-serif; text-decoration: none;">$watch()</a><span style="color: #333333; font-family: "helvetica neue" , "helvetica" , "arial" , sans-serif; font-size: 14px;"> where it watches an array of </span><code style="background: rgb(244, 244, 244); border-radius: 5px; box-sizing: border-box; color: maroon; font-family: menlo, monaco, consolas, "courier new", monospace; padding: 2px 5px; white-space: nowrap;"><span class="pln" style="box-sizing: border-box; color: #333333;">watchExpressions</span></code><span style="color: #333333; font-family: "helvetica neue" , "helvetica" , "arial" , sans-serif; font-size: 14px;">. If any one expression in the collection changes the </span><code style="background: rgb(244, 244, 244); border-radius: 5px; box-sizing: border-box; color: maroon; font-family: menlo, monaco, consolas, "courier new", monospace; padding: 2px 5px; white-space: nowrap;"><span class="pln" style="box-sizing: border-box; color: #333333;">listener</span></code><span style="color: #333333; font-family: "helvetica neue" , "helvetica" , "arial" , sans-serif; font-size: 14px;"> is executed.</span><br />
<span style="color: #333333; font-family: "helvetica neue" , "helvetica" , "arial" , sans-serif; font-size: 14px;"><b style="color: black; font-family: "Times New Roman";">$apply - </b></span><code style="background: rgb(244, 244, 244); border-radius: 5px; box-sizing: border-box; color: maroon; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; padding: 2px 5px; white-space: nowrap;"><span class="pln" style="box-sizing: border-box; color: #333333;">$apply</span><span class="pun" style="box-sizing: border-box; color: #333333;">()</span></code><span style="color: #333333; font-family: "helvetica neue" , "helvetica" , "arial" , sans-serif;"> is used to execute an expression in angular from outside of the angular framework. (For example from browser DOM events, setTimeout, XHR or third party libraries). Because we are calling into the angular framework we need to perform proper scope life cycle of </span><a href="https://docs.angularjs.org/api/ng/service/$exceptionHandler" style="background-position: 0px 0px; box-sizing: border-box; color: #428bca; font-family: "helvetica neue", helvetica, arial, sans-serif; text-decoration: none;">exception handling</a><span style="color: #333333; font-family: "helvetica neue" , "helvetica" , "arial" , sans-serif;">, </span><a href="https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$digest" style="background-position: 0px 0px; box-sizing: border-box; color: #428bca; font-family: "helvetica neue", helvetica, arial, sans-serif; text-decoration: none;">executing watches</a><span style="color: #333333; font-family: "helvetica neue" , "helvetica" , "arial" , sans-serif;">.</span><br />
<pre style="background-color: whitesmoke; border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: menlo, monaco, consolas, "courier new", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 10px; overflow: auto; padding: 9.5px; white-space: pre-wrap; word-break: normal; word-wrap: break-word;"><code class="lang-js" style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: inherit; padding: 0px;"><span class="kwd" style="box-sizing: border-box;">function</span><span class="pln" style="box-sizing: border-box;"> $apply</span><span class="pun" style="box-sizing: border-box;">(</span><span class="pln" style="box-sizing: border-box;">expr</span><span class="pun" style="box-sizing: border-box;">)</span><span class="pln" style="box-sizing: border-box;"> </span><span class="pun" style="box-sizing: border-box;">{</span><span class="pln" style="box-sizing: border-box;">
</span><span class="kwd" style="box-sizing: border-box;">try</span><span class="pln" style="box-sizing: border-box;"> </span><span class="pun" style="box-sizing: border-box;">{</span><span class="pln" style="box-sizing: border-box;">
</span><span class="kwd" style="box-sizing: border-box;">return</span><span class="pln" style="box-sizing: border-box;"> $eval</span><span class="pun" style="box-sizing: border-box;">(</span><span class="pln" style="box-sizing: border-box;">expr</span><span class="pun" style="box-sizing: border-box;">);</span><span class="pln" style="box-sizing: border-box;">
</span><span class="pun" style="box-sizing: border-box;">}</span><span class="pln" style="box-sizing: border-box;"> </span><span class="kwd" style="box-sizing: border-box;">catch</span><span class="pln" style="box-sizing: border-box;"> </span><span class="pun" style="box-sizing: border-box;">(</span><span class="pln" style="box-sizing: border-box;">e</span><span class="pun" style="box-sizing: border-box;">)</span><span class="pln" style="box-sizing: border-box;"> </span><span class="pun" style="box-sizing: border-box;">{</span><span class="pln" style="box-sizing: border-box;">
$exceptionHandler</span><span class="pun" style="box-sizing: border-box;">(</span><span class="pln" style="box-sizing: border-box;">e</span><span class="pun" style="box-sizing: border-box;">);</span><span class="pln" style="box-sizing: border-box;">
</span><span class="pun" style="box-sizing: border-box;">}</span><span class="pln" style="box-sizing: border-box;"> finally </span><span class="pun" style="box-sizing: border-box;">{</span><span class="pln" style="box-sizing: border-box;">
$root</span><span class="pun" style="box-sizing: border-box;">.</span><span class="pln" style="box-sizing: border-box;">$digest</span><span class="pun" style="box-sizing: border-box;">();</span><span class="pln" style="box-sizing: border-box;">
</span><span class="pun" style="box-sizing: border-box;">}</span><span class="pln" style="box-sizing: border-box;">
</span><span class="pun" style="box-sizing: border-box;">}</span></code></pre>
</div>
<div style="background-color: white;">
<div style="box-sizing: border-box; color: #333333; font-family: "helvetica neue", helvetica, arial, sans-serif; margin-bottom: 10px;">
Scope's <code style="background: rgb(244, 244, 244); border-radius: 5px; box-sizing: border-box; color: maroon; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; padding: 2px 5px; white-space: nowrap;"><span class="pln" style="box-sizing: border-box; color: #333333;">$apply</span><span class="pun" style="box-sizing: border-box; color: #333333;">()</span></code> method transitions through the following stages:</div>
<span style="color: #073763; font-family: "arial" , "tahoma" , "helvetica" , "freesans" , sans-serif; font-size: 13.2px;"></span><br />
<ol style="box-sizing: border-box; color: #333333; font-family: "helvetica neue", helvetica, arial, sans-serif; margin-bottom: 0px; margin-top: 0px;">
<li style="box-sizing: border-box;">The <a href="https://docs.angularjs.org/guide/expression" style="background: 0px 0px; box-sizing: border-box; color: #428bca; text-decoration: none;">expression</a> is executed using the <a href="https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$eval" style="background: 0px 0px; box-sizing: border-box; color: #428bca; text-decoration: none;">$eval()</a> method.</li>
<li style="box-sizing: border-box;">Any exceptions from the execution of the expression are forwarded to the <a href="https://docs.angularjs.org/api/ng/service/$exceptionHandler" style="background: 0px 0px; box-sizing: border-box; color: #428bca; text-decoration: none;">$exceptionHandler</a> service.</li>
<li style="box-sizing: border-box;">The <a href="https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$watch" style="background: 0px 0px; box-sizing: border-box; color: #428bca; text-decoration: none;">watch</a> listeners are fired immediately after the expression was executed using the <a href="https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$digest" style="background: 0px 0px; box-sizing: border-box; color: #428bca; text-decoration: none;">$digest()</a> method.</li>
</ol>
<span style="color: #073763; font-family: "arial" , "tahoma" , "helvetica" , "freesans" , sans-serif; font-size: 13.2px;"><b style="color: black; font-family: "Times New Roman";">$digest - </b></span><span style="color: #333333; font-family: "helvetica neue" , "helvetica" , "arial" , sans-serif;">Processes all of the</span><span style="color: #333333; font-family: "helvetica neue" , "helvetica" , "arial" , sans-serif;"> </span><a href="https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$watch" style="background: 0px 0px; box-sizing: border-box; color: #428bca; font-family: "helvetica neue", helvetica, arial, sans-serif; text-decoration: none;">watchers</a><span style="color: #333333; font-family: "helvetica neue" , "helvetica" , "arial" , sans-serif;"> </span><span style="color: #333333; font-family: "helvetica neue" , "helvetica" , "arial" , sans-serif;">of the current scope and its children. Because a</span><span style="color: #333333; font-family: "helvetica neue" , "helvetica" , "arial" , sans-serif;"> </span><a href="https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$watch" style="background: 0px 0px; box-sizing: border-box; color: #428bca; font-family: "helvetica neue", helvetica, arial, sans-serif; text-decoration: none;">watcher</a><span style="color: #333333; font-family: "helvetica neue" , "helvetica" , "arial" , sans-serif;">'s listener can change the model, the</span><span style="color: #333333; font-family: "helvetica neue" , "helvetica" , "arial" , sans-serif;"> </span><code style="background: rgb(244, 244, 244); border-radius: 5px; box-sizing: border-box; color: maroon; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; padding: 2px 5px; white-space: nowrap;"><span class="pln" style="box-sizing: border-box; color: #333333;">$digest</span><span class="pun" style="box-sizing: border-box; color: #333333;">()</span></code><span style="color: #333333; font-family: "helvetica neue" , "helvetica" , "arial" , sans-serif;">keeps calling the</span><span style="color: #333333; font-family: "helvetica neue" , "helvetica" , "arial" , sans-serif;"> </span><a href="https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$watch" style="background: 0px 0px; box-sizing: border-box; color: #428bca; font-family: "helvetica neue", helvetica, arial, sans-serif; text-decoration: none;">watchers</a><span style="color: #333333; font-family: "helvetica neue" , "helvetica" , "arial" , sans-serif;"> </span><span style="color: #333333; font-family: "helvetica neue" , "helvetica" , "arial" , sans-serif;">until no more listeners are firing. This means that it is possible to get into an infinite loop. This function will throw</span><span style="color: #333333; font-family: "helvetica neue" , "helvetica" , "arial" , sans-serif;"> </span><code style="background: rgb(244, 244, 244); border-radius: 5px; box-sizing: border-box; color: maroon; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; padding: 2px 5px; white-space: nowrap;"><span class="str" style="box-sizing: border-box; color: #dd1144;">'Maximum iteration limit exceeded.'</span></code><span style="color: #333333; font-family: "helvetica neue" , "helvetica" , "arial" , sans-serif;"> </span><span style="color: #333333; font-family: "helvetica neue" , "helvetica" , "arial" , sans-serif;">if the number of iterations exceeds 10.</span><br />
<div style="box-sizing: border-box; color: #333333; font-family: "helvetica neue", helvetica, arial, sans-serif; margin-bottom: 10px;">
Usually, you don't call <code style="background: rgb(244, 244, 244); border-radius: 5px; box-sizing: border-box; color: maroon; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; padding: 2px 5px; white-space: nowrap;"><span class="pln" style="box-sizing: border-box; color: #333333;">$digest</span><span class="pun" style="box-sizing: border-box; color: #333333;">()</span></code> directly in <a href="https://docs.angularjs.org/api/ng/directive/ngController" style="background: 0px 0px; box-sizing: border-box; color: #428bca; text-decoration: none;">controllers</a> or in <a href="https://docs.angularjs.org/api/ng/provider/$compileProvider#directive" style="background: 0px 0px; box-sizing: border-box; color: #428bca; text-decoration: none;">directives</a>. Instead, you should call <a href="https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$apply" style="background: 0px 0px; box-sizing: border-box; color: #428bca; text-decoration: none;">$apply()</a> (typically from within a <a href="https://docs.angularjs.org/api/ng/provider/$compileProvider#directive" style="background: 0px 0px; box-sizing: border-box; color: #428bca; text-decoration: none;">directive</a>), which will force a <code style="background: rgb(244, 244, 244); border-radius: 5px; box-sizing: border-box; color: maroon; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; padding: 2px 5px; white-space: nowrap;"><span class="pln" style="box-sizing: border-box; color: #333333;">$digest</span><span class="pun" style="box-sizing: border-box; color: #333333;">().</span></code></div>
<div style="box-sizing: border-box; color: #333333; font-family: "helvetica neue", helvetica, arial, sans-serif; font-size: 14px; margin-bottom: 10px;">
<code style="background: rgb(244, 244, 244); border-radius: 5px; box-sizing: border-box; color: maroon; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12.6px; padding: 2px 5px; white-space: nowrap;"><span class="pun" style="box-sizing: border-box; color: #333333;"></span></code></div>
<pre style="background-color: whitesmoke; border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: menlo, monaco, consolas, "courier new", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 10px; overflow: auto; padding: 9.5px; white-space: pre-wrap; word-break: normal; word-wrap: break-word;"><code class="lang-js" style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: inherit; padding: 0px;"><span class="kwd" style="box-sizing: border-box;">var</span><span class="pln" style="box-sizing: border-box;"> scope </span><span class="pun" style="box-sizing: border-box;">=</span><span class="pln" style="box-sizing: border-box;"> </span><span class="pun" style="box-sizing: border-box;">...;</span><span class="pln" style="box-sizing: border-box;">
scope</span><span class="pun" style="box-sizing: border-box;">.</span><span class="pln" style="box-sizing: border-box;">name </span><span class="pun" style="box-sizing: border-box;">=</span><span class="pln" style="box-sizing: border-box;"> </span><span class="str" style="box-sizing: border-box; color: #dd1144;">'misko'</span><span class="pun" style="box-sizing: border-box;">;</span><span class="pln" style="box-sizing: border-box;">
scope</span><span class="pun" style="box-sizing: border-box;">.</span><span class="pln" style="box-sizing: border-box;">counter </span><span class="pun" style="box-sizing: border-box;">=</span><span class="pln" style="box-sizing: border-box;"> </span><span class="lit" style="box-sizing: border-box; color: #445588;">0</span><span class="pun" style="box-sizing: border-box;">;</span><span class="pln" style="box-sizing: border-box;">
expect</span><span class="pun" style="box-sizing: border-box;">(</span><span class="pln" style="box-sizing: border-box;">scope</span><span class="pun" style="box-sizing: border-box;">.</span><span class="pln" style="box-sizing: border-box;">counter</span><span class="pun" style="box-sizing: border-box;">).</span><span class="pln" style="box-sizing: border-box;">toEqual</span><span class="pun" style="box-sizing: border-box;">(</span><span class="lit" style="box-sizing: border-box; color: #445588;">0</span><span class="pun" style="box-sizing: border-box;">);</span><span class="pln" style="box-sizing: border-box;">
scope</span><span class="pun" style="box-sizing: border-box;">.</span><span class="pln" style="box-sizing: border-box;">$watch</span><span class="pun" style="box-sizing: border-box;">(</span><span class="str" style="box-sizing: border-box; color: #dd1144;">'name'</span><span class="pun" style="box-sizing: border-box;">,</span><span class="pln" style="box-sizing: border-box;"> </span><span class="kwd" style="box-sizing: border-box;">function</span><span class="pun" style="box-sizing: border-box;">(</span><span class="pln" style="box-sizing: border-box;">newValue</span><span class="pun" style="box-sizing: border-box;">,</span><span class="pln" style="box-sizing: border-box;"> oldValue</span><span class="pun" style="box-sizing: border-box;">)</span><span class="pln" style="box-sizing: border-box;"> </span><span class="pun" style="box-sizing: border-box;">{</span><span class="pln" style="box-sizing: border-box;">
scope</span><span class="pun" style="box-sizing: border-box;">.</span><span class="pln" style="box-sizing: border-box;">counter </span><span class="pun" style="box-sizing: border-box;">=</span><span class="pln" style="box-sizing: border-box;"> scope</span><span class="pun" style="box-sizing: border-box;">.</span><span class="pln" style="box-sizing: border-box;">counter </span><span class="pun" style="box-sizing: border-box;">+</span><span class="pln" style="box-sizing: border-box;"> </span><span class="lit" style="box-sizing: border-box; color: #445588;">1</span><span class="pun" style="box-sizing: border-box;">;</span><span class="pln" style="box-sizing: border-box;">
</span><span class="pun" style="box-sizing: border-box;">});</span><span class="pln" style="box-sizing: border-box;">
expect</span><span class="pun" style="box-sizing: border-box;">(</span><span class="pln" style="box-sizing: border-box;">scope</span><span class="pun" style="box-sizing: border-box;">.</span><span class="pln" style="box-sizing: border-box;">counter</span><span class="pun" style="box-sizing: border-box;">).</span><span class="pln" style="box-sizing: border-box;">toEqual</span><span class="pun" style="box-sizing: border-box;">(</span><span class="lit" style="box-sizing: border-box; color: #445588;">0</span><span class="pun" style="box-sizing: border-box;">);</span><span class="pln" style="box-sizing: border-box;">
scope</span><span class="pun" style="box-sizing: border-box;">.</span><span class="pln" style="box-sizing: border-box;">$digest</span><span class="pun" style="box-sizing: border-box;">();</span><span class="pln" style="box-sizing: border-box;">
</span><span class="com" style="box-sizing: border-box; color: #999988;">// the listener is always called during the first $digest loop after it was registered</span><span class="pln" style="box-sizing: border-box;">
expect</span><span class="pun" style="box-sizing: border-box;">(</span><span class="pln" style="box-sizing: border-box;">scope</span><span class="pun" style="box-sizing: border-box;">.</span><span class="pln" style="box-sizing: border-box;">counter</span><span class="pun" style="box-sizing: border-box;">).</span><span class="pln" style="box-sizing: border-box;">toEqual</span><span class="pun" style="box-sizing: border-box;">(</span><span class="lit" style="box-sizing: border-box; color: #445588;">1</span><span class="pun" style="box-sizing: border-box;">);</span><span class="pln" style="box-sizing: border-box;">
scope</span><span class="pun" style="box-sizing: border-box;">.</span><span class="pln" style="box-sizing: border-box;">$digest</span><span class="pun" style="box-sizing: border-box;">();</span><span class="pln" style="box-sizing: border-box;">
</span><span class="com" style="box-sizing: border-box; color: #999988;">// but now it will not be called unless the value changes</span><span class="pln" style="box-sizing: border-box;">
expect</span><span class="pun" style="box-sizing: border-box;">(</span><span class="pln" style="box-sizing: border-box;">scope</span><span class="pun" style="box-sizing: border-box;">.</span><span class="pln" style="box-sizing: border-box;">counter</span><span class="pun" style="box-sizing: border-box;">).</span><span class="pln" style="box-sizing: border-box;">toEqual</span><span class="pun" style="box-sizing: border-box;">(</span><span class="lit" style="box-sizing: border-box; color: #445588;">1</span><span class="pun" style="box-sizing: border-box;">);</span><span class="pln" style="box-sizing: border-box;">
scope</span><span class="pun" style="box-sizing: border-box;">.</span><span class="pln" style="box-sizing: border-box;">name </span><span class="pun" style="box-sizing: border-box;">=</span><span class="pln" style="box-sizing: border-box;"> </span><span class="str" style="box-sizing: border-box; color: #dd1144;">'adam'</span><span class="pun" style="box-sizing: border-box;">;</span><span class="pln" style="box-sizing: border-box;">
scope</span><span class="pun" style="box-sizing: border-box;">.</span><span class="pln" style="box-sizing: border-box;">$digest</span><span class="pun" style="box-sizing: border-box;">();</span><span class="pln" style="box-sizing: border-box;">
expect</span><span class="pun" style="box-sizing: border-box;">(</span><span class="pln" style="box-sizing: border-box;">scope</span><span class="pun" style="box-sizing: border-box;">.</span><span class="pln" style="box-sizing: border-box;">counter</span><span class="pun" style="box-sizing: border-box;">).</span><span class="pln" style="box-sizing: border-box;">toEqual</span><span class="pun" style="box-sizing: border-box;">(</span><span class="lit" style="box-sizing: border-box; color: #445588;">2</span><span class="pun" style="box-sizing: border-box;">);</span></code></pre>
<div style="box-sizing: border-box; color: #333333; font-family: "helvetica neue", helvetica, arial, sans-serif; margin-bottom: 10px;">
<b>$destroy - </b>Removes the current scope (and all of its children) from the parent scope. Removal implies that calls to <a href="https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$digest" style="background: 0px 0px; box-sizing: border-box; color: #428bca; text-decoration: none;">$digest()</a> will no longer propagate to the current scope and its children. Removal also implies that the current scope is eligible for garbage collection.</div>
<span style="color: #073763; font-family: "arial" , "tahoma" , "helvetica" , "freesans" , sans-serif; font-size: 13.2px;"></span><br />
<div style="box-sizing: border-box; color: #333333; font-family: "helvetica neue", helvetica, arial, sans-serif; margin-bottom: 10px;">
The <code style="background: rgb(244, 244, 244); border-radius: 5px; box-sizing: border-box; color: maroon; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; padding: 2px 5px; white-space: nowrap;"><span class="pln" style="box-sizing: border-box; color: #333333;">$destroy</span><span class="pun" style="box-sizing: border-box; color: #333333;">()</span></code> is usually used by directives such as <a href="https://docs.angularjs.org/api/ng/directive/ngRepeat" style="background: 0px 0px; box-sizing: border-box; color: #428bca; text-decoration: none;">ngRepeat</a> for managing the unrolling of the loop.</div>
<div style="box-sizing: border-box; color: #333333; font-family: "helvetica neue", helvetica, arial, sans-serif; margin-bottom: 10px;">
<b>$eval -</b> <span style="font-size: 14px;">Executes the </span><code style="background: rgb(244, 244, 244); border-radius: 5px; box-sizing: border-box; color: maroon; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12.6px; padding: 2px 5px; white-space: nowrap;"><span class="pln" style="box-sizing: border-box; color: #333333;">expression</span></code><span style="font-size: 14px;"> on the current scope and returns the result. Any exceptions in the expression are propagated (uncaught). This is useful when evaluating Angular expressions.</span></div>
<pre style="background-color: whitesmoke; border-radius: 4px; border: 1px solid rgb(204, 204, 204); box-sizing: border-box; color: #333333; font-family: menlo, monaco, consolas, "courier new", monospace; font-size: 13px; line-height: 1.42857; margin-bottom: 10px; overflow: auto; padding: 9.5px; white-space: pre-wrap; word-break: normal; word-wrap: break-word;"><code class="lang-js" style="background-color: transparent; border-radius: 0px; box-sizing: border-box; color: inherit; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: inherit; padding: 0px;"><span class="kwd" style="box-sizing: border-box;">var</span><span class="pln" style="box-sizing: border-box;"> scope </span><span class="pun" style="box-sizing: border-box;">=</span><span class="pln" style="box-sizing: border-box;"> ng</span><span class="pun" style="box-sizing: border-box;">.</span><span class="pln" style="box-sizing: border-box;">$rootScope</span><span class="pun" style="box-sizing: border-box;">.</span><span class="typ" style="box-sizing: border-box; color: #445588;">Scope</span><span class="pun" style="box-sizing: border-box;">();</span><span class="pln" style="box-sizing: border-box;">
scope</span><span class="pun" style="box-sizing: border-box;">.</span><span class="pln" style="box-sizing: border-box;">a </span><span class="pun" style="box-sizing: border-box;">=</span><span class="pln" style="box-sizing: border-box;"> </span><span class="lit" style="box-sizing: border-box; color: #445588;">1</span><span class="pun" style="box-sizing: border-box;">;</span><span class="pln" style="box-sizing: border-box;">
scope</span><span class="pun" style="box-sizing: border-box;">.</span><span class="pln" style="box-sizing: border-box;">b </span><span class="pun" style="box-sizing: border-box;">=</span><span class="pln" style="box-sizing: border-box;"> </span><span class="lit" style="box-sizing: border-box; color: #445588;">2</span><span class="pun" style="box-sizing: border-box;">;</span><span class="pln" style="box-sizing: border-box;">
expect</span><span class="pun" style="box-sizing: border-box;">(</span><span class="pln" style="box-sizing: border-box;">scope</span><span class="pun" style="box-sizing: border-box;">.</span><span class="pln" style="box-sizing: border-box;">$eval</span><span class="pun" style="box-sizing: border-box;">(</span><span class="str" style="box-sizing: border-box; color: #dd1144;">'a+b'</span><span class="pun" style="box-sizing: border-box;">)).</span><span class="pln" style="box-sizing: border-box;">toEqual</span><span class="pun" style="box-sizing: border-box;">(</span><span class="lit" style="box-sizing: border-box; color: #445588;">3</span><span class="pun" style="box-sizing: border-box;">);</span><span class="pln" style="box-sizing: border-box;">
expect</span><span class="pun" style="box-sizing: border-box;">(</span><span class="pln" style="box-sizing: border-box;">scope</span><span class="pun" style="box-sizing: border-box;">.</span><span class="pln" style="box-sizing: border-box;">$eval</span><span class="pun" style="box-sizing: border-box;">(</span><span class="kwd" style="box-sizing: border-box;">function</span><span class="pun" style="box-sizing: border-box;">(</span><span class="pln" style="box-sizing: border-box;">scope</span><span class="pun" style="box-sizing: border-box;">){</span><span class="pln" style="box-sizing: border-box;"> </span><span class="kwd" style="box-sizing: border-box;">return</span><span class="pln" style="box-sizing: border-box;"> scope</span><span class="pun" style="box-sizing: border-box;">.</span><span class="pln" style="box-sizing: border-box;">a </span><span class="pun" style="box-sizing: border-box;">+</span><span class="pln" style="box-sizing: border-box;"> scope</span><span class="pun" style="box-sizing: border-box;">.</span><span class="pln" style="box-sizing: border-box;">b</span><span class="pun" style="box-sizing: border-box;">;</span><span class="pln" style="box-sizing: border-box;"> </span><span class="pun" style="box-sizing: border-box;">})).</span><span class="pln" style="box-sizing: border-box;">toEqual</span><span class="pun" style="box-sizing: border-box;">(</span><span class="lit" style="box-sizing: border-box; color: #445588;">3</span><span class="pun" style="box-sizing: border-box;">);</span></code></pre>
<div style="box-sizing: border-box; color: #333333; font-family: "helvetica neue", helvetica, arial, sans-serif; margin-bottom: 10px;">
<span style="font-size: 14px;"><b>$evalAsync- </b></span><span style="font-family: "helvetica neue" , "helvetica" , "arial" , sans-serif;">Executes the expression on the current scope at a later point in time.</span><br />
<div style="box-sizing: border-box; margin-bottom: 10px;">
The <code style="background: rgb(244, 244, 244); border-radius: 5px; box-sizing: border-box; color: maroon; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; padding: 2px 5px; white-space: nowrap;"><span class="pln" style="box-sizing: border-box; color: #333333;">$evalAsync</span></code> makes no guarantees as to when the <code style="background: rgb(244, 244, 244); border-radius: 5px; box-sizing: border-box; color: maroon; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; padding: 2px 5px; white-space: nowrap;"><span class="pln" style="box-sizing: border-box; color: #333333;">expression</span></code> will be executed, only that:</div>
<ul style="box-sizing: border-box; margin-bottom: 0px; margin-top: 0px;">
<li style="box-sizing: border-box;">it will execute after the function that scheduled the evaluation (preferably before DOM rendering).</li>
<li style="box-sizing: border-box;">at least one <a href="https://docs.angularjs.org/api/ng/type/$rootScope.Scope#$digest" style="background: 0px 0px; box-sizing: border-box; color: #428bca; text-decoration: none;">$digest cycle</a> will be performed after <code style="background-color: #f9f2f4; border-radius: 4px; box-sizing: border-box; color: #c7254e; font-family: menlo, monaco, consolas, "courier new", monospace; padding: 2px 4px; white-space: nowrap;"><span class="pln" style="box-sizing: border-box; color: #333333;">expression</span></code> execution.</li>
</ul>
<div>
<br /></div>
<span style="font-size: 14px;">For other <b>methods</b> click <a href="https://docs.angularjs.org/api/ng/type/$rootScope.Scope" target="_blank">$digest $eval $destry, $apply etc</a></span></div>
</div>
</div>
Bhanu Prataphttp://www.blogger.com/profile/17529341083467033319noreply@blogger.com17tag:blogger.com,1999:blog-45468370605306946.post-33231041487025422332016-12-11T01:41:00.001-08:002016-12-11T06:30:03.507-08:00Points to be remember for Deploying ASP.Net MVC Application in Production<div dir="ltr" style="text-align: left;" trbidi="on">
<div>
<b>1. To make application Offline us</b><b>e App_Offline.htm</b></div>
<div>
<b><br /></b>
<br />
<div class="ArticleContent" style="color: #333333; font-family: Verdana, Helvetica; font-size: 10pt; font-stretch: normal; line-height: 20px;">
<span style="background-color: white; font-family: "verdana" , "geneva"; font-size: 14px;">If you are running your application within a .NET 4.0 application pool (IIS 7.0 or IIS 7.5) or your web site is configured as an ASP.NET 4.0 web site (IIS 6.0) and would like to show your clients an “under maintenance” page for a limited time, the easiest way is to put a file named app_offline.htm in your web site’s root folder. Then your application will be taking offline and the clients will be served with app_offline.htm page whatever their requests are.</span></div>
<div class="ArticleContent" style="color: #333333; font-family: Verdana, Helvetica; font-size: 10pt; font-stretch: normal; line-height: 20px;">
<span style="background-color: white; font-family: "verdana" , "geneva"; font-size: 14px;"><br /></span></div>
</div>
<b>2. Deploy application with debug="false"</b><br />
<b><br /></b>
<span style="color: #333333; font-family: "verdana" , "helvetica"; font-size: 10pt;">When we develop asp.net application using Visual Studio, the default value for debug attribute is true. This setting will help developers to debug the application in development environment. For example, executing the application in this mode will not cache the resource files rendered by WebResources.axd handler. This prevents the need to clear the temporary cache every time when the developer needs to check the changes done. There will be other useful things done for developers for debugging like debug symbols, settings that will enable breakpoints etc. These setting will give a poor performance in production if released in the default debug mode (false).</span><br />
<div class="ArticleContent" style="color: #333333; font-family: Verdana, Helvetica; font-size: 10pt; font-stretch: normal; line-height: 20px;">
So, never release your website with debug mode set to true. It should be set to false in web.config when moving to production.</div>
<div class="ArticleContent" style="color: #333333; font-family: Verdana, Helvetica; font-size: 10pt; font-stretch: normal; line-height: 20px;">
<span style="background-color: #fdffe8; color: maroon; font-family: "courier new"; font-size: 12px;"><compilation debug=”false”/></span></div>
<div class="ArticleContent" style="color: #333333; font-family: Verdana, Helvetica; font-size: 10pt; font-stretch: normal; line-height: 20px;">
Debug mode to 'true' results following disadvantage.</div>
<div class="ArticleContent" style="color: #333333; font-stretch: normal; line-height: 20px; margin-left: 36pt; text-indent: -18pt;">
<span style="font-size: 7pt; font-stretch: normal; line-height: normal;"> </span><span style="font-family: "verdana" , "helvetica";"><span style="font-size: 10pt;">Code execution will be slow.</span></span></div>
<div class="ArticleContent" style="color: #333333; font-family: Verdana, Helvetica; font-size: 10pt; font-stretch: normal; line-height: 20px; margin-left: 36pt; text-indent: -18pt;">
<span style="font-family: "wingdings";"><span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal; line-height: normal;"> </span></span>Compilation will be slow since batch compilation is disabled.</div>
<div class="ArticleContent" style="color: #333333; font-family: Verdana, Helvetica; font-size: 10pt; font-stretch: normal; line-height: 20px; margin-left: 36pt; text-indent: -18pt;">
<span style="font-family: "wingdings";"><span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal; line-height: normal;"> </span></span>Memory consumption is higher since there are additional debug symbols, etc.</div>
<div class="ArticleContent" style="color: #333333; font-family: Verdana, Helvetica; font-size: 10pt; font-stretch: normal; line-height: 20px; margin-left: 36pt; text-indent: -18pt;">
<span style="font-family: "wingdings";"><span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal; line-height: normal;"> </span></span>Resources downloaded with webresources.axd will not be cached.</div>
<div class="ArticleContent" style="color: #333333; font-family: Verdana, Helvetica; font-size: 10pt; font-stretch: normal; line-height: 20px;">
<br /></div>
<div class="ArticleContent" style="color: #333333; font-family: Verdana, Helvetica; font-size: 10pt; font-stretch: normal; line-height: 20px;">
<span style="font-size: 13.3333px;">Alternate will be </span><b style="font-size: 13.3333px;"><span lang="EN"><deployment retail=”true”/> </span></b><span lang="EN" style="font-size: 13.3333px;">in machine.config. If you are a server administrator, make this change in machine.config so that it will enforce the debug attribute in the application’s web.config to false. It also disables the page output tracing and the ability to show the detailed exception report to the remote users when there is an exception.</span></div>
<div class="ArticleContent" style="color: #333333; font-family: Verdana, Helvetica; font-size: 10pt; font-stretch: normal; line-height: 20px;">
<span lang="EN" style="font-size: 13.3333px;"><br /></span></div>
<b>3. Configure Custom Error Page in Web.Config file</b><br />
<div>
<b><br /></b>
<br />
<div>
In web.config use code.</div>
<div>
<div class="ArticleCode" style="background-color: #fdffe8; color: maroon; font-family: "Courier New"; font-size: 12px;">
<customErrors defaultRedirect="url"</div>
<div class="ArticleCode" style="background-color: #fdffe8; color: maroon; font-family: "Courier New"; font-size: 12px;">
mode="On|Off|RemoteOnly"></div>
<div class="ArticleCode" style="background-color: #fdffe8; color: maroon; font-family: "Courier New"; font-size: 12px;">
<error. . ./></div>
<div class="ArticleCode" style="background-color: #fdffe8; color: maroon; font-family: "Courier New"; font-size: 12px;">
</customErrors></div>
<br />
<br />
<b>ON </b>- Specifies that custom errors are enabled. If no defaultRedirect attribute is specified, users see a generic error. The custom errors are shown to the remote clients and to the local host.<br />
<br />
<b>OFF </b>- Specifies that custom errors are disabled. The detailed ASP.NET errors are shown to the remote clients and to the local host.<br />
<br />
<b>REMOTEONLY</b> - Specifies that custom errors are shown only to the remote clients, and that ASP.NET errors are shown to the local host. This is the default value.<br />
<div class="ArticleContent" style="color: #333333; font-family: Verdana, Helvetica; font-size: 10pt; font-stretch: normal; line-height: 20px;">
<br /></div>
<b>4. Separate Application pool For each Application</b></div>
<div>
<b><br /></b></div>
<div>
<span style="background-color: white; color: #404040; font-family: "droid sans" , "arial" , sans-serif;">Application pool is the container of worker process. Application pools is used to separate sets of IIS worker processes that share the same configuration. Application pools enables a better </span><em style="border: 0px; color: #404040; font-family: "Droid Sans", Arial, sans-serif; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">security, reliability, and availability</em><span style="background-color: white; color: #404040; font-family: "droid sans" , "arial" , sans-serif;"> for any web application. The worker process serves as the process boundary that separates each application pool so that when one worker process or application is having an issue or recycles, other applications or worker processes are not affected.</span></div>
<div>
<span style="color: #404040; font-family: "droid sans" , "arial" , sans-serif;"><br /></span></div>
<div>
<b>5. Custom Service Account for ASP.NET app pools</b></div>
<div>
<b><br /></b></div>
<div>
<div style="color: #2a2a2a; font-family: "Segoe UI", "Lucida Grande", Verdana, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 18px; padding-bottom: 15px;">
A custom account is useful in the following situations:</div>
<ul style="font-family: "Segoe UI", "Lucida Grande", Verdana, Arial, Helvetica, sans-serif; font-size: 13px;">
<li class="unordered" style="list-style-image: none;">When you want to improve security and make it easier to trace security events to the corresponding application.</li>
<li class="unordered" style="list-style-image: none;">When you are hosting Web sites for multiple customers on a single Web server. If you use the same process account for multiple customers, source code from one customer's application may be able to access source code from another customer's application. In this case, you should also configure a custom account for the anonymous user account.</li>
<li class="unordered" style="list-style-image: none;">When an application requires rights or permissions in addition to the default permissions for an application pool. In this case, you can create an application pool and assign a custom identity to the new application pool.</li>
<li class="unordered" style="list-style-image: none;">For More <a href="https://technet.microsoft.com/en-us/library/cc771170(v=ws.10).aspx" target="_blank">Identity for an Application Pool</a></li>
</ul>
<div>
<span style="font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif;"><span style="font-size: 13px;">How to Set</span></span></div>
<div>
<ol class="ordered" style="font-family: "Segoe UI", "Lucida Grande", Verdana, Arial, Helvetica, sans-serif; font-size: 13px;">
<li><div style="color: #2a2a2a; line-height: 18px; padding-bottom: 15px;">
Open IIS Manager. For information about opening IIS Manager, see <a href="https://technet.microsoft.com/en-us/library/cc770472(v=ws.10).aspx" style="color: #00709f; text-decoration: none;">Open IIS Manager (IIS 7)</a>.</div>
</li>
<li><div style="color: #2a2a2a; line-height: 18px; padding-bottom: 15px;">
In the <strong>Connections</strong> pane, expand the server node and click <strong>Application Pools</strong>.</div>
</li>
<li><div style="color: #2a2a2a; line-height: 18px; padding-bottom: 15px;">
On the <strong>Application Pools</strong> page, select the application pool for which you want to specify an identity, and then click <strong>Advanced Settings</strong> in the <strong>Actions</strong> pane.</div>
</li>
<li><div style="color: #2a2a2a; line-height: 18px; padding-bottom: 15px;">
For the <strong>Identity</strong> property, click the <strong>...</strong> button to open the <strong>Application Pool Identity</strong> dialog box.</div>
</li>
<li><div style="color: #2a2a2a; line-height: 18px; padding-bottom: 15px;">
If you want to use a built-in account, select the <strong>Built-in account</strong> option and select an account from the list.</div>
</li>
<li><div style="color: #2a2a2a; line-height: 18px; padding-bottom: 15px;">
If you want to use a custom identity, select the <strong>Custom account</strong> option and click <strong>Set</strong> to open the <strong>Set Credentials</strong> dialog box. Then type the custom account name in the <strong>User name</strong> text box, type a password in the <strong>Password</strong> text box, retype the password in the <strong>Confirm password</strong> text box, and then click <strong>OK</strong>.</div>
</li>
<li><div style="color: #2a2a2a; line-height: 18px; padding-bottom: 15px;">
Click <strong>OK</strong> to dismiss the <strong>Application Pool Identity</strong> dialog box.</div>
</li>
</ol>
</div>
<br />
<b>6. Encrypt the Sensitive data in Web.Config file.</b></div>
<div>
<b><br /></b></div>
<div>
For more click <a href="https://www.asp.net/identity/overview/features-api/best-practices-for-deploying-passwords-and-other-sensitive-data-to-aspnet-and-azure" target="_blank">Encrypt sensitive data</a></div>
<div>
<br /></div>
<b>7. Deploying a Web Application Project Using a Web Deployment Package</b></div>
<div>
<b><br /></b></div>
<div>
For more <a href="https://msdn.microsoft.com/en-us/library/dd483479(v=vs.100).aspx" target="_blank">Web Deployment Package</a> and <a href="https://msdn.microsoft.com/en-us/library/dd465318(v=vs.100).aspx" target="_blank">Transform Web.config</a></div>
<div>
<br /></div>
<div>
<b>8. For Intranet applications use Windows Authentication to connect to database</b></div>
<div>
<b><br /></b>
<br />
<div class="ArticleContent" style="color: #333333; font-family: Verdana, Helvetica; font-size: 10pt; font-stretch: normal; line-height: 20px;">
If your application is hosted in an intranet domain, then use windows authentication to connect to the database. The advantage of this approach, you can use the same windows service account configured to run your app pool in IIS 6.0 to connect to the database. This prevents the need to store the password as a clear text in web.config.</div>
<div>
<div class="ArticleContent" style="color: #333333; font-family: Verdana, Helvetica; font-size: 10pt; font-stretch: normal; line-height: 20px;">
<br /></div>
</div>
</div>
</div>
Bhanu Prataphttp://www.blogger.com/profile/17529341083467033319noreply@blogger.com1tag:blogger.com,1999:blog-45468370605306946.post-56417649938102070392016-12-10T05:44:00.000-08:002016-12-10T05:44:17.017-08:00What is CSRF attack and how can we prevent the same in MVC?<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; color: #505050; font-family: "Segoe UI", Tahoma, Arial, Helvetica, sans-serif; font-size: 13.008px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;">
Cross-Site Request Forgery (CSRF) is an attack where a malicious site sends a request to a vulnerable site where the user is currently logged in</div>
<div style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; color: #505050; font-family: "Segoe UI", Tahoma, Arial, Helvetica, sans-serif; font-size: 13.008px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;">
Here is an example of a CSRF attack:</div>
<ol style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; color: #505050; font-family: "Segoe UI", Tahoma, Arial, Helvetica, sans-serif; font-size: 13.008px; list-style-image: initial; list-style-position: initial; margin: 0px 0px 18px 30px; outline: 0px; padding: 0px; vertical-align: baseline;">
<li style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; font-size: 13.008px; margin: 0px 0px 5px; outline: 0px; padding: 0px; vertical-align: baseline;">A user logs into www.example.com, using forms authentication.</li>
<li style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; font-size: 13.008px; margin: 0px 0px 5px; outline: 0px; padding: 0px; vertical-align: baseline;">The server authenticates the user. The response from the server includes an authentication cookie.</li>
<li style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; font-size: 13.008px; margin: 0px 0px 5px; outline: 0px; padding: 0px; vertical-align: baseline;">Without logging out, the user visits a malicious web site. This malicious site contains the following HTML form:</li>
</ol>
<div>
<pre class="prettyprint lang-html prettyprinted" style="background: rgb(239, 239, 239); border: 1px solid rgb(210, 210, 210); color: #505050; font-family: Consolas, monospace; font-size: 13.008px; margin-bottom: 30px; margin-top: 18px; outline: 0px; overflow: auto; padding: 5px; vertical-align: baseline;"><span class="tag" style="background: transparent; border: 0px; color: #a31515; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"><h1></span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">You Are a Winner!</span><span class="tag" style="background: transparent; border: 0px; color: #a31515; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"></h1></span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="tag" style="background: transparent; border: 0px; color: #a31515; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"><form</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="atn" style="background: transparent; border: 0px; color: red; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">action</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="atv" style="background: transparent; border: 0px; color: blue; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">"http://example.com/api/account"</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="atn" style="background: transparent; border: 0px; color: red; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">method</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="atv" style="background: transparent; border: 0px; color: blue; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">"post"</span><span class="tag" style="background: transparent; border: 0px; color: #a31515; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">></span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="tag" style="background: transparent; border: 0px; color: #a31515; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"><input</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="atn" style="background: transparent; border: 0px; color: red; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">type</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="atv" style="background: transparent; border: 0px; color: blue; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">"hidden"</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="atn" style="background: transparent; border: 0px; color: red; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">name</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="atv" style="background: transparent; border: 0px; color: blue; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">"Transaction"</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="atn" style="background: transparent; border: 0px; color: red; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">value</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="atv" style="background: transparent; border: 0px; color: blue; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">"withdraw"</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="tag" style="background: transparent; border: 0px; color: #a31515; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">/></span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="tag" style="background: transparent; border: 0px; color: #a31515; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"><input</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="atn" style="background: transparent; border: 0px; color: red; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">type</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="atv" style="background: transparent; border: 0px; color: blue; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">"hidden"</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="atn" style="background: transparent; border: 0px; color: red; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">name</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="atv" style="background: transparent; border: 0px; color: blue; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">"Amount"</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="atn" style="background: transparent; border: 0px; color: red; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">value</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="atv" style="background: transparent; border: 0px; color: blue; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">"1000000"</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="tag" style="background: transparent; border: 0px; color: #a31515; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">/></span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="tag" style="background: transparent; border: 0px; color: #a31515; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"><input</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="atn" style="background: transparent; border: 0px; color: red; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">type</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="atv" style="background: transparent; border: 0px; color: blue; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">"submit"</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="atn" style="background: transparent; border: 0px; color: red; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">value</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="atv" style="background: transparent; border: 0px; color: blue; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">"Click Me"</span><span class="tag" style="background: transparent; border: 0px; color: #a31515; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">/></span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="tag" style="background: transparent; border: 0px; color: #a31515; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"></form></span></pre>
</div>
<div>
<ol style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; color: #505050; font-family: "Segoe UI", Tahoma, Arial, Helvetica, sans-serif; font-size: 13.008px; list-style-image: initial; list-style-position: initial; margin: 0px 0px 18px 30px; outline: 0px; padding: 0px; vertical-align: baseline;">
<li style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; font-size: 13.008px; margin: 0px 0px 5px; outline: 0px; padding: 0px; vertical-align: baseline;"><div style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; font-size: 13.008px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;">
Notice that the form action posts to the vulnerable site, not to the malicious site. This is the “cross-site” part of CSRF.</div>
</li>
<li style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; font-size: 13.008px; margin: 0px 0px 5px; outline: 0px; padding: 0px; vertical-align: baseline;">The user clicks the submit button. The browser includes the authentication cookie with the request.</li>
<li style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; font-size: 13.008px; margin: 0px 0px 5px; outline: 0px; padding: 0px; vertical-align: baseline;">The request runs on the server with the user’s authentication context, and can do anything that an authenticated user is allowed to do.</li>
</ol>
<div style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; color: #505050; font-family: "Segoe UI", Tahoma, Arial, Helvetica, sans-serif; font-size: 13.008px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;">
Although this example requires the user to click the form button, the malicious page could just as easily run a script that sends an AJAX request. Moreover, using SSL does not prevent a CSRF attack, because the malicious site can send an “https://” request.</div>
<div style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; color: #505050; font-family: "Segoe UI", Tahoma, Arial, Helvetica, sans-serif; font-size: 13.008px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;">
Typically, CSRF attacks are possible against web sites that use cookies for authentication, because browsers send all relevant cookies to the destination web site. However, CSRF attacks are not limited to exploiting cookies. For example, Basic and Digest authentication are also vulnerable. After a user logs in with Basic or Digest authentication. the browser automatically sends the credentials until the session ends.</div>
<h2 style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; clear: both; color: #222222; font-family: "Segoe UI Light", "Segoe UI", Tahoma, Arial, Helvetica, sans-serif; font-size: 24px; font-weight: normal; line-height: 1.4em; margin: 30px 0px 13px; outline: 0px; padding: 0px; vertical-align: baseline;">
Anti-Forgery Tokens</h2>
<div style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; color: #505050; font-family: "Segoe UI", Tahoma, Arial, Helvetica, sans-serif; font-size: 13.008px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;">
To help prevent CSRF attacks, ASP.NET MVC uses anti-forgery tokens, also called <em style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">request verification tokens</em>.</div>
<ol style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; color: #505050; font-family: "Segoe UI", Tahoma, Arial, Helvetica, sans-serif; font-size: 13.008px; list-style-image: initial; list-style-position: initial; margin: 0px 0px 18px 30px; outline: 0px; padding: 0px; vertical-align: baseline;">
<li style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; font-size: 13.008px; margin: 0px 0px 5px; outline: 0px; padding: 0px; vertical-align: baseline;">The client requests an HTML page that contains a form.</li>
<li style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; font-size: 13.008px; margin: 0px 0px 5px; outline: 0px; padding: 0px; vertical-align: baseline;">The server includes two tokens in the response. One token is sent as a cookie. The other is placed in a hidden form field. The tokens are generated randomly so that an adversary cannot guess the values.</li>
<li style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; font-size: 13.008px; margin: 0px 0px 5px; outline: 0px; padding: 0px; vertical-align: baseline;">When the client submits the form, it must send both tokens back to the server. The client sends the cookie token as a cookie, and it sends the form token inside the form data. (A browser client automatically does this when the user submits the form.)</li>
<li style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; font-size: 13.008px; margin: 0px 0px 5px; outline: 0px; padding: 0px; vertical-align: baseline;">If a request does not include both tokens, the server disallows the request.</li>
</ol>
<div style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; color: #505050; font-family: "Segoe UI", Tahoma, Arial, Helvetica, sans-serif; font-size: 13.008px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;">
Here is an example of an HTML form with a hidden form token:</div>
<pre class="prettyprint prettyprinted" style="background: rgb(239, 239, 239); border: 1px solid rgb(210, 210, 210); color: #505050; font-family: Consolas, monospace; font-size: 13.008px; margin-bottom: 30px; margin-top: 18px; outline: 0px; overflow: auto; padding: 5px; vertical-align: baseline;"><span class="tag" style="background: transparent; border: 0px; color: #a31515; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"><form</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="atn" style="background: transparent; border: 0px; color: red; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">action</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="atv" style="background: transparent; border: 0px; color: blue; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">"/Home/Test"</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="atn" style="background: transparent; border: 0px; color: red; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">method</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="atv" style="background: transparent; border: 0px; color: blue; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">"post"</span><span class="tag" style="background: transparent; border: 0px; color: #a31515; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">></span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="tag" style="background: transparent; border: 0px; color: #a31515; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"><input</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="atn" style="background: transparent; border: 0px; color: red; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">name</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="atv" style="background: transparent; border: 0px; color: blue; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">"__RequestVerificationToken"</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="atn" style="background: transparent; border: 0px; color: red; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">type</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="atv" style="background: transparent; border: 0px; color: blue; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">"hidden"</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="atn" style="background: transparent; border: 0px; color: red; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">value</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="atv" style="background: transparent; border: 0px; color: blue; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">"6fGBtLZmVBZ59oUad1Fr33BuPxANKY9q3Srr5y[...]"</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="tag" style="background: transparent; border: 0px; color: #a31515; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">/></span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="tag" style="background: transparent; border: 0px; color: #a31515; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"><input</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="atn" style="background: transparent; border: 0px; color: red; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">type</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="atv" style="background: transparent; border: 0px; color: blue; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">"submit"</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="atn" style="background: transparent; border: 0px; color: red; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">value</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="atv" style="background: transparent; border: 0px; color: blue; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">"Submit"</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="tag" style="background: transparent; border: 0px; color: #a31515; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">/></span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="tag" style="background: transparent; border: 0px; color: #a31515; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"></form></span></pre>
<div style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; color: #505050; font-family: "Segoe UI", Tahoma, Arial, Helvetica, sans-serif; font-size: 13.008px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;">
Anti-forgery tokens work because the malicious page cannot read the user’s tokens, due to same-origin policies. (<a href="http://www.w3.org/Security/wiki/Same_Origin_Policy" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; color: #2b59a9; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; text-decoration: none; vertical-align: baseline;">Same-orgin policies</a> prevent documents hosted on two different sites from accessing each other’s content. So in the earlier example, the malicious page can send requests to example.com, but it cannot read the response.)</div>
<div style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; color: #505050; font-family: "Segoe UI", Tahoma, Arial, Helvetica, sans-serif; font-size: 13.008px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;">
To prevent CSRF attacks, use anti-forgery tokens with any authentication protocol where the browser silently sends credentials after the user logs in. This includes cookie-based authentication protocols, such as forms authentication, as well as protocols such as Basic and Digest authentication.</div>
<div style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; color: #505050; font-family: "Segoe UI", Tahoma, Arial, Helvetica, sans-serif; font-size: 13.008px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;">
You should require anti-forgery tokens for any nonsafe methods (POST, PUT, DELETE). Also, make sure that safe methods (GET, HEAD) do not have any side effects. Moreover, if you enable cross-domain support, such as CORS or JSONP, then even safe methods like GET are potentially vulnerable to CSRF attacks, allowing the attacker to read potentially sensitive data.</div>
<h2 style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; clear: both; color: #222222; font-family: "Segoe UI Light", "Segoe UI", Tahoma, Arial, Helvetica, sans-serif; font-size: 24px; font-weight: normal; line-height: 1.4em; margin: 30px 0px 13px; outline: 0px; padding: 0px; vertical-align: baseline;">
Anti-Forgery Tokens in ASP.NET MVC</h2>
<div style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; color: #505050; font-family: "Segoe UI", Tahoma, Arial, Helvetica, sans-serif; font-size: 13.008px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;">
To add the anti-forgery tokens to a Razor page, use the <strong style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">HtmlHelper.AntiForgeryToken</strong> helper method:</div>
<pre class="prettyprint prettyprinted" style="background: rgb(239, 239, 239); border: 1px solid rgb(210, 210, 210); color: #505050; font-family: Consolas, monospace; font-size: 13.008px; margin-bottom: 30px; margin-top: 18px; outline: 0px; overflow: auto; padding: 5px; vertical-align: baseline;"><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">@using (Html.BeginForm("Manage", "Account")) {
@Html.AntiForgeryToken()
}</span></pre>
<div style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; color: #505050; font-family: "Segoe UI", Tahoma, Arial, Helvetica, sans-serif; font-size: 13.008px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;">
This method adds the hidden form field and also sets the cookie token.</div>
<h2 style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; clear: both; color: #222222; font-family: "Segoe UI Light", "Segoe UI", Tahoma, Arial, Helvetica, sans-serif; font-size: 24px; font-weight: normal; line-height: 1.4em; margin: 30px 0px 13px; outline: 0px; padding: 0px; vertical-align: baseline;">
Anti-CSRF and AJAX</h2>
<div style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; color: #505050; font-family: "Segoe UI", Tahoma, Arial, Helvetica, sans-serif; font-size: 13.008px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;">
The form token can be a problem for AJAX requests, because an AJAX request might send JSON data, not HTML form data. One solution is to send the tokens in a custom HTTP header. The following code uses Razor syntax to generate the tokens, and then adds the tokens to an AJAX request. The tokens are generated at the server by calling <strong style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">AntiForgery.GetTokens</strong>.</div>
<pre class="prettyprint prettyprinted" style="background: rgb(239, 239, 239); border: 1px solid rgb(210, 210, 210); color: #505050; font-family: Consolas, monospace; font-size: 13.008px; margin-bottom: 30px; margin-top: 18px; outline: 0px; overflow: auto; padding: 5px; vertical-align: baseline;"><span class="tag" style="background: transparent; border: 0px; color: #a31515; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"><script></span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="lit" style="background: transparent; border: 0px; color: red; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">@functions</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">{</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="kwd" style="background: transparent; border: 0px; color: blue; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">public</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> string </span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">TokenHeaderValue</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">()</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">{</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
string cookieToken</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> formToken</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">;</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">AntiForgery</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">GetTokens</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="kwd" style="background: transparent; border: 0px; color: blue; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">null</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> out cookieToken</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> out formToken</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">);</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="kwd" style="background: transparent; border: 0px; color: blue; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">return</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> cookieToken </span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">+</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="str" style="background: transparent; border: 0px; color: #a31515; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">":"</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">+</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> formToken</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">;</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">}</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">}</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
$</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">ajax</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="str" style="background: transparent; border: 0px; color: #a31515; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">"api/values"</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">{</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
type</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">:</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="str" style="background: transparent; border: 0px; color: #a31515; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">"post"</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
contentType</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">:</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="str" style="background: transparent; border: 0px; color: #a31515; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">"application/json"</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
data</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">:</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">{</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">},</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="com" style="background: transparent; border: 0px; color: green; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">// JSON data goes here</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
dataType</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">:</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="str" style="background: transparent; border: 0px; color: #a31515; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">"json"</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
headers</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">:</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">{</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="str" style="background: transparent; border: 0px; color: #a31515; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">'RequestVerificationToken'</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">:</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="str" style="background: transparent; border: 0px; color: #a31515; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">'@TokenHeaderValue()'</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">}</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">});</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="tag" style="background: transparent; border: 0px; color: #a31515; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"></script></span></pre>
<div style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; color: #505050; font-family: "Segoe UI", Tahoma, Arial, Helvetica, sans-serif; font-size: 13.008px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;">
When you process the request, extract the tokens from the request header. Then call the <strong style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">AntiForgery.Validate</strong> method to validate the tokens. The <strong style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Validate</strong> method throws an exception if the tokens are not valid.</div>
<pre class="prettyprint prettyprinted" style="background: rgb(239, 239, 239); border: 1px solid rgb(210, 210, 210); color: #505050; font-family: Consolas, monospace; font-size: 13.008px; margin-bottom: 30px; margin-top: 18px; outline: 0px; overflow: auto; padding: 5px; vertical-align: baseline;"><span class="kwd" style="background: transparent; border: 0px; color: blue; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">void</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">ValidateRequestHeader</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">HttpRequestMessage</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> request</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">)</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">{</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="kwd" style="background: transparent; border: 0px; color: blue; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">string</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> cookieToken </span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="str" style="background: transparent; border: 0px; color: #a31515; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">""</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">;</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="kwd" style="background: transparent; border: 0px; color: blue; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">string</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> formToken </span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="str" style="background: transparent; border: 0px; color: #a31515; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">""</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">;</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">IEnumerable</span><span class="str" style="background: transparent; border: 0px; color: #a31515; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"><string></span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> tokenHeaders</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">;</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="kwd" style="background: transparent; border: 0px; color: blue; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">if</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">request</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Headers</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">TryGetValues</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="str" style="background: transparent; border: 0px; color: #a31515; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">"RequestVerificationToken"</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="background: transparent; border: 0px; color: blue; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">out</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> tokenHeaders</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">))</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">{</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="kwd" style="background: transparent; border: 0px; color: blue; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">string</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">[]</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> tokens </span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> tokenHeaders</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">First</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">().</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Split</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="str" style="background: transparent; border: 0px; color: #a31515; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">':'</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">);</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="kwd" style="background: transparent; border: 0px; color: blue; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">if</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">tokens</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Length</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">==</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="lit" style="background: transparent; border: 0px; color: red; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">2</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">)</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">{</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
cookieToken </span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> tokens</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">[</span><span class="lit" style="background: transparent; border: 0px; color: red; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">0</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">].</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Trim</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">();</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
formToken </span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> tokens</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">[</span><span class="lit" style="background: transparent; border: 0px; color: red; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">1</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">].</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Trim</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">();</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">}</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">}</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">AntiForgery</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Validate</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">cookieToken</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> formToken</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">);</span><span class="pln" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="background: transparent; border: 0px; color: black; font-size: 13.008px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">}</span></pre>
<div style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; color: #505050; font-family: "Segoe UI", Tahoma, Arial, Helvetica, sans-serif; font-size: 13.008px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;">
<span style="background-color: white; color: #111111; font-family: "Segoe UI", Arial, sans-serif; font-size: 14px;">The below code illustrates how </span><strong style="background-color: white; border: 0px; color: #111111; font-family: "Segoe UI", Arial, sans-serif; font-size: 14px; margin: 0px; padding: 0px;">Anti-Forgery Token </strong><span style="background-color: white; color: #111111; font-family: "Segoe UI", Arial, sans-serif; font-size: 14px;">Cross Site Request Forgery:</span></div>
<div style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; color: #505050; font-family: "Segoe UI", Tahoma, Arial, Helvetica, sans-serif; font-size: 13.008px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;">
<span style="background-color: white; color: #111111; font-family: "Segoe UI", Arial, sans-serif; font-size: 14px;"><b>Controller</b></span></div>
<pre class="notranslate" id="pre688465" lang="cs" style="background-color: #fbedbb; border: 1px solid rgb(251, 237, 187); font-family: Consolas, "Courier New", Courier, mono; font-size: 9pt; font-stretch: normal; line-height: normal; overflow: auto; padding: 6px; tab-size: 4; white-space: pre-wrap; word-break: break-word; word-wrap: break-word;"><strong style="border: 0px; margin: 0px; padding: 0px;">[ValidateAntiForgeryToken] </strong>
<span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">public</span> <span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">class</span> UserController : Controller
{
<span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">public</span> ActionResult DeleteUser()
{
<span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">var</span> userId = (<span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">int</span>)Session[<span class="code-string" style="border: 0px; color: purple; margin: 0px; padding: 0px;">"</span><span class="code-string" style="border: 0px; color: purple; margin: 0px; padding: 0px;">userId"</span>];
DeleteUserFromDb(userId);<span class="code-comment" style="border: 0px; color: green; font-style: italic; margin: 0px; padding: 0px;">//</span><span class="code-comment" style="border: 0px; color: green; font-style: italic; margin: 0px; padding: 0px;">Function for deleting the user from Database
</span> <span class="code-keyword" style="border: 0px; color: blue; margin: 0px; padding: 0px;">return</span> View();
}
}</pre>
<div style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; color: #505050; font-family: "Segoe UI", Tahoma, Arial, Helvetica, sans-serif; font-size: 13.008px; margin-bottom: 18px; outline: 0px; padding: 0px; vertical-align: baseline;">
<span style="background-color: white; color: #111111; font-family: "Segoe UI", Arial, sans-serif; font-size: 14px;"><b>View</b></span></div>
<pre class="notranslate" id="pre682966" lang="cs" style="background-color: #fbedbb; border: 1px solid rgb(251, 237, 187); font-family: Consolas, "Courier New", Courier, mono; font-size: 9pt; font-stretch: normal; line-height: normal; overflow: auto; padding: 6px; tab-size: 4; white-space: pre-wrap; word-break: break-word; word-wrap: break-word;">@using (Html.BeginForm(<span class="code-string" style="border: 0px; color: purple; margin: 0px; padding: 0px;">"</span><span class="code-string" style="border: 0px; color: purple; margin: 0px; padding: 0px;">DeleteUser"</span>, <span class="code-string" style="border: 0px; color: purple; margin: 0px; padding: 0px;">"</span><span class="code-string" style="border: 0px; color: purple; margin: 0px; padding: 0px;">User"</span>))
{
<strong style="border: 0px; margin: 0px; padding: 0px;">@Html.AntiForgeryToken()</strong>
<input type=<span class="code-string" style="border: 0px; color: purple; margin: 0px; padding: 0px;">"</span><span class="code-string" style="border: 0px; color: purple; margin: 0px; padding: 0px;">submit"</span> <span class="code-sdkkeyword" style="border: 0px; color: #339999; margin: 0px; padding: 0px;">value</span>=<span class="code-string" style="border: 0px; color: purple; margin: 0px; padding: 0px;">"</span><span class="code-string" style="border: 0px; color: purple; margin: 0px; padding: 0px;">Delete My Account"</span> />
} </pre>
</div>
</div>
Bhanu Prataphttp://www.blogger.com/profile/17529341083467033319noreply@blogger.com0tag:blogger.com,1999:blog-45468370605306946.post-24815137886442975722016-12-09T21:11:00.001-08:002016-12-09T23:28:35.508-08:00Collection was modified after the enumerator was instantiated.<div dir="ltr" style="text-align: left;" trbidi="on">
IF you are using <b>Foreach </b>loop for modifying collection then you will get this error as below.<br />
<br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">List</span><span style="font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">string</span><span style="font-family: Consolas; font-size: 9.5pt;">> li = </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">new</span><span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">List</span><span style="font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">string</span><span style="font-family: Consolas; font-size: 9.5pt;">>();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> li.Add(</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">"bhanu"</span><span style="font-family: Consolas; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> li.Add(</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">"test"</span><span style="font-family: Consolas; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">foreach</span><span style="font-family: Consolas; font-size: 9.5pt;"> (</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">string</span><span style="font-family: Consolas; font-size: 9.5pt;"> s </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">in</span><span style="font-family: Consolas; font-size: 9.5pt;"> li)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> li.Remove(s);<o:p></o:p></span></div>
<br />
<div class="MsoNormal">
<span style="font-family: Consolas; font-size: 9.5pt; line-height: 115%;"> }</span><o:p></o:p></div>
<div class="MsoNormal">
<span style="font-family: Consolas; font-size: 9.5pt; line-height: 115%;"><br /></span></div>
<b>Solution - </b>use <b>For </b>Loop as below.<div>
<br /></div>
<div>
<br /></div>
<div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">for</span><span style="font-family: Consolas; font-size: 9.5pt;"> (</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">int</span><span style="font-family: Consolas; font-size: 9.5pt;"> i = 0; i < li.Count; i++)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> li.RemoveAt(i);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> i--;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }</span></div>
</div>
</div>
Bhanu Prataphttp://www.blogger.com/profile/17529341083467033319noreply@blogger.com0tag:blogger.com,1999:blog-45468370605306946.post-20342558356335732702016-12-07T07:00:00.002-08:002016-12-07T20:17:37.201-08:00Asynchronous Programming with async and await<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="line-height: 18px;">
<span style="color: #2a2a2a; font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif;">Asynchrony is essential for activities that are potentially blocking, such as when your application accesses the web. Access to a web resource sometimes is slow or delayed. If such an activity is blocked within a synchronous process, the entire application must wait. In an asynchronous process, the application can continue with other work that doesn't depend on the web resource until the potentially blocking task finishes.</span><br />
<span style="color: #2a2a2a; font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif;">There are following areas where asynchronous programming improves responsiveness.</span><br />
<div style="line-height: 18px;">
<table border="0" cellpadding="0" cellspacing="0" class="MsoNormalTable" style="border-collapse: collapse; mso-padding-alt: 0in 0in 0in 0in; mso-yfti-tbllook: 1184;">
<thead>
<tr>
<td style="background: #EDEDED; border: solid #BBBBBB 1.0pt; padding: 7.5pt 6.0pt 7.5pt 6.0pt;">
<div style="margin-bottom: .0001pt; margin: 0in;">
<b>Application area<o:p></o:p></b><o:p></o:p></div>
</td>
<td style="background: #EDEDED; border-left: none; border: solid #BBBBBB 1.0pt; mso-border-left-alt: solid #BBBBBB 1.0pt; padding: 7.5pt 6.0pt 7.5pt 6.0pt;">
<div style="margin-bottom: .0001pt; margin: 0in;">
<b>Supporting APIs that contain
async methods<o:p></o:p></b><o:p></o:p></div>
</td>
</tr>
</thead>
<tbody>
<tr>
<td style="border-top: none; border: solid #BBBBBB 1.0pt; mso-border-top-alt: solid #BBBBBB 1.0pt; padding: 7.5pt 6.0pt 7.5pt 6.0pt;" valign="top">
<div style="margin-bottom: .0001pt; margin: 0in;">
Web access<o:p></o:p></div>
</td>
<td style="border-bottom: solid #BBBBBB 1.0pt; border-left: none; border-right: solid #BBBBBB 1.0pt; border-top: none; padding: 7.5pt 6.0pt 7.5pt 6.0pt;" valign="top">
<div style="margin-bottom: .0001pt; margin: 0in;">
<a href="https://msdn.microsoft.com/en-us/library/system.net.http.httpclient.aspx">HttpClient</a>, <a href="http://go.microsoft.com/fwlink/p/?LinkId=259441">SyndicationClient</a><o:p></o:p></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid #BBBBBB 1.0pt; mso-border-top-alt: solid #BBBBBB 1.0pt; padding: 7.5pt 6.0pt 7.5pt 6.0pt;" valign="top">
<div style="margin-bottom: .0001pt; margin: 0in;">
Working with files<o:p></o:p></div>
</td>
<td style="border-bottom: solid #BBBBBB 1.0pt; border-left: none; border-right: solid #BBBBBB 1.0pt; border-top: none; padding: 7.5pt 6.0pt 7.5pt 6.0pt;" valign="top">
<div style="margin-bottom: .0001pt; margin: 0in;">
<a href="http://go.microsoft.com/fwlink/p/?LinkId=248220">StorageFile</a>, <a href="https://msdn.microsoft.com/en-us/library/system.io.streamwriter.aspx">StreamWriter</a>, <a href="https://msdn.microsoft.com/en-us/library/system.io.streamreader.aspx">StreamReader</a>, <a href="https://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx">XmlReader</a><o:p></o:p></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid #BBBBBB 1.0pt; mso-border-top-alt: solid #BBBBBB 1.0pt; padding: 7.5pt 6.0pt 7.5pt 6.0pt;" valign="top">
<div style="margin-bottom: .0001pt; margin: 0in;">
Working with images<o:p></o:p></div>
</td>
<td style="border-bottom: solid #BBBBBB 1.0pt; border-left: none; border-right: solid #BBBBBB 1.0pt; border-top: none; padding: 7.5pt 6.0pt 7.5pt 6.0pt;" valign="top">
<div style="margin-bottom: .0001pt; margin: 0in;">
<a href="http://go.microsoft.com/fwlink/p/?LinkId=261839">MediaCapture</a>, <a href="http://go.microsoft.com/fwlink/p/?LinkId=261840">BitmapEncoder</a>, <a href="http://go.microsoft.com/fwlink/p/?LinkId=261841">BitmapDecoder</a><o:p></o:p></div>
</td>
</tr>
<tr>
<td style="border-top: none; border: solid #BBBBBB 1.0pt; mso-border-top-alt: solid #BBBBBB 1.0pt; padding: 7.5pt 6.0pt 7.5pt 6.0pt;" valign="top">
<div style="margin-bottom: .0001pt; margin: 0in;">
WCF programming<o:p></o:p></div>
</td>
<td style="border-bottom: solid #BBBBBB 1.0pt; border-left: none; border-right: solid #BBBBBB 1.0pt; border-top: none; padding: 7.5pt 6.0pt 7.5pt 6.0pt;" valign="top">
<div style="margin-bottom: .0001pt; margin: 0in;">
<a href="http://go.microsoft.com/fwlink/p/?LinkID=192382">Synchronous and
Asynchronous Operations</a><o:p></o:p></div>
</td>
</tr>
</tbody></table>
</div>
<br />Asynchrony proves especially valuable for applications that access the UI thread because all UI-related activity usually shares one thread. If any process is blocked in a synchronous application, all are blocked. Your application stops responding, and you might conclude that it has failed when instead it's just waiting.<br />
When you use asynchronous methods, the application continues to respond to the UI. You can resize or minimize a window, for example, or you can close the application if you don't want to wait for it to finish.</div>
<div style="line-height: 18px;">
<span style="color: #2a2a2a; font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif;">The </span><a href="https://msdn.microsoft.com/en-us/library/hh156513.aspx" style="color: #00709f; font-family: "Segoe UI", "Lucida Grande", Verdana, Arial, Helvetica, sans-serif; text-decoration: none;">async</a><span style="color: #2a2a2a; font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif;"> and </span><a href="https://msdn.microsoft.com/en-us/library/hh156528.aspx" style="color: #00709f; font-family: "Segoe UI", "Lucida Grande", Verdana, Arial, Helvetica, sans-serif; text-decoration: none;">await</a><span style="color: #2a2a2a; font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif;"> keywords in C# are the heart of async programming. By using those two keywords, you can use resources in the .NET Framework or the Windows Runtime to create an asynchronous method almost as easily as you create a synchronous method. Asynchronous methods that you define by using </span><code style="color: #2a2a2a;">async</code><span style="color: #2a2a2a; font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif;"> and </span><code style="color: #2a2a2a;">await</code><span style="color: #2a2a2a; font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif;"> are referred to as async methods.</span><br />
<span style="color: #2a2a2a; font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif;"><br /></span>
<span style="color: #2a2a2a; font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif;"><b>Example1.</b></span><br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">class</span><span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">Program</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;">
</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">static</span><span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">void</span><span style="font-family: Consolas; font-size: 9.5pt;"> Main(</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">string</span><span style="font-family: Consolas; font-size: 9.5pt;">[] args)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">Program</span><span style="font-family: Consolas; font-size: 9.5pt;"> p=</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">new</span><span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">Program</span><span style="font-family: Consolas; font-size: 9.5pt;">();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> p.DispTask();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">Console</span><span style="font-family: Consolas; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">"hello"</span><span style="font-family: Consolas; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">Console</span><span style="font-family: Consolas; font-size: 9.5pt;">.ReadLine();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;">
</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">async</span><span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">void</span><span style="font-family: Consolas; font-size: 9.5pt;"> DispTask()<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">Console</span><span style="font-family: Consolas; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">"disp"</span><span style="font-family: Consolas; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">Task</span><span style="font-family: Consolas; font-size: 9.5pt;"> t = test();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: green; font-family: Consolas; font-size: 9.5pt;">// using await control will go to caller
method and here it will suspend . once task completed again control will be
here.</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">await</span><span style="font-family: Consolas; font-size: 9.5pt;"> t;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">Console</span><span style="font-family: Consolas; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">"disp after vali"</span><span style="font-family: Consolas; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;">
</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">async</span><span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">Task</span><span style="font-family: Consolas; font-size: 9.5pt;"> test()<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: green; font-family: Consolas; font-size: 9.5pt;">// using await control will go to caller
method (DispTask) and here it will suspend . once task completed again control
will be here.</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">await</span><span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">Task</span><span style="font-family: Consolas; font-size: 9.5pt;">.Delay(30000);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">Console</span><span style="font-family: Consolas; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">"test"</span><span style="font-family: Consolas; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<span style="color: #2a2a2a; font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif;">
</span><br />
<div class="MsoNormal">
<span style="font-family: Consolas; font-size: 9.5pt; line-height: 115%;"> }</span><o:p></o:p></div>
<div class="MsoNormal">
<span style="font-family: Consolas; font-size: 9.5pt; line-height: 115%;"><br /></span></div>
<div class="MsoNormal">
<span style="font-family: Consolas; font-size: 9.5pt; line-height: 115%;"><b>Example 2</b></span></div>
</div>
<div style="line-height: 18px;">
<pre style="font-family: Consolas, Courier, monospace !important; overflow: auto; padding: 5px; word-wrap: normal;"><span style="font-size: normal;"><span style="color: green;">// Three things to note in the signature: </span>
<span style="color: green;">// - The method has an async modifier. </span>
<span style="color: green;">// - The return type is Task or Task<T>. (See "Return Types" section.) </span>
<span style="color: green;">// Here, it is Task<int> because the return statement returns an integer. </span>
<span style="color: green;">// - The method name ends in "Async." </span>
<span style="color: blue;">async</span> Task<<span style="color: blue;">int</span>> AccessTheWebAsync()
{
<span style="color: green;">// You need to add a reference to System.Net.Http to declare client. </span>
HttpClient client = <span style="color: blue;">new</span> HttpClient();
<span style="color: green;">// GetStringAsync returns a Task<string>. That means that when you await the </span>
<span style="color: green;">// task you'll get a string (urlContents). </span>
Task<<span style="color: blue;">string</span>> getStringTask = client.GetStringAsync(<span style="color: #a31515;">"http://msdn.microsoft.com"</span>);
<span style="color: green;">// You can do work here that doesn't rely on the string from GetStringAsync. </span>
DoIndependentWork();
<span style="color: green;">// The await operator suspends AccessTheWebAsync. </span>
<span style="color: green;">// - AccessTheWebAsync can't continue until getStringTask is complete. </span>
<span style="color: green;">// - Meanwhile, control returns to the caller of AccessTheWebAsync. </span>
<span style="color: green;">// - Control resumes here when getStringTask is complete. </span>
<span style="color: green;">// - The await operator then retrieves the string result from getStringTask. </span>
<span style="color: blue;">string</span> urlContents = <span style="color: blue;">await</span> getStringTask;
<span style="color: green;">// The return statement specifies an integer result. </span>
<span style="color: green;">// Any methods that are awaiting AccessTheWebAsync retrieve the length value. </span>
<span style="color: blue;">return</span> urlContents.Length;
} </span></pre>
<pre style="font-family: Consolas, Courier, monospace !important; overflow: auto; padding: 5px; word-wrap: normal;">If <code style="color: #2a2a2a; white-space: normal;">AccessTheWebAsync</code><span style="color: #2a2a2a; font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif; white-space: normal;"> doesn't have any work that it can do between calling </span><code style="color: #2a2a2a; white-space: normal;">GetStringAsync</code><span style="color: #2a2a2a; font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif; white-space: normal;"> and awaiting its completion, you can simplify your code by calling and awaiting in the following single statement.</span></pre>
<pre style="overflow: auto; padding: 5px; word-wrap: normal;"><pre style="font-family: Consolas, Courier, monospace !important; overflow: auto; padding: 5px; word-wrap: normal;"><span style="color: blue;">string</span> urlContents = <span style="color: blue;">await</span> client.GetStringAsync(); </pre>
<pre style="overflow: auto; padding: 5px; word-wrap: normal;"><div style="color: #2a2a2a; font-family: "Segoe UI", "Lucida Grande", Verdana, Arial, Helvetica, sans-serif; line-height: 18px; padding-bottom: 15px; white-space: normal;">
The following characteristics summarize what makes the previous example an async method.</div>
<ul style="font-family: "Segoe UI", "Lucida Grande", Verdana, Arial, Helvetica, sans-serif; white-space: normal;">
<li style="list-style-image: none;"><div style="color: #2a2a2a; line-height: 18px; padding-bottom: 15px;">
The method signature includes an <code>async</code> modifier.</div>
</li>
<li style="list-style-image: none;"><div style="color: #2a2a2a; line-height: 18px; padding-bottom: 15px;">
The name of an async method, by convention, ends with an "Async" suffix.</div>
</li>
<li style="list-style-image: none;"><div style="color: #2a2a2a; line-height: 18px; padding-bottom: 15px;">
The return type is one of the following types:</div>
<ul>
<li style="list-style-image: none;"><div style="color: #2a2a2a; line-height: 18px; padding-bottom: 15px;">
<a href="https://msdn.microsoft.com/en-us/library/dd321424.aspx" style="color: #00709f; text-decoration: none;">Task<TResult></a> if your method has a return statement in which the operand has type TResult.</div>
</li>
<li style="list-style-image: none;"><div style="color: #2a2a2a; line-height: 18px; padding-bottom: 15px;">
<a href="https://msdn.microsoft.com/en-us/library/system.threading.tasks.task.aspx" style="color: #00709f; text-decoration: none;">Task</a> if your method has no return statement or has a return statement with no operand.</div>
</li>
<li style="list-style-image: none;"><div style="color: #2a2a2a; line-height: 18px; padding-bottom: 15px;">
<code>Void</code> if you're writing an async event handler.</div>
</li>
</ul>
</li>
<li style="list-style-image: none;"><div style="color: #2a2a2a; line-height: 18px; padding-bottom: 15px;">
The method usually includes at least one await expression, which marks a point where the method can't continue until the awaited asynchronous operation is complete. In the meantime, the method is suspended, and control returns to the method's caller. The next section of this topic illustrates what happens at the suspension point.</div>
</li>
</ul>
<div>
<div style="color: #2a2a2a; font-family: "Segoe UI", "Lucida Grande", Verdana, Arial, Helvetica, sans-serif; line-height: 18px; padding-bottom: 15px; white-space: normal;">
<a class="LW_CollapsibleArea_TitleAhref" href="https://www.blogger.com/null" role="heading" style="color: #0072c6; cursor: text; font-family: "Segoe UI Semibold", "Segoe UI", "Lucida Grande", Verdana, Arial, Helvetica, sans-serif; outline: none;" title=""><span class="LW_CollapsibleArea_Title" style="color: black; display: inline-block; line-height: 1.3em;">async and await</span></a></div>
<div style="color: #2a2a2a; font-family: "Segoe UI", "Lucida Grande", Verdana, Arial, Helvetica, sans-serif; line-height: 18px; padding-bottom: 15px; white-space: normal;">
If you specify that a method is an async method by using an <a href="https://msdn.microsoft.com/en-us/library/hh156513.aspx" style="color: #00709f; text-decoration: none;">async</a> modifier, you enable the following two capabilities.</div>
<ul style="font-family: "Segoe UI", "Lucida Grande", Verdana, Arial, Helvetica, sans-serif; white-space: normal;">
<li style="list-style-image: none;"><div style="color: #2a2a2a; line-height: 18px; padding-bottom: 15px;">
The marked async method can use <a href="https://msdn.microsoft.com/en-us/library/hh156528.aspx" style="color: #00709f; text-decoration: none;">await</a> to designate suspension points. The await operator tells the compiler that the async method can't continue past that point until the awaited asynchronous process is complete. In the meantime, control returns to the caller of the async method.</div>
<div style="color: #2a2a2a; line-height: 18px; padding-bottom: 15px;">
The suspension of an async method at an <code>await</code> expression doesn't constitute an exit from the method, and <code>finally</code> blocks don’t run.</div>
</li>
<li style="list-style-image: none;"><div style="color: #2a2a2a; line-height: 18px; padding-bottom: 15px;">
The marked async method can itself be awaited by methods that call it.</div>
</li>
</ul>
<div>
<div style="font-family: "Segoe UI", "Lucida Grande", Verdana, Arial, Helvetica, sans-serif; white-space: normal;">
<h2 class="LW_CollapsibleArea_TitleDiv" style="font-family: "Segoe UI Semibold", "Segoe UI", "Lucida Grande", Verdana, Arial, Helvetica, sans-serif; font-weight: normal; line-height: 1.3em; margin: 0px; padding-bottom: 5px; padding-top: 20px;">
<a class="LW_CollapsibleArea_TitleAhref" href="https://www.blogger.com/null" role="heading" style="color: #0072c6; cursor: text; outline: none;" title=""><span class="LW_CollapsibleArea_Title" style="color: black; display: inline-block; line-height: 1.3em;"><span style="font-size: small;">Return Types and Parameters</span></span></a><div class="LW_CollapsibleArea_Anchor_Div active" id="Anchor_6" style="float: right; height: 20px; padding-top: 7px; visibility: visible; width: 20px;">
<span style="font-size: small;"><a class="LW_CollapsibleArea_Anchor_Img" href="https://msdn.microsoft.com/en-us/library/mt674882.aspx#Anchor_6" style="background-attachment: scroll; background-clip: initial; background-image: url("/Areas/Epx/Content/Images/ImageSprite.png?v=636146814391620512"); background-origin: initial; background-position: -502px -3px; background-repeat: no-repeat; background-size: initial; color: black; display: inline-block; height: 20px; overflow: hidden; text-decoration: none; width: 20px;" title="Right-click to copy and share the link for this section"></a></span></div>
<span style="font-size: small;">
</span><div class="LW_CollapsibleArea_HrDiv" style="padding-top: 0px;">
</div>
</h2>
<div class="sectionblock" style="padding-bottom: 20px; padding-left: 15px;">
<a href="https://www.blogger.com/null" id="Return-Types-and-Parameters" style="color: #0072c6;"></a><a href="https://www.blogger.com/null" id="BKMK_ReturnTypesandParameters" style="color: #0072c6;"></a></div>
</div>
<div style="color: #2a2a2a; font-family: "Segoe UI", "Lucida Grande", Verdana, Arial, Helvetica, sans-serif; line-height: 18px; white-space: normal;">
In .NET Framework programming, an async method typically returns a <a href="https://msdn.microsoft.com/en-us/library/system.threading.tasks.task.aspx" style="color: #00709f; text-decoration: none;">Task</a> or a <a href="https://msdn.microsoft.com/en-us/library/dd321424.aspx" style="color: #00709f; text-decoration: none;">Task<TResult></a>. Inside an async method, an <code>await</code> operator is applied to a task that's returned from a call to another async method.</div>
<div style="color: #2a2a2a; font-family: "Segoe UI", "Lucida Grande", Verdana, Arial, Helvetica, sans-serif; line-height: 18px; white-space: normal;">
You specify <a href="https://msdn.microsoft.com/en-us/library/dd321424.aspx" style="color: #00709f; text-decoration: none;">Task<TResult></a> as the return type if the method contains a <a href="https://msdn.microsoft.com/en-us/library/1h3swy84.aspx" style="color: #00709f; text-decoration: none;">return</a> statement that specifies an operand of type <code>TResult</code>.</div>
<div style="color: #2a2a2a; font-family: "Segoe UI", "Lucida Grande", Verdana, Arial, Helvetica, sans-serif; line-height: 18px; white-space: normal;">
You use <code>Task</code> as the return type if the method has no return statement or has a return statement that doesn't return an operand.</div>
<div style="color: #2a2a2a; font-family: "Segoe UI", "Lucida Grande", Verdana, Arial, Helvetica, sans-serif; line-height: 18px; white-space: normal;">
The following example shows how you declare and call a method that returns a <a href="https://msdn.microsoft.com/en-us/library/dd321424.aspx" style="color: #00709f; text-decoration: none;">Task<TResult></a> or a <a href="https://msdn.microsoft.com/en-us/library/system.threading.tasks.task.aspx" style="color: #00709f; text-decoration: none;">Task</a>.</div>
<pre style="overflow: auto; padding: 5px; word-wrap: normal;"><span style="color: green;">// Signature specifies Task<TResult> </span>
<span style="color: blue;">async</span> Task<<span style="color: blue;">int</span>> TaskOfTResult_MethodAsync()
{
<span style="color: blue;">int</span> hours;
<span style="color: green;">// . . . </span>
<span style="color: green;">// Return statement specifies an integer result. </span>
<span style="color: blue;">return</span> hours;
}
<span style="color: green;">// Calls to TaskOfTResult_MethodAsync </span>
Task<<span style="color: blue;">int</span>> returnedTaskTResult = TaskOfTResult_MethodAsync();
<span style="color: blue;">int</span> intResult = <span style="color: blue;">await</span> returnedTaskTResult;
<span style="color: green;">// or, in a single statement </span>
<span style="color: blue;">int</span> intResult = <span style="color: blue;">await</span> TaskOfTResult_MethodAsync();
<span style="color: green;">// Signature specifies Task </span>
<span style="color: blue;">async</span> Task Task_MethodAsync()
{
<span style="color: green;">// . . . </span>
<span style="color: green;">// The method has no return statement. </span>
}
<span style="color: green;">// Calls to Task_MethodAsync </span>
Task returnedTask = Task_MethodAsync();
<span style="color: blue;">await</span> returnedTask;
<span style="color: green;">// or, in a single statement </span>
<span style="color: blue;">await</span> Task_MethodAsync(); </pre>
<pre style="font-family: Consolas, Courier, monospace !important; overflow: auto; padding: 5px; word-wrap: normal;"></pre>
<pre style="font-family: Consolas, Courier, monospace !important; overflow: auto; padding: 5px; word-wrap: normal;"><div style="color: #2a2a2a; font-family: "Segoe UI", "Lucida Grande", Verdana, Arial, Helvetica, sans-serif; line-height: 18px; white-space: normal;">
An async method that has a <code>void</code> return type can’t be awaited, and the caller of a void-returning method can't catch any exceptions that the method throws.</div>
<div style="color: #2a2a2a; font-family: "Segoe UI", "Lucida Grande", Verdana, Arial, Helvetica, sans-serif; line-height: 18px; white-space: normal;">
An async method can't declare <a href="https://msdn.microsoft.com/en-us/library/14akc2c7.aspx" style="color: #00709f; text-decoration: none;">ref</a> or <a href="https://msdn.microsoft.com/en-us/library/t3c3bfhx.aspx" style="color: #00709f; text-decoration: none;">out</a> parameters, but the method can call methods that have such parameters.</div>
<div style="color: #2a2a2a; font-family: "Segoe UI", "Lucida Grande", Verdana, Arial, Helvetica, sans-serif; line-height: 18px; white-space: normal;">
For More <a href="https://msdn.microsoft.com/en-us/library/mt674882.aspx" target="_blank">Asynchronous Programming with async and await</a></div>
</pre>
</div>
</div>
</pre>
</pre>
</div>
</div>
Bhanu Prataphttp://www.blogger.com/profile/17529341083467033319noreply@blogger.com0tag:blogger.com,1999:blog-45468370605306946.post-59765951441485674012016-12-06T22:35:00.002-08:002016-12-06T22:39:08.947-08:00DLL Hell Problem and Solution in C#<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="background-color: white; color: #222222; font-family: "arial" , "tahoma" , "helvetica" , "freesans" , sans-serif; font-size: 13.2px;"><span style="color: #242729; font-family: "arial" , "helvetica neue" , "helvetica" , sans-serif;"><span style="font-size: 15px;"><span style="color: #2a2a2a; font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif; font-size: 13px;">DLL Hell refers to the set of problems caused when multiple applications attempt to share a common component like a dynamic-link library (DLL) or a Component Object Model (COM) class. In the most typical case, one application will install a new version of the shared component that is not backward compatible with the version already on the machine. Although the application that has just been installed works fine, existing applications that depended on a previous version of the shared component might no longer work. </span></span></span></span><br />
<span style="background-color: white; color: #222222; font-family: "arial" , "tahoma" , "helvetica" , "freesans" , sans-serif; font-size: 13.2px;"><span style="color: #242729; font-family: "arial" , "helvetica neue" , "helvetica" , sans-serif;"><span style="font-size: 15px;"><span style="color: #2a2a2a; font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif; font-size: 13px;">The reason for these issues is that version information about the different components of an application aren't recorded or enforced by the system.</span></span></span></span><br />
<span style="background-color: white; color: #222222; font-family: "arial" , "tahoma" , "helvetica" , "freesans" , sans-serif; font-size: 13.2px;"><span style="color: #242729; font-family: "arial" , "helvetica neue" , "helvetica" , sans-serif;"><span style="font-size: 15px;"><span style="color: #2a2a2a; font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif; font-size: 13px;"><br /></span></span></span></span>
<span style="background-color: white; color: #222222; font-family: "arial" , "tahoma" , "helvetica" , "freesans" , sans-serif; font-size: 13.2px;"><span style="color: #242729; font-family: "arial" , "helvetica neue" , "helvetica" , sans-serif;"><span style="font-size: 15px;"><span style="color: #2a2a2a; font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif; font-size: 13px;">For more and solution please visit <a href="http://crackindotnet.blogspot.in/p/c.html" target="_blank">http://crackindotnet.blogspot.in/p/c.html </a></span></span></span></span><span style="color: #2a2a2a; font-family: "segoe ui" , "lucida grande" , "verdana" , "arial" , "helvetica" , sans-serif;"><span style="font-size: 13px;"> <b>question number 36</b></span></span></div>
Bhanu Prataphttp://www.blogger.com/profile/17529341083467033319noreply@blogger.com1tag:blogger.com,1999:blog-45468370605306946.post-89861733979075187702016-12-05T08:13:00.004-08:002016-12-05T08:31:53.608-08:00Find all possible sub strings of a string<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas;"> </span><span style="color: blue; font-family: Consolas;">static</span><span style="font-family: Consolas;"> </span><span style="color: blue; font-family: Consolas;">void</span><span style="font-family: Consolas;"> Main(</span><span style="color: blue; font-family: Consolas;">string</span><span style="font-family: Consolas;">[] args)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas;"> </span><span style="color: blue; font-family: Consolas;">string</span><span style="font-family: Consolas;"> value = </span><span style="color: #2b91af; font-family: Consolas;">Console</span><span style="font-family: Consolas;">.ReadLine();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas;"> </span><span style="color: green; font-family: Consolas;">// Avoid full length.</span><span style="font-family: Consolas;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas;"> </span><span style="color: blue; font-family: Consolas;">for</span><span style="font-family: Consolas;"> (</span><span style="color: blue; font-family: Consolas;">int</span><span style="font-family: Consolas;"> length = 1; length < value.Length;
length++)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas;"> </span><span style="color: green; font-family: Consolas;">// End index is tricky.</span><span style="font-family: Consolas;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas;"> </span><span style="color: blue; font-family: Consolas;">for</span><span style="font-family: Consolas;"> (</span><span style="color: blue; font-family: Consolas;">int</span><span style="font-family: Consolas;"> start = 0; start <= value.Length -
length; start++)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas;"> </span><span style="color: blue; font-family: Consolas;">string</span><span style="font-family: Consolas;"> substring =
value.Substring(start, length);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas;"> </span><span style="color: #2b91af; font-family: Consolas;">Console</span><span style="font-family: Consolas;">.WriteLine(substring);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas;">
</span><span style="color: #2b91af; font-family: Consolas;">Console</span><span style="font-family: Consolas;">.ReadLine();<o:p></o:p></span></div>
<br />
<div class="MsoNormal">
<span style="font-family: Consolas; line-height: 115%;"> }</span><o:p></o:p></div>
</div>
Bhanu Prataphttp://www.blogger.com/profile/17529341083467033319noreply@blogger.com1tag:blogger.com,1999:blog-45468370605306946.post-66682508218833745612016-12-05T04:35:00.001-08:002016-12-05T08:31:53.600-08:00Reverse n numbers of a integer Array<div dir="ltr" style="text-align: left;" trbidi="on">
<b>Input:</b> n, int[] array<br />
For example <b>n</b>=2 , <b>array </b>= {2,3,4,5,6,7}<br />
<br />
<b>Output: </b>3,2,4,5,6,7 Explanation - just reverse first 2 elements remaining will same.<br />
<br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: Consolas;">int</span><span style="font-family: Consolas;"> n = </span><span style="color: #2b91af; font-family: Consolas;">Convert</span><span style="font-family: Consolas;">.ToInt32(</span><span style="color: #2b91af; font-family: Consolas;">Console</span><span style="font-family: Consolas;">.ReadLine());<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas;"> </span><span style="color: blue; font-family: Consolas;">int</span><span style="font-family: Consolas;">[] arr = </span><span style="color: blue; font-family: Consolas;">new</span><span style="font-family: Consolas;"> </span><span style="color: blue; font-family: Consolas;">int</span><span style="font-family: Consolas;">[] {2,3,4,5,6,7,8 };<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas;"> </span><span style="color: blue; font-family: Consolas;">int</span><span style="font-family: Consolas;">[] revarr = </span><span style="color: blue; font-family: Consolas;">new</span><span style="font-family: Consolas;"> </span><span style="color: blue; font-family: Consolas;">int</span><span style="font-family: Consolas;">[arr.Length];<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas;"> </span><span style="color: blue; font-family: Consolas;">for</span><span style="font-family: Consolas;">(</span><span style="color: blue; font-family: Consolas;">int</span><span style="font-family: Consolas;"> i = 0; i < n; i++)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas;"> revarr[n - i-1] = arr[i];<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas;"> </span><span style="color: blue; font-family: Consolas;">for</span><span style="font-family: Consolas;">(</span><span style="color: blue; font-family: Consolas;">int</span><span style="font-family: Consolas;"> i = n; i < arr.Length; i++)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas;"> revarr[i] = arr[i];<o:p></o:p></span></div>
<br />
<div class="MsoNormal">
<span style="font-family: Consolas; line-height: 115%;"> }</span><o:p></o:p></div>
</div>
Bhanu Prataphttp://www.blogger.com/profile/17529341083467033319noreply@blogger.com1tag:blogger.com,1999:blog-45468370605306946.post-42299114311579772312016-12-02T05:16:00.003-08:002016-12-05T08:31:53.587-08:00Storing child class objects in a List of base class<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">abstract</span><span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">class</span><span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">A</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;">
</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">abstract</span><span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">void</span><span style="font-family: Consolas; font-size: 9.5pt;"> diap();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;">
</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">class</span><span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">B</span><span style="font-family: Consolas; font-size: 9.5pt;">:</span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">A</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;">
</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">override</span><span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">void</span><span style="font-family: Consolas; font-size: 9.5pt;"> diap()<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">Console</span><span style="font-family: Consolas; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">"B"</span><span style="font-family: Consolas; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;">
</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">class</span><span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">C</span><span style="font-family: Consolas; font-size: 9.5pt;">:</span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">B</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;">
</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">public</span><span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">override</span><span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">void</span><span style="font-family: Consolas; font-size: 9.5pt;"> diap()<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">Console</span><span style="font-family: Consolas; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">"C"</span><span style="font-family: Consolas; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">base</span><span style="font-family: Consolas; font-size: 9.5pt;">.diap();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;">
</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">class</span><span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">Program</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;">
</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">static</span><span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">void</span><span style="font-family: Consolas; font-size: 9.5pt;"> Main(</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">string</span><span style="font-family: Consolas; font-size: 9.5pt;">[] args)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">List</span><span style="font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">A</span><span style="font-family: Consolas; font-size: 9.5pt;">> li = </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">new</span><span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">List</span><span style="font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">A</span><span style="font-family: Consolas; font-size: 9.5pt;">>();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">B</span><span style="font-family: Consolas; font-size: 9.5pt;"> obj1 = </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">new</span><span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">B</span><span style="font-family: Consolas; font-size: 9.5pt;">();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">C</span><span style="font-family: Consolas; font-size: 9.5pt;"> obj2 = </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">new</span><span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">C</span><span style="font-family: Consolas; font-size: 9.5pt;">();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> li.Add(obj1);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> li.Add(obj2);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">foreach</span><span style="font-family: Consolas; font-size: 9.5pt;">(</span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">A</span><span style="font-family: Consolas; font-size: 9.5pt;"> ob </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">in</span><span style="font-family: Consolas; font-size: 9.5pt;"> li)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> ob.diap();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">Console</span><span style="font-family: Consolas; font-size: 9.5pt;">.ReadLine();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;">
}<o:p></o:p></span></div>
<br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;">
}</span><o:p></o:p></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;">Output - </span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;">B</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;">C</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;">B</span></div>
</div>
Bhanu Prataphttp://www.blogger.com/profile/17529341083467033319noreply@blogger.com0tag:blogger.com,1999:blog-45468370605306946.post-8790744853780954272016-12-02T04:36:00.002-08:002016-12-05T08:31:53.604-08:00Read phone number from a from a phone book by name<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="msB challenge_input_format_title" style="background-color: white; border: 0px; color: #39424e; font-family: "Whitney SSm A", "Whitney SSm B", Avenir, "Segoe UI", Ubuntu, "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 16px; font-stretch: inherit; font-variant-numeric: inherit; line-height: inherit; margin: 0px 0px 10px; outline: 0px; padding: 0px; vertical-align: baseline; word-break: break-word; word-wrap: break-word;">
<div style="border: 0px; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: 1.5em; margin-bottom: 1em; outline: 0px; padding: 0px; vertical-align: baseline; word-break: break-word; word-wrap: break-word;">
<strong style="border: 0px; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; word-break: break-word; word-wrap: break-word;">Input Format</strong></div>
</div>
<div class="msB challenge_input_format_body" style="background-color: white; border: 0px; color: #39424e; font-family: "Whitney SSm A", "Whitney SSm B", Avenir, "Segoe UI", Ubuntu, "Helvetica Neue", Helvetica, Arial, sans-serif; font-size: 16px; font-stretch: inherit; font-variant-numeric: inherit; line-height: inherit; margin: 0px 0px 10px; outline: 0px; padding: 0px; vertical-align: baseline; word-break: break-word; word-wrap: break-word;">
<div class="hackdown-content" style="border: 0px; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; word-break: break-word; word-wrap: break-word;">
<div style="border: 0px; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: 1.5em; margin-bottom: 1em; margin-top: 12px; outline: 0px; padding: 0px; vertical-align: baseline; word-break: break-word; word-wrap: break-word;">
The first line contains an integer, <span class="MathJax_SVG" id="MathJax-Element-1-Frame" style="border: 0px; direction: ltr; display: inline-block; float: none; font-family: inherit; font-stretch: inherit; font-variant: inherit; line-height: normal; margin: 0px; max-height: none; max-width: none; min-height: 0px; min-width: 0px; outline: 0px; padding: 0px; vertical-align: baseline; white-space: nowrap; word-break: break-word; word-spacing: normal; word-wrap: normal;"><svg focusable="false" height="1.676ex" role="img" style="vertical-align: -0.338ex;" viewbox="0 -576.1 600.5 721.6" width="1.395ex" xmlns:xlink="http://www.w3.org/1999/xlink"><g fill="currentColor" stroke-width="0" stroke="currentColor" transform="matrix(1 0 0 -1 0 0)"><path d="M21 287Q22 293 24 303T36 341T56 388T89 425T135 442Q171 442 195 424T225 390T231 369Q231 367 232 367L243 378Q304 442 382 442Q436 442 469 415T503 336T465 179T427 52Q427 26 444 26Q450 26 453 27Q482 32 505 65T540 145Q542 153 560 153Q580 153 580 145Q580 144 576 130Q568 101 554 73T508 17T439 -10Q392 -10 371 17T350 73Q350 92 386 193T423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 180T152 343Q153 348 153 366Q153 405 129 405Q91 405 66 305Q60 285 60 284Q58 278 41 278H27Q21 284 21 287Z" stroke-width="1"></path></g></svg></span>, denoting the number of entries in the phone book. <br style="word-break: break-word; word-wrap: break-word;" />Each of the <span class="MathJax_SVG" id="MathJax-Element-2-Frame" style="border: 0px; direction: ltr; display: inline-block; float: none; font-family: inherit; font-stretch: inherit; font-variant: inherit; line-height: normal; margin: 0px; max-height: none; max-width: none; min-height: 0px; min-width: 0px; outline: 0px; padding: 0px; vertical-align: baseline; white-space: nowrap; word-break: break-word; word-spacing: normal; word-wrap: normal;"><svg focusable="false" height="1.676ex" role="img" style="vertical-align: -0.338ex;" viewbox="0 -576.1 600.5 721.6" width="1.395ex" xmlns:xlink="http://www.w3.org/1999/xlink"><g fill="currentColor" stroke-width="0" stroke="currentColor" transform="matrix(1 0 0 -1 0 0)"><path d="M21 287Q22 293 24 303T36 341T56 388T89 425T135 442Q171 442 195 424T225 390T231 369Q231 367 232 367L243 378Q304 442 382 442Q436 442 469 415T503 336T465 179T427 52Q427 26 444 26Q450 26 453 27Q482 32 505 65T540 145Q542 153 560 153Q580 153 580 145Q580 144 576 130Q568 101 554 73T508 17T439 -10Q392 -10 371 17T350 73Q350 92 386 193T423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 180T152 343Q153 348 153 366Q153 405 129 405Q91 405 66 305Q60 285 60 284Q58 278 41 278H27Q21 284 21 287Z" stroke-width="1"></path></g></svg></span> subsequent lines describes an entry in the form of <span class="MathJax_SVG" id="MathJax-Element-3-Frame" style="border: 0px; direction: ltr; display: inline-block; float: none; font-family: inherit; font-stretch: inherit; font-variant: inherit; line-height: normal; margin: 0px; max-height: none; max-width: none; min-height: 0px; min-width: 0px; outline: 0px; padding: 0px; vertical-align: baseline; white-space: nowrap; word-break: break-word; word-spacing: normal; word-wrap: normal;"><svg focusable="false" height="2.176ex" role="img" style="vertical-align: -0.338ex;" viewbox="0 -791.3 500.5 936.9" width="1.162ex" xmlns:xlink="http://www.w3.org/1999/xlink"><g fill="currentColor" stroke-width="0" stroke="currentColor" transform="matrix(1 0 0 -1 0 0)"><path d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z" stroke-width="1"></path></g></svg></span> space-separated values on a single line. The first value is a friend's name, and the second value is an <span class="MathJax_SVG" id="MathJax-Element-4-Frame" style="border: 0px; direction: ltr; display: inline-block; float: none; font-family: inherit; font-stretch: inherit; font-variant: inherit; line-height: normal; margin: 0px; max-height: none; max-width: none; min-height: 0px; min-width: 0px; outline: 0px; padding: 0px; vertical-align: baseline; white-space: nowrap; word-break: break-word; word-spacing: normal; word-wrap: normal;"><svg focusable="false" height="2.176ex" role="img" style="vertical-align: -0.338ex;" viewbox="0 -791.3 500.5 936.9" width="1.162ex" xmlns:xlink="http://www.w3.org/1999/xlink"><g fill="currentColor" stroke-width="0" stroke="currentColor" transform="matrix(1 0 0 -1 0 0)"><path d="M70 417T70 494T124 618T248 666Q319 666 374 624T429 515Q429 485 418 459T392 417T361 389T335 371T324 363L338 354Q352 344 366 334T382 323Q457 264 457 174Q457 95 399 37T249 -22Q159 -22 101 29T43 155Q43 263 172 335L154 348Q133 361 127 368Q70 417 70 494ZM286 386L292 390Q298 394 301 396T311 403T323 413T334 425T345 438T355 454T364 471T369 491T371 513Q371 556 342 586T275 624Q268 625 242 625Q201 625 165 599T128 534Q128 511 141 492T167 463T217 431Q224 426 228 424L286 386ZM250 21Q308 21 350 55T392 137Q392 154 387 169T375 194T353 216T330 234T301 253T274 270Q260 279 244 289T218 306L210 311Q204 311 181 294T133 239T107 157Q107 98 150 60T250 21Z" stroke-width="1"></path></g></svg></span>-digit phone number.</div>
<div style="border: 0px; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: 1.5em; margin-bottom: 1em; margin-top: 12px; outline: 0px; padding: 0px; vertical-align: baseline; word-break: break-word; word-wrap: break-word;">
After the <span class="MathJax_SVG" id="MathJax-Element-5-Frame" style="border: 0px; direction: ltr; display: inline-block; float: none; font-family: inherit; font-stretch: inherit; font-variant: inherit; line-height: normal; margin: 0px; max-height: none; max-width: none; min-height: 0px; min-width: 0px; outline: 0px; padding: 0px; vertical-align: baseline; white-space: nowrap; word-break: break-word; word-spacing: normal; word-wrap: normal;"><svg focusable="false" height="1.676ex" role="img" style="vertical-align: -0.338ex;" viewbox="0 -576.1 600.5 721.6" width="1.395ex" xmlns:xlink="http://www.w3.org/1999/xlink"><g fill="currentColor" stroke-width="0" stroke="currentColor" transform="matrix(1 0 0 -1 0 0)"><path d="M21 287Q22 293 24 303T36 341T56 388T89 425T135 442Q171 442 195 424T225 390T231 369Q231 367 232 367L243 378Q304 442 382 442Q436 442 469 415T503 336T465 179T427 52Q427 26 444 26Q450 26 453 27Q482 32 505 65T540 145Q542 153 560 153Q580 153 580 145Q580 144 576 130Q568 101 554 73T508 17T439 -10Q392 -10 371 17T350 73Q350 92 386 193T423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 180T152 343Q153 348 153 366Q153 405 129 405Q91 405 66 305Q60 285 60 284Q58 278 41 278H27Q21 284 21 287Z" stroke-width="1"></path></g></svg></span> lines of phone book entries, there are <em style="border: 0px; font-family: inherit; font-stretch: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; word-break: break-word; word-wrap: break-word;">an unknown number of lines of queries</em>. Each line (query) contains a<span class="MathJax_SVG" id="MathJax-Element-6-Frame" style="border: 0px; direction: ltr; display: inline-block; float: none; font-family: inherit; font-stretch: inherit; font-variant: inherit; line-height: normal; margin: 0px; max-height: none; max-width: none; min-height: 0px; min-width: 0px; outline: 0px; padding: 0px; vertical-align: baseline; white-space: nowrap; word-break: break-word; word-spacing: normal; word-wrap: normal;"><svg focusable="false" height="1.676ex" role="img" style="vertical-align: -0.338ex;" viewbox="0 -576.1 2475 721.6" width="5.748ex" xmlns:xlink="http://www.w3.org/1999/xlink"><g fill="currentColor" stroke-width="0" stroke="currentColor" transform="matrix(1 0 0 -1 0 0)"><path d="M21 287Q22 293 24 303T36 341T56 388T89 425T135 442Q171 442 195 424T225 390T231 369Q231 367 232 367L243 378Q304 442 382 442Q436 442 469 415T503 336T465 179T427 52Q427 26 444 26Q450 26 453 27Q482 32 505 65T540 145Q542 153 560 153Q580 153 580 145Q580 144 576 130Q568 101 554 73T508 17T439 -10Q392 -10 371 17T350 73Q350 92 386 193T423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 180T152 343Q153 348 153 366Q153 405 129 405Q91 405 66 305Q60 285 60 284Q58 278 41 278H27Q21 284 21 287Z" stroke-width="1"></path><g transform="translate(600,0)"><path d="M33 157Q33 258 109 349T280 441Q331 441 370 392Q386 422 416 422Q429 422 439 414T449 394Q449 381 412 234T374 68Q374 43 381 35T402 26Q411 27 422 35Q443 55 463 131Q469 151 473 152Q475 153 483 153H487Q506 153 506 144Q506 138 501 117T481 63T449 13Q436 0 417 -8Q409 -10 393 -10Q359 -10 336 5T306 36L300 51Q299 52 296 50Q294 48 292 46Q233 -10 172 -10Q117 -10 75 30T33 157ZM351 328Q351 334 346 350T323 385T277 405Q242 405 210 374T160 293Q131 214 119 129Q119 126 119 118T118 106Q118 61 136 44T179 26Q217 26 254 59T298 110Q300 114 325 217T351 328Z" stroke-width="1"></path></g><g transform="translate(1130,0)"><path d="M21 287Q22 293 24 303T36 341T56 388T88 425T132 442T175 435T205 417T221 395T229 376L231 369Q231 367 232 367L243 378Q303 442 384 442Q401 442 415 440T441 433T460 423T475 411T485 398T493 385T497 373T500 364T502 357L510 367Q573 442 659 442Q713 442 746 415T780 336Q780 285 742 178T704 50Q705 36 709 31T724 26Q752 26 776 56T815 138Q818 149 821 151T837 153Q857 153 857 145Q857 144 853 130Q845 101 831 73T785 17T716 -10Q669 -10 648 17T627 73Q627 92 663 193T700 345Q700 404 656 404H651Q565 404 506 303L499 291L466 157Q433 26 428 16Q415 -11 385 -11Q372 -11 364 -4T353 8T350 18Q350 29 384 161L420 307Q423 322 423 345Q423 404 379 404H374Q288 404 229 303L222 291L189 157Q156 26 151 16Q138 -11 108 -11Q95 -11 87 -5T76 7T74 17Q74 30 112 181Q151 335 151 342Q154 357 154 369Q154 405 129 405Q107 405 92 377T69 316T57 280Q55 278 41 278H27Q21 284 21 287Z" stroke-width="1"></path></g><g transform="translate(2008,0)"><path d="M39 168Q39 225 58 272T107 350T174 402T244 433T307 442H310Q355 442 388 420T421 355Q421 265 310 237Q261 224 176 223Q139 223 138 221Q138 219 132 186T125 128Q125 81 146 54T209 26T302 45T394 111Q403 121 406 121Q410 121 419 112T429 98T420 82T390 55T344 24T281 -1T205 -11Q126 -11 83 42T39 168ZM373 353Q367 405 305 405Q272 405 244 391T199 357T170 316T154 280T149 261Q149 260 169 260Q282 260 327 284T373 353Z" stroke-width="1"></path></g></g></svg></span> to look up, and you must continue reading lines until there is no more input.</div>
<div class="msB challenge_sample_input_title" style="border: 0px; font-stretch: inherit; font-variant-numeric: inherit; line-height: inherit; margin: 0px 0px 10px; outline: 0px; padding: 0px; vertical-align: baseline; word-break: break-word; word-wrap: break-word;">
<div style="border: 0px; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: 1.5em; margin-bottom: 1em; outline: 0px; padding: 0px; vertical-align: baseline; word-break: break-word; word-wrap: break-word;">
<strong style="border: 0px; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; word-break: break-word; word-wrap: break-word;">Sample Input</strong></div>
</div>
<div class="msB challenge_sample_input_body" style="border: 0px; font-stretch: inherit; font-variant-numeric: inherit; line-height: inherit; margin: 0px 0px 10px; outline: 0px; padding: 0px; vertical-align: baseline; word-break: break-word; word-wrap: break-word;">
<div class="hackdown-content" style="border: 0px; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; word-break: break-word; word-wrap: break-word;">
<pre style="background: rgb(244, 250, 255); border-radius: 2px; border: 0px; color: #454c59; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 14px; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 18px; margin-bottom: 9px; margin-top: 12px; outline: 0px; padding: 20px; vertical-align: baseline; white-space: pre-wrap; word-break: break-word; word-wrap: break-word;"><code style="background: transparent; border-radius: 3px; border: 0px; color: #454c5f; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 1em; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; word-break: break-word; word-wrap: break-word;">3
sam 99912222
tom 11122222
harry 12299933
sam
edward
harry</code></pre>
</div>
</div>
<div class="msB challenge_sample_output_title" style="border: 0px; font-stretch: inherit; font-variant-numeric: inherit; line-height: inherit; margin: 0px 0px 10px; outline: 0px; padding: 0px; vertical-align: baseline; word-break: break-word; word-wrap: break-word;">
<div style="border: 0px; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: 1.5em; margin-bottom: 1em; outline: 0px; padding: 0px; vertical-align: baseline; word-break: break-word; word-wrap: break-word;">
<strong style="border: 0px; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; word-break: break-word; word-wrap: break-word;">Sample Output</strong></div>
</div>
<div class="msB challenge_sample_output_body" style="border: 0px; font-stretch: inherit; font-variant-numeric: inherit; line-height: inherit; margin: 0px 0px 10px; outline: 0px; padding: 0px; vertical-align: baseline; word-break: break-word; word-wrap: break-word;">
<div class="hackdown-content" style="border: 0px; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; word-break: break-word; word-wrap: break-word;">
<pre style="background: rgb(244, 250, 255); border-radius: 2px; border: 0px; color: #454c59; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 14px; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: 18px; margin-bottom: 9px; margin-top: 12px; outline: 0px; padding: 20px; vertical-align: baseline; white-space: pre-wrap; word-break: break-word; word-wrap: break-word;"><code style="background: transparent; border-radius: 3px; border: 0px; color: #454c5f; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 1em; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline; word-break: break-word; word-wrap: break-word;">sam=99912222
Not found
harry=12299933</code></pre>
</div>
</div>
</div>
</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">Dictionary</span><span style="font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">string</span><span style="font-family: Consolas; font-size: 9.5pt;">, </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">string</span><span style="font-family: Consolas; font-size: 9.5pt;">> di = </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">new</span><span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">Dictionary</span><span style="font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">string</span><span style="font-family: Consolas; font-size: 9.5pt;">, </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">string</span><span style="font-family: Consolas; font-size: 9.5pt;">>();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">int</span><span style="font-family: Consolas; font-size: 9.5pt;"> length = </span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">Convert</span><span style="font-family: Consolas; font-size: 9.5pt;">.ToInt32(</span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">Console</span><span style="font-family: Consolas; font-size: 9.5pt;">.ReadLine());<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">while</span><span style="font-family: Consolas; font-size: 9.5pt;"> (di.Count < length)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">string</span><span style="font-family: Consolas; font-size: 9.5pt;"> s = </span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">Console</span><span style="font-family: Consolas; font-size: 9.5pt;">.ReadLine();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">string</span><span style="font-family: Consolas; font-size: 9.5pt;">[] arr = s.Split(</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">' '</span><span style="font-family: Consolas; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> di.Add(arr[0], arr[1]);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">string</span><span style="font-family: Consolas; font-size: 9.5pt;"> name = </span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">Console</span><span style="font-family: Consolas; font-size: 9.5pt;">.ReadLine(); <o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">List</span><span style="font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">string</span><span style="font-family: Consolas; font-size: 9.5pt;">> names = </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">new</span><span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">List</span><span style="font-family: Consolas; font-size: 9.5pt;"><</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">string</span><span style="font-family: Consolas; font-size: 9.5pt;">>();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">while</span><span style="font-family: Consolas; font-size: 9.5pt;"> (!</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">string</span><span style="font-family: Consolas; font-size: 9.5pt;">.IsNullOrWhiteSpace(name))<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> names.Add(name);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> name = </span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">Console</span><span style="font-family: Consolas; font-size: 9.5pt;">.ReadLine();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">foreach</span><span style="font-family: Consolas; font-size: 9.5pt;">(</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">string</span><span style="font-family: Consolas; font-size: 9.5pt;"> s </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">in</span><span style="font-family: Consolas; font-size: 9.5pt;"> names)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">if</span><span style="font-family: Consolas; font-size: 9.5pt;"> (di.ContainsKey(s.ToString()))<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">Console</span><span style="font-family: Consolas; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">"{0}={1}"</span><span style="font-family: Consolas; font-size: 9.5pt;">,s.ToString(),
di[s.ToString()]);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">else</span><span style="font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">Console</span><span style="font-family: Consolas; font-size: 9.5pt;">.WriteLine(</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">"Not
found"</span><span style="font-family: Consolas; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">Console</span><span style="font-family: Consolas; font-size: 9.5pt;">.ReadLine();</span><o:p></o:p></div>
</div>
Bhanu Prataphttp://www.blogger.com/profile/17529341083467033319noreply@blogger.com1tag:blogger.com,1999:blog-45468370605306946.post-25897141944030814822016-11-30T07:19:00.001-08:002016-12-05T08:31:53.583-08:00Print Staircase<div dir="ltr" style="text-align: left;" trbidi="on">
<b>Sample input 6</b><br />
<b>Sample Output </b><br />
<pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; box-sizing: border-box; line-height: 27px; margin-bottom: 27px; margin-top: -2px; max-width: 100%; outline: 0px; overflow: auto; padding: 0px; vertical-align: baseline;"><code style="background-color: white; border: 0px; box-sizing: border-box; color: #666666; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; font-size: 1em; font-stretch: normal; line-height: 2.09231em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> #
##
###
####
#####
######</code>
</pre>
Solution-<div>
<br /><div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: Consolas;">int</span><span style="font-family: Consolas;"> n = </span><span style="color: #2b91af; font-family: Consolas;">Convert</span><span style="font-family: Consolas;">.ToInt32(</span><span style="color: #2b91af; font-family: Consolas;">Console</span><span style="font-family: Consolas;">.ReadLine());<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas;"> </span><span style="color: blue; font-family: Consolas;">for</span><span style="font-family: Consolas;">(</span><span style="color: blue; font-family: Consolas;">int</span><span style="font-family: Consolas;"> i = 1; i <= n; i++)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas;"> </span><span style="color: blue; font-family: Consolas;">for</span><span style="font-family: Consolas;">(</span><span style="color: blue; font-family: Consolas;">int</span><span style="font-family: Consolas;"> j = 1; j <= n; j++)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas;"> </span><span style="color: blue; font-family: Consolas;">if</span><span style="font-family: Consolas;"> (j <= n - i)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas;"> </span><span style="color: #2b91af; font-family: Consolas;">Console</span><span style="font-family: Consolas;">.Write(</span><span style="color: #a31515; font-family: Consolas;">" "</span><span style="font-family: Consolas;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas;"> </span><span style="color: blue; font-family: Consolas;">else</span><span style="font-family: Consolas;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas;"> </span><span style="color: #2b91af; font-family: Consolas;">Console</span><span style="font-family: Consolas;">.Write(</span><span style="color: #a31515; font-family: Consolas;">"#"</span><span style="font-family: Consolas;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas;"> </span><span style="color: #2b91af; font-family: Consolas;">Console</span><span style="font-family: Consolas;">.WriteLine();<o:p></o:p></span></div>
<span style="font-family: Consolas; line-height: 115%;"> }</span></div>
</div>
</div>
Bhanu Prataphttp://www.blogger.com/profile/17529341083467033319noreply@blogger.com0tag:blogger.com,1999:blog-45468370605306946.post-8332032495437204252016-11-26T09:20:00.003-08:002016-12-05T08:34:38.058-08:00Design Principles and Patterns<div dir="ltr" style="text-align: left;" trbidi="on">
For Design Principles and Patterns please click on below link. I have created a separated page.<br />
<br />
<a href="http://crackindotnet.blogspot.in/p/design-pr.html" target="_blank">Design Principles and Patterns</a></div>
Bhanu Prataphttp://www.blogger.com/profile/17529341083467033319noreply@blogger.com0tag:blogger.com,1999:blog-45468370605306946.post-48617857004956912092016-11-24T09:28:00.000-08:002016-12-05T08:31:53.593-08:00Reverse a String word by word<div dir="ltr" style="text-align: left;" trbidi="on">
Given a string, reverse it word by word.<br />
<br />
<b>input : </b>I like to play with c-sharp.<br />
<b>expected output : </b>c-sharp with play to like I.<br />
<br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">static</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">void</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> Main(</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">string</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">[] args)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">string</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> s = Console.ReadLine();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">string</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">[] arr = s.Split(</span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">' '</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> length = arr.Length;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: green; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">// it
will hold reverse array</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">string</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">[] revArr = </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">new</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">string</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">[length];<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: green; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">// this
index holds index value for new reversed string.</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> index = length - 1;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">for</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> (</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">int</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> j = length - 1; j >= 0; j--)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> revArr[index-j] = arr[j];<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">string</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;"> revString = String.Join(</span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">"
"</span><span style="background: white; font-family: Consolas; font-size: 9.5pt;">, revArr);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> Console.WriteLine(revString);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; font-family: Consolas; font-size: 9.5pt;"> Console.ReadLine();<o:p></o:p></span></div>
<br />
<div class="MsoNormal">
<span style="background: white; font-family: Consolas; font-size: 9.5pt; line-height: 115%;"> }</span><o:p></o:p></div>
<br />
<br />
<br /></div>
Bhanu Prataphttp://www.blogger.com/profile/17529341083467033319noreply@blogger.com1tag:blogger.com,1999:blog-45468370605306946.post-66030432983080933402016-11-24T09:05:00.004-08:002016-12-05T08:31:53.596-08:00Count the length of last word in a String<div dir="ltr" style="text-align: left;" trbidi="on">
Suppose you have a string "I love Programming"<br />
<b>Output - </b>should be 11.<br />
<b>if there is no word then Output</b>: 0<br />
<br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="color: blue; font-family: Consolas; font-size: 9.5pt;">static</span><span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">void</span><span style="font-family: Consolas; font-size: 9.5pt;"> Main1(</span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">string</span><span style="font-family: Consolas; font-size: 9.5pt;">[] args)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;">
{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">string</span><span style="font-family: Consolas; font-size: 9.5pt;"> s = </span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">Console</span><span style="font-family: Consolas; font-size: 9.5pt;">.ReadLine();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">string</span><span style="font-family: Consolas; font-size: 9.5pt;">[] arr = s.Split(</span><span style="color: #a31515; font-family: Consolas; font-size: 9.5pt;">' '</span><span style="font-family: Consolas; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: blue; font-family: Consolas; font-size: 9.5pt;">int</span><span style="font-family: Consolas; font-size: 9.5pt;"> length = arr.Length;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">Console</span><span style="font-family: Consolas; font-size: 9.5pt;">.WriteLine(arr[length - 1].Length);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="font-family: Consolas; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: Consolas; font-size: 9.5pt;">Console</span><span style="font-family: Consolas; font-size: 9.5pt;">.ReadLine();<o:p></o:p></span></div>
<br />
<div class="MsoNormal">
<span style="font-family: Consolas; font-size: 9.5pt; line-height: 115%;"> }</span><o:p></o:p></div>
</div>
Bhanu Prataphttp://www.blogger.com/profile/17529341083467033319noreply@blogger.com1