tag:blogger.com,1999:blog-85453071708936009432024-02-25T13:13:31.845-08:00Welcome to, WEB ZONEThis blog related to all the experience that i gain and face during past career. It covers Microsoft technology like. Microsoft Dot Net, SQL Server, Scripting Language etc,.योगेश चन्द्र उप्रेतीhttp://www.blogger.com/profile/17009545633002694199noreply@blogger.comBlogger172125tag:blogger.com,1999:blog-8545307170893600943.post-75986082159958151772019-11-16T12:53:00.001-08:002019-11-16T13:04:41.475-08:00Fill Factor in sql server performance tuning server level, index/table level<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="text-align: left;">
<span style="font-size: large;">Fill Facto</span><span style="font-size: large;">r:</span></h2>
<div>
<div class="MsoNoSpacing">
Working in <b>performance tuning</b> area, one has to
know about Index and Index Maintenance. For any Index the most important
property is <b>Fill Factor</b>. <b>Fill Factor</b> is directly related to
Indexes.<o:p></o:p></div>
<div class="MsoNoSpacing">
<br /></div>
<div class="MsoNoSpacing">
<b>Fill factor</b> is the value that determines the
percentage of space on each leaf-level page to be filled with data. In an SQL
Server, the smallest unit is a page, which is made of Page with size 8K.<o:p></o:p></div>
<div class="MsoNoSpacing">
<br /></div>
</div>
<b>A page is the basic unit of data storage in SQL server.</b> Its size is 8KB(8192 bytes). Data is stored in the leaf-level pages of Index. The percentage of space to be filled with data in a leaf level page is decided by fill factor. The remaining space left is used for future growth of data in the page. Fill factor is a number from 1 to 100. Its default value is 0, which is same as 100. So when we say fill factor is 70 means, 70% of space is filled with data and remaining 30% is vacant for future use. So higher the fill factor, more data is stored in the page. Fill factor setting is applied when we create/rebuild index.<br />
<h2 style="text-align: left;">
<span style="font-size: large;">Page Split:</span></h2>
Page split occurs, when there is no more space to accommodate data in leaf-level pages. Page split is movement of data from one page to another page, when data changes in the table.<br />
<h2 style="text-align: left;">
<span style="font-size: large;">Fill factor and its effect on performance:</span></h2>
From above, we have gathered some idea about fill factor. Now let see how fill factor can performance. From above discussion we can say if we have higher value of fill factor, we can save more data in a page. By storing more data in less space, we can save memory, resource uses(like IO, CPU).<br />
<br />
But downside of it is page split. Page split hamper performance.<br />
<br />
Now let us discuss about low value of fill factor. By setting low value of fill factor, page split can be avoided. But it will need more memory(although memory is cheap now), more resource uses. Let say we have a table where the data was fit in 50 pages when the fill factor setting was 100 or 0. Now let say we reduced the fill factor to 50. So it will take 100 pages. When we need to read all the rows, the number of read is doubled now.<br />
<h2 style="text-align: left;">
<span style="font-size: large;">How to decide fill factor?</span></h2>
What is correct value of fill factor, we need to set for better performance ? There is no specific answer. It depend upon your application. You are the best person to decide its value. Below is the criteria you need to consider while choosing fill factor.<br />
<br />
<b>1:For static/look-up table:</b> This type of tables have static data means data changes very rarely in the table. So we can set high value of fill factor means 100 or 0.<br />
<br />
<b>2:For Dynamic table:</b> In this type of table, data get changes(inserted/updated) frequently. So we need to set low fill factor, between 80 to 90.<br />
<br />
<b>3:For Table with Clustered Index on Identity Column: </b> Here the data is inserted at the end of the table always. So we can have higher value of fill factor between 95 to 100.<br />
<br />
<h2 style="text-align: left;">
<b>How to set Fill factor ? We can set fill factor in 2 ways </b></h2>
<h2 style="text-align: left;">
<b>1) Server level:</b></h2>
<div>
A generic fill factor setting is applied at the server level for all table/index. To see what is the current current default fill factor set at the server level, you can use below script.<br />
<div>
<br />
Hide Copy CodeEXEC [sys].[sp_configure] 'fill factor'
GO<br />
<br />
You can set a server level default fill factor by using sp_configure with a parameter as below script. Here are setting a fill factor value of 90.</div>
<div>
EXEC sys.sp_configure 'fill factor', 90 </div>
<div>
GO </div>
<div>
RECONFIGURE WITH OVERRIDE </div>
<div>
GO<br />
<h2 style="text-align: left;">
2) At Index/Table level: </h2>
</div>
<div>
While creating/rebuilding index we can set a specific fill factor. We can use below script to set fill factor while rebuilding index.</div>
<div>
<br />
USE YourDatabaseName </div>
<div>
GO </div>
<div>
ALTER INDEX YourIndexName ON [YourSchemaName].[YourTableName] </div>
<div>
REBUILD WITH (FILLFACTOR = 80); </div>
<div>
GO
<br />
<br />
<b>Points of Interest</b><br />
<br />
Index play an important role to increase the performance of the query. But it is not only the magic pain killer. As fill factor is related to Index, so it has some role on performance improvement. Before setting a fill factor, analyses your requirement, do experiment with different fill factor value and finally set correct value.<br />
<h2 style="text-align: left;">
Measuring Counters</h2>
</div>
</div>
All of the above discussions lead us to think that a higher Fill Factor and high transaction server implies higher page split. However, Fill Factor can help us to reduce the number of the page splits as the new data will be accommodated in the page right away without much difficulty and further page splits. We can measure the page split by the watching performance monitor counter<br />
<div>
“SQLServer:AccessMethods:Page Splits/Sec”.<br />
<br />
Additionally, you can measure the same using the following sys query.<br />
<br />
SELECT cntr_value
FROM MASTER.dbo.sysperfinfo
WHERE counter_name ='Page Splits/sec' AND
OBJECT_NAME LIKE'%Access methods%'<br />
<br />
Fill factor is usually measured at the server level as well as table level. Below we have the scripts for the same.<br />
<br />
Here is the script to measure the Fill Factor at the server level:<br />
<br />
SELECT *
FROM sys.configurations
WHERE name ='fill factor (%)'
<br />
<br />
And, here is the script to measure the Fill Factor at the table/index level:<br />
<br />
USE YourDatabaseName;
SELECT OBJECT_NAME(OBJECT_ID) Name, type_desc, fill_factor
FROM sys.indexes<br />
<h2>
SQL SERVER – Set Server Level FILLFACTOR Using T-SQL Script</h2>
<div>
<img alt="SQL SERVER - Set Server Level FILLFACTOR Using T-SQL Script fillfactor-800x633 " src="https://blog.sqlauthority.com/wp-content/uploads/2007/10/fillfactor-800x633.png" /></div>
<div>
<br /></div>
<div>
<em style="background-color: white; box-sizing: border-box; color: #363b3f; font-family: "open sans", sans-serif; font-size: 14px; text-align: justify;"><span style="box-sizing: border-box; font-weight: 700;">SQL Script to set Server level FILL FACTOR to 90</span></em></div>
<div>
<em style="background-color: white; box-sizing: border-box; color: #363b3f; font-family: "open sans", sans-serif; font-size: 14px; text-align: justify;"><span style="box-sizing: border-box; font-weight: 700;"><br /></span></em></div>
<div>
<em style="background-color: white; box-sizing: border-box; color: #363b3f; font-family: "open sans", sans-serif; font-size: 14px; text-align: justify;"><span style="box-sizing: border-box; font-weight: 700;"></span></em><br />
<div class="line number1 index0 alt2" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-style: normal; font-weight: 400; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; text-align: left; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<em style="background-color: white; box-sizing: border-box; color: #363b3f; font-family: "open sans", sans-serif; font-size: 14px; text-align: justify;"><span style="box-sizing: border-box; font-weight: 700;"><code class="sql keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">EXEC</code> <code class="sql plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">sys.sp_configure </code><code class="sql string" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: blue !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">'show advanced options'</code><code class="sql plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">, </code><code class="sql string" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: blue !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">'1'</code></span></em></div>
<em style="background-color: white; box-sizing: border-box; color: #363b3f; font-family: "open sans", sans-serif; font-size: 14px; text-align: justify;"><span style="box-sizing: border-box; font-weight: 700;">
</span></em>
<div class="line number2 index1 alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; background-position: initial !important; background-repeat: initial !important; background-size: initial !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; direction: ltr !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace; font-style: normal; font-weight: 400; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 1em !important; position: static !important; right: auto !important; text-align: left; top: auto !important; vertical-align: baseline !important; white-space: pre !important; width: auto !important;">
<em style="background-color: white; box-sizing: border-box; color: #363b3f; font-family: "open sans", sans-serif; font-size: 14px; text-align: justify;"><span style="box-sizing: border-box; font-weight: 700;"><code class="sql plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">RECONFIGURE </code><code class="sql keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">WITH</code> <code class="sql plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">OVERRIDE</code></span></em></div>
<em style="background-color: white; box-sizing: border-box; color: #363b3f; font-family: "open sans", sans-serif; font-size: 14px; text-align: justify;"><span style="box-sizing: border-box;">
<div class="line number3 index2 alt2" style="background-image: none; border-radius: 0px; border: 0px; bottom: auto; box-shadow: none; box-sizing: content-box; direction: ltr; float: none; font-family: monaco, consolas, "bitstream vera sans mono", "courier new", courier, monospace; font-style: normal; font-weight: 400; height: auto; left: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; right: auto; text-align: left; top: auto; vertical-align: baseline; white-space: pre; width: auto;">
<code class="sql plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">GO</code></div>
<div class="line number4 index3 alt1" style="background-image: none; border-radius: 0px; border: 0px; bottom: auto; box-shadow: none; box-sizing: content-box; direction: ltr; float: none; font-family: monaco, consolas, "bitstream vera sans mono", "courier new", courier, monospace; font-style: normal; font-weight: 400; height: auto; left: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; right: auto; text-align: left; top: auto; vertical-align: baseline; white-space: pre; width: auto;">
<code class="sql keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">EXEC</code> <code class="sql plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">sys.sp_configure </code><code class="sql string" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: blue !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">'fill factor (%)'</code><code class="sql plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">, </code><code class="sql string" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: blue !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">'90'</code></div>
<div class="line number5 index4 alt2" style="background-image: none; border-radius: 0px; border: 0px; bottom: auto; box-shadow: none; box-sizing: content-box; direction: ltr; float: none; font-family: monaco, consolas, "bitstream vera sans mono", "courier new", courier, monospace; font-style: normal; font-weight: 400; height: auto; left: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; right: auto; text-align: left; top: auto; vertical-align: baseline; white-space: pre; width: auto;">
<code class="sql plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">GO</code></div>
<div class="line number6 index5 alt1" style="background-image: none; border-radius: 0px; border: 0px; bottom: auto; box-shadow: none; box-sizing: content-box; direction: ltr; float: none; font-family: monaco, consolas, "bitstream vera sans mono", "courier new", courier, monospace; font-style: normal; font-weight: 400; height: auto; left: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; right: auto; text-align: left; top: auto; vertical-align: baseline; white-space: pre; width: auto;">
<code class="sql plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">RECONFIGURE </code><code class="sql keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">WITH</code> <code class="sql plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">OVERRIDE</code></div>
<div class="line number7 index6 alt2" style="background-image: none; border-radius: 0px; border: 0px; bottom: auto; box-shadow: none; box-sizing: content-box; direction: ltr; float: none; font-family: monaco, consolas, "bitstream vera sans mono", "courier new", courier, monospace; font-style: normal; font-weight: 400; height: auto; left: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; right: auto; text-align: left; top: auto; vertical-align: baseline; white-space: pre; width: auto;">
<code class="sql plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">GO</code></div>
<div class="line number8 index7 alt1" style="background-image: none; border-radius: 0px; border: 0px; bottom: auto; box-shadow: none; box-sizing: content-box; direction: ltr; float: none; font-family: monaco, consolas, "bitstream vera sans mono", "courier new", courier, monospace; font-style: normal; font-weight: 400; height: auto; left: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; right: auto; text-align: left; top: auto; vertical-align: baseline; white-space: pre; width: auto;">
<code class="sql keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">EXEC</code> <code class="sql plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">sys.sp_configure </code><code class="sql string" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: blue !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">'show advanced options'</code><code class="sql plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">, </code><code class="sql string" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: blue !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">'0'</code></div>
<div class="line number9 index8 alt2" style="background-image: none; border-radius: 0px; border: 0px; bottom: auto; box-shadow: none; box-sizing: content-box; direction: ltr; float: none; font-family: monaco, consolas, "bitstream vera sans mono", "courier new", courier, monospace; font-style: normal; font-weight: 400; height: auto; left: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; right: auto; text-align: left; top: auto; vertical-align: baseline; white-space: pre; width: auto;">
<code class="sql plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">RECONFIGURE </code><code class="sql keyword" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: rgb(0, 102, 153) !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; font-weight: bold !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">WITH</code> <code class="sql plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">OVERRIDE</code></div>
<div class="line number10 index9 alt1" style="background-image: none; border-radius: 0px; border: 0px; bottom: auto; box-shadow: none; box-sizing: content-box; direction: ltr; float: none; font-family: monaco, consolas, "bitstream vera sans mono", "courier new", courier, monospace; font-style: normal; height: auto; left: auto; line-height: 1.1em; margin: 0px; outline: 0px; overflow: visible; padding: 0px 1em; position: static; right: auto; text-align: left; top: auto; vertical-align: baseline; white-space: pre; width: auto;">
<div style="font-weight: 400;">
<code class="sql plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;">GO</code></div>
<div style="font-weight: 400;">
<code class="sql plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"><br /></code></div>
<h2 style="box-sizing: inherit; color: #222222; font-family: "helvetica neue", Helvetica, Roboto, Arial, sans-serif; font-size: 16px; line-height: 1.6; margin-bottom: 1.25rem; padding: 0px; text-align: left; text-rendering: optimizelegibility; white-space: normal;">
<b>For Example Using a table named tblTransactionTable.</b></h2>
<div style="box-sizing: inherit; color: #222222; font-family: "helvetica neue", Helvetica, Roboto, Arial, sans-serif; font-size: 16px; line-height: 1.6; margin-bottom: 1.25rem; padding: 0px; text-rendering: optimizelegibility; white-space: normal;">
The image below displays the sp_spaceused command and the output from the command to display table information such as space used for the table and the underlying indexes. This table has 11.7 million rows and there are no indexes currently on this table.</div>
<div class="imageborder" style="box-sizing: inherit; color: #222222; font-family: "helvetica neue", Helvetica, Roboto, Arial, sans-serif; font-size: 16px; font-weight: 400; margin: 0px 0px 20px; padding: 0px; text-align: center; white-space: normal;">
<img alt="sql query" src="https://www.mssqltips.com/tipimages2/1940_Image1.png" style="box-sizing: inherit; display: inline-block; height: auto; max-width: 100%; vertical-align: middle;" /></div>
<h2 style="box-sizing: inherit; color: #cc3300; font-family: "helvetica neue", Helvetica, Roboto, Arial, sans-serif; font-weight: 400; line-height: 1.4; margin: 0.2rem 0px 0.5rem; padding: 0px; text-rendering: optimizelegibility; white-space: normal;">
Step 1 - High Fill Factor Value</h2>
<div style="box-sizing: inherit; color: #222222; font-family: "helvetica neue", Helvetica, Roboto, Arial, sans-serif; font-size: 16px; font-weight: 400; line-height: 1.6; margin-bottom: 1.25rem; padding: 0px; text-rendering: optimizelegibility; white-space: normal;">
From SQL Server Management Studio, I am creating a non-clustered index with one column, CustomerID, as the Index Key.</div>
<div class="imageborder" style="box-sizing: inherit; color: #222222; font-family: "helvetica neue", Helvetica, Roboto, Arial, sans-serif; font-size: 16px; font-weight: 400; margin: 0px 0px 20px; padding: 0px; text-align: center; white-space: normal;">
<img alt="new index" src="https://www.mssqltips.com/tipimages2/1940_Image2.png" style="box-sizing: inherit; display: inline-block; height: auto; max-width: 100%; vertical-align: middle;" /></div>
<div style="box-sizing: inherit; color: #222222; font-family: "helvetica neue", Helvetica, Roboto, Arial, sans-serif; font-size: 16px; font-weight: 400; line-height: 1.6; margin-bottom: 1.25rem; padding: 0px; text-rendering: optimizelegibility; white-space: normal;">
I then move to the Options page and set the Fill Factor to 100 %. If the value for the fill factor is not set, the index is created using the value from the default index fill factor value set at the instance level. Remember that a value of 0 and 100 are both same and the page will be completely filled. For this example we will ignore the other options on this page. I finish creating the index by clicking OK.</div>
<div class="imageborder" style="box-sizing: inherit; color: #222222; font-family: "helvetica neue", Helvetica, Roboto, Arial, sans-serif; font-size: 16px; font-weight: 400; margin: 0px 0px 20px; padding: 0px; text-align: center; white-space: normal;">
<img alt="new index" src="https://www.mssqltips.com/tipimages2/1940_Image3.png" style="box-sizing: inherit; display: inline-block; height: auto; max-width: 100%; vertical-align: middle;" /></div>
<div style="box-sizing: inherit; color: #222222; font-family: "helvetica neue", Helvetica, Roboto, Arial, sans-serif; font-size: 16px; font-weight: 400; line-height: 1.6; margin-bottom: 1.25rem; padding: 0px; text-rendering: optimizelegibility; white-space: normal;">
After creating the index, I run sp_spaceused again to view the table information and find that the index size is now 327040 KB, where before it was 72KB before we created the index.</div>
<div class="imageborder" style="box-sizing: inherit; color: #222222; font-family: "helvetica neue", Helvetica, Roboto, Arial, sans-serif; font-size: 16px; font-weight: 400; margin: 0px 0px 20px; padding: 0px; text-align: center; white-space: normal;">
<img alt="sql query" src="https://www.mssqltips.com/tipimages2/1940_Image4.png" style="box-sizing: inherit; display: inline-block; height: auto; max-width: 100%; vertical-align: middle;" /></div>
<div style="box-sizing: inherit; color: #222222; font-family: "helvetica neue", Helvetica, Roboto, Arial, sans-serif; font-size: 16px; font-weight: 400; line-height: 1.6; margin-bottom: 1.25rem; padding: 0px; text-rendering: optimizelegibility; white-space: normal;">
You might choose a high fill factor value when there is very little or no change in the underlying table's data, such as a decision support system where data modification is not frequent, but on a regular and scheduled basis. Such a fill factor value would be better, since it creates an index smaller in size and hence queries can retrieve the required data with less disk I/O operations since it has to read less pages.</div>
<div style="box-sizing: inherit; color: #222222; font-family: "helvetica neue", Helvetica, Roboto, Arial, sans-serif; font-size: 16px; font-weight: 400; line-height: 1.6; margin-bottom: 1.25rem; padding: 0px; text-rendering: optimizelegibility; white-space: normal;">
On the other hand if you have an index that is constantly changing you would want to have a lower value to keep some free space available for new index entries. Otherwise SQL Server would have to constantly do page splits to fit the new values into the index pages.</div>
<h2 style="box-sizing: inherit; color: #cc3300; font-family: "helvetica neue", Helvetica, Roboto, Arial, sans-serif; font-weight: 400; line-height: 1.4; margin: 0.2rem 0px 0.5rem; padding: 0px; text-rendering: optimizelegibility; white-space: normal;">
Step 2 - Low Fill Factor Value</h2>
<div style="box-sizing: inherit; color: #222222; font-family: "helvetica neue", Helvetica, Roboto, Arial, sans-serif; font-size: 16px; font-weight: 400; line-height: 1.6; margin-bottom: 1.25rem; padding: 0px; text-rendering: optimizelegibility; white-space: normal;">
Next, I drop the index and create the same index with a change in the fill factor value set to 50. When I created the index earlier (in Step 1), I chose to script the create index task which is used in this step (image below) which will change the fill factor value to 50%.</div>
<div class="imageborder" style="box-sizing: inherit; color: #222222; font-family: "helvetica neue", Helvetica, Roboto, Arial, sans-serif; font-size: 16px; font-weight: 400; margin: 0px 0px 20px; padding: 0px; text-align: center; white-space: normal;">
<img alt="sql query" src="https://www.mssqltips.com/tipimages2/1940_Image5.png" style="box-sizing: inherit; display: inline-block; height: auto; max-width: 100%; vertical-align: middle;" /></div>
<div style="box-sizing: inherit; color: #222222; font-family: "helvetica neue", Helvetica, Roboto, Arial, sans-serif; font-size: 16px; font-weight: 400; line-height: 1.6; margin-bottom: 1.25rem; padding: 0px; text-rendering: optimizelegibility; white-space: normal;">
The resulting index will be created with pages being 50% full. I check and confirm this by running sp_spaceused again which displays the table details. The image below is the result set displayed when the query is executed. Notice that the index size is now 651640 KB which is approximately twice the size of the index created earlier using fill factor as 100. Which makes sense since we left the pages half empty.</div>
<div class="imageborder" style="box-sizing: inherit; color: #222222; font-family: "helvetica neue", Helvetica, Roboto, Arial, sans-serif; font-size: 16px; font-weight: 400; margin: 0px 0px 20px; padding: 0px; text-align: center; white-space: normal;">
<img alt="sql query" src="https://www.mssqltips.com/tipimages2/1940_Image6.png" style="box-sizing: inherit; display: inline-block; height: auto; max-width: 100%; vertical-align: middle;" /></div>
<div style="box-sizing: inherit; color: #222222; font-family: "helvetica neue", Helvetica, Roboto, Arial, sans-serif; font-size: 16px; font-weight: 400; line-height: 1.6; margin-bottom: 1.25rem; padding: 0px; text-rendering: optimizelegibility; white-space: normal;">
Using a fill factor value of less than 100 would be necessary in situations where data is added to the underlying indexes more frequently. With new data records added, the index pages need to have sufficient space to take the new entries. When there is not enough space a page split needs to occur which could impact performance depending on how frequently page splits need to occur.</div>
<h2 style="box-sizing: inherit; color: #cc3300; font-family: "helvetica neue", Helvetica, Roboto, Arial, sans-serif; font-weight: 400; line-height: 1.4; margin: 0.2rem 0px 0.5rem; padding: 0px; text-rendering: optimizelegibility; white-space: normal;">
Some points to be remembered</h2>
<div style="font-weight: 400;">
<code class="sql plain" style="background: none !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; box-shadow: none !important; box-sizing: content-box !important; color: black !important; direction: ltr !important; display: inline !important; float: none !important; font-family: Monaco, Consolas, "Bitstream Vera Sans Mono", "Courier New", Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.1em !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"></code></div>
<ul style="box-sizing: inherit; color: #222222; font-family: "helvetica neue", Helvetica, Roboto, Arial, sans-serif; font-size: 16px; font-weight: 400; line-height: 1.6; list-style-position: outside; margin: 0px 0px 1.25rem 1.1rem; padding: 0px; white-space: normal;">
<li style="box-sizing: inherit; margin: 0px; padding: 0px;">When 0 or 100 is specified, the page is filled to the maximum possible, not 100 % since a page may not accommodate the Index Key the final time and results in leaving that much empty space unused.</li>
<li style="box-sizing: inherit; margin: 0px; padding: 0px;">The size of the index is proportional to the columns that are used in the index, hence more columns (the limit being 16) will create a larger index and therefore require more index pages to store the index</li>
<li style="box-sizing: inherit; margin: 0px; padding: 0px;">Since indexes need storage space, creating an appropriate index along with the fill factor should be well planned.</li>
<li style="box-sizing: inherit; margin: 0px; padding: 0px;">Before finalizing a fill factor value, it should be tested and analyzed before deploying on the actual SQL Server.</li>
</ul>
</div>
</span></em></div>
</div>
</div>
योगेश चन्द्र उप्रेतीhttp://www.blogger.com/profile/17009545633002694199noreply@blogger.com0tag:blogger.com,1999:blog-8545307170893600943.post-36286751816079289632019-11-16T11:45:00.002-08:002019-11-16T11:45:58.214-08:00What is bit, nibble, byte, kilobyte, megabyte, gigabyte, terabyte, petabyte, exabyte, zettabyte, yottabyte etc.?<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<h1 itemprop="headline" style="background-color: white; border-bottom-color: rgb(218, 218, 218); border-bottom-style: solid; border-image: initial; border-left-color: initial; border-left-style: initial; border-right-color: initial; border-right-style: initial; border-top-color: initial; border-top-style: initial; border-width: 0px 0px 1px; clear: both; font-family: arial, helvetica, sans-serif; font-size: 1.96em; line-height: 1.1; margin: 0px -1.2em 0px -0.65em; padding: 0px 0px 0.16em;">
How much is 1 byte, kilobyte, megabyte, gigabyte, etc.?</h1>
<div class="updated" style="background-color: white; color: #b9b9b9; font-family: Verdana, sans-serif; font-size: 0.9em; margin: 0.25em 0px 2.5em -0.7em;">
<span itemprop="author publisher creator" itemscope="" itemtype="https://schema.org/Organization">Thanks to Computer Hope for sharing this knowledge.</span></div>
<div class="intro" style="background-color: white; border: 0px; color: #454545; font-family: Verdana, sans-serif; font-size: 1.2em !important; line-height: 1.5em !important; margin-bottom: 2em; margin-right: 14em; padding: 0px;">
Below is a list of each of the accepted disk drive space values. It is important to realize that not all manufacturers and developers list their value using binary, which is <a href="https://www.computerhope.com/jargon/b/binary.htm" style="border-bottom: 1px solid rgb(219, 219, 219); color: #663366; text-decoration-line: none;">base 2</a>. For example, a manufacturer may list a product's capacity as one gigabyte (1,000,000,000 bytes, a metric value) and not 1,073,741,824 bytes (<a href="https://www.computerhope.com/jargon/g/gibibyte.htm" style="border-bottom: 1px solid rgb(219, 219, 219); color: #663366; text-decoration-line: none;">gibibyte</a>) that it actually is. For this page, we are using the "common names" and listing all values in base 2.</div>
<div class="tip" style="background-color: #fffbce; color: #454545; display: table; font-family: Verdana, sans-serif; font-size: 16.1px; margin: 3.2em 14em 2em 0px; padding: 0px 2em 2em;">
<span class="title" style="background: rgb(255, 237, 0); display: inline-block; font-weight: 600; left: -15px; padding: 0.6em 2em; position: relative; top: -20px;">Note</span><div style="border: 0px; font-family: inherit; font-size: 1.1em; line-height: 1.7em; padding: 0px;">
All values are listed as <a href="https://www.computerhope.com/jargon/w/wholnumb.htm" style="border-bottom: 1px solid rgb(219, 219, 219); color: #663366; text-decoration-line: none;">whole numbers</a>, which means a GB shows it can only contain one 650 MB CD. Technically, 1 GB could hold 1.5753 CDs worth of data, but this document isn't meant to show you how many "parts" of an object a value can hold. Therefore, we are omitting decimal values. More plainly, you can only fit one complete 650 MB CD on a 1 GB drive since two full 650 MB discs exceed 1 GB.</div>
</div>
<div class="tip" style="background-color: #fffbce; color: #454545; display: table; font-family: Verdana, sans-serif; font-size: 16.1px; margin: 3.2em 14em 2em 0px; padding: 0px 2em 2em;">
<span class="title" style="background: rgb(255, 237, 0); display: inline-block; font-weight: 600; left: -15px; padding: 0.6em 2em; position: relative; top: -20px;">Tip</span><div style="border: 0px; font-family: inherit; font-size: 1.1em; line-height: 1.7em; padding: 0px;">
Except for a bit and a nibble, all values explained below are in bytes and not bits. For example, a kilobyte (KB) is different than a <a href="https://www.computerhope.com/jargon/k/kilobit.htm" style="border-bottom: 1px solid rgb(219, 219, 219); color: #663366; text-decoration-line: none;">kilobit</a> (Kb). When referring to storage, bytes are used whereas data transmission speeds are measured in bits.</div>
</div>
<h2 style="background-color: white; border: 0px; clear: left; font-family: arial, helvetica, sans-serif; font-size: 1.6em; line-height: 1; margin: 2.2em 0px 0.8em; padding: 0px;">
Bit</h2>
<img alt="Computer bit and byte" class="floatRight" height="250" src="https://www.computerhope.com/cdn/bit-byte.jpg" style="background-color: white; border: 0px; color: #454545; float: right; font-family: Verdana, sans-serif; font-size: 16.1px; line-height: 1; margin: 0.3em -4.3em 4em 4em; padding: 0px;" width="300" /><br />
<div class="tab" style="background-color: white; border: 0px; color: #454545; font-family: Verdana, sans-serif; font-size: 1.1em; line-height: 1.7em; margin-bottom: 2em; margin-left: 2.5em !important; margin-right: 14em; padding: 0px;">
A <a href="https://www.computerhope.com/jargon/b/bit.htm" style="border-bottom: 1px solid rgb(219, 219, 219); color: #663366; text-decoration-line: none;">bit</a> is a value of either a 1 or 0 (on or off).</div>
<h2 style="background-color: white; border: 0px; clear: left; font-family: arial, helvetica, sans-serif; font-size: 1.6em; line-height: 1; margin: 2.2em 0px 0.8em; padding: 0px;">
Nibble</h2>
<div class="tab" style="background-color: white; border: 0px; color: #454545; font-family: Verdana, sans-serif; font-size: 1.1em; line-height: 1.7em; margin-bottom: 2em; margin-left: 2.5em !important; margin-right: 14em; padding: 0px;">
A <a href="https://www.computerhope.com/jargon/n/nibble.htm" style="border-bottom: 1px solid rgb(219, 219, 219); color: #663366; text-decoration-line: none;">nibble</a> is 4 bits.</div>
<h2 style="background-color: white; border: 0px; clear: left; font-family: arial, helvetica, sans-serif; font-size: 1.6em; line-height: 1; margin: 2.2em 0px 0.8em; padding: 0px;">
Byte</h2>
<div class="tab" style="background-color: white; border: 0px; color: #454545; font-family: Verdana, sans-serif; font-size: 1.1em; line-height: 1.7em; margin-bottom: 2em; margin-left: 2.5em !important; margin-right: 14em; padding: 0px;">
Today, a <a href="https://www.computerhope.com/jargon/b/byte.htm" style="border-bottom: 1px solid rgb(219, 219, 219); color: #663366; text-decoration-line: none;">byte</a> is 8 bits.</div>
<ul style="background-color: white; border: 0px; color: #454545; font-family: Verdana, sans-serif; font-size: 16.1px; line-height: 1; margin: 0px 14em 2em 0px; padding: 0px;">
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>1</b> <a href="https://www.computerhope.com/jargon/c/charact.htm" style="border-bottom: 1px solid rgb(219, 219, 219); color: #663366; text-decoration-line: none;">character</a>, e.g., "a", is one byte.</li>
</ul>
<h2 style="background-color: white; border: 0px; clear: left; font-family: arial, helvetica, sans-serif; font-size: 1.6em; line-height: 1; margin: 2.2em 0px 0.8em; padding: 0px;">
Kilobyte (KB)</h2>
<div class="tab" style="background-color: white; border: 0px; color: #454545; font-family: Verdana, sans-serif; font-size: 1.1em; line-height: 1.7em; margin-bottom: 2em; margin-left: 2.5em !important; margin-right: 14em; padding: 0px;">
A <a href="https://www.computerhope.com/jargon/k/kilobyte.htm" style="border-bottom: 1px solid rgb(219, 219, 219); color: #663366; text-decoration-line: none;">kilobyte</a> is 1,024 bytes.</div>
<ul style="background-color: white; border: 0px; color: #454545; font-family: Verdana, sans-serif; font-size: 16.1px; line-height: 1; margin: 0px 14em 2em 0px; padding: 0px;">
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>2 or 3</b> paragraphs of text.</li>
</ul>
<h2 style="background-color: white; border: 0px; clear: left; font-family: arial, helvetica, sans-serif; font-size: 1.6em; line-height: 1; margin: 2.2em 0px 0.8em; padding: 0px;">
Megabyte (MB)</h2>
<div class="tab" style="background-color: white; border: 0px; color: #454545; font-family: Verdana, sans-serif; font-size: 1.1em; line-height: 1.7em; margin-bottom: 2em; margin-left: 2.5em !important; margin-right: 14em; padding: 0px;">
A <a href="https://www.computerhope.com/jargon/m/megabyte.htm" style="border-bottom: 1px solid rgb(219, 219, 219); color: #663366; text-decoration-line: none;">megabyte</a> is 1,048,576 bytes or 1,024 kilobytes.</div>
<ul style="background-color: white; border: 0px; color: #454545; font-family: Verdana, sans-serif; font-size: 16.1px; line-height: 1; margin: 0px 14em 2em 0px; padding: 0px;">
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>873</b> pages of <a href="https://www.computerhope.com/jargon/p/plaintex.htm" style="border-bottom: 1px solid rgb(219, 219, 219); color: #663366; text-decoration-line: none;">plain text</a> (1,200 characters).</li>
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>4</b> books (200 pages or 240,000 characters).</li>
</ul>
<h2 style="background-color: white; border: 0px; clear: left; font-family: arial, helvetica, sans-serif; font-size: 1.6em; line-height: 1; margin: 2.2em 0px 0.8em; padding: 0px;">
Gigabyte (GB)</h2>
<div class="tab" style="background-color: white; border: 0px; color: #454545; font-family: Verdana, sans-serif; font-size: 1.1em; line-height: 1.7em; margin-bottom: 2em; margin-left: 2.5em !important; margin-right: 14em; padding: 0px;">
A <a href="https://www.computerhope.com/jargon/g/gigabyte.htm" style="border-bottom: 1px solid rgb(219, 219, 219); color: #663366; text-decoration-line: none;">gigabyte</a> is 1,073,741,824 (2<sup>30</sup>) bytes. 1,024 megabytes, or 1,048,576 kilobytes.</div>
<ul style="background-color: white; border: 0px; color: #454545; font-family: Verdana, sans-serif; font-size: 16.1px; line-height: 1; margin: 0px 14em 2em 0px; padding: 0px;">
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>894,784</b> pages of plain text (1,200 characters).</li>
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>4,473</b> books (200 pages or 240,000 characters).</li>
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>640</b> <a href="https://www.computerhope.com/jargon/w/webpage.htm" style="border-bottom: 1px solid rgb(219, 219, 219); color: #663366; text-decoration-line: none;">web pages</a> (with 1.6 MB average file size).</li>
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>341</b> <a href="https://www.computerhope.com/jargon/p/picture.htm" style="border-bottom: 1px solid rgb(219, 219, 219); color: #663366; text-decoration-line: none;">digital pictures</a> (with 3 MB average file size).</li>
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>256</b> <a href="https://www.computerhope.com/jargon/m/mp3.htm" style="border-bottom: 1px solid rgb(219, 219, 219); color: #663366; text-decoration-line: none;">MP3</a> audio files (with 4 MB average file size).</li>
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>1</b> 650 MB <a href="https://www.computerhope.com/jargon/c/compactd.htm" style="border-bottom: 1px solid rgb(219, 219, 219); color: #663366; text-decoration-line: none;">CD</a>.</li>
</ul>
<h2 style="background-color: white; border: 0px; clear: left; font-family: arial, helvetica, sans-serif; font-size: 1.6em; line-height: 1; margin: 2.2em 0px 0.8em; padding: 0px;">
Terabyte (TB)</h2>
<div class="tab" style="background-color: white; border: 0px; color: #454545; font-family: Verdana, sans-serif; font-size: 1.1em; line-height: 1.7em; margin-bottom: 2em; margin-left: 2.5em !important; margin-right: 14em; padding: 0px;">
A <a href="https://www.computerhope.com/jargon/t/terabyte.htm" style="border-bottom: 1px solid rgb(219, 219, 219); color: #663366; text-decoration-line: none;">terabyte</a> is 1,099,511,627,776 (2<sup>40</sup>) bytes, 1,024 gigabytes, or 1,048,576 megabytes.</div>
<ul style="background-color: white; border: 0px; color: #454545; font-family: Verdana, sans-serif; font-size: 16.1px; line-height: 1; margin: 0px 14em 2em 0px; padding: 0px;">
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>916,259,689</b> pages of plain text (1,200 characters).</li>
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>4,581,298</b> books (200 pages or 240,000 characters).</li>
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>655,360</b> web pages (with 1.6 MB average file size).</li>
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>349,525</b> digital pictures (with 3 MB average file size).</li>
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>262,144</b> MP3 audio files (with 4 MB average file size).</li>
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>1,613</b> 650 MB CD's.</li>
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>233</b> 4.38 GB <a href="https://www.computerhope.com/jargon/d/dvd.htm" style="border-bottom: 1px solid rgb(219, 219, 219); color: #663366; text-decoration-line: none;">DVDs</a>.</li>
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>40</b> 25 GB <a href="https://www.computerhope.com/jargon/b/bd.htm" style="border-bottom: 1px solid rgb(219, 219, 219); color: #663366; text-decoration-line: none;">Blu-ray</a> discs.</li>
</ul>
<h2 style="background-color: white; border: 0px; clear: left; font-family: arial, helvetica, sans-serif; font-size: 1.6em; line-height: 1; margin: 2.2em 0px 0.8em; padding: 0px;">
Petabyte (PB)</h2>
<div class="tab" style="background-color: white; border: 0px; color: #454545; font-family: Verdana, sans-serif; font-size: 1.1em; line-height: 1.7em; margin-bottom: 2em; margin-left: 2.5em !important; margin-right: 14em; padding: 0px;">
A <a href="https://www.computerhope.com/jargon/p/petabyte.htm" style="border-bottom: 1px solid rgb(219, 219, 219); color: #663366; text-decoration-line: none;">petabyte</a> is 1,125,899,906,842,624 (2<sup>50</sup>) bytes, 1,024 terabytes, 1,048,576 gigabytes, or 1,073,741,824 megabytes.</div>
<ul style="background-color: white; border: 0px; color: #454545; font-family: Verdana, sans-serif; font-size: 16.1px; line-height: 1; margin: 0px 14em 2em 0px; padding: 0px;">
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>938,249,922,368</b> pages of plain text (1,200 characters).</li>
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>4,691,249,611</b> books (200 pages or 240,000 characters).</li>
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>671,088,640</b> web pages (with 1.6 MB average file size).</li>
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>357,913,941</b> digital pictures (with 3 MB average file size).</li>
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>268,435,456</b> MP3 audio files (with 4 MB average file size).</li>
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>1,651,910</b> 650 MB CD's.</li>
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>239,400</b> 4.38 GB DVDs.</li>
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>41,943</b> 25 GB Blu-ray discs.</li>
</ul>
<h2 style="background-color: white; border: 0px; clear: left; font-family: arial, helvetica, sans-serif; font-size: 1.6em; line-height: 1; margin: 2.2em 0px 0.8em; padding: 0px;">
Exabyte (EB)</h2>
<div class="tab" style="background-color: white; border: 0px; color: #454545; font-family: Verdana, sans-serif; font-size: 1.1em; line-height: 1.7em; margin-bottom: 2em; margin-left: 2.5em !important; margin-right: 14em; padding: 0px;">
An <a href="https://www.computerhope.com/jargon/e/exabyte.htm" style="border-bottom: 1px solid rgb(219, 219, 219); color: #663366; text-decoration-line: none;">exabyte</a> is 1,152,921,504,606,846,976 (2<sup>60</sup>) bytes, 1,024 petabytes, 1,048,576 terabytes, 1,073,741,824 gigabytes, or 1,099,511,627,776 megabytes.</div>
<ul style="background-color: white; border: 0px; color: #454545; font-family: Verdana, sans-serif; font-size: 16.1px; line-height: 1; margin: 0px 14em 2em 0px; padding: 0px;">
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>960,767,920,505,705</b> pages of plain text (1,200 characters).</li>
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>4,803,839,602,528</b> books (200 pages or 240,000 characters).</li>
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>687,194,767,360</b> web pages (with 1.6 MB average file size).</li>
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>366,503,875,925</b> digital pictures (with 3 MB average file size).</li>
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>274,877,906,944</b> MP3 audio files (with 4 MB average file size).</li>
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>1,691,556,350</b> 650 MB CD's.</li>
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>245,146,535</b> 4.38 GB DVDs.</li>
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>42,949,672</b> 25 GB Blu-ray discs.</li>
</ul>
<h2 style="background-color: white; border: 0px; clear: left; font-family: arial, helvetica, sans-serif; font-size: 1.6em; line-height: 1; margin: 2.2em 0px 0.8em; padding: 0px;">
Zettabyte (ZB)</h2>
<div class="tab" style="background-color: white; border: 0px; color: #454545; font-family: Verdana, sans-serif; font-size: 1.1em; line-height: 1.7em; margin-bottom: 2em; margin-left: 2.5em !important; margin-right: 14em; padding: 0px;">
A <a href="https://www.computerhope.com/jargon/z/zettabyt.htm" style="border-bottom: 1px solid rgb(219, 219, 219); color: #663366; text-decoration-line: none;">zettabyte</a> is 1,180,591,620,717,411,303,424 (2<sup>70</sup>) bytes, 1,024 exabytes, 1,048,576 petabytes, 1,073,741,824 terabytes, 1,099,511,627,776 gigabytes, or 1,125,899,910,000,000 megabytes.</div>
<ul style="background-color: white; border: 0px; color: #454545; font-family: Verdana, sans-serif; font-size: 16.1px; line-height: 1; margin: 0px 14em 2em 0px; padding: 0px;">
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>983,826,350,597,842,752</b> pages of plain text (1,200 characters).</li>
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>4,919,131,752,989,213</b> books (200 pages or 240,000 characters).</li>
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>703,687,443,750,000</b> web pages (with 1.6 MB average file size).</li>
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>375,299,970,000,000</b> digital pictures (with 3 MB average file size).</li>
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>281,474,977,500,000</b> MP3 audio files (with 4 MB average file size).</li>
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>1,732,153,707,691</b> 650 MB CD's.</li>
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>251,030,052,003</b> 4.38 GB DVDs.</li>
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>43,980,465,111</b> 25 GB Blu-ray discs.</li>
</ul>
<h2 style="background-color: white; border: 0px; clear: left; font-family: arial, helvetica, sans-serif; font-size: 1.6em; line-height: 1; margin: 2.2em 0px 0.8em; padding: 0px;">
Yottabyte (YB)</h2>
<div class="tab" style="background-color: white; border: 0px; color: #454545; font-family: Verdana, sans-serif; font-size: 1.1em; line-height: 1.7em; margin-bottom: 2em; margin-left: 2.5em !important; margin-right: 14em; padding: 0px;">
A <a href="https://www.computerhope.com/jargon/y/yottabyt.htm" style="border-bottom: 1px solid rgb(219, 219, 219); color: #663366; text-decoration-line: none;">yottabyte</a> is 1,208,925,819,614,629,174,706,176 (2<sup>80</sup>) bytes, 1,024 zettabytes, 1,048,576 exabytes, 1,073,741,824 petabytes, 1,099,511,627,776 terabytes, 1,125,899,910,000,000 gigabytes, or 1,152,921,500,000,000,000 megabytes.</div>
<ul style="background-color: white; border: 0px; color: #454545; font-family: Verdana, sans-serif; font-size: 16.1px; line-height: 1; margin: 0px 14em 2em 0px; padding: 0px;">
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>1,007,438,183,012,190,978,921</b> pages of plain text (1,200 characters).</li>
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>5,037,190,915,060,954,894</b> books (200 pages or 240,000 characters).</li>
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>720,575,937,500,000,000</b> web pages (with 1.6 MB average file size).</li>
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>384,307,166,666,666,666</b> digital pictures (with 3 MB average file size).</li>
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>288,230,375,000,000,000</b> MP3 audio files (with 4 MB average file size).</li>
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>1,773,725,384,615,384</b> 650 MB CD's.</li>
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>257,054,773,251,740</b> 4.38 GB DVDs.</li>
<li style="border: 0px; font-family: inherit; font-size: 1.1rem; line-height: 2.2; margin: 0px 0px 0px 4em; padding: 0px;"><b>45,035,996,273,704</b> 25 GB Blu-ray discs.</li>
</ul>
<h2 style="background-color: white; border: 0px; clear: left; font-family: arial, helvetica, sans-serif; font-size: 1.6em; line-height: 1; margin: 2.2em 0px 0.8em; padding: 0px;">
Is there anything bigger than a yottabyte?</h2>
<div class="tab" style="background-color: white; border: 0px; color: #454545; font-family: Verdana, sans-serif; font-size: 1.1em; line-height: 1.7em; margin-bottom: 2em; margin-left: 2.5em !important; margin-right: 14em; padding: 0px;">
As of 2019, there are no approved standard sizes for anything bigger than a yottabyte. However, the two standards that have been proposed are the <a href="https://www.computerhope.com/jargon/h/hellabyte.htm" style="border-bottom: 1px solid rgb(219, 219, 219); color: #663366; text-decoration-line: none;">hellabyte</a> or <a href="https://www.computerhope.com/jargon/b/brontobyte.htm" style="border-bottom: 1px solid rgb(219, 219, 219); color: #663366; text-decoration-line: none;">brontobyte</a>.</div>
</div>
योगेश चन्द्र उप्रेतीhttp://www.blogger.com/profile/17009545633002694199noreply@blogger.com1tag:blogger.com,1999:blog-8545307170893600943.post-32366145122566518202019-10-29T01:23:00.000-07:002019-10-29T01:23:09.563-07:00C# Interview Questions and Answers<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<div align="center" style="box-sizing: border-box;">
Here is a list of most popular C# interview questions and answers with their details explaination and code examples. The interview questions are for both, beginners and professional C# developers. </div>
<div style="box-sizing: border-box;">
</div>
</div>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
1. What is C#?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
C# is a computer programming language. C# was created by Microsoft in 2000 to provide a modern general-purpose programming language that can be used to develop all kind of software targeting various platforms including Windows, Web, and Mobile using just one programming language. Today, C# is one of the most popular programming languages in the world. Millions of software developers use C# to build all kind of software. </div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
C# is the primary language for building Microsoft .NET software applications. Developers can build almost every kind of software using C# including Windows UI apps, console apps, backend services, cloud APIs, Web services, controls and libraries, serverless applications, Web applications, native iOS and Android apps, AI and machine learning software, and blockchain applications.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
If you want to learn more about C# application types, read,</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<ul style="box-sizing: border-box;">
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/UploadFile/mahesh/what-can-C-Sharp-do-for-you/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">What can C# Do For You</a></li>
</ul>
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
C# with the help of Visual Studio IDE, provides a rapid application development. C# is a modern, object-oriented, simple, versatile, and performance-oriented programming language. C# is developed based on the best features and use cases of several programming languages including C++, Java, Pascal, and SmallTalk. </div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
C# syntaxes are like C++. .NET and C# library is similar to Java. C# supports modern object-oriented programming language features including Abstraction, Encapsulation, Polymorphism, and Inheritance. C# is a strongly typed language and most types are inherited by the Object class.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
C# supports concepts of classes and objects. Classes have members such as fields, properties, events, and methods. Here is a detailed article on C# and OOP. </div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
</div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/UploadFile/84c85b/object-oriented-programming-using-C-Sharp-net/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Object Oriented Programming using C#.NET</a></li>
</ul>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
C# is versatile, modern and supports modern programming needs. Since its inception, C# language has gone through various upgrades. C# 8.0 is the latest build of C# language that is expected to release this year. Read the following article for some of the newest features of C#: </div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
</div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/article/c-sharp-7-0-and-c-sharp-7-1-new-features-part-two/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">C# 7.0, 7.1, 7.2 and 7.3 and New Features</a></li>
</ul>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
2. What is an object in C#?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
C# language is an object-oriented programming language. Classes are the foundation of C#. A class is a template that defines how a data structure will look like, how data will be stored, managed, and transferred. A class has fields, properties, methods, and other members.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
While classes are concepts, objects are real. Objects are created using class instances. A class defines the type of an object. Objects store real values in computer memory.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Any real-world entity, which can have some characteristics, or which can perform some work is called as Object. This object is also called as an instance i.e. a copy of an entity in programming language. Objects are instances of classes.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
For example, we need to create a program that deals with cars. We need to create an entity for the car. Let’s call it a class, Car. A car has four properties, i.e., model, type, color, and size.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
To represent a car in programming, we can create a class Car with four properties, Model, Type, Color, and Size. These are called members of a class. A class has several types of members, constructors, fields, properties, methods, delegates, and events. A class member can be private, protected, and pubic. Since these properties may be accessed outside the class, these can be public.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
An object is an instance of a class. A class can have as many instances as needed. For example, Honda Civic is an instance of Car. In real programming, Honda Civic is an object. Honda Civic is an instance of class Car. The Model, Type, Color, and Size properties of Honda Civic are Civic, Honda, Red, 4 respectively. BMW 330, Toyota Carolla, Ford 350, Honda CR4, Honda Accord, and Honda Pilot are some examples of objects of Car.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
To learn more about real world example of objects and instance, please read</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
</div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/UploadFile/cda5ba/object-oriented-programming-with-real-world-scenario/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Object Oriented Programming with Real World Scenario</a></li>
</ul>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
3. What is Managed or Unmanaged Code?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<strong style="box-sizing: border-box;">Managed Code</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
“The code is the code that is developed using the .NET framework and its supported programming languages such as C# or VB.NET. The manage code is directly executed by the Common Language Runtime (CLR or Runtime) and its lifecycle including object creation, memory allocation, and object disposal is managed by the Runtime. Any language that is written in .NET Framework is managed code”.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><strong style="box-sizing: border-box;">Unmanaged Code</strong><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />The code, which is developed outside of the .NET framework is known as unmanaged code.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />“Applications that do not run under the control of the CLR are said to be unmanaged. The languages such as C or C++ or Visual Basic are unmanaged. The object creation, execution, and disposal on unmanaged code is directly managed by the programmers. If programmers write bad code, it may lead to the memory leaks and unwanted resource allocations.”<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />The .NET Framework provides a mechanism for unmanaged code to be used in managed code and vice versa. The process is done with the help of wrapper classes.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Here is a detailed article on managed and unmanaged code.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
</div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/uploadfile/puranindia/managed-code-and-unmanaged-code-in-net/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Managed Code And Unmanaged Code In .NET</a></li>
</ul>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
4. What is Boxing and Unboxing in C#?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
Boxing and Unboxing both are used for type conversions.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
The process of converting from a value type to a reference type is called boxing. Boxing is an implicit conversion. Here is an example of boxing in C#.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
</div>
<div class="dp-highlighter" style="background-color: #e7e5dc; box-sizing: border-box; color: #212121; font-family: Consolas, "Courier New", Courier, mono, serif; font-size: 16px; margin: 18px 0px !important; overflow: auto; padding-top: 1px; width: 815.094px;">
<div class="bar" style="box-sizing: border-box; padding-left: 45px;">
</div>
<ol class="dp-c" start="1" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="comment" style="background-color: inherit; border: none; box-sizing: border-box; color: #008200; margin: 0px; padding: 0px;">// Boxing</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">int</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> anum = 123; </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">Object obj = anum; </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">Console.WriteLine(anum); </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">Console.WriteLine(obj); </span></li>
</ol>
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
The process of converting from a reference type to a value type is called unboxing. Here is an example of unboxing in C#.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
</div>
<div class="dp-highlighter" style="background-color: #e7e5dc; box-sizing: border-box; color: #212121; font-family: Consolas, "Courier New", Courier, mono, serif; font-size: 16px; margin: 18px 0px !important; overflow: auto; padding-top: 1px; width: 815.094px;">
<div class="bar" style="box-sizing: border-box; padding-left: 45px;">
</div>
<ol class="dp-c" start="1" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="comment" style="background-color: inherit; border: none; box-sizing: border-box; color: #008200; margin: 0px; padding: 0px;">// Unboxing</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">Object obj2 = 123; </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">int</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> anum2 = (</span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">int</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">)obj; </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">Console.WriteLine(anum2); </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">Console.WriteLine(obj); </span></li>
</ol>
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Check out these two articles to learn more about boxing and unboxing.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
</div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/article/boxing-and-unboxing-in-C-Sharp/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Boxing and Unboxing in C#</a></li>
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/uploadfile/puranindia/type-conversions-in-C-Sharp/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Type Conversions in C#</a></li>
</ul>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
5. What is the difference between a struct and a class in C#?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<strong style="box-sizing: border-box;">Answer</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Class and struct both are the user defined data type but have some major differences:<br style="box-sizing: border-box;" /><strong style="box-sizing: border-box;"><br style="box-sizing: border-box;" />Struct</strong></div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;">The struct is value type in C# and it inherits from System.Value Type.</li>
<li style="box-sizing: border-box;">Struct is usually used for smaller amounts of data.</li>
<li style="box-sizing: border-box;">Struct can’t be inherited to other types.</li>
<li style="box-sizing: border-box;">A structure can't be abstract.</li>
<li style="box-sizing: border-box;">No need to create object by new keyword.</li>
<li style="box-sizing: border-box;">Do not have permission to create any default constructor.</li>
</ul>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Class</strong></div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;">The class is reference type in C# and it inherits from the System.Object Type.</li>
<li style="box-sizing: border-box;">Classes are usually used for large amounts of data.</li>
<li style="box-sizing: border-box;">Classes can be inherited to other class.</li>
<li style="box-sizing: border-box;">A class can be abstract type.</li>
<li style="box-sizing: border-box;">We can create a default constructor.</li>
</ul>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Read the following articles to learn more on structs vs classes.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
</div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/UploadFile/yougerthen/what-really-make-difference-between-structures-and-classes/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Some Real Differences Between Structures and Classes</a></li>
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/blogs/difference-between-struct-and-class-in-c-sharp" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Struct and Class Differences in C#</a></li>
</ul>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
</div>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
6. What is the difference between Interface and Abstract Class in C#?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<strong style="box-sizing: border-box;">Answer</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Here are some of the common differences between an interface and an abstract class in C#.</div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;">A class can implement any number of interfaces but a subclass can at most use only one abstract class.</li>
<li style="box-sizing: border-box;">An abstract class can have non-abstract methods (concrete methods) while in case of interface all the methods has to be abstract.</li>
<li style="box-sizing: border-box;">An abstract class can declare or use any variables while an interface is not allowed to do so.</li>
<li style="box-sizing: border-box;">In an abstract class all data member or functions are private by default while in interface all are public, we can’t change them manually.</li>
<li style="box-sizing: border-box;">In an abstract class we need to use abstract keyword to declare abstract methods while in an interface we don’t need to use that.</li>
<li style="box-sizing: border-box;">An abstract class can’t be used for multiple inheritance while interface can be used as multiple inheritance.</li>
<li style="box-sizing: border-box;">An abstract class use constructor while in an interface we don’t have any type of constructor.</li>
</ul>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
To learn more about the difference between an abstract class and an interface, visit the following articles.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
</div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/uploadfile/prasoonk/abstract-class-vs-interface/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Abstract Class vs Interface</a></li>
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/UploadFile/c210df/explore-interface-vs-abstract-class/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Explore Interface Vs Abstract Class</a></li>
</ul>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
7. What is enum in C#?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<strong style="box-sizing: border-box;">Answer</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
An enum is a value type with a set of related named constants often referred to as an enumerator list. The enum keyword is used to declare an enumeration. It is a primitive data type, which is user defined.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />An enum type can be an integer (float, int, byte, double etc.). But if you use it beside int it has to be cast.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />An enum is used to create numeric constants in .NET framework. All the members of enum are of enum type. Their must be a numeric value for each enum type.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />The default underlying type of the enumeration element is int. By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1. </div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<div class="dp-highlighter" style="background-color: #e7e5dc; box-sizing: border-box; font-family: Consolas, "Courier New", Courier, mono, serif; margin: 18px 0px !important; overflow: auto; padding-top: 1px; width: 815.094px;">
<div class="bar" style="box-sizing: border-box; padding-left: 45px;">
</div>
<ol class="dp-c" start="1" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">enum</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Dow {Sat, Sun, Mon, Tue, Wed, Thu, Fri}; </span></span></li>
</ol>
</div>
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Some points about enum</strong></div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;">Enums are enumerated data type in c#.</li>
<li style="box-sizing: border-box;">Enums are not for the end-user, they are meant for developers.</li>
<li style="box-sizing: border-box;">Enums are strongly typed constant. They are strongly typed, i.e. an enum of one type may not be implicitly assigned to an enum of another type even though the underlying value of their members are the same.</li>
<li style="box-sizing: border-box;">Enumerations (enums) make your code much more readable and understandable.</li>
<li style="box-sizing: border-box;">Enum values are fixed. Enum can be displayed as a string and processed as an integer.</li>
<li style="box-sizing: border-box;">The default type is int, and the approved types are byte, sbyte, short, ushort, uint, long, and ulong.</li>
<li style="box-sizing: border-box;">Every enum type automatically derives from System.Enum and thus we can use System.Enum methods on enums.</li>
<li style="box-sizing: border-box;">Enums are value types and are created on the stack and not on the heap.</li>
</ul>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
For more details follow the link,</div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/uploadfile/puranindia/enums-in-C-Sharp/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Enums in C#</a></li>
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/UploadFile/3d39b4/enumeration-in-C-Sharp/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Enumeration In C#</a></li>
</ul>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
8. What is the difference between “continue” and “break” statements in C#?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<strong style="box-sizing: border-box;">Answer</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Using break statement, you can 'jump out of a loop' whereas by using continue statement, you can 'jump over one iteration' and then resume your loop execution.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><strong style="box-sizing: border-box;">Eg. Break Statement </strong> </div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<div class="dp-highlighter" style="background-color: #e7e5dc; box-sizing: border-box; font-family: Consolas, "Courier New", Courier, mono, serif; margin: 18px 0px !important; overflow: auto; padding-top: 1px; width: 815.094px;">
<div class="bar" style="box-sizing: border-box; padding-left: 45px;">
</div>
<ol class="dp-c" start="1" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">using</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> System; </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">using</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> System.Collections; </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">using</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> System.Linq; </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">using</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> System.Text; </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">namespace</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> break_example { </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Class brk_stmt { </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">public</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">static</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">void</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> main(String[] args) { </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">for</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> (</span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">int</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> i = 0; i <= 5; i++) { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">if</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> (i == 4) { </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">break</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">; </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Console.WriteLine(<span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"The number is "</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> + i); </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Console.ReadLine(); </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">} </span></li>
</ol>
</div>
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Output </strong><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><em style="box-sizing: border-box;">The number is 0; </em><em style="box-sizing: border-box;"><br style="box-sizing: border-box;" />The number is 1; <br style="box-sizing: border-box;" />The number is 2; <br style="box-sizing: border-box;" />The number is 3;</em></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Eg. Continue Statement</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<div class="dp-highlighter" style="background-color: #e7e5dc; box-sizing: border-box; font-family: Consolas, "Courier New", Courier, mono, serif; margin: 18px 0px !important; overflow: auto; padding-top: 1px; width: 815.094px;">
<div class="bar" style="box-sizing: border-box; padding-left: 45px;">
</div>
<ol class="dp-c" start="1" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">using</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> System; </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">using</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> System.Collections; </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">using</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> System.Linq; </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">using</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> System.Text; </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">namespace</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> continue_example { </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Class cntnu_stmt { </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">public</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">static</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">void</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> main(String[] { </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">for</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> (</span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">int</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> i = 0; i <= 5; i++) { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">if</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> (i == 4) { </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">continue</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">; </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Console.WriteLine(“The number <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">is</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> "+ i); </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Console.ReadLine(); </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
</ol>
</div>
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Output</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<em style="box-sizing: border-box;">The number is 1;</em><em style="box-sizing: border-box;"><br style="box-sizing: border-box;" />The number is 2;<br style="box-sizing: border-box;" />The number is 3;<br style="box-sizing: border-box;" />The number is 5;</em><strong style="box-sizing: border-box;"><br style="box-sizing: border-box;" /></strong><br style="box-sizing: border-box;" />For more details follow link,</div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/blogs/difference-between-break-statement-and-continue-statement-in-c-sharp1" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Difference Between Break Statement and Continue Statement in C#</a></li>
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/UploadFile/2072a9/break-and-continue-statements-in-C-Sharp/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Break and Continue Statements in C#</a></li>
</ul>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
</div>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
9. What is the difference between constant and readonly in C#?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<strong style="box-sizing: border-box;">Answer</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Const is nothing but "constant", a variable of which the value is constant but at compile time. And it's mandatory to assign a value to it. By default, a const is static and we cannot change the value of a const variable throughout the entire program.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Readonly is the keyword whose value we can change during runtime or we can assign it at run time but only through the non-static constructor.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">See the example</strong><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />We have a Test Class in which we have two variables, one is readonly and other is a constant.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<div class="dp-highlighter" style="background-color: #e7e5dc; box-sizing: border-box; font-family: Consolas, "Courier New", Courier, mono, serif; margin: 18px 0px !important; overflow: auto; padding-top: 1px; width: 815.094px;">
<div class="bar" style="box-sizing: border-box; padding-left: 45px;">
</div>
<ol class="dp-c" start="1" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">class</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Test { </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">readonly</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">int</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> read = 10; </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">const</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">int</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> cons = 10; </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">public</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Test() { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> read = 100; </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> cons = 100; </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">public</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">void</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Check() { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Console.WriteLine(<span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"Read only : {0}"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">, read); </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Console.WriteLine(<span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"const : {0}"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">, cons); </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">} </span></li>
</ol>
</div>
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Here I was trying to change the value of both the variables in constructor but when I am trying to change the constant it gives an error to change their value in that block which I have to call at run time.<br style="box-sizing: border-box;" /></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; text-align: center;">
<img alt="ASP.NET" class="" data-src="https://www.c-sharpcorner.com/UploadFile/puranindia/C-Sharp-interview-questions/Images/image003.jpg" src="https://www.c-sharpcorner.com/UploadFile/puranindia/C-Sharp-interview-questions/Images/image003.jpg" style="box-sizing: border-box; max-width: 100%;" /></div>
<br style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;" /><span style="background-color: white; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">So finally remove that line of code from class and call this Check() function like the following code snippet,</span><br />
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<br /></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<div class="dp-highlighter" style="background-color: #e7e5dc; box-sizing: border-box; font-family: Consolas, "Courier New", Courier, mono, serif; margin: 18px 0px !important; overflow: auto; padding-top: 1px; width: 815.094px;">
<div class="bar" style="box-sizing: border-box; padding-left: 45px;">
</div>
<ol class="dp-c" start="1" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">class</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Program { </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">static</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">void</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Main(</span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">string</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">[] args) { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Test obj = <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Test(); </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> obj.Check(); </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Console.ReadLine(); </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">} </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">class</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Test { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">readonly</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">int</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> read = 10; </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">const</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">int</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> cons = 10; </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">public</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Test() { </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> read = 100; </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">public</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">void</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Check() { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Console.WriteLine(<span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"Read only : {0}"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">, read); </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Console.WriteLine(<span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"const : {0}"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">, cons); </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">} </span></li>
</ol>
</div>
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Output</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; text-align: center;">
<img alt="ASP.NET" class="" data-src="https://www.c-sharpcorner.com/UploadFile/puranindia/C-Sharp-interview-questions/Images/image004.png" src="https://www.c-sharpcorner.com/UploadFile/puranindia/C-Sharp-interview-questions/Images/image004.png" style="box-sizing: border-box; max-width: 100%;" /> </div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Learn more about const and readonly here:</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
</div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/UploadFile/c210df/difference-between-const-readonly-and-static-readonly-in-C-Sharp/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Difference Between Const, ReadOnly and Static ReadOnly in C#</a></li>
</ul>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
10. What is the difference between ref and out keywords?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<strong style="box-sizing: border-box;">Answer</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
The ref keyword passes arguments by reference. It means any changes made to this argument in the method will be reflected in that variable when control returns to the calling method.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
The out keyword passes arguments by reference. This is very similar to the ref keyword.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<br /></div>
<div align="center" style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<img alt="ASP.NET" class="" data-src="https://www.c-sharpcorner.com/UploadFile/puranindia/C-Sharp-interview-questions/Images/image005.jpg" src="https://www.c-sharpcorner.com/UploadFile/puranindia/C-Sharp-interview-questions/Images/image005.jpg" style="box-sizing: border-box; max-width: 100%;" /></div>
<br style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;" /><span style="background-color: white; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">To learn more about ref and out keywords, read the following article:</span><br />
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<br /></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
</div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/UploadFile/ff2f08/ref-vs-out-keywords-in-C-Sharp/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Ref Vs Out Keywords in C#</a></li>
</ul>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
</div>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
11. Can “this” be used within a static method?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<strong style="box-sizing: border-box;">Answer</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
We can't use this in a static method because the keyword 'this' returns a reference to the current instance of the class containing it. Static methods (or any static member) do not belong to a particular instance. They exist without creating an instance of the class and are called with the name of a class not by instance, so we can’t use this keyword in the body of static Methods, but in the case of Extension Methods we can use it the functions parameters.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Let’s have a look at the “this” keyword.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />The "this" keyword in C# is a special type of reference variable that is implicitly defined within each constructor and non-static method as a first parameter of the type class in which it is defined.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Learn more here on <a href="https://www.c-sharpcorner.com/UploadFile/puranindia/this-keyword-in-C-Sharp/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">The this Keyword In C#</a>.</div>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
12. What are Properties in C#?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<strong style="box-sizing: border-box;">Answer</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
C# properties are members of a C# class that provide a flexible mechanism to read, write or compute the values of private fields, in other words, by using properties, we can access private fields and set their values. Properties in C# are always public data members. C# properties use get and set methods, also known as accessors to access and assign values to private fields.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><strong style="box-sizing: border-box;">Now the question is what are accessors?</strong><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />The get and set portions or blocks of a property are called accessors. These are useful to restrict the accessibility of a property, the set accessor specifies that we can assign a value to a private field in a property and without the set accessor property it is like a readonly field. By the get accessor we can access the value of the private field, in other words, it returns a single value. A Get accessor specifies that we can access the value of a field publically.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />We have three types of properties: Read/Write, ReadOnly and WriteOnly. Let's see each one by one.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Learn more here: <a href="https://www.c-sharpcorner.com/UploadFile/3d39b4/property-in-C-Sharp/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Property in C#</a></div>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
13. What are extension methods in C#?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<strong style="box-sizing: border-box;">Answer</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<strong style="box-sizing: border-box;">What are extension methods?</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Extension methods enable you to add methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. <br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />An extension method is a special kind of static method, but they are called as if they were instance methods on the extended type.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">How to use extension methods?</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
An extension method is a static method of a static class, where the "this" modifier is applied to the first parameter. The type of the first parameter will be the type that is extended.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />Extension methods are only in scope when you explicitly import the namespace into your source code with a using directive.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
For more details on extension methods, you can read these articles,</div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/uploadfile/puranindia/extension-methods-in-C-Sharp-3-0/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Extension Methods in C#</a></li>
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/UploadFile/3d39b4/extension-method-in-C-Sharp/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Extension Method In C#</a></li>
</ul>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
14. What is the difference between dispose and finalize methods in C#?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<strong style="box-sizing: border-box;">Answer</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
The finalize and dispose, both methods are used to free unmanaged resources. Here are the difference between the two.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Finalize</strong></div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;">Finalize used to free unmanaged resources those are not in use like files, database connections in application domain and more, held by an object before that object is destroyed.</li>
<li style="box-sizing: border-box;">In the Internal process it is called by Garbage Collector and can’t called manual by user code or any service.</li>
<li style="box-sizing: border-box;">Finalize belongs to System.Object class.</li>
<li style="box-sizing: border-box;">Implement it when you have unmanaged resources in your code, and make sure that these resources are freed when the Garbage collection happens.</li>
</ul>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Dispose</strong></div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;">Dispose is also used to free unmanaged resources those are not in use like files, database connections in Application domain at any time.</li>
<li style="box-sizing: border-box;">Dispose explicitly it is called by manual user code.</li>
<li style="box-sizing: border-box;">If we need to dispose method so must implement that class by IDisposable interface.</li>
<li style="box-sizing: border-box;">It belongs to IDisposable interface.</li>
<li style="box-sizing: border-box;">Implement this when you are writing a custom class that will be used by other users.</li>
</ul>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
For more details follow this link,</div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/UploadFile/nityaprakash/back-to-basics-dispose-vs-finalize/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Back To Basics - Dispose Vs Finalize</a></li>
</ul>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
15. What is the difference between string and StringBuilder in C#?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<strong style="box-sizing: border-box;">Answer</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
StringBuilder and string both use to string value but both have many differences on the bases of instance creation and also for performance:<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><strong style="box-sizing: border-box;">String</strong><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />String is an immutable object. Immutable like when we create string object in code so we cannot modify or change that object in any operations like insert new value, replace or append any value with existing value in string object, when we have to do some operations to change string simply it will dispose the old value of string object and it will create new instance in memory for hold the new value in string object like,</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; text-align: center;">
<img alt="ASP.NET" class="" data-src="https://www.c-sharpcorner.com/UploadFile/puranindia/C-Sharp-interview-questions/Images/image008.png" src="https://www.c-sharpcorner.com/UploadFile/puranindia/C-Sharp-interview-questions/Images/image008.png" style="box-sizing: border-box; max-width: 100%;" /> </div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<br style="box-sizing: border-box;" /><strong style="box-sizing: border-box;">Note</strong></div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;">It’s an immutable object that hold string value.</li>
<li style="box-sizing: border-box;">Performance wise string is slow because its’ create a new instance to override or change the previous value.</li>
<li style="box-sizing: border-box;">String belongs to System namespace.</li>
</ul>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">StringBuilder</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
System.Text.Stringbuilder is mutable object which also hold the string value, mutable means once we create a System.Text.Stringbuilder object we can use this object for any operation like insert value in existing string with insert functions also replace or append without creating new instance of System.Text.Stringbuilder for every time so it’s use the previous object so it’s work fast as compare than System.String. Let’s have an example to understand System.Text.Stringbuilder like,</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; text-align: center;">
<img alt="ASP.NET" class="" data-src="https://www.c-sharpcorner.com/UploadFile/puranindia/C-Sharp-interview-questions/Images/image009.png" src="https://www.c-sharpcorner.com/UploadFile/puranindia/C-Sharp-interview-questions/Images/image009.png" style="box-sizing: border-box; max-width: 100%;" /> </div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Note</strong></div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;">StringBuilder is a mutable object.</li>
<li style="box-sizing: border-box;">Performance wise StringBuilder is very fast because it will use same instance of StringBuilder object to perform any operation like insert value in existing string.</li>
<li style="box-sizing: border-box;">StringBuilder belongs to System.Text.Stringbuilder namespace.</li>
</ul>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
For More details read this article by following link:</div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/UploadFile/19b1bd/comparison-of-string-and-stringbuilder-in-C-Sharp/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Comparison of String and StringBuilder in C#</a></li>
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/uploadfile/jitendra1987/string-and-stringbuilder-classes/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">String and StringBuilder Classes</a></li>
</ul>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
16. What is delegates in C# and uses of delegates?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<strong style="box-sizing: border-box;">Answer</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
A Delegate is an abstraction of one or more function pointers (as existed in C++; the explanation about this is out of the scope of this article). The .NET has implemented the concept of function pointers in the form of delegates. With delegates, you can treat a function as data. Delegates allow functions to be passed as parameters, returned from a function as a value and stored in an array. Delegates have the following characteristics,</div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;">Delegates are derived from the System.MulticastDelegate class.</li>
<li style="box-sizing: border-box;">They have a signature and a return type. A function that is added to delegates must be compatible with this signature.</li>
<li style="box-sizing: border-box;">Delegates can point to either static or instance methods.</li>
<li style="box-sizing: border-box;">Once a delegate object has been created, it may dynamically invoke the methods it points to at runtime.</li>
<li style="box-sizing: border-box;">Delegates can call methods synchronously and asynchronously.</li>
</ul>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
The delegate contains a couple of useful fields. The first one holds a reference to an object, and the second holds a method pointer. When you invoke the delegate, the instance method is called on the contained reference. However, if the object reference is null then the runtime understands this to mean that the method is a static method. Moreover, invoking a delegate syntactically is the exact same as calling a regular function. Therefore, delegates are perfect for implementing callbacks.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><strong style="box-sizing: border-box;">Why Do We Need Delegates</strong><br style="box-sizing: border-box;" />Historically, the Windows API made frequent use of C-style function pointers to create callback functions. Using a callback, programmers were able to configure one function to report back to another function in the application. So the objective of using a callback is to handle button-clicking, menu-selection, and mouse-moving activities. But the problem with this traditional approach is that the callback functions were not type-safe. In the .NET framework, callbacks are still possible using delegates with a more efficient approach. Delegates maintain three important pieces of information, as in the following,</div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;">The parameters of the method.</li>
<li style="box-sizing: border-box;">The address of the method it calls.</li>
<li style="box-sizing: border-box;">The return type of the method.</li>
</ul>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
A delegate is a solution for situations in which you want to pass methods around to other methods. You are so accustomed to passing data to methods as parameters that the idea of passing methods as an argument instead of data might sound a little strange. However, there are cases in which you have a method that does something, for instance, invoking some other method. You do not know at compile time what this second methods is. That information is available only at runtime hence Delegates are the device to overcome such complications.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Learn more about <a href="https://www.c-sharpcorner.com/UploadFile/84c85b/delegates-and-events-C-Sharp-net/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Delegates and Events in C# .NET</a></div>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
17. What are sealed classes in C#?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<strong style="box-sizing: border-box;">Answer</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Sealed classes are used to restrict the inheritance feature of object oriented programming. Once a class is defined as a sealed class, the class cannot be inherited. <br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />In C#, the sealed modifier is used to define a class as sealed. In Visual Basic .NET the Not Inheritable keyword serves the purpose of sealed. If a class is derived from a sealed class then the compiler throws an error. <br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />If you have ever noticed, structs are sealed. You cannot derive a class from a struct. <br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />The following class definition defines a sealed class in C#,</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<div class="dp-highlighter" style="background-color: #e7e5dc; box-sizing: border-box; font-family: Consolas, "Courier New", Courier, mono, serif; margin: 18px 0px !important; overflow: auto; padding-top: 1px; width: 815.094px;">
<div class="bar" style="box-sizing: border-box; padding-left: 45px;">
</div>
<ol class="dp-c" start="1" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="comment" style="background-color: inherit; border: none; box-sizing: border-box; color: #008200; margin: 0px; padding: 0px;">// Sealed class </span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">sealed</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">class</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> SealedClass </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">{ </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">} </span></li>
</ol>
</div>
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Learn more about sealed classes here: <a href="https://www.c-sharpcorner.com/article/sealed-class-in-C-Sharp/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Sealed Class in C#</a></div>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
18. What are partial classes?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<strong style="box-sizing: border-box;">Answer</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
A partial class is only use to splits the definition of a class in two or more classes in a same source code file or more than one source files. You can create a class definition in multiple files but it will be compiled as one class at run time and also when you’ll create an instance of this class so you can access all the methods from all source file with a same object.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />Partial Classes can be create in the same namespace it’s doesn’t allowed to create a partial class in different namespace. So use “partial” keyword with all the class name which you want to bind together with the same name of class in same namespace, let’s have an example,</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; text-align: center;">
<img alt="ASP.NET" class="" data-src="https://www.c-sharpcorner.com/UploadFile/puranindia/C-Sharp-interview-questions/Images/image011.png" src="https://www.c-sharpcorner.com/UploadFile/puranindia/C-Sharp-interview-questions/Images/image011.png" style="box-sizing: border-box; max-width: 100%;" /> </div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<br style="box-sizing: border-box;" />To learn about partial classes, visit <a href="https://www.c-sharpcorner.com/UploadFile/3d39b4/partial-classes-in-C-Sharp-with-real-example/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Partial Classes in C# With Real Example</a>.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
</div>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
19. What is boxing and unboxing in C#?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<strong style="box-sizing: border-box;">Answer</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Boxing and Unboxing both using for type converting but have some difference:<br style="box-sizing: border-box;" /><strong style="box-sizing: border-box;"><br style="box-sizing: border-box;" />Boxing</strong><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />Boxing is the process of converting a value type data type to the object or to any interface data type which is implemented by this value type. When the CLR boxes a value means when CLR converting a value type to Object Type, it wraps the value inside a System.Object and stores it on the heap area in application domain. <br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><strong style="box-sizing: border-box;">Example</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<div align="center" style="box-sizing: border-box;">
<img alt="ASP.NET" class="" data-src="https://www.c-sharpcorner.com/UploadFile/puranindia/C-Sharp-interview-questions/Images/image001.png" src="https://www.c-sharpcorner.com/UploadFile/puranindia/C-Sharp-interview-questions/Images/image001.png" style="box-sizing: border-box; max-width: 100%;" /></div>
<br style="box-sizing: border-box;" /><strong style="box-sizing: border-box;">Unboxing</strong><strong style="box-sizing: border-box;"><br style="box-sizing: border-box;" /></strong><br style="box-sizing: border-box;" />Unboxing is also a process which is use to extracts the value type from the object or any implemented interface type. Boxing may be done implicit but unboxing have to be explicit by code. <br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><strong style="box-sizing: border-box;">Example</strong><strong style="box-sizing: border-box;"><br style="box-sizing: border-box;" /></strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<div align="center" style="box-sizing: border-box;">
<img alt="ASP.NET" class="" data-src="https://www.c-sharpcorner.com/UploadFile/puranindia/C-Sharp-interview-questions/Images/image002.png" src="https://www.c-sharpcorner.com/UploadFile/puranindia/C-Sharp-interview-questions/Images/image002.png" style="box-sizing: border-box; max-width: 100%;" /></div>
<br style="box-sizing: border-box;" />The concept of boxing and unboxing underlies the C# unified view of the type system in which a value of any type can be treated as an object.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
To learn more about boxing and unboding, visit <a href="https://www.c-sharpcorner.com/article/boxing-and-unboxing-in-C-Sharp/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Boxing and Unboxing in C#.</a></div>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
20. What is IEnumerable<> in C#?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<strong style="box-sizing: border-box;">Answer</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
IEnumerable is the parent interface for all non-generic collections in System.Collections namespace like ArrayList, HastTable etc. that can be enumerated. For the generic version of this interface as IEnumerable<t> which a parent interface of all generic collections class in System.Collections.Generic namespace like List<> and more. <br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />In System.Collections.Generic.IEnumerable<t> have only a single method which is GetEnumerator() that returns an IEnumerator. IEnumerator provides the power to iterate through the collection by exposing a Current property and Move Next and Reset methods, if we doesn’t have this interface as a parent so we can’t use iteration by foreach loop or can’t use that class object in our LINQ query.</t></t></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; text-align: center;">
<img alt="ASP.NET" class="" data-src="https://www.c-sharpcorner.com/UploadFile/puranindia/C-Sharp-interview-questions/Images/image012.jpg" src="https://www.c-sharpcorner.com/UploadFile/puranindia/C-Sharp-interview-questions/Images/image012.jpg" style="box-sizing: border-box; max-width: 100%;" /> </div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<br style="box-sizing: border-box;" />For more details, visit <a href="https://www.c-sharpcorner.com/UploadFile/dacca2/implement-ienumerable-interface-in-C-Sharp/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Implement IEnumerable Interface in C#</a>.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
</div>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
21. What is difference between late binding and early binding in C#?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<strong style="box-sizing: border-box;">Answer</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Early Binding and late Binding concepts belong to polymorphism in C#. Polymorphism is the feature of object oriented programming that allows a language to use same name in different forms. For example, a method name Add that can add integers, doubles, and decimals.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Polymorphism we have 2 different types to achieve that: </div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;">Compile Time also known as Early Binding or Overloading.</li>
<li style="box-sizing: border-box;">Run Time also known as Late Binding or Overriding.</li>
</ul>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Compile Time Polymorphism or Early Binding</strong><strong style="box-sizing: border-box;"><br style="box-sizing: border-box;" /></strong>In Compile time polymorphism or Early Binding we will use multiple methods with same name but different type of parameter or may be the number or parameter because of this we can perform different-different tasks with same method name in the same class which is also known as Method overloading.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />See how we can do that by the following example,</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; text-align: center;">
<img alt="ASP.NET" class="" data-src="https://www.c-sharpcorner.com/UploadFile/puranindia/C-Sharp-interview-questions/Images/image013.png" src="https://www.c-sharpcorner.com/UploadFile/puranindia/C-Sharp-interview-questions/Images/image013.png" style="box-sizing: border-box; max-width: 100%;" /> </div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Run Time Polymorphism or Late Binding</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Run time polymorphism also known as late binding, in Run Time polymorphism or Late Binding we can do use same method names with same signatures means same type or same number of parameters but not in same class because compiler doesn’t allowed that at compile time so we can use in derived class that bind at run time when a child class or derived class object will instantiated that’s way we says that Late Binding. For that we have to create my parent class functions as partial and in driver or child class as override functions with override keyword. <br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><strong style="box-sizing: border-box;">Like as following example,</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; text-align: center;">
<img alt="ASP.NET" class="" data-src="https://www.c-sharpcorner.com/UploadFile/puranindia/C-Sharp-interview-questions/Images/image014.png" src="https://www.c-sharpcorner.com/UploadFile/puranindia/C-Sharp-interview-questions/Images/image014.png" style="box-sizing: border-box; max-width: 100%;" /> </div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/UploadFile/ff2f08/understanding-polymorphism-in-C-Sharp/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Understanding Polymorphism in C#</a></li>
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/UploadFile/mkagrahari/polymorphism-in-net/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Polymorphism in .NET</a></li>
</ul>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
22. What are the differences between IEnumerable and IQueryable?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<strong style="box-sizing: border-box;">Answer</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Before we go into the differences, lets learn what the IEnumerable and IQueryable are.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><strong style="box-sizing: border-box;">IEnumerable</strong><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />Is the parent interface for all non-generic collections in System.Collections namespace like ArrayList, HastTable etc. that can be enumerated. For the generic version of this interface as IEnumerable<t> which a parent interface of all generic collections class in System.Collections.Generic namespace like List<> and more. <br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><strong style="box-sizing: border-box;">IQueryable</strong><strong style="box-sizing: border-box;"><br style="box-sizing: border-box;" /></strong><br style="box-sizing: border-box;" />As per MSDN IQueryable interface is intended for implementation by query providers. It is only supposed to be implemented by providers that also implement IQueryable<t>. If the provider does not also implement IQueryable<t>, the standard query operators cannot be used on the provider's data source.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />The IQueryable interface inherits the IEnumerable interface so that if it represents a query, the results of that query can be enumerated. Enumeration causes the expression tree associated with an IQueryable object to be executed. The definition of "executing an expression tree" is specific to a query provider. For example, it may involve translating the expression tree to an appropriate query language for the underlying data source. Queries that do not return enumerable results are executed when the Execute method is called.</t></t></t></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; text-align: center;">
<img alt="ASP.NET" class="" data-src="https://www.c-sharpcorner.com/UploadFile/puranindia/C-Sharp-interview-questions/Images/image015.jpg" src="https://www.c-sharpcorner.com/UploadFile/puranindia/C-Sharp-interview-questions/Images/image015.jpg" style="box-sizing: border-box; max-width: 100%;" /> </div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/UploadFile/a3d5d0/ienumerable-vs-iquerable/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">IEnumerable vs IQuerable</a></li>
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/UploadFile/rahul4_saxena/ienumerable-vs-iqueryable/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">IEnumerable Vs IQueryable</a></li>
</ul>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
23. What happens if the inherited interfaces have conflicting method names?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<strong style="box-sizing: border-box;">Answer</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
If we implement multiple interfaces in the same class with conflict method names so we don’t need to define all or in other words we can say if we have conflict methods in same class so we can’t implement their body independently in the same class coz of same name and same signature so we have to use interface name before method name to remove this method confiscation let’s see an example,</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<div class="dp-highlighter" style="background-color: #e7e5dc; box-sizing: border-box; font-family: Consolas, "Courier New", Courier, mono, serif; margin: 18px 0px !important; overflow: auto; padding-top: 1px; width: 815.094px;">
<div class="bar" style="box-sizing: border-box; padding-left: 45px;">
</div>
<ol class="dp-c" start="1" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">interface</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> testInterface1 { </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">void</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Show(); </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">} </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">interface</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> testInterface2 { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">void</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Show(); </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">} </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">class</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Abc: testInterface1, </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> testInterface2 { </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">void</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> testInterface1.Show() { </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Console.WriteLine(<span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"For testInterface1 !!"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">); </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">void</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> testInterface2.Show() { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Console.WriteLine(<span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"For testInterface2 !!"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">); </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
</ol>
</div>
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Now see how to use those in a class,</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<div class="dp-highlighter" style="background-color: #e7e5dc; box-sizing: border-box; font-family: Consolas, "Courier New", Courier, mono, serif; margin: 18px 0px !important; overflow: auto; padding-top: 1px; width: 815.094px;">
<div class="bar" style="box-sizing: border-box; padding-left: 45px;">
</div>
<ol class="dp-c" start="1" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">class</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Program { </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">static</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">void</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Main(</span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">string</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">[] args) { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> testInterface1 obj1 = <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Abc(); </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> testInterface1 obj2 = <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Abc(); </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> obj1.Show(); </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> obj2.Show(); </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Console.ReadLine(); </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">} </span></li>
</ol>
</div>
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Output</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; text-align: center;">
<img alt="ASP.NET" class="" data-src="https://www.c-sharpcorner.com/UploadFile/puranindia/C-Sharp-interview-questions/Images/image016.png" src="https://www.c-sharpcorner.com/UploadFile/puranindia/C-Sharp-interview-questions/Images/image016.png" style="box-sizing: border-box; max-width: 100%;" /> </div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<br style="box-sizing: border-box;" />For one more example follow the link:</div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/blogs/inherit-multiple-interfaces-and-they-have-conflicting-method-name1" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Inherit Multiple Interfaces and They have Conflicting Method Name</a></li>
</ul>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
24. What are the Arrays in C#?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<strong style="box-sizing: border-box;">Answer</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
In C#, an array index starts at zero. That means the first item of an array starts at the 0<sup style="box-sizing: border-box;">th</sup>position. The position of the last item on an array will total number of items - 1. So if an array has 10 items, the last 10<sup style="box-sizing: border-box;">th</sup> item is at 9<sup style="box-sizing: border-box;">th</sup> position. <br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />In C#, arrays can be declared as fixed length or dynamic.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
A <em style="box-sizing: border-box;">fixed length</em> array can store a predefined number of items.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
A <em style="box-sizing: border-box;">dynamic array</em> does not have a predefined size. The size of a <em style="box-sizing: border-box;">dynamic array</em> increases as you add new items to the array. You can declare an array of fixed length or dynamic. You can even change a dynamic array to static after it is defined.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Let's take a look at simple declarations of arrays in C#. The following code snippet defines the simplest dynamic array of integer types that do not have a fixed size.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<em style="box-sizing: border-box;">int[] intArray;</em></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<em style="box-sizing: border-box;">As you can see from the above code snippet, the declaration of an array starts with a type of array followed by a square bracket ([]) and name of the array.</em></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
The following code snippet declares an array that can store 5 items only starting from index 0 to 4. </div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<div class="dp-highlighter" style="background-color: #e7e5dc; box-sizing: border-box; font-family: Consolas, "Courier New", Courier, mono, serif; margin: 18px 0px !important; overflow: auto; padding-top: 1px; width: 815.094px;">
<div class="bar" style="box-sizing: border-box; padding-left: 45px;">
</div>
<ol class="dp-c" start="1" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">int</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">[] intArray; </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">intArray = <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">int</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">[5]; </span></span></li>
</ol>
</div>
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
The following code snippet declares an array that can store 100 items starting from index 0 to 99. </div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<div class="dp-highlighter" style="background-color: #e7e5dc; box-sizing: border-box; font-family: Consolas, "Courier New", Courier, mono, serif; margin: 18px 0px !important; overflow: auto; padding-top: 1px; width: 815.094px;">
<div class="bar" style="box-sizing: border-box; padding-left: 45px;">
</div>
<ol class="dp-c" start="1" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">int</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">[] intArray; </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">intArray = <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">int</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">[100]; </span></span></li>
</ol>
</div>
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Learn more about Arrays in C#: <a href="https://www.c-sharpcorner.com/article/working-with-arrays-in-C-Sharp/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Working with Arrays In C#</a></div>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
25. What is the Constructor Chaining in C#?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<strong style="box-sizing: border-box;">Answer</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
Constructor chaining is a way to connect two or more classes in a relationship as Inheritance, in Constructor Chaining every child class constructor is mapped to parent class Constructor implicitly by base keyword so when you create an instance of child class to it’ll call parent’s class Constructor without it inheritance is not possible.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />For more example follow the link:</div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/UploadFile/825933/constructor-chaining-in-C-Sharp/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Constructor Chaining In C#</a></li>
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/article/constructors-in-C-Sharp/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Constructors In C#</a> </li>
</ul>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
26. What’s the difference between the Array.CopyTo() and Array.Clone()?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<strong style="box-sizing: border-box;">Answer</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
The Array.Clone() method creates a shallow copy of an array. A shallow copy of an Array copies only the elements of the Array, whether they are reference types or value types, but it does not copy the objects that the references refer to. The references in the new Array point to the same objects that the references in the original Array point to.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />The CopyTo() static method of the Array class copies a section of an array to another array. The CopyTo method copies all the elements of an array to another one-dimension array. The code listed in Listing 9 copies contents of an integer array to an array of object types. <br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />Learn more about arrays here,</div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/article/working-with-arrays-in-C-Sharp/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Working with Arrays in C#</a></li>
</ul>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
27. Can Multiple Catch Blocks executed in c#?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<strong style="box-sizing: border-box;">Answer</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
We can use multiple catch blocks with a try statement. Each catch block can catch a different exception. The following code example shows how to implement multiple catch statements with a single try statement.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<div class="dp-highlighter" style="background-color: #e7e5dc; box-sizing: border-box; font-family: Consolas, "Courier New", Courier, mono, serif; margin: 18px 0px !important; overflow: auto; padding-top: 1px; width: 815.094px;">
<div class="bar" style="box-sizing: border-box; padding-left: 45px;">
</div>
<ol class="dp-c" start="1" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">using</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> System; </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">class</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> MyClient { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">public</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">static</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">void</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Main() { </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">int</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> x = 0; </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">int</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> div = 0; </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">try</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> div = 100 / x; </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Console.WriteLine(<span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"Not executed line"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">); </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">catch</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> (DivideByZeroException de) { </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Console.WriteLine(<span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"DivideByZeroException"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">); </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">catch</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> (Exception ee) { </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Console.WriteLine(<span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"Exception"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">); </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">finally</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> { </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Console.WriteLine(<span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"Finally Block"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">); </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Console.WriteLine(<span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"Result is {0}"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">, div); </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">} </span></li>
</ol>
</div>
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
To learn more about Exception Handling in C#, please visit:</div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/article/exception-handling-in-C-Sharp/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Exception Handling in C#</a></li>
</ul>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
28. What is Singleton Design Patterns and How to implement in C#?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<strong style="box-sizing: border-box;">Answer</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">What isSingleton Design Pattern?</strong> </div>
<ol style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;">Ensures a class has only one instance and provides a global point of access to it.</li>
<li style="box-sizing: border-box;">A singleton is a class that only allows a single instance of itself to be created, and usually gives simple access to that instance.</li>
<li style="box-sizing: border-box;">Most commonly, singletons don't allow any parameters to be specified when creating the instance, since a second request of an instance with a different parameter could be problematic! (If the same instance should be accessed for all requests with the same parameter then the factory pattern is more appropriate.)</li>
<li style="box-sizing: border-box;">There are various ways to implement the Singleton Pattern in C#. The following are the common characteristics of a Singleton Pattern.</li>
</ol>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;"><ul style="box-sizing: border-box;">
<li style="box-sizing: border-box;">A single constructor, that is private and parameterless.</li>
<li style="box-sizing: border-box;">The class is sealed.</li>
<li style="box-sizing: border-box;">A static variable that holds a reference to the single created instance, if any.</li>
<li style="box-sizing: border-box;">A public static means of getting the reference to the single created instance, creating one if necessary.</li>
</ul>
</ul>
<ol style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;"></ol>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">This is the example how to write the code with Singleton,</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<div class="dp-highlighter" style="background-color: #e7e5dc; box-sizing: border-box; font-family: Consolas, "Courier New", Courier, mono, serif; margin: 18px 0px !important; overflow: auto; padding-top: 1px; width: 815.094px;">
<div class="bar" style="box-sizing: border-box; padding-left: 45px;">
</div>
<ol class="dp-c" start="1" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">namespace</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Singleton { </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">class</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Program { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">static</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">void</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Main(</span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">string</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">[] args) { </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Calculate.Instance.ValueOne = 10.5; </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Calculate.Instance.ValueTwo = 5.5; </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Console.WriteLine(<span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"Addition : "</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> + Calculate.Instance.Addition()); </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Console.WriteLine(<span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"Subtraction : "</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> + Calculate.Instance.Subtraction()); </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Console.WriteLine(<span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"Multiplication : "</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> + Calculate.Instance.Multiplication()); </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Console.WriteLine(<span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"Division : "</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> + Calculate.Instance.Division()); </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Console.WriteLine(<span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"\n----------------------\n"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">); </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Calculate.Instance.ValueTwo = 10.5; </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Console.WriteLine(<span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"Addition : "</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> + Calculate.Instance.Addition()); </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Console.WriteLine(<span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"Subtraction : "</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> + Calculate.Instance.Subtraction()); </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Console.WriteLine(<span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"Multiplication : "</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> + Calculate.Instance.Multiplication()); </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Console.WriteLine(<span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"Division : "</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> + Calculate.Instance.Division()); </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Console.ReadLine(); </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">public</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">sealed</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">class</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Calculate { </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">private</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Calculate() {} </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">private</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">static</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Calculate instance = </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">null</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">; </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">public</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">static</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Calculate Instance { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">get</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> { </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">if</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> (instance == </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">null</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">) { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> instance = <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Calculate(); </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">return</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> instance; </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">public</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">double</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> ValueOne { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">get</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">; </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">set</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">; </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">public</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">double</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> ValueTwo { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">get</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">; </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">set</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">; </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">public</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">double</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Addition() { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">return</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> ValueOne + ValueTwo; </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">public</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">double</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Subtraction() { </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">return</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> ValueOne - ValueTwo; </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">public</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">double</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Multiplication() { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">return</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> ValueOne * ValueTwo; </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">public</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">double</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Division() { </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">return</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> ValueOne / ValueTwo; </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">} </span></li>
</ol>
</div>
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
To read more about Singleton in depth so follow the link:</div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/UploadFile/8911c4/singleton-design-pattern-in-C-Sharp/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Singleton Design Pattern in C#</a></li>
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/UploadFile/b430c7/implementing-singleton-design-patterns/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Implementing Singleton Design Patterns</a></li>
</ul>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
29. Difference between Throw Exception and Throw Clause</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<strong style="box-sizing: border-box;">Answer</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
The basic difference is that the Throw exception overwrites the stack trace and this makes it hard to find the original code line number that has thrown the exception.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />Throw basically retains the stack information and adds to the stack information in the exception that it is thrown.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />Let us see what it means rather speaking so many words to better understand the differences. I am using a console application to easily test and see how the usage of the two differ in their functionality.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<div class="dp-highlighter" style="background-color: #e7e5dc; box-sizing: border-box; font-family: Consolas, "Courier New", Courier, mono, serif; margin: 18px 0px !important; overflow: auto; padding-top: 1px; width: 815.094px;">
<div class="bar" style="box-sizing: border-box; padding-left: 45px;">
</div>
<ol class="dp-c" start="1" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">using</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> System; </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">using</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> System.Collections.Generic; </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">using</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> System.Linq; </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">using</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> System.Text; </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">namespace</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> TestingThrowExceptions { </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">class</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Program { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">public</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">void</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> ExceptionMethod() { </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">throw</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Exception(</span><span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"Original Exception occurred in ExceptionMethod"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">); </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">static</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">void</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Main(</span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">string</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">[] args) { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Program p = <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Program(); </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">try</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> p.ExceptionMethod(); </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">catch</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> (Exception ex) { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">throw</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> ex; </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">} </span></li>
</ol>
</div>
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Now run the code by pressing the F5 key of the keyboard and see what happens. It returns an exception and look at the stack trace:<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />To learn more about throw exceptions, please visit:</div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/UploadFile/akkiraju/difference-between-throw-exception-and-throw-clause/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Difference Between Throw Exception and Throw Clause</a></li>
</ul>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
30. What are Indexer in C#?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<strong style="box-sizing: border-box;">Answer</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
C# introduces a new concept known as Indexers which are used for treating an object as an array. The indexers are usually known as smart arrays in C#. They are not essential part of object-oriented programming.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />Defining an indexer allows you to create classes that act like virtual arrays. Instances of that class can be accessed using the [] array access operator.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><strong style="box-sizing: border-box;">Creating an Indexer</strong> </div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<div class="dp-highlighter" style="background-color: #e7e5dc; box-sizing: border-box; font-family: Consolas, "Courier New", Courier, mono, serif; margin: 18px 0px !important; overflow: auto; padding-top: 1px; width: 815.094px;">
<div class="bar" style="box-sizing: border-box; padding-left: 45px;">
</div>
<ol class="dp-c" start="1" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">< modifier > < </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">return</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> type > </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">this</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">[argument list] { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">get</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> { </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="comment" style="background-color: inherit; border: none; box-sizing: border-box; color: #008200; margin: 0px; padding: 0px;">// your get block code </span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">set</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="comment" style="background-color: inherit; border: none; box-sizing: border-box; color: #008200; margin: 0px; padding: 0px;">// your set block code </span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">} </span></li>
</ol>
</div>
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">In the above code,</strong><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><em style="box-sizing: border-box;"><modifier></modifier></em><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />can be private, public, protected or internal.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><em style="box-sizing: border-box;"><return type=""></return></em><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />can be any valid C# types.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />To learn more about indexers in C#, visit <a href="https://www.c-sharpcorner.com/uploadfile/puranindia/indexers-in-C-Sharp/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Indexers in C#</a>.</div>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
31. What is multicast delegate in c#?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<br style="box-sizing: border-box;" /><strong style="box-sizing: border-box;">Answer</strong><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />Delegate is one of the base types in .NET. Delegate is a class, which is used to create and invoke delegates at runtime.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
A delegate in C# allows developers to treat methods as objects and invoke them from their code.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Implement Multicast Delegates Example,</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<div class="dp-highlighter" style="background-color: #e7e5dc; box-sizing: border-box; font-family: Consolas, "Courier New", Courier, mono, serif; margin: 18px 0px !important; overflow: auto; padding-top: 1px; width: 815.094px;">
<div class="bar" style="box-sizing: border-box; padding-left: 45px;">
</div>
<ol class="dp-c" start="1" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">using</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> System; </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">using</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> System.Collections.Generic; </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">using</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> System.Linq; </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">using</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> System.Text; </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">delegate</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">void</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> MDelegate(); </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">class</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> DM { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">static</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">public</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">void</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Display() { </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Console.WriteLine(<span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"Meerut"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">); </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">static</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">public</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">void</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> print() { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Console.WriteLine(<span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"Roorkee"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">); </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">} </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">class</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> MTest { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">public</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">static</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">void</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Main() { </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> MDelegate m1 = <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> MDelegate(DM.Display); </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> MDelegate m2 = <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> MDelegate(DM.print); </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> MDelegate m3 = m1 + m2; </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> MDelegate m4 = m2 + m1; </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> MDelegate m5 = m3 - m2; </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> m3(); </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> m4(); </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> m5(); </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">} </span></li>
</ol>
</div>
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Learn more about delegates in C# here,</strong></div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/UploadFile/puranindia/C-Sharp-net-delegates-and-events/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Delegates in C#</a></li>
</ul>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
32. Difference between Equality Operator (==) and Equals() Method in C#</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Answer</strong><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />Both the == Operator and the Equals() method are used to compare two value type data items or reference type data items. The Equality Operator (==) is the comparison operator and the Equals() method compares the contents of a string. The == Operator compares the reference identity while the Equals() method compares only contents. Let’s see with some examples.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />In this example we assigned a string variable to another variable. A string is a reference type and in the following example, a string variable is assigned to another string variable so they are referring to the same identity in the heap and both have the same content so you get True output for both the == Operator and the Equals() method.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<div class="dp-highlighter" style="background-color: #e7e5dc; box-sizing: border-box; font-family: Consolas, "Courier New", Courier, mono, serif; margin: 18px 0px !important; overflow: auto; padding-top: 1px; width: 815.094px;">
<div class="bar" style="box-sizing: border-box; padding-left: 45px;">
</div>
<ol class="dp-c" start="1" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">using</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> System; </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">namespace</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> ComparisionExample { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">class</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Program { </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">static</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">void</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Main(</span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">string</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">[] args) { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">string</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> name = </span><span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"sandeep"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">; </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">string</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> myName = name; </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Console.WriteLine(<span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"== operator result is {0}"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">, name == myName); </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Console.WriteLine(<span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"Equals method result is {0}"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">, name.Equals(myName)); </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Console.ReadKey(); </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">} </span></li>
</ol>
</div>
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
For more details go with this following Links,</div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/UploadFile/3d39b4/difference-between-operator-and-equals-method-in-C-Sharp/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Difference Between Equality Operator ( ==) and Equals() Method in C#</a></li>
</ul>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
33. Difference between is and as operator in C#</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Answer</strong><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><strong style="box-sizing: border-box;">"is" operator</strong><strong style="box-sizing: border-box;"><br style="box-sizing: border-box;" /></strong><br style="box-sizing: border-box;" />In C# language, we use the "is" operator to check the object type. If two objects are of the same type, it returns true, else it returns false.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Let's understand this in our C# code. We declare two classes, Speaker and Author.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<div class="dp-highlighter" style="background-color: #e7e5dc; box-sizing: border-box; font-family: Consolas, "Courier New", Courier, mono, serif; margin: 18px 0px !important; overflow: auto; padding-top: 1px; width: 815.094px;">
<div class="bar" style="box-sizing: border-box; padding-left: 45px;">
</div>
<ol class="dp-c" start="1" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">class</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Speaker { </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">public</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">string</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Name { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">get</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">; </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">set</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">; </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">} </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">class</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Author { </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">public</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">string</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Name { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">get</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">; </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">set</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">; </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">} </span></li>
</ol>
</div>
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Now, let's create an object of type Speaker,</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<div class="dp-highlighter" style="background-color: #e7e5dc; box-sizing: border-box; font-family: Consolas, "Courier New", Courier, mono, serif; margin: 18px 0px !important; overflow: auto; padding-top: 1px; width: 815.094px;">
<div class="bar" style="box-sizing: border-box; padding-left: 45px;">
</div>
<ol class="dp-c" start="1" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">var speaker = </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Speaker { Name=</span><span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"Gaurav Kumar Arora"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">}; </span></span></li>
</ol>
</div>
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Now, let’s check if the object is Speaker type,</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<div class="dp-highlighter" style="background-color: #e7e5dc; box-sizing: border-box; font-family: Consolas, "Courier New", Courier, mono, serif; margin: 18px 0px !important; overflow: auto; padding-top: 1px; width: 815.094px;">
<div class="bar" style="box-sizing: border-box; padding-left: 45px;">
</div>
<ol class="dp-c" start="1" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">var isTrue = speaker </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">is</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Speaker; </span></span></li>
</ol>
</div>
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
In the preceding, we are checking the matching type. Yes, our speaker is an object of Speaker type.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<div class="dp-highlighter" style="background-color: #e7e5dc; box-sizing: border-box; font-family: Consolas, "Courier New", Courier, mono, serif; margin: 18px 0px !important; overflow: auto; padding-top: 1px; width: 815.094px;">
<div class="bar" style="box-sizing: border-box; padding-left: 45px;">
</div>
<ol class="dp-c" start="1" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">Console.WriteLine(</span><span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"speaker is of Speaker type:{0}"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">, isTrue); </span></span></li>
</ol>
</div>
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
So, the results as true.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />But, here we get false,</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<div class="dp-highlighter" style="background-color: #e7e5dc; box-sizing: border-box; font-family: Consolas, "Courier New", Courier, mono, serif; margin: 18px 0px !important; overflow: auto; padding-top: 1px; width: 815.094px;">
<div class="bar" style="box-sizing: border-box; padding-left: 45px;">
</div>
<ol class="dp-c" start="1" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">var author = </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Author { Name = </span><span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"Gaurav Kumar Arora"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> }; </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">var isTrue = speaker <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">is</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Author; </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">Console.WriteLine(<span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"speaker is of Author type:{0}"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">, isTrue); </span></span></li>
</ol>
</div>
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Because our speaker is not an object of Author type.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><strong style="box-sizing: border-box;">"as" operator</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
The "as" operator behaves in similar way as the "is" operator. The only difference is it returns the object if both are compatible to that type. Else it returns a null. </div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Let's understand this in our C# code.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<div class="dp-highlighter" style="background-color: #e7e5dc; box-sizing: border-box; font-family: Consolas, "Courier New", Courier, mono, serif; margin: 18px 0px !important; overflow: auto; padding-top: 1px; width: 815.094px;">
<div class="bar" style="box-sizing: border-box; padding-left: 45px;">
</div>
<ol class="dp-c" start="1" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">public</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">static</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">string</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> GetAuthorName(dynamic obj) </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> { </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Author authorObj = obj <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">as</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Author; </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">return</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> (authorObj != </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">null</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">) ? authorObj.Name : </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">string</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">.Empty; </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
</ol>
</div>
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
We have a method that accepts a dynamic object and returns the object name property if the object is of the Author type. <br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><strong style="box-sizing: border-box;">Here, we’ve declared two objects,</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<div class="dp-highlighter" style="background-color: #e7e5dc; box-sizing: border-box; font-family: Consolas, "Courier New", Courier, mono, serif; margin: 18px 0px !important; overflow: auto; padding-top: 1px; width: 815.094px;">
<div class="bar" style="box-sizing: border-box; padding-left: 45px;">
</div>
<ol class="dp-c" start="1" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">var speaker = </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Speaker { Name=</span><span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"Gaurav Kumar Arora"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">}; </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">var author = <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Author { Name = </span><span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"Gaurav Kumar Arora"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> }; </span></span></li>
</ol>
</div>
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">The following returns the "Name" property,</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<div class="dp-highlighter" style="background-color: #e7e5dc; box-sizing: border-box; font-family: Consolas, "Courier New", Courier, mono, serif; margin: 18px 0px !important; overflow: auto; padding-top: 1px; width: 815.094px;">
<div class="bar" style="box-sizing: border-box; padding-left: 45px;">
</div>
<ol class="dp-c" start="1" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">var authorName = GetAuthorName(author); </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">Console.WriteLine(<span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"Author name is:{0}"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">, authorName); </span></span></li>
</ol>
</div>
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">It returns an empty string,</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<div class="dp-highlighter" style="background-color: #e7e5dc; box-sizing: border-box; font-family: Consolas, "Courier New", Courier, mono, serif; margin: 18px 0px !important; overflow: auto; padding-top: 1px; width: 815.094px;">
<div class="bar" style="box-sizing: border-box; padding-left: 45px;">
</div>
<ol class="dp-c" start="1" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">authorName = GetAuthorName(speaker); </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">Console.WriteLine(<span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"Author name is:{0}"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">, authorName); </span></span></li>
</ol>
</div>
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Learn more about is vs as operators here,</strong></div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/UploadFile/g_arora/discussing-is-and-as-operators-of-C-Sharp-language/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">"is" and "as" Operators of C#</a></li>
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/uploadfile/abhikumarvatsa/the-is-and-as-operators-in-C-Sharp/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">The Is and As Operators in C#</a></li>
</ul>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
34. How to use Nullable<> Types in C#?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Answer</strong><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />A nullable type is a data type is that contains the defined data type or the null value.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />This nullable type concept is not compatible with "var".<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />Any data type can be declared nullable type with the help of operator "?". </div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
For example, the following code declares int i as a null.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<div class="dp-highlighter" style="background-color: #e7e5dc; box-sizing: border-box; font-family: Consolas, "Courier New", Courier, mono, serif; margin: 18px 0px !important; overflow: auto; padding-top: 1px; width: 815.094px;">
<div class="bar" style="box-sizing: border-box; padding-left: 45px;">
</div>
<ol class="dp-c" start="1" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">int</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">? i = </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">null</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">; </span></span></li>
</ol>
</div>
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
As discussed in previous section "var" is not compatible with nullable types. <br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />So, if you declare the following, you will get an error.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<div class="dp-highlighter" style="background-color: #e7e5dc; box-sizing: border-box; font-family: Consolas, "Courier New", Courier, mono, serif; margin: 18px 0px !important; overflow: auto; padding-top: 1px; width: 815.094px;">
<div class="bar" style="box-sizing: border-box; padding-left: 45px;">
</div>
<ol class="dp-c" start="1" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">var? i = </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">null</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">; </span></span></li>
</ol>
</div>
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
To learn more about nullable types in C#, read the following:</div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/uploadfile/prasoonk/getting-started-with-nullable-types-in-C-Sharp/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Getting started with Nullable Types in C#</a></li>
</ul>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
35. Different Ways of Method can be overloaded</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Answer</strong><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />Method overloading is a way to achieve compile time polymorphism where we can use a method with the same name but different signatures. For example, the following code example has a method volume with three different signatures base don the number and type of parameters and return values.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Example</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<div class="dp-highlighter" style="background-color: #e7e5dc; box-sizing: border-box; font-family: Consolas, "Courier New", Courier, mono, serif; margin: 18px 0px !important; overflow: auto; padding-top: 1px; width: 815.094px;">
<div class="bar" style="box-sizing: border-box; padding-left: 45px;">
</div>
<ol class="dp-c" start="1" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">using</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> System; </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">using</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> System.Collections.Generic; </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">using</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> System.Linq; </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">using</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> System.Text; </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">namespace</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Hello_Word { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">class</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> overloding { </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">public</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">static</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">void</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Main() { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Console.WriteLine(volume(10)); </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Console.WriteLine(volume(2.5F, 8)); </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Console.WriteLine(volume(100L, 75, 15)); </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Console.ReadLine(); </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">static</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">int</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> volume(</span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">int</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> x) { </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">return</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> (x * x * x); </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">static</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">double</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> volume(</span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">float</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> r, </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">int</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> h) { </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">return</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> (3.14 * r * r * h); </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">static</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">long</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> volume(</span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">long</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> l, </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">int</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> b, </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">int</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> h) { </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">return</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> (l * b * h); </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">} </span></li>
</ol>
</div>
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;"><u style="box-sizing: border-box;">Note</u></strong><br style="box-sizing: border-box;" />If we have a method that have two parameter object type and have a same name method with two integer parameter so when we call that method with int value so it’ll call that method have integer parameter instead of object type parameters method.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />Read the following article to learn more about method overloading in C#.</div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/UploadFile/2072a9/method-overloading-in-C-Sharp/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Method Overloading in C#</a></li>
</ul>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
36. What is an Object Pool in .Net?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Answer</strong><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />Object Pooling in .NET allows objects to keep in the memory pool so the objects can be reused without recreating them. This article explains what object pooling is in .NET and how to implement object pooling in C#.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">What does it mean?</strong><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />Object Pool is a container of objects that are ready for use. Whenever there is a request for a new object, the pool manager will take the request and it will be served by allocating an object from the pool.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><strong style="box-sizing: border-box;">How it works?</strong><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />We are going to use the Factory pattern for this purpose. We will have a factory method, which will take care about the creation of objects. Whenever there is a request for a new object, the factory method will look into the object pool (we use Queue object). If there is any object available within the allowed limit, it will return the object (value object), otherwise a new object will be created and give you back.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
To learn more about object pooling in C# and .NET, read the following,</div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/article/object-pooling-in-net/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Object Pooling in .NET</a></li>
</ul>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
37. What are Generics in C#?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Answer</strong><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />Generics allow you to delay the specification of the data type of programming elements in a class or a method, until it is actually used in the program. In other words, generics allow you to write a class or method that can work with any data type.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />You write the specifications for the class or the method, with substitute parameters for data types. When the compiler encounters a constructor for the class or a function call for the method, it generates code to handle the specific data type.<br style="box-sizing: border-box;" /></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; text-align: center;">
<img alt="ASP.NET" class="" data-src="https://www.c-sharpcorner.com/UploadFile/puranindia/C-Sharp-interview-questions/Images/image017.jpg" src="https://www.c-sharpcorner.com/UploadFile/puranindia/C-Sharp-interview-questions/Images/image017.jpg" style="box-sizing: border-box; max-width: 100%;" /></div>
<br style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;" /><span style="background-color: white; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">Generic classes and methods combine reusability, type safety and efficiency in a way that their non-generic counterparts cannot. Generics are most frequently used with collections and the methods that operate on them. Version 2.0 of the .NET Framework class library provides a new namespace, System.Collections.Generic, that contains several new generic-based collection classes. It is recommended that all applications that target the .NET Framework 2.0 and later use the new generic collection classes instead of the older non-generic counterparts such as ArrayList.</span><br style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;" /><br style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;" /><strong style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">Features of Generics</strong><br style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;" /><br style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;" /><span style="background-color: white; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">Generics is a technique that enriches your programs in the following ways,</span><br />
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;">It helps you to maximize code reuse, type safety and performance.</li>
<li style="box-sizing: border-box;">You can create generic collection classes. The .NET Framework class library contains several new generic collection classes in the System.Collections.Generic namespace. You may use these generic collection classes instead of the collection classes in the System.Collections namespace.</li>
<li style="box-sizing: border-box;">You can create your own generic interfaces, classes, methods, events and delegates.</li>
<li style="box-sizing: border-box;">You may create generic classes constrained to enable access to methods on specific data types.</li>
<li style="box-sizing: border-box;">You may get information on the types used in a generic data type at run-time using reflection.</li>
</ul>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Learn more here about generic classes in C# here,</div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/UploadFile/84c85b/using-generics-with-C-Sharp/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Using Generics In C#</a></li>
</ul>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
38. Describe Accessibility Modifiers in C#</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Answer </strong><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />Access modifiers are keywords used to specify the declared accessibility of a member or a type.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />Access modifiers are keywords used to specify the scope of accessibility of a member of a type or the type itself. For example, a public class is accessible to the entiere world, while an internal class may be accessible to the assembly only. </div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Why to use access modifiers?</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Access modifiers are an integral part of object-oriented programming. Access modifiers are used to implement encapsulation of OOP. Access modifiers allow you to define who does or who doesn't have access to certain features.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
In C# there are 6 different types of Access Modifiers.</div>
<table bgcolor="#ffffff" border="1" cellspacing="1" style="border-collapse: collapse; box-sizing: border-box; color: #333333; font-family: "open sans", sans-serif; font-size: 16px; outline: 0px; width: 100%px;"><tbody style="box-sizing: border-box; outline: 0px;">
<tr bgcolor="#0270bf" style="box-sizing: border-box; outline: 0px;"><td style="border: 1px dashed rgb(171, 171, 171); box-sizing: border-box; color: white;">Modifier</td><td style="border: 1px dashed rgb(171, 171, 171); box-sizing: border-box;"><span style="box-sizing: border-box; color: white;">Description</span></td></tr>
<tr style="box-sizing: border-box;"><td style="border: 1px dashed rgb(171, 171, 171); box-sizing: border-box;">public</td><td style="border: 1px dashed rgb(171, 171, 171); box-sizing: border-box;">There are no restrictions on accessing public members.</td></tr>
<tr style="box-sizing: border-box;"><td style="border: 1px dashed rgb(171, 171, 171); box-sizing: border-box;">private</td><td style="border: 1px dashed rgb(171, 171, 171); box-sizing: border-box;">Access is limited to within the class definition. This is the default access modifier type if none is formally specified</td></tr>
<tr style="box-sizing: border-box;"><td style="border: 1px dashed rgb(171, 171, 171); box-sizing: border-box;">protected</td><td style="border: 1px dashed rgb(171, 171, 171); box-sizing: border-box;">Access is limited to within the class definition and any class that inherits from the class</td></tr>
<tr style="box-sizing: border-box;"><td style="border: 1px dashed rgb(171, 171, 171); box-sizing: border-box;">internal</td><td style="border: 1px dashed rgb(171, 171, 171); box-sizing: border-box;">Access is limited exclusively to classes defined within the current project assembly</td></tr>
<tr style="box-sizing: border-box;"><td style="border: 1px dashed rgb(171, 171, 171); box-sizing: border-box;">protected internal</td><td style="border: 1px dashed rgb(171, 171, 171); box-sizing: border-box;">Access is limited to the current assembly and types derived from the containing class. All members in current project and all members in derived class can access the variables.</td></tr>
<tr style="box-sizing: border-box;"><td style="border: 1px dashed rgb(171, 171, 171); box-sizing: border-box;">private protected</td><td style="border: 1px dashed rgb(171, 171, 171); box-sizing: border-box;">Access is limited to the containing class or types derived from the containing class within the current assembly.</td></tr>
</tbody></table>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
To learn more about access modifiers in C#, learn here,</div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/uploadfile/puranindia/what-are-access-modifiers-in-C-Sharp/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">What are Access Modifiers in C#?</a></li>
</ul>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
39. What is a Virtual Method in C#?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Answer</strong><strong style="box-sizing: border-box;"><br style="box-sizing: border-box;" /></strong><br style="box-sizing: border-box;" />A virtual method is a method that can be redefined in derived classes. A virtual method has an implementation in a base class as well as derived the class. It is used when a method's basic functionality is the same but sometimes more functionality is needed in the derived class. A virtual method is created in the base class that can be overridden in the derived class. We create a virtual method in the base class using the virtual keyword and that method is overridden in the derived class using the override keyword.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />When a method is declared as a virtual method in a base class then that method can be defined in a base class and it is optional for the derived class to override that method. The overriding method also provides more than one form for a method. Hence it is also an example for polymorphism.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />When a method is declared as a virtual method in a base class and that method has the same definition in a derived class then there is no need to override it in the derived class. But when a virtual method has a different definition in the base class and the derived class then there is a need to override it in the derived class.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />When a virtual method is invoked, the run-time type of the object is checked for an overriding member. The overriding member in the most derived class is called, which might be the original member, if no derived class has overridden the member.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><strong style="box-sizing: border-box;">Virtual Method</strong></div>
<ol style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;">By default, methods are non-virtual. We can't override a non-virtual method.</li>
<li style="box-sizing: border-box;">We can't use the virtual modifier with the static, abstract, private or override modifiers.</li>
</ol>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Learn more about virtual methods in C#,</div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/UploadFile/3d39b4/virtual-method-in-C-Sharp/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Virtual Method in C#</a></li>
</ul>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
40. What are the Difference between Array and ArrayList in C#?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Answer</strong><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />Here is a list of difference between the two.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<div align="center" style="box-sizing: border-box;">
<img alt="" class="" data-src="/UploadFile/puranindia/C-Sharp-interview-questions/Images/Difference between Array and ArrayList in C%23.jpg" src="https://www.c-sharpcorner.com/UploadFile/puranindia/C-Sharp-interview-questions/Images/Difference%20between%20Array%20and%20ArrayList%20in%20C%23.jpg" style="box-sizing: border-box; max-width: 100%;" /></div>
<br style="box-sizing: border-box;" />To learn more about arrays, collections, and ArrayList here,</div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/UploadFile/skumaar_mca/collections-in-C-Sharp-arraylist-and-arrays/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Collections in C#: ArrayList and Arrays</a></li>
</ul>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
</div>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
41. What do you understand by Value types and Reference types in C#?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Answer</strong><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />In C#, data types can be of two types, value types and reference types. Value type variables contain their object (or data) directly. If we copy one value type variable to another then we are actually making a copy of the object for the second variable. Both of them will independently operate on their values, Value type data types are stored on a stack and reference data types are stored on a heap.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
In C#, basic data types such as int, char, bool, and long are value types. Classes and collections are reference types.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
For more details follow this link,</div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/UploadFile/ca6c61/concepts-of-C-Sharp-value-type-and-reference-type/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">C# Concepts: Value Type and Reference Type</a></li>
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/UploadFile/2d4b04/value-types-and-reference-types-variables/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Value Types and Reference Types Variables</a></li>
</ul>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
42. What is Serialization in C#?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Answer</strong><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />Serialization in C# is the process of converting an object into a stream of bytes to store the object to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">There are three types of serialization,</strong></div>
<ol style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;">Binary serialization (Save your object data into binary format).</li>
<li style="box-sizing: border-box;">Soap Serialization (Save your object data into binary format; mainly used in network related communication).</li>
<li style="box-sizing: border-box;">XmlSerialization (Save your object data into an XML file).</li>
</ol>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Learn more about serialization in C# here,</div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/article/serializing-objects-in-C-Sharp/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Serializing Objects in C#</a></li>
</ul>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
43. What is the use of the “using” statement in C#?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Answer</strong><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />There are two ways to use the using keyword in C#. One is as a directive and the other is as a statement. Let's explain!</div>
<ol style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><em style="box-sizing: border-box;">using Directive</em><br style="box-sizing: border-box;" />Generally we use the using keyword to add namespaces in code-behind and class files. Then it makes available all the classes, interfaces and abstract classes and their methods and properties in the current page. Adding a namespace can be done in the following two ways:</li>
</ol>
<ol start="2" style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><em style="box-sizing: border-box;">Using Statement</em><br style="box-sizing: border-box;" />This is another way to use the using keyword in C#. It plays a vital role in improving performance in Garbage Collection.</li>
</ol>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Learn more here,</div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/UploadFile/manas1/usage-and-importance-of-using-in-C-Sharp472/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">The "Using" Statement in C#</a></li>
</ul>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
44. What is a Jagged Array in C#?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Answer</strong><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />A jagged array is an array whose elements are arrays. The elements of a jagged array can be of different dimensions and sizes. A jagged array is sometimes called an "array of arrays."<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />A special type of array is introduced in C#. A Jagged Array is an array of an array in which the length of each array index can differ.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><strong style="box-sizing: border-box;">Example</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<div class="dp-highlighter" style="background-color: #e7e5dc; box-sizing: border-box; font-family: Consolas, "Courier New", Courier, mono, serif; margin: 18px 0px !important; overflow: auto; padding-top: 1px; width: 815.094px;">
<div class="bar" style="box-sizing: border-box; padding-left: 45px;">
</div>
<ol class="dp-c" start="1" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">int</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">[][] jagArray = </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">int</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">[5][]; </span></span></li>
</ol>
</div>
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
In the above declaration the rows are fixed in size. But columns are not specified as they can vary.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><strong style="box-sizing: border-box;">Declaring and initializing jagged array.</strong></div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<div class="dp-highlighter" style="background-color: #e7e5dc; box-sizing: border-box; font-family: Consolas, "Courier New", Courier, mono, serif; margin: 18px 0px !important; overflow: auto; padding-top: 1px; width: 815.094px;">
<div class="bar" style="box-sizing: border-box; padding-left: 45px;">
</div>
<ol class="dp-c" start="1" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">int</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">[][] jaggedArray = </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">int</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">[5][]; </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">jaggedArray[0] = <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">int</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">[3]; </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">jaggedArray[1] = <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">int</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">[5]; </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">jaggedArray[2] = <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">int</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">[2]; </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">jaggedArray[3] = <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">int</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">[8]; </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">jaggedArray[4] = <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">int</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">[10]; </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">jaggedArray[0] = <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">int</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">[] { 3, 5, 7, }; </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">jaggedArray[1] = <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">int</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">[] { 1, 0, 2, 4, 6 }; </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">jaggedArray[2] = <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">int</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">[] { 1, 6 }; </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">jaggedArray[3] = <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">int</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">[] { 1, 0, 2, 4, 6, 45, 67, 78 }; </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">jaggedArray[4] = <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">int</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">[] { 1, 0, 2, 4, 6, 34, 54, 67, 87, 78 }; </span></span> </li>
</ol>
</div>
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Learn more here,</div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/UploadFile/puranindia/jagged-arrays-in-C-Sharp-net/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Jagged Array in C#</a></li>
</ul>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
45. What is Multithreading with .NET?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Answer</strong><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />Multithreading allows a program to run multiple threads concurrently. This article explains how multithreading works in .NET. This article covers the entire range of threading areas from thread creation, race conditions, deadlocks, monitors, mutexes, synchronization and semaphores and so on.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
The real usage of a thread is not about a single sequential thread, but rather using multiple threads in a single program. Multiple threads running at the same time and performing various tasks is referred as Multithreading. A thread is considered to be a lightweight process because it runs within the context of a program and takes advantage of resources allocated for that program.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
A single-threaded process contains only one thread while a multithreaded process contains more than one thread for execution.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />To learn more about threading in .NET, visit,</div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/UploadFile/84c85b/multithreading-with-net/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Multithreading with .NET</a></li>
</ul>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
</div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;"></ul>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
46. Explain Anonymous Types in C#?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Answer</strong><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />Anonymous types allow us to create new type without defining them. This is way to defining read only properties into a single object without having to define type explicitly. Here Type is generating by the compiler and it is accessible only for the current block of code. The type of properties is also inferred by the compiler.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />We can create anonymous types by using “new” keyword together with the object initializer. <br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><strong style="box-sizing: border-box;">Example</strong> </div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<div class="dp-highlighter" style="background-color: #e7e5dc; box-sizing: border-box; font-family: Consolas, "Courier New", Courier, mono, serif; margin: 18px 0px !important; overflow: auto; padding-top: 1px; width: 815.094px;">
<div class="bar" style="box-sizing: border-box; padding-left: 45px;">
</div>
<ol class="dp-c" start="1" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">var anonymousData = </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">{ </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> ForeName = <span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"Jignesh"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">, </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> SurName = <span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"Trivedi"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">}; </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">Console.WriteLine(<span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"First Name : "</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> + anonymousData.ForeName); </span></span></li>
</ol>
</div>
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Anonymous Types with LINQ Example</strong><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />Anonymous types are also used with the "Select" clause of LINQ query expression to return subset of properties.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><strong style="box-sizing: border-box;">Example</strong><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />If Any object collection having properties called FirstName , LastName, DOB etc. and you want only FirstName and LastName after the Querying the data then.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<div class="dp-highlighter" style="background-color: #e7e5dc; box-sizing: border-box; font-family: Consolas, "Courier New", Courier, mono, serif; margin: 18px 0px !important; overflow: auto; padding-top: 1px; width: 815.094px;">
<div class="bar" style="box-sizing: border-box; padding-left: 45px;">
</div>
<ol class="dp-c" start="1" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">class</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> MyData { </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">public</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">string</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> FirstName { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">get</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">; </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">set</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">; </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">public</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">string</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> LastName { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">get</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">; </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">set</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">; </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">public</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> DateTime DOB { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">get</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">; </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">set</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">; </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">public</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">string</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> MiddleName { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">get</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">; </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">set</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">; </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">static</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">void</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Main(</span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">string</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">[] args) { </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="comment" style="background-color: inherit; border: none; box-sizing: border-box; color: #008200; margin: 0px; padding: 0px;">// Create Dummy Data to fill Collection. </span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> List < MyData > data = <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> List < MyData > (); </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> data.Add(<span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> MyData { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> FirstName = <span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"Jignesh"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">, LastName = </span><span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"Trivedi"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">, MiddleName = </span><span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"G"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">, DOB = </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> DateTime(1990, 12, 30) </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> }); </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> data.Add(<span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> MyData { </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> FirstName = <span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"Tejas"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">, LastName = </span><span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"Trivedi"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">, MiddleName = </span><span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"G"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">, DOB = </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> DateTime(1995, 11, 6) </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> }); </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> data.Add(<span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> MyData { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> FirstName = <span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"Rakesh"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">, LastName = </span><span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"Trivedi"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">, MiddleName = </span><span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"G"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">, DOB = </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> DateTime(1993, 10, 8) </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> }); </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> data.Add(<span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> MyData { </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> FirstName = <span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"Amit"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">, LastName = </span><span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"Vyas"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">, MiddleName = </span><span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"P"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">, DOB = newDateTime(1983, 6, 15) </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> }); </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> data.Add(<span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> MyData { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> FirstName = <span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"Yash"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">, LastName = </span><span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"Pandiya"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">, MiddleName = </span><span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"K"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">, DOB = newDateTime(1988, 7, 20) </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> }); </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> var anonymousData = from pl <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">in</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> data </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> select <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> { </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> pl.FirstName, pl.LastName </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> }; </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">foreach</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">(var m </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">in</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> anonymousData) { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Console.WriteLine(<span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"Name : "</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> + m.FirstName + </span><span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">" "</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> + m.LastName); </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">} </span> </li>
</ol>
</div>
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Learn more about anonymous types here,</div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/UploadFile/ff2f08/anonymous-types-in-C-Sharp/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Anonymous Types in C#</a></li>
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/UploadFile/pranayamr/return-anonymous-type-in-C-Sharp/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Return Anonymous Type in C#</a></li>
</ul>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
47. Explain Hashtable in C#?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Answer</strong><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />A Hashtable is a collection that stores (Keys, Values) pairs. Here, the Keys are used to find the storage location and is immutable and cannot have duplicate entries in a Hashtable. The .Net Framework has provided a Hash Table class that contains all the functionality required to implement a hash table without any additional development. The hash table is a general-purpose dictionary collection. Each item within the collection is a DictionaryEntry object with two properties: a key object and a value object. These are known as Key/Value. When items are added to a hash table, a hash code is generated automatically. This code is hidden from the developer. All access to the table's values is achieved using the key object for identification. As the items in the collection are sorted according to the hidden hash code, the items should be considered to be randomly ordered.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><strong style="box-sizing: border-box;">The Hashtable Collection</strong><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />The Base Class libraries offers a Hashtable Class that is defined in the System.Collections namespace, so you don't have to code your own hash tables. It processes each key of the hash that you add every time and then uses the hash code to look up the element very quickly. The capacity of a hash table is the number of elements the hash table can hold. As elements are added to a hash table, the capacity is automatically increased as required through reallocation. It is an older .Net Framework type.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><strong style="box-sizing: border-box;">Declaring a Hashtable</strong><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />The Hashtable class is generally found in the namespace called System.Collections. So to execute any of the examples, we have to add using System.Collections; to the source code. The declaration for the Hashtable is,</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<div class="dp-highlighter" style="background-color: #e7e5dc; box-sizing: border-box; font-family: Consolas, "Courier New", Courier, mono, serif; margin: 18px 0px !important; overflow: auto; padding-top: 1px; width: 815.094px;">
<div class="bar" style="box-sizing: border-box; padding-left: 45px;">
</div>
<ol class="dp-c" start="1" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">Hashtable HT = </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Hashtable (); </span></span></li>
</ol>
</div>
</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Learn more about HashTable, visit the following,</div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/UploadFile/e83792/C-Sharp-nethash-table-class/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">C# .Net : HashTable Class</a></li>
</ul>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
48. What is LINQ in C#?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Answer</strong><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />LINQ stands for Language Integrated Query. LINQ is a data querying methodology which provides querying capabilities to .NET languages with a syntax similar to a SQL query<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />LINQ has a great power of querying on any source of data. The data source could be collections of objects, database or XML files. We can easily retrieve data from any object that implements the IEnumerable<t> interface.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><strong style="box-sizing: border-box;">Advantages of LINQ</strong> </t></div>
<ol style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;">LINQ offers an object-based, language-integrated way to query over data no matter where that data came from. So through LINQ we can query database, XML as well as collections. </li>
<li style="box-sizing: border-box;">Compile time syntax checking.</li>
<li style="box-sizing: border-box;">It allows you to query collections like arrays, enumerable classes etc in the native language of your application, like VB or C# in much the same way as you would query a database using SQL.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><div style="box-sizing: border-box; text-align: center;">
<img alt="ASP.NET" class="" data-src="https://www.c-sharpcorner.com/UploadFile/puranindia/C-Sharp-interview-questions/Images/image023.png" src="https://www.c-sharpcorner.com/UploadFile/puranindia/C-Sharp-interview-questions/Images/image023.png" style="box-sizing: border-box; max-width: 100%;" /></div>
</li>
</ol>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
For more details follow the link,</div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/UploadFile/72d20e/concept-of-linq-with-C-Sharp/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Concept of LINQ with C#</a></li>
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/UploadFile/84c85b/using-linq-with-C-Sharp-2012/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">Using LINQ With C# 2012</a></li>
</ul>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
49. What is File Handling in C#.Net?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Answer</strong><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />The System.IO namespace provides four classes that allow you to manipulate individual files, as well as interact with a machine directory structure. The Directory and File directly extends System.Object and supports the creation, copying, moving and deletion of files using various static methods. They only contain static methods and are never instantiated. The FileInfo and DirecotryInfo types are derived from the abstract class FileSystemInfo type and they are typically, employed for obtaining the full details of a file or directory because their members tend to return strongly typed objects. They implement roughly the same public methods as a Directory and a File but they are stateful and the members of these classes are not static.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<div align="center" style="box-sizing: border-box;">
<img alt="ASP.NET" class="" data-src="https://www.c-sharpcorner.com/UploadFile/puranindia/C-Sharp-interview-questions/Images/image024.jpg" src="https://www.c-sharpcorner.com/UploadFile/puranindia/C-Sharp-interview-questions/Images/image024.jpg" style="box-sizing: border-box; max-width: 100%;" /></div>
<br style="box-sizing: border-box;" />For more details follow the link,</div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/UploadFile/ajyadav123/file-handling-in-C-Sharp-net/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">File Handling in C# .NET</a></li>
<li style="box-sizing: border-box;"><a href="https://www.c-sharpcorner.com/UploadFile/puranindia/file-handling-in-C-Sharp/" style="box-sizing: border-box; color: blue; cursor: pointer; text-decoration-line: none;" target="_blank">File handling in C#</a></li>
</ul>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
50. What is Reflection in C#?</h2>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Answer</strong><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />Reflection is the process of runtime type discovery to inspect metadata, CIL code, late binding and self-generating code. At run time by using reflection, we can access the same "type" information as displayed by the ildasm utility at design time. The reflection is analogous to reverse engineering in which we can break an existing *.exe or *.dll assembly to explore defined significant contents information, including methods, fields, events and properties.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />You can dynamically discover the set of interfaces supported by a given type using the System.Reflection namespace.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Reflection typically is used to dump out the loaded assemblies list, their reference to inspect methods, properties etcetera. Reflection is also used in the external disassembling tools such Reflector, Fxcop and NUnit because .NET tools don't need to parse the source code similar to C++. <br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><strong style="box-sizing: border-box;">Metadata Investigation </strong><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />The following program depicts the process of reflection by creating a console based application. This program will display the details of the fields, methods, properties and interfaces for any type within the mscorlib.dll assembly. Before proceeeding, it is mandatory to import "System.Reflection".<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />Here, we are defining a number of static methods in the program class to enumerate fields, methods and interfaces in the specified type. The static method takes a single "System.Type" parameter and returns void. </div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<div class="dp-highlighter" style="background-color: #e7e5dc; box-sizing: border-box; font-family: Consolas, "Courier New", Courier, mono, serif; margin: 18px 0px !important; overflow: auto; padding-top: 1px; width: 815.094px;">
<div class="bar" style="box-sizing: border-box; padding-left: 45px;">
</div>
<ol class="dp-c" start="1" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">static</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">void</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> FieldInvestigation(Type t) { </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Console.WriteLine(<span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"*********Fields*********"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">); </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> FieldInfo[] fld = t.GetFields(); </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">foreach</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">(FieldInfo f </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">in</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> fld) { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Console.WriteLine(<span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"-->{0}"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">, f.Name); </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">} </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">static</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">void</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> MethodInvestigation(Type t) { </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Console.WriteLine(<span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"*********Methods*********"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">); </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> MethodInfo[] mth = t.GetMethods(); </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">foreach</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">(MethodInfo m </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">in</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> mth) { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Console.WriteLine(<span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"-->{0}"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">, m.Name); </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">} </span></li>
<li></li>
</ol>
</div>
</div>
</div>
योगेश चन्द्र उप्रेतीhttp://www.blogger.com/profile/17009545633002694199noreply@blogger.com3tag:blogger.com,1999:blog-8545307170893600943.post-67975567754979062112019-10-28T23:14:00.003-07:002019-10-28T23:18:35.398-07:00Web Service in ASP.NET (Return JSON and XML)<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-size: x-large;">Web Service in ASP.NET</span><br />
<div>
<br /></div>
This article explains how to return JSON and XML data from a [WebMethod].<br />
<div>
<br /></div>
<div>
<div class="PaddingLeft5" id="div2" style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px !important;">
<div style="box-sizing: border-box; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
First we need to create a Web service as was already discussed in Part 1. Again create a new Web Service in Visual Studio.<br />
<br style="box-sizing: border-box;" />
<strong style="box-sizing: border-box;">Step 1</strong></div>
<div style="box-sizing: border-box; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Open Visual Studio and select "File" -> "New" -> "Web Site...".<br />
<br style="box-sizing: border-box;" />
<img alt="empty web site" class="" data-src="/UploadFile/8ef97c/web-service-in-Asp-Net-part-4/Images/empty web site.jpg" src="https://www.c-sharpcorner.com/UploadFile/8ef97c/web-service-in-Asp-Net-part-4/Images/empty%20web%20site.jpg" style="box-sizing: border-box; max-width: 100%;" /><br />
<br style="box-sizing: border-box;" />
<strong style="box-sizing: border-box;">Step 2</strong></div>
<div style="box-sizing: border-box; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Now add a Web Service file (.asmx) to the Web site.<br />
<br style="box-sizing: border-box;" />
<img alt="Web site" class="" data-src="/UploadFile/8ef97c/web-service-in-Asp-Net-part-4/Images/Web site.jpg" src="https://www.c-sharpcorner.com/UploadFile/8ef97c/web-service-in-Asp-Net-part-4/Images/Web%20site.jpg" style="box-sizing: border-box; max-width: 100%;" /><br />
<br style="box-sizing: border-box;" />
Provide a name for the web service file.<br />
<br style="box-sizing: border-box;" />
<img alt="service file" class="" data-src="/UploadFile/8ef97c/web-service-in-Asp-Net-part-4/Images/service file.jpg" src="https://www.c-sharpcorner.com/UploadFile/8ef97c/web-service-in-Asp-Net-part-4/Images/service%20file.jpg" style="box-sizing: border-box; max-width: 100%;" /><br />
<br style="box-sizing: border-box;" />
And delete the existing code file from the Solution Explorer so that we can create our own class file to host on this web service.<br />
<br style="box-sizing: border-box;" />
<img alt="host on this web service" class="" data-src="/UploadFile/8ef97c/web-service-in-Asp-Net-part-4/Images/host on this web service.jpg" src="https://www.c-sharpcorner.com/UploadFile/8ef97c/web-service-in-Asp-Net-part-4/Images/host%20on%20this%20web%20service.jpg" style="box-sizing: border-box; max-width: 100%;" /><br />
<br style="box-sizing: border-box;" />
<strong style="box-sizing: border-box;">Step 3</strong></div>
<div style="box-sizing: border-box; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Now add a new class file to [<strong style="box-sizing: border-box;">WebService</strong>] and also a class for a user data type to define the structure.<br />
<br style="box-sizing: border-box;" />
<img alt="Class " class="" data-src="/UploadFile/8ef97c/web-service-in-Asp-Net-part-4/Images/Class.jpg" src="https://www.c-sharpcorner.com/UploadFile/8ef97c/web-service-in-Asp-Net-part-4/Images/Class.jpg" style="box-sizing: border-box; max-width: 100%;" /><br />
<br style="box-sizing: border-box;" />
<strong style="box-sizing: border-box;">[WebSerivce] Class</strong><br />
<br style="box-sizing: border-box;" />
<img alt="WebSerivce" class="" data-src="/UploadFile/8ef97c/web-service-in-Asp-Net-part-4/Images/WebSerivce.jpg" src="https://www.c-sharpcorner.com/UploadFile/8ef97c/web-service-in-Asp-Net-part-4/Images/WebSerivce.jpg" style="box-sizing: border-box; max-width: 100%;" /><br />
<br style="box-sizing: border-box;" />
User-define data type for JSON and XML Stucture:<br />
<br style="box-sizing: border-box;" />
<img alt="Define Data type" class="" data-src="/UploadFile/8ef97c/web-service-in-Asp-Net-part-4/Images/Define Data type.jpg" src="https://www.c-sharpcorner.com/UploadFile/8ef97c/web-service-in-Asp-Net-part-4/Images/Define%20Data%20type.jpg" style="box-sizing: border-box; max-width: 100%;" /><br />
<br style="box-sizing: border-box;" />
<strong style="box-sizing: border-box;">Step 4</strong></div>
<div style="box-sizing: border-box; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Edit the Employee Class and declare a property that we need to use in the JSON and XML object.<br />
<br style="box-sizing: border-box;" />
<img alt="Employee Class" class="" data-src="/UploadFile/8ef97c/web-service-in-Asp-Net-part-4/Images/Employee Class.jpg" src="https://www.c-sharpcorner.com/UploadFile/8ef97c/web-service-in-Asp-Net-part-4/Images/Employee%20Class.jpg" style="box-sizing: border-box; max-width: 100%;" /><br />
<br style="box-sizing: border-box;" />
<strong style="box-sizing: border-box;">Code:</strong></div>
<div class="dp-highlighter" style="background-color: #e7e5dc; box-sizing: border-box; font-family: Consolas, "Courier New", Courier, mono, serif; margin: 18px 0px !important; overflow: auto; padding-top: 1px; width: 515.094px;">
<div class="bar" style="box-sizing: border-box; padding-left: 45px;">
</div>
<ol class="dp-c" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">public</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">class</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Employee </span></span></li>
<li style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">{ </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">public</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">int</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Id { </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">get</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">; </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">set</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">; } </span></span></li>
<li style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">public</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">string</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Name { </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">get</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">; </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">set</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">; } </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">public</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">int</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Salary { </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">get</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">; </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">set</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">; } </span></span></li>
<li style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">} </span></li>
</ol>
</div>
<div style="box-sizing: border-box; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Step 5</strong></div>
<div style="box-sizing: border-box; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Now edit your .asmx file to define the CodeBehind and Class Properties.<br />
<br style="box-sizing: border-box;" />
<strong style="box-sizing: border-box;">Step 6</strong></div>
<div style="box-sizing: border-box; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Next we need to create a [WebService] class MyServiceClass and add all the namespaces first that are required.<br />
<br style="box-sizing: border-box;" />
<img alt="MyServiceClass" class="" data-src="/UploadFile/8ef97c/web-service-in-Asp-Net-part-4/Images/MyServiceClass.jpg" src="https://www.c-sharpcorner.com/UploadFile/8ef97c/web-service-in-Asp-Net-part-4/Images/MyServiceClass.jpg" style="box-sizing: border-box; max-width: 100%;" /><br />
<br style="box-sizing: border-box;" />
<strong style="box-sizing: border-box;">Step 7</strong></div>
<div style="box-sizing: border-box; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Write the [WebService] attribute over the class name and create the [<strong style="box-sizing: border-box;">WebMethod</strong>].</div>
<div style="box-sizing: border-box; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<br /></div>
<ol style="box-sizing: border-box;" type="A">
<li style="box-sizing: border-box;"><strong style="box-sizing: border-box;">GetEmployeeXML()</strong> for returning the data in XML.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><img alt="GetEmployeeXML" class="" data-src="/UploadFile/8ef97c/web-service-in-Asp-Net-part-4/Images/GetEmployeeXML.jpg" src="https://www.c-sharpcorner.com/UploadFile/8ef97c/web-service-in-Asp-Net-part-4/Images/GetEmployeeXML.jpg" style="box-sizing: border-box; max-width: 100%;" /></li>
<li style="box-sizing: border-box;"><strong style="box-sizing: border-box;">GetEmployeeJSON()</strong> for returning the data in JSON.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><img alt="GetEmployeeJSON" class="" data-src="/UploadFile/8ef97c/web-service-in-Asp-Net-part-4/Images/GetEmployeeJSON.jpg" src="https://www.c-sharpcorner.com/UploadFile/8ef97c/web-service-in-Asp-Net-part-4/Images/GetEmployeeJSON.jpg" style="box-sizing: border-box; max-width: 100%;" /></li>
</ol>
<div style="box-sizing: border-box; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Code:</strong></div>
<div class="dp-highlighter" style="background-color: #e7e5dc; box-sizing: border-box; font-family: Consolas, "Courier New", Courier, mono, serif; margin: 18px 0px !important; overflow: auto; padding-top: 1px; width: 815.094px;">
<div class="bar" style="box-sizing: border-box; padding-left: 45px;">
</div>
<ol class="dp-c" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">using</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> System; </span></span></li>
<li style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">using</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> System.Collections.Generic; </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">using</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> System.Linq; </span></span></li>
<li style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">using</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> System.Web; </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">using</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> System.Web.Services; </span></span></li>
<li style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">using</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> System.Web.Script.Services; </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">using</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> System.Web.Script.Serialization; </span></span></li>
<li style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">[WebService] </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">public</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">class</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> MyServiceClass </span></span></li>
<li style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">{ </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> [WebMethod] </span></li>
<li style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">public</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Employee[] GetEmployessXML() </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> { </span></li>
<li style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Employee[] emps= <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Employee[] { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Employee() </span></span></li>
<li style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> { </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Id=101, </span></li>
<li style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Name=<span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"Nitin"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">, </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Salary=10000 </span></li>
<li style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> }, </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Employee() </span></span></li>
<li style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> { </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Id=102, </span></li>
<li style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Name=<span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"Dinesh"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">, </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Salary=100000 </span></li>
<li style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> }; </span></li>
<li style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">return</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> emps; </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> [WebMethod] </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> [ScriptMethod(ResponseFormat = ResponseFormat.Json)] </span></li>
<li style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">public</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">string</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> GetEmployessJSON() </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> { </span></li>
<li style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Employee[] emps = <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Employee[] { </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Employee() </span></span></li>
<li style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> { </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Id=101, </span></li>
<li style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Name=<span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"Nitin"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">, </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Salary=10000 </span></li>
<li style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> }, </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Employee() </span></span></li>
<li style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> { </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Id=102, </span></li>
<li style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Name=<span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"Dinesh"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">, </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Salary=100000 </span></li>
<li style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> }; </span></li>
<li style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">return</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">new</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> JavaScriptSerializer().Serialize(emps); </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">} </span></li>
</ol>
</div>
<div style="box-sizing: border-box; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Step 8</strong></div>
<div style="box-sizing: border-box; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Build the application and view the output in a web browser.<br />
<br style="box-sizing: border-box;" />
<img alt="output in web browser" class="" data-src="/UploadFile/8ef97c/web-service-in-Asp-Net-part-4/Images/output in web browser.jpg" src="https://www.c-sharpcorner.com/UploadFile/8ef97c/web-service-in-Asp-Net-part-4/Images/output%20in%20web%20browser.jpg" style="box-sizing: border-box; max-width: 100%;" /><br />
<br style="box-sizing: border-box;" />
There are two methods, one from the return of <strong style="box-sizing: border-box;">JSON</strong> and the second for the return of <strong style="box-sizing: border-box;">XML</strong> result.<br />
<br style="box-sizing: border-box;" />
Click on any function name and Invoke to test the [<strong style="box-sizing: border-box;">WebMethod</strong>].</div>
<div style="box-sizing: border-box; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<br /></div>
<ol style="box-sizing: border-box;">
<li style="box-sizing: border-box;"><strong style="box-sizing: border-box;">Click on GetEmployessJSON<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><img alt="Click on GetEmployessJSON" class="" data-src="/UploadFile/8ef97c/web-service-in-Asp-Net-part-4/Images/Click on GetEmployessJSON.jpg" src="https://www.c-sharpcorner.com/UploadFile/8ef97c/web-service-in-Asp-Net-part-4/Images/Click%20on%20GetEmployessJSON.jpg" style="box-sizing: border-box; max-width: 100%;" /></strong></li>
<li style="box-sizing: border-box;"><strong style="box-sizing: border-box;">Click on GetEmployessXML<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><img alt="Click on GetEmployessXML" class="" data-src="/UploadFile/8ef97c/web-service-in-Asp-Net-part-4/Images/Click on GetEmployessXML.jpg" src="https://www.c-sharpcorner.com/UploadFile/8ef97c/web-service-in-Asp-Net-part-4/Images/Click%20on%20GetEmployessXML.jpg" style="box-sizing: border-box; max-width: 100%;" /></strong></li>
</ol>
<div style="box-sizing: border-box; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Thank you for reading this article.</div>
</div>
</div>
</div>
योगेश चन्द्र उप्रेतीhttp://www.blogger.com/profile/17009545633002694199noreply@blogger.com0tag:blogger.com,1999:blog-8545307170893600943.post-64482004736051257172019-10-28T22:46:00.002-07:002019-10-28T22:46:19.724-07:00ASP.Net Caching Page Caching Fragment Caching Data Caching<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-size: x-large;">Caching in ASP.Net</span><br /><br /><h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
<span style="font-family: "open sans", sans-serif; font-size: 16px;">Caching is one of the most interesting concepts and operations in ASP.NET. If you can handle it, you can run any web application by applying the caching concept depending on the requirements.</span></h2>
<br style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;" /><span style="background-color: white; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">Currrently a majority of websites/portals (or I can say simply web pages) are dynamic (if I do talk about a dynamic website, then it doesn't mean all the pages of that website is dynamic or will be. The probability of happening this is dependent on the user's perspective and the requirements).</span><br style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;" /><br style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;" /><span style="background-color: white; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">In very common words I can define dynamic pages as including the following:</span><span style="background-color: white; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;"> </span><ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;"><div style="box-sizing: border-box; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
- Pages that directly interact with people<br style="box-sizing: border-box;" />- Communication (on page)<br style="box-sizing: border-box;" />- Any media content<br style="box-sizing: border-box;" />- Any type of graphic interaction</div>
</ul>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
So, generally these types of pages or webs are called dynamic. Now let's find why we really need caching.</div>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
<span style="box-sizing: border-box;">Why Caching</span></h2>
<br style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;" /><span style="background-color: white; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">Caching is for providing solutions or the results to the users depending on their requested request, admin needs to recreate the pages often depending on user requests.</span><strong style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;"><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />The process</strong><br style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;" /><span style="background-color: white; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">The process is quite bulky and time-consuming. So to overcoming that problem some websites have a page creation engine that automatically creates all the pages in one action and directly saves those pages as a HTML structured page. These HTML pages serve the user depending on their requirements.</span><br style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;" /><br style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;" /><strong style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">Multiple sorts of pages</strong><span style="background-color: white; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;"> </span><div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
But, do you still think this will be enough? If your answer is yes, then please think some more!<br style="box-sizing: border-box;" />Actually, the preceding solution will only work if and only if the requested pages are of the same type. Now think, what will happen if the users request a different sort of page?<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />In that case your web will be stuck again.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />So for dealing with that kind of complex but necessary requirements, ASP.NET provides support for <strong style="box-sizing: border-box;">caching</strong>. Caching is the hero/heroine in this context that will help us to a great extent.<strong style="box-sizing: border-box;"><br style="box-sizing: border-box;" /></strong></div>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
<span style="box-sizing: border-box;">What a Cache does</span></h2>
<br style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;" /><span style="background-color: white; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">What a cache does, in the most simple words I can say is:</span><br style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;" /><br style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;" /><span style="background-color: white; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">"A cache simply stores the output generated by a page in the memory and this saved output (cache) will serve us (users) in the future." That's it.</span><br style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;" /><br style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;" /><h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
<span style="box-sizing: border-box;">Types of Catching</span></h2>
<strong style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;"><br style="box-sizing: border-box;" /></strong><img alt="Types of Catching in ASP.NET" class="" data-src="/UploadFile/2072a9/caching-in-Asp-Net/Images/caching.jpg" height="273" src="https://www.c-sharpcorner.com/UploadFile/2072a9/caching-in-Asp-Net/Images/caching.jpg" style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; max-width: 100%;" width="650" /><br style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;" /><br style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;" /><strong style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">Page Caching</strong><br style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;" /><span style="background-color: white; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">Let's explore the caching of an entire page, first.</span><br style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;" /><br style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;" /><span style="background-color: white; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">To cache an entire page's output we need to specify a directive at the top of our page, this directive is the @ OutputCache.</span><br style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;" /><br style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;" /><span style="background-color: white; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">Let's figure out a simple demo of it.</span><br style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;" /><div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<%@ OutputCache Duration = 5 VaryByParam = "ID" %> </div>
<span style="background-color: white; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">Here, in that statement </span><strong style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">Duration</strong><span style="background-color: white; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;"> and </span><strong style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">VarByParam</strong><span style="background-color: white; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;"> are the two attributes of the </span><strong style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">OutputCache</strong><span style="background-color: white; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;"> directive. Now let's explore how they work. </span><ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><strong style="box-sizing: border-box;">Duration Attribute</strong><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />This attributes represents the time in seconds of how long the output cache should be stored in memory. After the defined duration the content stored in the memory will be cleared automatically.<br style="box-sizing: border-box;" /></li>
<li style="box-sizing: border-box;"><strong style="box-sizing: border-box;">VarByParam Attribute</strong><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />This is the most important attributes; you can't afford to miss that in the OutputCache directory statement. It generally defines the query string parameters to vary the cache (in memory).</li>
</ul>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
You can also multiple parameter names too, but for that you need to separate them using a semicolon (;).<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />You can also specify it as "*". In this case the cached content is varied for all the parameters ed using the querysrting. For example:</div>
<div style="background-color: white; box-sizing: border-box; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<span style="background-color: transparent;"><span style="font-family: Consolas, Courier New, Courier, mono, serif;"><%@ OutputCache Duration = 5 VaryByParam = "*"%></span></span><span style="background-color: inherit; color: black; font-family: Consolas, "Courier New", Courier, mono, serif; font-size: 16px;"> </span></div>
<span style="background-color: white; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">In case of caching a page, some pages can generate different content for different browsers. In that scenario we need to add an additional attribute to our statement for overcoming the preceding problem. </span><span style="background-color: white; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">For example:</span><div>
<br /></div>
<div>
<span style="color: #212121; font-family: open sans, sans-serif;"><%@ OutputCache Duration = 5 VaryByParam = "ID" VaryByCustom = "Browser" %></span></div>
<div>
<span style="font-family: Consolas, "Courier New", Courier, mono, serif; font-size: 16px;"> </span><span style="color: #212121; font-family: open sans, sans-serif;"><br /></span><span style="background-color: white; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">Or:</span><div>
<br /></div>
<div>
<span style="color: #212121; font-family: open sans, sans-serif;"><%@ OutputCache Duration = 5 VaryByParam = "*" VaryByCustom = "Browser" %></span></div>
<div>
<span style="font-family: Consolas, "Courier New", Courier, mono, serif; font-size: 16px;"> </span><span style="color: #212121; font-family: open sans, sans-serif;"><br /></span><strong style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">Fragment caching</strong><br style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;" /><div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
In some scenarios we only need to cache only a segment of a page. For example a contact us page in a main page will be the same for all the users and for that there is no need to cache the entire page.</div>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
</div>
<span style="background-color: white; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">So for that we prefer to use fragment caching option. </span><span style="background-color: white; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">For example:</span></div>
<div>
<span style="color: #212121; font-family: open sans, sans-serif;"><br /></span></div>
<div>
<span style="color: #212121; font-family: open sans, sans-serif;"><%@ OutputCache Duration = 10 VaryByParam = "None" %> </span><div>
<span style="color: #212121; font-family: open sans, sans-serif;"><br /></span><span style="background-color: white; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">Or:</span></div>
<div>
<br /></div>
<div>
<span style="color: #212121; font-family: open sans, sans-serif;"><%@ OutputCache Duration = 5 VaryByParam = "None" VaryByCustom = "Browser" %> </span></div>
<div>
<span style="color: #212121; font-family: open sans, sans-serif;"><br /></span><strong style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">Data Caching</strong><br style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;" /><span style="background-color: white; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">Data caching is slightly different from the 2 other caching types. It's much more interesting to see how data caching actually works.</span><br style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;" /><br style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;" /><span style="background-color: white; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">As we know in C# everything is about classes and objects. So ASP.NET supports data caching by treating them as small sets of objects. We can store objects in memory very easily and use them depending on our functionality and needs, anywhere across the page.</span><br style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;" /><br style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;" /><span style="background-color: white; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">Now you must be thinking </span><strong style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">where is the class in that entire scenario</strong><span style="background-color: white; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">?</span><br style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;" /><br style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;" /><span style="background-color: white; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">Actually, this feature is implemented using the Cache class and data is treated as its object. Let's see how it works using a demo.</span><br style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;" /><br style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;" /><span style="background-color: white; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">I am inserting a string value in the cache as:</span><div>
<br /></div>
<div>
<span style="color: #212121; font-family: open sans, sans-serif;">Cache["Website"] = "CSharpCorner"; </span></div>
<div>
<span style="color: #212121;"><span style="font-family: Consolas, Courier New, Courier, mono, serif;"><br /></span></span><span style="background-color: white; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">Now, for inserting the cache into the objects, the insert method of the Cache class can be used. This insert method is used as follows:</span></div>
<div>
<span style="background-color: white; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;"><br /></span></div>
<div>
<div>
<span style="color: #212121; font-family: open sans, sans-serif;">Cache.Insert("Website", strName, </span></div>
<div>
<span style="color: #212121; font-family: open sans, sans-serif;">new CacheDependency(Sever.MapPath("Website.txt")); </span></div>
<div>
<strong style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;"><br /></strong></div>
<div>
<strong style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">What we are missing something</strong></div>
<br style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;" /><span style="background-color: white; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">We missed the Time for the cache (don't forget to use it), let's provide it:</span></div>
<div>
<br /></div>
<div>
<div>
<span style="color: #212121; font-family: open sans, sans-serif;">Cache.Insert("Website", strName, </span></div>
<div>
<span style="color: #212121; font-family: open sans, sans-serif;">new CacheDependency(Sever.MapPath("Website.txt") </span></div>
<div>
<span style="color: #212121; font-family: open sans, sans-serif;">DateTime.Now.Addminutes(5), TimeSpan.Zero);</span></div>
</div>
</div>
</div>
</div>
</div>
योगेश चन्द्र उप्रेतीhttp://www.blogger.com/profile/17009545633002694199noreply@blogger.com0tag:blogger.com,1999:blog-8545307170893600943.post-84338810045819336162019-10-28T12:40:00.002-07:002019-10-28T12:40:30.817-07:00Page Life Cycle in ASP.Net and its Events<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-size: large;">Following are the different stages of an ASP.Net page:</span><br /><br /><span style="font-size: large;">Page request</span><br /><br />When ASP.Net gets a page request, it decides whether to parse and compile the page or there would be a cached version of the page; accordingly the response is sent<br /><br /><span style="font-size: large;">Starting of page life cycle</span><br /><br />at this stage, the Request and Response objects are set. If the request is an old request or post back, the IsPostBack property of the page is set to true. The UICulture property of the page is also set.<br /><span style="font-size: large;"><br />Page initialization</span><br /><br />at this stage, the controls on the page are assigned unique ID by setting the UniqueID property and themes are applied. For a new request postback data is loaded and the control properties are restored to the view-state values.<br /><br /><span style="font-size: large;">Page load</span><br /><br />at this stage, control properties are set using the view state and control state values.<br /><br /><span style="font-size: large;">Validation</span><br /><br />Validate method of the validation control is called and if it runs successfully, the IsValid property of the page is set to true.<br /><br /><span style="font-size: large;">Postback event handling</span><br /><br />if the request is a postback (old request), the related event handler is called.<br /><br /><br /><span style="font-size: large;">Page rendering</span><br /><br />. at this stage, view state for the page and all controls are saved. The page calls the Render method for each control and the output of rendering is written to the OutputStream class of the Page's Response property.<br /><br /><span style="font-size: large;">Unload</span><br /><br />the rendered page is sent to the client and page properties, such as Response and Request are unloaded and all cleanup done.<br /><br /><span style="font-size: x-large;">ASP.Net Page Life Cycle Events</span><br /><br />Following are the page life cycle events:<br /><br /><b>PreInit</b><br /><br />PreInit is the first event in page life cycle. It checks the IsPostBack property and determines whether the page is a postback. It sets the themes and master pages, creates dynamic controls and gets and sets profile property values. This event can be handled by overloading the OnPreInit method or creating a Page_PreInit handler.<br /><br /><b>Init</b><br /><br />Init event initializes the control property and the control tree is built. This event can be handled by overloading the OnInit method or creating a Page_Init handler.<br /><br /><b>InitComplete</b><br /><br />InitComplete event allows tracking of view state. All the controls turn on view-state tracking.<br /><br /><b>LoadViewState</b><br /><br />LoadViewState event allows loading view state information into the controls.<br /><br /><b>LoadPostData</b><br /><br />during this phase, the contents of all the input fields defined with the <form>
tag are processed.<br /><br /><b>PreLoad</b><br /><br />PreLoad occurs before the post back data is loaded in the controls. This event can be handled by overloading the OnPreLoad method or creating a Page_PreLoad handler.<br /><br /><b>Load</b><br /> <br />The Load event is raised for the page first and then recursively for all child controls. The controls in the control tree are created. This event can be handled by overloading the OnLoad method or creating a Page_Load handler.<br /><br /><b>LoadComplete</b><br /><br />The loading process is completed, control event handlers are run and page validation takes place. This event can be handled by overloading the OnLoadComplete method or creating a Page_LoadComplete handler.<br /><br /><b>PreRender</b><br /><br />The PreRender event occurs just before the output is rendered. By handling this event, pages and controls can perform any updates before the output is rendered.<br /><br /><b>PreRenderComplete</b><br /><br />as the PreRender event is recursively fired for all child controls, this event ensures the completion of the pre-rendering phase.<br /><br /><b>SaveStateComplete</b><br /><br />state of control on the page is saved. Personalization, control state and view state information is saved. The HTML markup is generated. This stage can be handled by overriding the Render method or creating a Page_Render handler.<br /><br /><b>UnLoad</b><br /><br />the UnLoad phase is the last phase of the page life cycle. It raises the UnLoad event for all controls recursively and lastly for the page itself. Final cleanup is done and all resources and references, such as database connections, are freed. This event can be handled by modifying the OnUnLoad method or creating a Page_UnLoad handler.</form>
</div>
योगेश चन्द्र उप्रेतीhttp://www.blogger.com/profile/17009545633002694199noreply@blogger.com0tag:blogger.com,1999:blog-8545307170893600943.post-74561568281142001422019-10-28T10:56:00.000-07:002019-10-28T12:45:07.422-07:00ASP.NET Session Management Techniques State Management in ASP.NET<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
What are the different Session state management options available in ASP.NET?</h2>
<div>
<br /></div>
<div>
<span style="background-color: white; color: #212121; font-family: "open sans" , sans-serif; font-size: 16px;">State Management in ASP.NET</span><br />
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;">A new instance of the Web page class is created each time the page is posted to the server.</li>
<li style="box-sizing: border-box;">In traditional Web programming, all information that is associated with the page, along with the controls on the page, would be lost with each roundtrip.</li>
<li style="box-sizing: border-box;">The Microsoft ASP.NET framework includes several options to help you preserve data on both a per-page basis and an application-wide basis.<br style="box-sizing: border-box;" />These options can be broadly divided into the following two categories:<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><ul style="box-sizing: border-box;">
<li style="box-sizing: border-box;">Client-Side State Management Options</li>
<li style="box-sizing: border-box;">Server-Side State Management Options</li>
</ul>
</li>
</ul>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Client-Side State Management</strong></div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;">Client-based options involve storing information either in the page or on the client computer.</li>
<li style="box-sizing: border-box;">Some client-based state management options are:<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><ul style="box-sizing: border-box;">
<li style="box-sizing: border-box;">Hidden fields</li>
<li style="box-sizing: border-box;">View state</li>
<li style="box-sizing: border-box;">Cookies</li>
<li style="box-sizing: border-box;">Query strings</li>
</ul>
</li>
</ul>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Server-Side State Management</strong></div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;">There are situations where you need to store the state information on the server side.</li>
<li style="box-sizing: border-box;">Server-side state management enables you to manage application-related and session-related information on the server.</li>
<li style="box-sizing: border-box;">ASP.NET provides the following options to manage state at the server side:<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><ul style="box-sizing: border-box;">
<li style="box-sizing: border-box;">Application state</li>
<li style="box-sizing: border-box;">Session state</li>
</ul>
</li>
</ul>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;"><img alt="State Management" class="" data-src="/UploadFile/8ef97c/Asp-Net-interview-questions-and-answers/Images/State Management.jpg" src="https://www.c-sharpcorner.com/UploadFile/8ef97c/Asp-Net-interview-questions-and-answers/Images/State%20Management.jpg" style="box-sizing: border-box; max-width: 100%;" /></strong></div>
</div>
<div>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
What is Query String in ASP?</h2>
<br style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;" />
<strong style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">Answer: </strong><span style="background-color: white; color: #212121; font-family: "open sans" , sans-serif; font-size: 16px;">A QueryString is a collection of characters input to a computer or web browser. A Query String is helpful when we want to transfer a value from one page to another. When we need to pass content between the HTML pages or aspx Web Forms in the context of ASP.NET, a Query String is Easy to use and the Query String follows a separating character, usually a Question Mark (?). It is basically used for identifying data appearing after this separating symbol. A Query String Collection is used to retrieve the variable values in the HTTP query string. If we want to transfer a large amount of data then we can't use the Request.QueryString. Query Strings are also generated by form submission or can be used by a user typing a query into the address bar of the browsers.</span><br />
<br style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;" />
<strong style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">Syntax of Query String</strong><br />
<em style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">Request.QueryString(variable)[(index).count]</em><br />
<br style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;" />
<img alt="Query String" class="" data-src="/UploadFile/8ef97c/Asp-Net-interview-questions-and-answers/Images/Query String.jpg" src="https://www.c-sharpcorner.com/UploadFile/8ef97c/Asp-Net-interview-questions-and-answers/Images/Query%20String.jpg" style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; max-width: 100%;" /><strong style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;"></strong><br />
<br style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;" />
<strong style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">Advantages:</strong><span style="background-color: white; color: #212121; font-family: "open sans" , sans-serif; font-size: 16px;"></span><br />
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;">Simple to Implement</li>
<li style="box-sizing: border-box;">Easy to get information from Query string.</li>
<li style="box-sizing: border-box;">Used to send or read cross domain (from different domain).</li>
</ul>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Disadvantages:</strong></div>
<ul style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;">Human Readable</li>
<li style="box-sizing: border-box;">Client browser limit on URL length</li>
<li style="box-sizing: border-box;">Cross paging functionality makes it redundant</li>
<li style="box-sizing: border-box;">Easily modified by end user</li>
</ul>
<div>
<h2 style="background-color: white; box-sizing: border-box; color: #212121; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
Different methods of session maintenance in ASP.NET?</h2>
<br style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;" />
<span style="background-color: white; color: #212121; font-family: "open sans" , sans-serif; font-size: 16px;">Session is a State Management Technique. A Session can store the value on the Server. It can support any type of object to be stored along with our own custom objects. A session is one of the best techniques for State Management because it stores the data as client-based, in other words the data is stored for every user separately and the data is secured also because it is on the server.</span><br />
<br style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;" />
<span style="background-color: white; color: #212121; font-family: "open sans" , sans-serif; font-size: 16px;">We can set the session on one of the following 2 types of configuration files:</span><br />
<ol style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px;">
<li style="box-sizing: border-box;"><strong style="box-sizing: border-box;">Machine Configuration file:</strong> Machine Configuration is applied for all application.</li>
<li style="box-sizing: border-box;"><strong style="box-sizing: border-box;">Application Configuration file: </strong>It's applied for only application by application basis.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><img alt="Application Configuration file" class="" data-src="/UploadFile/8ef97c/Asp-Net-interview-questions-and-answers/Images/Application Configuration file.jpg" src="https://www.c-sharpcorner.com/UploadFile/8ef97c/Asp-Net-interview-questions-and-answers/Images/Application%20Configuration%20file.jpg" style="box-sizing: border-box; max-width: 100%;" /><strong style="box-sizing: border-box;"></strong></li>
</ol>
<div style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Session Mode</strong><br />
In ASP.NET there are 4 types of Session Mode.<br />
<br style="box-sizing: border-box;" />
<strong style="box-sizing: border-box;">Off: </strong>We can disable the session mode for the entire application using the off mode.<br />
<br style="box-sizing: border-box;" />
<img alt="ASP.NET page" class="" data-src="/UploadFile/8ef97c/Asp-Net-interview-questions-and-answers/Images/ASP.NET page.jpg" src="https://www.c-sharpcorner.com/UploadFile/8ef97c/Asp-Net-interview-questions-and-answers/Images/ASP.NET%20page.jpg" style="box-sizing: border-box; max-width: 100%;" /><br />
<br style="box-sizing: border-box;" />
According to performance and durability the difference between InProc,State Server and SQL Server is:</div>
<table bgcolor="#ffffff" border="1" cellspacing="1" style="border-collapse: collapse; box-sizing: border-box; color: #333333; font-family: "open sans", sans-serif; font-size: 16px; outline: 0px; width: 100%px;"><tbody style="box-sizing: border-box; outline: 0px;">
<tr bgcolor="#0270bf" style="box-sizing: border-box; outline: 0px;"><td style="border: 1px dashed rgb(171, 171, 171); box-sizing: border-box;"><strong style="box-sizing: border-box;"><span style="box-sizing: border-box; color: white;">Session mode</span></strong></td><td style="border: 1px dashed rgb(171, 171, 171); box-sizing: border-box;"><strong style="box-sizing: border-box;"><span style="box-sizing: border-box; color: white;">Performance Durability</span></strong></td></tr>
<tr style="box-sizing: border-box;"><td style="border: 1px dashed rgb(171, 171, 171); box-sizing: border-box;">InProc</td><td style="border: 1px dashed rgb(171, 171, 171); box-sizing: border-box;">More(1 processor and 1 server) less.</td></tr>
<tr style="box-sizing: border-box;"><td style="border: 1px dashed rgb(171, 171, 171); box-sizing: border-box;">State Server</td><td style="border: 1px dashed rgb(171, 171, 171); box-sizing: border-box;">Medium(n processor and 1 server) Medium</td></tr>
<tr style="box-sizing: border-box;"><td style="border: 1px dashed rgb(171, 171, 171); box-sizing: border-box;">SQL Server</td><td style="border: 1px dashed rgb(171, 171, 171); box-sizing: border-box;">Less More</td></tr>
</tbody></table>
</div>
</div>
<div>
<br />
<div class="PaddingLeft5" id="div2" style="background-color: white; box-sizing: border-box; color: #212121; font-family: "open sans", sans-serif; font-size: 16px !important;">
<strong style="box-sizing: border-box;">1. InProc<br style="box-sizing: border-box;" /></strong>The InProc Session mode is the default Session Mode. Using this Session Mode the Session Mode is stored in the application worker process (aspnet_wp.exe) in the application domain. The Worker Process is dependent on the IIS server version. The memory location was handled by the ASP.NET worker thread. It only involves a considerable overhead for the worker thread to manage these. Also, since this is in the memory of server, chances are that large session information would lead to more memory usage and thus lowering the performance.<br style="box-sizing: border-box;" />I had explained by an example the use inproc session mode.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />In Inproc Session mode the important point is:<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />1. When we get the WebForm2 then if we end the task of aspnet_wp.exe from the Task Manager and then again reload the WebForm2 then you will get no output, that means that all the sessions are stored in a worker process and after closing the task the session will be lost.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />The following are the advantages of the InProc mode:<div style="box-sizing: border-box; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
</div>
<ol style="box-sizing: border-box;">
<li style="box-sizing: border-box;">Inproc session mode is very easy to implement, the only thing that is required is sessionState mode="InProc".</li>
<li style="box-sizing: border-box;">It will perform fast because the session is kept on the web server within the ASP.NET Worker Process.</li>
<li style="box-sizing: border-box;">Data is stored separately and the data is secure so it is suitable for web applications.</li>
<li style="box-sizing: border-box;">In this mode there is no need to serialize and deserialize the object for storage and retrieval of the data.</li>
</ol>
<div style="box-sizing: border-box; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
The following are the disadvantages of InProc mode:</div>
<ol style="box-sizing: border-box;">
<li style="box-sizing: border-box;">Session data is lost when the worker process or application process is recycled.</li>
<li style="box-sizing: border-box;"><strong style="box-sizing: border-box;">Not Suitable for WebFarms and WebGardens:</strong> In a webFarm that the web application is deployed on the various web servers.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><img alt="" class="" data-src="/UploadFile/225740/introduction-of-session-in-Asp-Net/Images/Session in ASP.NET6.JPG" src="https://www.c-sharpcorner.com/UploadFile/225740/introduction-of-session-in-Asp-Net/Images/Session%20in%20ASP.NET6.JPG" style="box-sizing: border-box; max-width: 100%;" /><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />If the client sends the request and the request goes to the server load balancer and then it sees which Web Server is needed to be used, for example if the Web Server1 is needed to be used then the request goes to WebServer1 and the session variable is stored in Web Server1 and if we refresh web form2 and again make a request to Web Form2 then the server load balancer sends the request to web server2 and the session variable is not stored in Web Server2 so the session variable is lost. So the InProc mode is dependent on the Web Server.</li>
<li style="box-sizing: border-box;"><strong style="box-sizing: border-box;">Increase the load of server</strong><br style="box-sizing: border-box;" />In Session Mode the sessions are stored on the web server. If the number of sessions is increased then the load of the server is also increased and the scalability could be an issue.</li>
</ol>
<div style="box-sizing: border-box; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">2. StateServer<br style="box-sizing: border-box;" /></strong>This is also the Out-Proc Session mode. StateServer uses a stand-alone Windows Service that is independent on IIS and can also be run on a separate server. This session state is totally managed by aspnet_state.exe. And the Session Variables are stored in an ASP.NET State service.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />Now configure with the ASP.NET State Service.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><strong style="box-sizing: border-box;">Step 1: </strong>Go To Start and from there go to "Run" and type "services.msc" as in the following:<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><img alt="" class="" data-src="/UploadFile/225740/introduction-of-session-in-Asp-Net/Images/Session in ASP.NET7.PNG" src="https://www.c-sharpcorner.com/UploadFile/225740/introduction-of-session-in-Asp-Net/Images/Session%20in%20ASP.NET7.PNG" style="box-sizing: border-box; max-width: 100%;" /><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><strong style="box-sizing: border-box;">Step 2: </strong>Now open the Services Management Window and right-click on ASP.NET State Service and start the service; by default these services are stopped.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><img alt="" class="" data-src="/UploadFile/225740/introduction-of-session-in-Asp-Net/Images/Session in ASP.NET8.jpg" src="https://www.c-sharpcorner.com/UploadFile/225740/introduction-of-session-in-Asp-Net/Images/Session%20in%20ASP.NET8.jpg" style="box-sizing: border-box; max-width: 100%;" /><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><strong style="box-sizing: border-box;">Step 3:</strong> For configuration with web.config write the code to web.config file.</div>
<div class="dp-highlighter" style="background-color: #e7e5dc; box-sizing: border-box; font-family: Consolas, "Courier New", Courier, mono, serif; margin: 18px 0px !important; overflow: auto; padding-top: 1px; width: 815.094px;">
<ol class="dp-c" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"><sessionstate mode="</span" nbsp=""><span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"StateServer"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> stateConnectionString=</span><span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"tcpip=localhost:42424"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">> </span></sessionstate></span></span></li>
</ol>
</div>
<div style="box-sizing: border-box; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
Generally the state services and web services are not in the same machine of a dedicated server so for the connection we need to write the stateConnectionString and here we need to provide the IP address or the name of the machine where the state service is running and here my services are run on localhost and 42424 is the port number.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />The ASP.NET State Services can be present on a Web Server or a dedicated machine. So if we closed the worker Process (aspnet_wp.exe) then it is also not affected.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><img alt="" class="" data-src="/UploadFile/225740/introduction-of-session-in-Asp-Net/Images/Session in ASP.NET9.jpg" src="https://www.c-sharpcorner.com/UploadFile/225740/introduction-of-session-in-Asp-Net/Images/Session%20in%20ASP.NET9.jpg" style="box-sizing: border-box; max-width: 100%;" /><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />When the client sends a request to the web server, the web server stores the session data on the state server. The StateServer may be the current system or a different system. But it will be totally independent of IIS. The destination of the StateServer will depend on the web.config stateConnectionString setting. If we set it to localhost:42424 then it will store data in the local system itself. For changing the StateServer destination, we need to change the IP and ensure aspnet_state.exe is up and running on that system. Otherwise you will get the following exception while trying to store data on the session.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><strong style="box-sizing: border-box;">Advantage of State Service</strong></div>
<ol style="box-sizing: border-box;">
<li style="box-sizing: border-box;">No issue about Worker Process because it's not dependent on Worker Process.</li>
<li style="box-sizing: border-box;">Can be used with Web Farm and Web Garden: It supports a Web Farm and Web Garden both<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><img alt="" class="" data-src="/UploadFile/225740/introduction-of-session-in-Asp-Net/Images/Session in ASP.NET10.jpg" src="https://www.c-sharpcorner.com/UploadFile/225740/introduction-of-session-in-Asp-Net/Images/Session%20in%20ASP.NET10.jpg" style="box-sizing: border-box; max-width: 100%;" /><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />When a client sends a request of WebForm1.aspx then the server load balancer checks which server needs to the used and if the request goes to web server1 then web server sees the request store the session on the state server and again provides the response of WebForm2.aspx and now if the client again sends a request of webform2 and the load balancer provides the request to web server2 and then web server2 has the web.config file and it sends a request to the state server and the state server already has a session so it provides the response.</li>
<li style="box-sizing: border-box;">Scalability is also increased because it keeps data separate from IIS.</li>
</ol>
<div style="box-sizing: border-box; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Disadvantage of State Service</strong></div>
<ol style="box-sizing: border-box;">
<li style="box-sizing: border-box;"><strong style="box-sizing: border-box;">Performance Decrease</strong><br style="box-sizing: border-box;" />When the request goes to the server the object is serialized and deserialized so for that the performance is decreased so it is slower than the OnProc State Mode.</li>
<li style="box-sizing: border-box;">If the request goes to the server and for some reason the ASP.NET State Service is restarted then all the sessions will be destroyed.</li>
</ol>
<div style="box-sizing: border-box; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<br style="box-sizing: border-box;" /><strong style="box-sizing: border-box;">3. SQL Server</strong><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />In this mode the session data is stored inside the SQL Server database so to store the session in the database we need to follow these steps.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><strong style="box-sizing: border-box;">Step 1:</strong> From the command prompt, go to your Framework version directory, for example: c:\windows\microsoft.net\framework\<version>. and search for the aspnet_regsql.exe that in which version these files are present and execute the file in the command prompt.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><img alt="" class="" data-src="/UploadFile/225740/introduction-of-session-in-Asp-Net/Images/Session in ASP.NET11.PNG" src="https://www.c-sharpcorner.com/UploadFile/225740/introduction-of-session-in-Asp-Net/Images/Session%20in%20ASP.NET11.PNG" style="box-sizing: border-box; max-width: 100%;" /><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><strong style="box-sizing: border-box;">Parameter Description:</strong></version></div>
<ol style="box-sizing: border-box;">
<li style="box-sizing: border-box;">ssadd: Add support for SQLServer mode session state.</li>
<li style="box-sizing: border-box;">sstype p: P stands for Persisted. It persists the session data on the server.</li>
<li style="box-sizing: border-box;">S : Server name.</li>
<li style="box-sizing: border-box;">U : User name.</li>
<li style="box-sizing: border-box;">P : Password.</li>
<li style="box-sizing: border-box;">E : Authentication using the windows credential of the currently logged on users.</li>
</ol>
<div style="box-sizing: border-box; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">Step 2:</strong> After executing the command, open the database:<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><img alt="" class="" data-src="/UploadFile/225740/introduction-of-session-in-Asp-Net/Images/Session in ASP.NET12.PNG" src="https://www.c-sharpcorner.com/UploadFile/225740/introduction-of-session-in-Asp-Net/Images/Session%20in%20ASP.NET12.PNG" style="box-sizing: border-box; max-width: 100%;" /><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><strong style="box-sizing: border-box;">Step 3:</strong> Now for configuration we need to write the connection string in the web.config file:<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><sessionstate mode="SQLServer" sqlconnectionstring="Server=DIVS\SQLEXPRESS;Integrated Security=true"><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><strong style="box-sizing: border-box;">Step 4:</strong> And the webfrom1.aspx and webform2.aspx code are the same.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><strong style="box-sizing: border-box;">Step 5: </strong>Now after sending a request to the server open the database and here the session id is stored and by default its expiration time is 20 minutes.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><img alt="" class="" data-src="/UploadFile/225740/introduction-of-session-in-Asp-Net/Images/Session in ASP.NET13.jpg" src="https://www.c-sharpcorner.com/UploadFile/225740/introduction-of-session-in-Asp-Net/Images/Session%20in%20ASP.NET13.jpg" style="box-sizing: border-box; max-width: 100%;" /><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />The following are the advantages of SQLServer mode:</sessionstate></div>
<ol style="box-sizing: border-box;">
<li style="box-sizing: border-box;">SQL Server is a more reliable and secure option.</li>
<li style="box-sizing: border-box;">It's not dependent on the Worker Process and ASP.NET State Service so if it restarts then it is also not affected by the session.</li>
<li style="box-sizing: border-box;">It is useful for a Web Farm and Web Garden: it is the same as State Server mode. It works the same also, the only difference is that the session is stored on the SQL server Database.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><img alt="" class="" data-src="/UploadFile/225740/introduction-of-session-in-Asp-Net/Images/Session in ASP.NET14.JPG" src="https://www.c-sharpcorner.com/UploadFile/225740/introduction-of-session-in-Asp-Net/Images/Session%20in%20ASP.NET14.JPG" style="box-sizing: border-box; max-width: 100%;" /><br style="box-sizing: border-box;" /></li>
<li style="box-sizing: border-box;">Scalability: The scalability is increased compared to InProc and State Server because the session is stored in the database, no matter how many requests on the web server.</li>
</ol>
<div style="box-sizing: border-box; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
The following are the disadvantages of SQLServer mode:</div>
<ol style="box-sizing: border-box;">
<li style="box-sizing: border-box;">It's slower than StateServer and InProc Session mode.</li>
<li style="box-sizing: border-box;">It must be serialized and deserialized.</li>
</ol>
<div style="box-sizing: border-box; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
The following are the reasons to use the SQLServer Session mode:</div>
<ol style="box-sizing: border-box;">
<li style="box-sizing: border-box;">When we need a session with more security, in other words we need that the data of the session is more secure.</li>
<li style="box-sizing: border-box;">If there happen to be frequent server restarts, this is an ideal choice.</li>
<li style="box-sizing: border-box;">We can use SQLServer session mode when we need to share sessions between two different applications.</li>
</ol>
<div style="box-sizing: border-box; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
According to performance and durability the difference between InProc, State Server and SQL Server is:</div>
<table bgcolor="#ffffff" border="1" cellspacing="1" style="border-collapse: collapse; box-sizing: border-box; color: #333333; outline: 0px; width: 100%px;"><tbody style="box-sizing: border-box; outline: 0px;">
<tr bgcolor="#0270bf" style="box-sizing: border-box; outline: 0px;"><td style="box-sizing: border-box;"><span style="box-sizing: border-box; color: white;">Session mode</span></td><td style="box-sizing: border-box;"><span style="box-sizing: border-box; color: white;">Performance</span></td><td style="box-sizing: border-box;"><span style="box-sizing: border-box; color: white;">Durability</span></td></tr>
<tr style="box-sizing: border-box;"><td style="box-sizing: border-box;">InProc</td><td style="box-sizing: border-box;">more(1 processor and 1 server)</td><td style="box-sizing: border-box;">less</td></tr>
<tr style="box-sizing: border-box;"><td style="box-sizing: border-box;">State Server</td><td style="box-sizing: border-box;">Medium(n processor and 1 server)</td><td style="box-sizing: border-box;">Medium</td></tr>
<tr style="box-sizing: border-box;"><td style="box-sizing: border-box;">SQL Server</td><td style="box-sizing: border-box;">Less</td><td style="box-sizing: border-box;">More</td></tr>
</tbody></table>
<div style="box-sizing: border-box; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
<strong style="box-sizing: border-box;">4. Custom<br style="box-sizing: border-box;" /></strong>Using this session mode we can control everything, like session id and all it means that you can create your own algorithm to create a session id. It uses less something compared to others. You can create your own session state provider for example: Oracle.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />The method of the implement of the custom session mode is:<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><img alt="" class="" data-src="/UploadFile/225740/introduction-of-session-in-Asp-Net/Images/Session in ASP.NET15.jpg" src="https://www.c-sharpcorner.com/UploadFile/225740/introduction-of-session-in-Asp-Net/Images/Session%20in%20ASP.NET15.jpg" style="box-sizing: border-box; max-width: 100%;" /><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><strong style="box-sizing: border-box;">Step 1:</strong> The initialize method sets the custom provider and provides the connection with the provider.<br style="box-sizing: border-box;" /><strong style="box-sizing: border-box;">Step 2: </strong>SetItemExpireCallback is used to set the expiration time.<br style="box-sizing: border-box;" /><strong style="box-sizing: border-box;">Step 3:</strong> InitializeRequest is called on every request and CreateNewStoreData is used to create a new instance of SessionStateStoreData.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />Now configure with the web.config file as in the following:</div>
<div class="dp-highlighter" style="background-color: #e7e5dc; box-sizing: border-box; font-family: Consolas, "Courier New", Courier, mono, serif; margin: 18px 0px !important; overflow: auto; padding-top: 1px; width: 815.094px;">
<ol class="dp-c" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"><sessionstate mode="</span" nbsp=""><span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"Custom"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> customProvider=</span><span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"demo"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">> </span></sessionstate></span></span></li>
<li style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <providers> </providers></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <add class="string" name="<span" nbsp="" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"demo"</add></span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> type=</span><span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"CustomDataType"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> /> </span></li>
</ol>
</div>
</div>
</div>
</div>
<li style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"></span></li>
<br style="box-sizing: border-box;" /><div style="box-sizing: border-box; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
The following are the advantages of custom session mode:</div>
<ol style="box-sizing: border-box;">
<li style="box-sizing: border-box;">We can use an existing table for storing the session data</li>
<li style="box-sizing: border-box;">It's not dependent on IIS.</li>
</ol>
<div style="box-sizing: border-box; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
The following are the disadvantages of custom session mode:</div>
<ol style="box-sizing: border-box;">
<li style="box-sizing: border-box;">Processing of data is very slow</li>
</ol>
<div style="box-sizing: border-box; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
The following is when should we use should use a custom control:</div>
<ol style="box-sizing: border-box;">
<li style="box-sizing: border-box;">When we want to create our own session id.</li>
<li style="box-sizing: border-box;">Where we want to store session data in other location</li>
<li style="box-sizing: border-box;">When we need to use an existing table to store session data.</li>
</ol>
<div style="box-sizing: border-box; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
</div>
<h2 style="box-sizing: border-box; color: inherit; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
Cookieless session</h2>
<br style="box-sizing: border-box;" />The session id is stored as a cookie on the client machine. The session id is then used by the web server to identify if the request is coming from the same user or a different user.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />Now by taking a previous example if we give the request to the server of webForm1.aspx and store the cookies and give the response and here go to the browser and right-click on the browser and go to inspect element or press F12.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><img alt="" class="" data-src="/UploadFile/225740/introduction-of-session-in-Asp-Net/Images/Session in ASP.NET16.jpg" src="https://www.c-sharpcorner.com/UploadFile/225740/introduction-of-session-in-Asp-Net/Images/Session%20in%20ASP.NET16.jpg" style="box-sizing: border-box; max-width: 100%;" /><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />Here this is the Session Id that was stored in the cookies, it will never change if we run webForm1 or WebForm2 because for a specific client or browser it is unique. But if by default cookies are disabled on the client machine then we need to configure it in the web.config file as in the following:<br style="box-sizing: border-box;" /><div style="box-sizing: border-box; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
</div>
<div class="dp-highlighter" style="background-color: #e7e5dc; box-sizing: border-box; font-family: Consolas, "Courier New", Courier, mono, serif; margin: 18px 0px !important; overflow: auto; padding-top: 1px; width: 815.094px;">
<ol class="dp-c" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"><sessionstate mode="</span" nbsp=""><span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"InProc"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> cookieless=</span><span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"true"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">> </span></sessionstate> </span></span></li>
</ol>
</div>
<div style="box-sizing: border-box; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
And if we run the webForm1 then we get the session id in the URL and it is sent back between the client and Web Server with every request and response.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><img alt="" class="" data-src="/UploadFile/225740/introduction-of-session-in-Asp-Net/Images/Session in ASP.NET17.PNG" src="https://www.c-sharpcorner.com/UploadFile/225740/introduction-of-session-in-Asp-Net/Images/Session%20in%20ASP.NET17.PNG" style="box-sizing: border-box; max-width: 100%;" /><br style="box-sizing: border-box;" /><img alt="" class="" data-src="/UploadFile/225740/introduction-of-session-in-Asp-Net/Images/Session in ASP.NET18.PNG" src="https://www.c-sharpcorner.com/UploadFile/225740/introduction-of-session-in-Asp-Net/Images/Session%20in%20ASP.NET18.PNG" style="box-sizing: border-box; max-width: 100%;" /><br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />Both the session ids are the same and if we change the session id from the URL and refresh the page it goes to the server and the server thinks that the request comes from a different user so it never provides the required output.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" /><strong style="box-sizing: border-box;">Remove Session<br style="box-sizing: border-box;" /></strong>Mostly we can remove session using one of the following 4 methods:</div>
<ol style="box-sizing: border-box;">
<li style="box-sizing: border-box;">Session.Remove(strSessionName):- Removes an item from the session state collection</li>
<li style="box-sizing: border-box;">Session.RemoveAll():- Removes all items from the session collection.</li>
<li style="box-sizing: border-box;">Session.Clear():- it is same as sesion.RemoveAll() method.</li>
<li style="box-sizing: border-box;">Session.Abandon():-Cancels the current session.</li>
</ol>
<div style="box-sizing: border-box; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
</div>
<h2 style="box-sizing: border-box; color: inherit; font-family: Roboto, sans-serif; font-size: 24px; font-weight: 400; line-height: 1.1; margin-bottom: 0px; margin-top: 0px;">
Enable and Disable Sessions</h2>
<br style="box-sizing: border-box;" />We can enable and disable session state in one of two ways:<div style="box-sizing: border-box; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
</div>
<ol style="box-sizing: border-box;">
<li style="box-sizing: border-box;"><strong style="box-sizing: border-box;">Page Level:</strong> We have the attribute of page level that EnableSessionState<br style="box-sizing: border-box;" /><div class="dp-highlighter" style="background-color: #e7e5dc; box-sizing: border-box; font-family: Consolas, "Courier New", Courier, mono, serif; margin: 18px 0px !important; overflow: auto; padding-top: 1px; width: 775.484px;">
<ol class="dp-c" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"><%@ Page Language=</span><span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"C#"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> EnableSessionState=</span><span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"False"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span></span></li>
</ol>
</div>
It disables the session state.<br style="box-sizing: border-box;" /><br style="box-sizing: border-box;" />We can make it read-only also. This will permit access to session data but will not allow writing data on the session.<br style="box-sizing: border-box;" /><div class="dp-highlighter" style="background-color: #e7e5dc; box-sizing: border-box; font-family: Consolas, "Courier New", Courier, mono, serif; margin: 18px 0px !important; overflow: auto; padding-top: 1px; width: 775.484px;">
<ol class="dp-c" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"><%@ Page Language=</span><span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"C#"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> EnableSessionState=</span><span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"ReadOnly"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span></span></li>
</ol>
</div>
</li>
<li style="box-sizing: border-box;"><strong style="box-sizing: border-box;">Application Level</strong>: Disables a Session for an entire web application; we need to use this at the application level. Set the property EnableSessionState in Web.Config.<br style="box-sizing: border-box;" /><div class="dp-highlighter" style="background-color: #e7e5dc; box-sizing: border-box; font-family: Consolas, "Courier New", Courier, mono, serif; margin: 18px 0px !important; overflow: auto; padding-top: 1px; width: 775.484px;">
<ol class="dp-c" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"><pages enablesessionstate="</span" nbsp=""><span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"false"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">></span></pages> </span></span></li>
</ol>
</div>
</li>
</ol>
<div style="box-sizing: border-box; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
The following are the advantages of session:</div>
<ol style="box-sizing: border-box;">
<li style="box-sizing: border-box;">It is easy to implement.</li>
<li style="box-sizing: border-box;">Store data separately.</li>
<li style="box-sizing: border-box;">Session is secure and transparent from the user.</li>
</ol>
<div style="box-sizing: border-box; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
The following are the disadvantages of session:</div>
<ol style="box-sizing: border-box;">
<li style="box-sizing: border-box;">Performance decrease if we do not use InProc Session mode.</li>
<li style="box-sizing: border-box;">Overhead involved in serializing and de-serializing session data.</li>
</ol>
<div style="box-sizing: border-box; line-height: 1.5; margin-bottom: 1em; margin-top: 1em;">
</div>
<div class="suggested-article" id="divSuggestedArticle" style="background-color: white; box-sizing: border-box; color: #212121; font-family: "Open Sans", sans-serif; font-size: 14px; margin-bottom: 20px; margin-top: 20px; overflow: hidden; padding: 5px 0px;">
</div>
योगेश चन्द्र उप्रेतीhttp://www.blogger.com/profile/17009545633002694199noreply@blogger.com0tag:blogger.com,1999:blog-8545307170893600943.post-31246684718297288622019-10-28T09:40:00.001-07:002019-10-28T12:37:07.530-07:00ASP.NET Interview Questions Part-1<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-size: large;">A list of top frequently asked ASP.NET interview questions and answers are given below.</span><br />
<br />
1) What is ASP?<br />
ASP stands for Active Server Pages. It is also known as classic ASP. It is a server-side technology provided by Microsoft which is used to create dynamic and user-friendly web pages. It uses different scripting languages to create dynamic web pages which can be run on any browsers.<br />
<br />
<div>
2) What is ASP.NET?<br />
ASP.Net is a specification by Microsoft which is used to create web applications and web services. It is a part of ".Net framework". You can create ASP.Net applications in most of the .Net compatible languages like Visual Basic, C#, etc. ASP.Net provides much better performance than scripting languages.<br />
<br />
<b>Advantages of ASP.NET </b></div>
<div>
<b>Separation of Code from HTML:</b><br />
To make a clean sweep, with ASP.NET you have the ability to completely separate layout and business logic. This makes it much easier for teams of programmers and designers to collaborate efficiently.<br />
<br />
<b>Support for compiled languages:</b><br />
Developer can use VB.NET and access features such as strong typing and object-oriented programming. Using compiled languages also means that ASP.NET pages do not suffer the performance penalties associated with interpreted code. ASP.NET pages are precompiled to byte-code and Just In Time (JIT) compiled when first requested. Subsequent requests are directed to the fully compiled code, which is cached until the source changes.<br />
<br />
<b>Use services provided by the .NET Framework:</b><br />
The .NET Framework provides class libraries that can be used by your application. Some of the key classes help you with input/output, access to operating system services, data access, or even debugging. We will go into more detail on some of them in this module.<br />
<br />
<b>Graphical Development Environment:</b><br />
Visual Studio .NET provides a very rich development environment for web developers. You can drag and drop controls and set properties the way you do in Visual Basic 6. And you have full IntelliSense support, not only for your code, but also for HTML and XML</div>
<div>
<b>State management:</b><br />
To refer to the problems mentioned before, ASP.NET provides solutions for session and application state management. State information can, for example, be kept in memory or stored in a database. It can be shared across web farms, and state information can be recovered, even if the server fails or the connection breaks down.<br />
<br />
<b>Update files while the server is running:</b><br />
Components of your application can be updated while the server is online and clients are connected. The framework will use the new files as soon as they are copied to the application. Removed or old files that are still in use are kept in memory until the clients have finished.<br />
<br />
<b>XML-Based Configuration Files:</b><br />
Configuration settings in ASP.NET are stored in XML files that you can easily read and edit. You can also easily copy these to another server, along with the other files that comprise your application.<br />
3) What is the difference between the ASP and ASP.NET?<br />
The main difference between ASP and ASP.Net is that ASP is interpreted, while ASP.Net is compiled. ASP uses VBScript, therefore when the ASP page is executed, it is interpreted. On the other hand, ASP.Net uses .Net languages like C# and VB.NET, which is compiled to Microsoft intermediate language.<br />
<br />
Here are some points that give the quick overview of ASP.NET.</div>
<div>
<br />
1. ASP.NET provides services to allow the creation, deployment, and execution of Web Applications and Web Services.<br />
<br />
2. Like ASP, ASP.NET is a server-side technology.<br />
<br />
3. Web Applications are built using Web Forms. ASP.NET comes with built-in Web Forms controls, which are responsible for generating the user interface. They mirror typical HTML widgets like text boxes or buttons. If these controls do not fit your needs, you are free to create your own user controls.<br />
<br />
4. Web Forms are designed to make building web-based applications as easy as building Visual Basic applications.<br />
<br />
4) What is IIS?<br />
IIS stands for Internet Information Services. It is created by Microsoft to provide Internet-based services to ASP.NET Web applications.</div>
<div>
<br />
5) What is the usage of IIS?<br />
Following are the main usage of IIS: </div>
<div>
IIS is used to make your computer to work as a Web server and provides the functionality to develop and deploy Web applications on the server. </div>
<div>
IIS handles the request and response cycle on the Web server. </div>
<div>
IIS also offers the services of SMTP and FrontPage server extensions. </div>
<div>
The SMTP is used to send emails and use FrontPage server extensions to get the dynamic features of </div>
<div>
IIS, such as form handler. </div>
<div>
<br />
6) What is a multilingual website?<br />
If a website provides content in many languages, it is known as a multilingual website. It contains multiple copies of its content and other resources, such as date and time, in different languages.</div>
<div>
<br /></div>
<div>
7) What is caching? Explain.<br />
Caching is the technique which facilitates you to store frequently used items in memory so that they can be accessed more quickly.</div>
<div>
<br /></div>
<div>
8) what are the main requirements for caching? <br />
By caching the response, your request is served by the response already stored in memory. </div>
<div>
You must be very careful while choosing the items to cache because Caching incurs overhead. <br />
A frequently used web form which data doesn't frequently change is good for caching. <br />
A cached web form freezes form?s server-side content, and changes to that content do not appear until the cache is refreshed. </div>
<div>
<br />
9) What are the advantages of ASP.NET?<br />
ASP.Net is the next generation of ASP technology platform. It is superior to ASP in the following ways: </div>
<div>
<br /></div>
<div>
Highly Scalable <br />
Compiled Code <br />
User Authentication <br />
Language Support <br />
Third party control <br />
Configuration and Deployment are easy. <br />
Object and Page caching <br />
Strict coding requirements </div>
<div>
<br />
10) What is the concept of Postback in ASP.NET?<br />
Postback is a request which is sent from a client to the server from the same page user is working with. There is an HTTP POST request mechanism in ASP.NET. It posts a complete page back to the server to refresh the whole page.<br />
<br />
If we create a web Page, which consists of one or more Web Controls that are configured to use AutoPostBack (Every Web controls will have their own AutoPostBack property), the ASP.NET adds a special JavaScipt function to the rendered HTML Page. This function is named _doPostBack() . When Called, it triggers a PostBack, sending data back to the web Server.<br />
<br />
ASP.NET also adds two additional hidden input fields that are used to pass information back to the server. This information consists of ID of the Control that raised the event and any additional information if needed. These fields will empty initially as shown below, <br />
<br />
<input id="__EVENTTARGET" name="__EVENTTARGET" type="hidden" value="" /> <br />
<input id="__EVENTARGUMENT" name="__EVENTARGUMENT" type="hidden" value="" /> <br />
<br />
The following actions will be taken place when a user changes a control that has the AutoPostBack property set to true: <br />
<br />
1. On the client side, the JavaScript _doPostBack function is invoked, and the page is resubmitted to the server.<br />
<br />
2. ASP.NET re-creates the Page object using the .aspx file.<br />
<br />
3. ASP.NET retrieves state information from the hidden view state field and updates the controls accordingly.<br />
<br />
4. The Page.Load event is fired.<br />
<br />
5. The appropriate change event is fired for the control. (If more than one control has been changed, the order of change events is undetermined.)<br />
<br />
6. The Page.PreRender event fires, and the page is rendered (transformed from a set of objects to an HTML page).<br />
<br />
7. Finally, the Page.Unload event is fired.<br />
<br />
8. The new page is sent to the client.<br />
<br />
11) What is the used of "isPostBack" property?<br />
The "IsPostBack" property of page object is used to check that the page is posted back or not.</div>
<div>
<br /></div>
<div>
12) How do you identify that the page is PostBack?<br />
There is a property named "IsPostBack" property in Post object, which can be checked to know that the page is posted back.</div>
<div>
<br />
13) What is the parent class of all web server control?<br />
System.Web.UI.Control class</div>
<div>
<br />
14) What is the difference between ASP.NET Webforms and ASP.NET MVC?<br />
ASP.NET Webforms uses the page controller approach for rendering layout. In this approach, every page has its controller.<br />
<br />
On the other hand, ASP.NET MVC uses the Front Controller approach. In this approach, there is a common controller for all pages.</div>
<div>
<br />
15) What is the difference between the GET method () and POST method ()?<br />
get method( )<br />
<br />
post method( ) </div>
<div>
1. Get method Data is affixed to the URL. <br />
<br />
Post method Data is not affixed to the URL. <br />
<br />
2. Get method Data is not secured. <br />
<br />
Post method Data is secured. <br />
<br />
3. Get method Data transmission is faster in this method. <br />
<br />
Post method Data transmission is comparatively slow. <br />
<br />
4. Get method It is a single call system. <br />
<br />
Post method is a two call system. <br />
<br />
5. Get method Only a limited amount of data can be sent. <br />
<br />
Post method - A large amount of data can be sent. <br />
<br />
6. Get method It is a default method for many browsers. <br />
<br />
Post method is not set as default. It should be explicitly specified. <br />
<br />
16) What is the difference between session object and application object?<br />
The session object is used to maintain the session of each user. A session id is generated if a user enters in the application and when the user leaves the application, the session id is automatically deleted.</div>
<div>
<br />
On the other hand, the application object is used to store the information and access variables from any page in the application.</div>
<div>
<br />
17) What is the difference between trace and debug?<br />
Debug class is used to debug builds. Trace class is used for both debug and release builds.<br />
<br />
Tracing helps to see the information of issues at the runtime of the application. By default Tracing is disabled.<br />
<br />
Tracing has the following important features:</div>
<div>
<br />
1. We can see the execution path of the page and application using the debug statement.<br />
2. We can access and manipulate trace messages programmatically.<br />
3. We can see the most recent tracing of the data.<br />
<br />
Tracing can be done with the following 2 types.</div>
<div>
1. Page Level: When the trace output is displayed on the page and for the page-level tracing we need to set the property of tracing at the page level.<br />
<br />
<%@ Page Trace="true" Language="C#"<br />
<br />
2. Application: Level: In Application-Level tracing the information is stored for each request of the application. The default number of requests to store is 10. But if you want to increase the number of requests and discard the older request and display a recent request then you need to set the property in the web.config file.</div>
<div>
<trace enabled="true"></trace></div>
<div>
<br /></div>
<div>
18) What is the difference between client-side and server-side validations in WebPages?<br />
The client-side validation happens at the client's side with the help of JavaScript and VBScript. This validation has occurred before the Web page is sent to the server.<br />
<br />
The server-side validation happens at the server side.</div>
<div>
<br />
19) What is the difference between file-based dependency and key-based dependency?<br />
File-based dependency: File-based dependency facilitates you to save the dependency on a file in a disk.<br />
<br />
Key-based dependency: In key-based dependency, you depend on another cached item.</div>
<div>
<br />
20) What is the difference between globalization and localization?<br />
Globalization: Globalization is a technique to identify the part of a Web application that is different for different languages and separate it out from the web application.<br />
<br />
Localization: In localization, you try to configure a Web application so that it can be supported for a specific language or locale.</div>
<div>
<br />
21) What is the difference between a page theme and a global theme?<br />
Page Theme: The page theme is applied to particular web pages of the project. It is stored inside a subfolder of the App_Themes folder.</div>
<div>
<br /></div>
<div>
Global Theme: The Global theme is applied to all the web applications on the web server. It is stored inside the Themes folder on a Web server.</div>
<div>
<br />
22) What is the difference between early binding and late binding?<br />
Early Binding: In early binding, a non-virtual method is called which is decided at a compile time.</div>
<div>
<br />
Late Binding: In late binding, a virtual method is called which is decided at runtime.</div>
<div>
<br /></div>
<div>
23) What is the difference between server-side scripting and client-side scripting?<br />
Server-side scripting: In server-side scripting, all the script are executed by the server and interpreted as needed.<br />
<br />
Client-side scripting: In client-side scripting, the script will be executed immediately in the browser such as form field validation, email validation, etc.The client-side scripting is usually carried out in VBScript or JavaScript.</div>
<div>
<br />
24) How to sign out from forms authentication?<br />
FormsAuthentication.Signout() method is used to sign out from forms authentication.</div>
<div>
<br />
25) How to display validation messages in one control?<br />
By the help of ValidationSummary control, we can display all validation messages in one control.</div>
<div>
<br />
26) What is the difference between authentication and authorization?<br />
Authentication is a process of identifying user whereas authorization is used to check the access rights of an identified user.</div>
<div>
<br />
27) Which object encapsulates state or data of a user?<br />
Session object.</div>
<div>
<br /></div>
<div>
28) What is ViewState?<br />
ViewState is a feature of ASP.NET to store the values of a page before it is submitted to the server. After posting the page, data from is ViewState is restored.<br />
<br />
Features of View State. These are the main features of view state:<br />
1. Retains the value of the Control after post-back without using a session.<br />
2. Stores the value of Pages and Control Properties defined in the page.<br />
3. Creates a custom View State Provider that lets you store View State Information in a SQL Server Database or in another data store.<br />
<br />
Advantages of View State<br />
1. Easy to Implement.<br />
2. No server resources are required: The View State is contained in a structure within the page load.<br />
Enhanced security features: It can be encoded and compressed or Unicode implementation.</div>
<div>
<br />
29) What is ViewState information stored?<br />
It is stored in HTML hidden field.</div>
<div>
<br />
30) What are the differences between the Response.Write() and Response.Output.Write()?<br />
Response.Write() is used for normal output whereas Response.Output.Write() is used for formatted output.</div>
<div>
<br />
31) Define the types of configuration files.<br />
There are two types of configuration files: <br />
Application Level config = Web.config. <br />
Machine Level config = Machine.config. </div>
<div>
<br />
32) What is the difference between Web config and Machine config files?<br />
Web config file is specific to web application whereas Machine config file is specific to machine or server.</div>
<div>
<br />
There can be multiple web config files in an application but only one machine config file.</div>
<div>
<br />
33) What is MVC?<br />
MVC stands for Model View Controller. It is a design pattern that is used to separate business logic and presentation logic. It is used to develop the highly customized application.</div>
<div>
<br />
The Model represents data, View represents presentation and controller acts as an interface between Model and View.</div>
<div>
<br />
34) What are the built-in objects in ASP.NET?<br />
The major built-in objects are given below: <br />
Application <br />
Session <br />
Context <br />
Request <br />
Response <br />
Server <br />
Trace </div>
<div>
<br />
35) What do you mean by Role-based security?<br />
Role-based security is used in almost all organization, and the Role-based security assigns certain privileges to each role. </div>
<div>
Each user is assigned a particular role from the list. <br />
Privileges as per role restrict the user's actions on the system and ensure that a user can do only what he is permitted to do on the system. </div>
<div>
<br />
36) What is a cookie?<br />
A Cookie is a small piece of information which is stored at the client side. There are two types of cookie: <br />
Session/Temporary Cookie: valid for a single session <br />
Persistent Cookie: valid for multiple session<br />
<br />
Cookies are a State Management Technique that can store the values of control after a post-back. Cookies can store user-specific Information on the client's machine like when the user last visited your site. Cookies are also known by many names, such as HTTP Cookies, Browser Cookies, Web Cookies, Session Cookies and so on. Basically cookies are a small text file sent by the web server and saved by the Web Browser on the client's machine.<br />
<br />
List of properties containing the HttpCookies Class:<br />
Domain: Using these properties we can set the domain of the cookie.<br />
<br />
Expires: This property sets the Expiration time of the cookies.<br />
<br />
HasKeys: If the cookies have a subkey then it returns True.<br />
<br />
Name: Contains the name of the Key.<br />
<br />
Path: Contains the Virtual Path to be submitted with the Cookies.<br />
<br />
Secured: If the cookies are to be passed in a secure connection then it only returns True.<br />
<br />
Value: Contains the value of the cookies.<br />
<br />
Limitation of the Cookies<br />
The size of cookies is limited to 4096 bytes.<br />
A total of 20 cookies can be used in a single website.</div>
<div>
<br />
37) What is the default timeout for a cookie?<br />
30 minutes.</div>
<div>
<br />
38) How would you turn off cookies on a page of a website?<br />
You have to follow the procedures given below: <br />
Use the "Cookie.Discard" property. <br />
It gets or sets the discard flag set by the server. <br />
When set to true, this property instructs the client application not to save the Cookie on the hard disk of the user at the end of the session. </div>
<div>
<br />
39) Which protocol is used to call web service?<br />
HTTP protocol.</div>
<div>
<br />
40) What is the file extension of web service?<br />
The File extension of web service is .asmx.</div>
<div>
<br /></div>
<div>
41) What are the HTML server controls in ASP.NET? <br />
HTML server controls are just like HTML elements that we use on the HTML pages. </div>
<div>
HTML server controls are used to expose properties and events for use. <br />
To make these controls programmatically accessible, we specify that the HTML controls act as a server control by adding the runat="server" attribute. </div>
<div>
<br />
42) What is the use of Global.asax file?<br />
The Global.asax file is used to execute the application-level events and sets application-level variables.</div>
<div>
<br />
43) What is event bubbling?<br />
When child control sends events to parent, it is termed as event bubbling. Server controls like Data Grid, Data List, and Repeater can have other child controls inside them.<br />
<br />
44) What are the different validators in ASP.NET?<br />
ASP.NET validation controls define an important role in validating the user input data. Whenever the user gives input, it must always be validated before sending it across the various layers of an application. There are two types of validation in ASP.NET:</div>
<div>
<br />
Client-Side Validation<br />
Server-Side Validation<br />
<br />
Client-Side Validation: When validation is done on the client browser, it is known as Client-Side Validation. You can use JavaScript to do the Client-Side Validation. </div>
<div>
<br /></div>
<div>
Server-Side Validation: When validation occurs on the server, then it is known as Server-Side Validation. Server-Side Validation is a secure form of validation. The main advantage of Server-Side Validation is if the user bypasses the Client-Side Validation, the problem can be caught on the server-side. </div>
<div>
<br /></div>
<div>
The following are the Validation Controls in ASP.NET:</div>
<div>
<br />
RequiredFieldValidator Control<br />
CompareValidator Control<br />
RangeValidator Control<br />
RegularExpressionValidator Control<br />
CustomFieldValidator Control<br />
ValidationSummary<br />
45) What is the difference between Server.Transfer and Response.Redirect?<br />
In Server.Transfer page processing transfers from one page to the other page without making a round-trip back to the client's browser. This provides a faster response with a little less overhead on the server. The clients url history list or current url Server does not update in case of Server.Transfer.<br />
<br />
Response.Redirect is used to redirect the user's browser to another page or site. It performs trip back to the client where the client's browser is redirected to the new page. The user's browser history list is updated to reflect the new address.<br />
<br />
46) In which event of page cycle is the ViewState available?<br />
After the Init() and before the Page_Load().<br />
<br />
47) What are the different Session state management options available in ASP.NET?<br />
In-Process<br />
Out-of-Process.<br />
<br />
In-Process stores the session in memory on the web server.<br />
<br />
Out-of-Process Session state management stores data in an external server. The external server may be either a SQL Server or a State Server. All objects stored in session are required to be serializable for Out-of-Process state management.<br />
<br />
48) How you can add an event handler?<br />
Using the Attributes property of server side control.<br />
<br />
btnSubmit.Attributes.Add("onMouseOver","JavascriptCode();")<br />
<br />
49) What are the different types of caching?ASP.NET has 3 kinds of caching :<br />
Output Caching,<br />
Fragment Caching,<br />
Data Caching.<br />
<br />
50) List the events in page life cycle.<br />
<b>1) Page_PreInit</b><br />
<b>2) Page_Init</b><br />
<b>3) Page_InitComplete</b><br />
<b>4) Page_PreLoad</b><br />
<b>5) Page_Load</b><br />
<b>6) Page_LoadComplete</b><br />
<b>7) Page_PreRender</b><br />
<b>8) Render</b><br />
<br />
<b>ASP.NET goes through a series of stages in the life cycle of each page.</b></div>
<div>
Page request. The user requests a page. ASP.NET decides whether to compile it or serve it from a cache.</div>
<div>
<br />
Page Start. The Request and Response objects are created.</div>
<div>
<br />
Page Initialization. All page controls are initialized, and any themes are applied.</div>
<div>
<br />
Page Load. ASP.NET uses the view state and control state properties to set the control properties. Default values are set in the controls.</div>
<div>
<br />
Postback event handling. This event is triggered if the same page is loaded again.</div>
<div>
<br />
Rendering. ASP.NET saves the view state for the page and writes the output of rendering to the output stream. It happens just before the complete web page is sent to the user.</div>
<div>
<br />
Unload. The rendered page gets sent to the client. ASP.NET unloads page properties and performs cleanup. All unwanted objects are removed from memory.<br />
<b><br />ASP.NET Page Life Cycle Events</b><br />
<br />
Following are the page life cycle events:<br />
<b><br /></b>
<b>PreInit</b><br />
PreInit is the first event in page life cycle. It checks the IsPostBack property and determines whether the page is a postback. It sets the themes and master pages, creates dynamic controls and gets and sets profile property values. This event can be handled by overloading the OnPreInit method or creating a Page_PreInit handler.<br />
<br />
<b>Init</b><br />
Init event initializes the control property and the control tree is built. This event can be handled by overloading the OnInit method or creating a Page_Init handler.<br />
<br />
<b>InitComplete</b><br />
InitComplete event allows tracking of view state. All the controls turn on view-state tracking.<br />
<br />
<b>LoadViewState</b><br />
LoadViewState event allows loading view state information into the controls.<br />
<br />
<b>LoadPostData</b><br />
During this phase, the contents of all the input fields defined with the <br />
<form>
tag are processed.<br />
<br />
<b>PreLoad</b><br />
PreLoad occurs before the post back data is loaded in the controls. This event can be handled by overloading the OnPreLoad method or creating a Page_PreLoad handler.<br />
<br />
<b>Load</b><br />
The Load event is raised for the page first and then recursively for all child controls. The controls in the control tree are created. This event can be handled by overloading the OnLoad method or creating a Page_Load handler.<br />
<br />
<b>LoadComplete</b><br />
The loading process is completed, control event handlers are run and page validation takes place. This event can be handled by overloading the OnLoadComplete method or creating a Page_LoadComplete handler.<br />
<br />
<b>PreRender</b><br />
The PreRender event occurs just before the output is rendered. By handling this event, pages and controls can perform any updates before the output is rendered.<br />
<br />
<b>PreRenderComplete</b><br />
as the PreRender event is recursively fired for all child controls, this event ensures the completion of the pre-rendering phase.<br />
<br />
<b>SaveStateComplete</b><br />
State of control on the page is saved. Personalization, control state and view state information is saved. The HTML markup is generated. This stage can be handled by overriding the Render method or creating a Page_Render handler.<br />
<br />
<b>UnLoad</b><br />
The UnLoad phase is the last phase of the page life cycle. It raises the UnLoad event for all controls recursively and lastly for the page itself. Final cleanup is done and all resources and references, such as database connections, are freed. This event can be handled by modifying the OnUnLoad method or creating a Page_UnLoad handler.</form>
<br />
51) What is <b>Cross Page Posting</b>?<br />
When we click submit button on a web page, the page post the data to the same page. The technique in which we post the data to different pages is called Cross Page posting. This can be achieved by setting POSTBACKURL property of the button that causes the postback. Findcontrol method of PreviousPage can be used to get the posted values on the page to which the page has been posted.<br />
<br />
ASP.NET 1.1 provides for web forms posting back only to themselves. In many situations, the solution requires posting to a different web page. The traditional workaround alternatives were to use Response.Redirect and/or Server.Transfer to move to a different page and simulate cross page post-back behavior.<br />
<br />
ASP.NET 2.0 provides a feature known as Cross Page PostBack for a web form to post-back to a different web form (other than itself)<br />
<br />
<b>How to post to a different page</b><br />
<br />
To set a web form to post back to a different web form, in the source web form, set the PostBackURL property of a control that implements IButtonControl (eg. Button, ImageButton, LinkButton) to the target web form. When the user clicks on this button control, the web form is cross-posted to the target web form. No other settings or code is required in the source web form.<br />
<br />
<b>Access source page info within the posted page:</b> FindControl Method<br />
<br />
The target web form resulting from the cross-page postback provides a non-null PreviousPage property. This property represents the source page and provides reference to the source web form and its controls.<br />
<br />
The controls on the source page can be accessed via the FindControl method on the object returned by the PreviousPage property of the target page. <br />
<b><br />protected void Page_Load(object sender, EventArgs e) <br />{ <br /> ... <br /> TextBox txtStartDate = (TextBox) PreviousPage.FindControl("txtStartDate "); <br /> ... <br />} </b></div>
<div>
<br />
At this point the target page does not have any knowledge of the source page. The PreviousPage property is of the type Page. For accessing controls using FindControl, the developer has to presume a certain structure in the source web form. This approach using FindControl has a few limitations. FindControl is dependent on the developer to provide the ids of the controls to access. The code will stop working if the control id is changed in the source web form. The FindControl method can retrieve controls only within the current container. If you need to access a control within another control, you need to first get a reference to the parent control.<br />
<br />
<b>Access source page info within the posted page: </b>@PreviousPageType Directive<br />
There is another more direct option to get access to the source page controls if the source page is pre-determined. The @PreviousPageType directive can be used in the target page to strongly type the source page. The directive specifies the source page using either the VirtualPath attribute or the TypeName attribute. The PreviousPage property then returns a strongly typed reference to the source page. It allows access to the public properties of the source page.<br />
<br />
<b>SourcePage.aspx:</b><br />
<form runat="server">
... <br />
<asp:textbox id="txtFirstName" runat="server"> <br /> <asp:textbox id="txtLastName" runat="server"> <br /> <asp:button id="btnViewReport" postbackurl="~/targetpage.aspx" runat="server" text="View Report"> ... public string FirstName { get { return txtFirstName.Text; } } ... </asp:button></asp:textbox></asp:textbox></form>
</div>
<div>
<br />
<b>TargetPage.aspx</b></div>
<div>
<b><br /></b><%@ PreviousPageType VirtualPath="sourcepage.aspx" %> <br />
string strFirstName; <br />
strFirstName = PreviousPage.FirstName //Strongly Typed PreviousPage allows direct access to the public properties of the source page.</div>
<div>
<br /></div>
<div>
52) How can we apply Themes to an asp.net application?<br />
We can specify the theme in web.config file. Below is the code example to apply theme:<configuration>
<system .web="">
<pages theme="Windows7">
</pages></system>
</configuration></div>
<div>
<br /></div>
<div>
53) What is RedirectPermanent in ASP.Net?<br />
RedirectPermanent Performs a permanent redirection from the requested URL to the specified URL. Once the redirection is done, it also returns 301 Moved Permanently responses.</div>
<div>
<br /></div>
<div>
54) Explain the working of passport authentication.<br />
First of all it checks passport authentication cookie. If the cookie is not available then the application redirects the user to Passport Sign on page. Passport service authenticates the user details on sign on page and if valid then stores the authenticated cookie on client machine and then redirect the user to requested page<br />
<br />
55) What are the advantages of Passport authentication?<br />
All the websites can be accessed using single login credentials. So no need to remember login credentials for each web site.</div>
<div>
Users can maintain his/ her information in a single location.</div>
<div>
<br /></div>
56) What is boxing and unboxing?<br />
Boxing is assigning a value type to reference type variable.<br />
Unboxing is reverse of boxing ie. Assigning reference type variable to value type variable.<br />
<br />
57) What are the components of ADO.NET?<br />
The components of ADO.Net are Dataset, Data Reader, Data Adaptor, Command, connection.<br />
<br />
58) What is the difference between ExecuteScalar and ExecuteNonQuery?<br />
ExecuteScalar returns output value where as ExecuteNonQuery does not return any value but the number of rows affected by the query. ExecuteScalar used for fetching a single value and ExecuteNonQuery used to execute Insert and Update statements.<br />
<br />
59) Which data type does the RangeValidator control support?<br />
The data types supported by the RangeValidator control are Integer, Double, String, Currency, and Date.<br />
<br />
60) What is the difference between an HtmlInputCheckBox control and an HtmlInputRadioButton control?<br />
In HtmlInputCheckBoxcontrol, multiple item selection is possible whereas in HtmlInputRadioButton controls, we can select only single item from the group of items.<br />
<br />
61) Which namespaces are necessary to create a localized application?<br />
System.Globalization<br />
System.Resources<br />
<div>
<br /></div>
<div>
62) What is the appSettings Section in the web.config file?<br />
The appSettings block in web config file sets the user-defined values for the whole application.<br />
For example, in the following code snippet, the specified ConnectionString section is used throughout the project for database connection:<br />
<br />
<appsettings></appsettings><br />
<add key="ConnectionString" value="server=local; pwd=password; database=default"></add><br /></div>
<br />
63) List all templates of the Repeater control.<br />
ItemTemplate<br />
AlternatingltemTemplate<br />
SeparatorTemplate<br />
HeaderTemplate<br />
FooterTemplate<br />
<div>
<br /></div>
64) In which event are the controls fully loaded?<br />
Page load event.<br />
<br />
65) What is boxing and unboxing?<br />
Boxing is assigning a value type to reference type variable.<br />
Unboxing is reverse of boxing ie. Assigning reference type variable to value type variable.<br />
<br />
66) Differentiate strong typing and weak typing<br />
In strong typing, the data types of variable are checked at compile time. On the other hand, in case of weak typing the variable data types are checked at runtime. In case of strong typing, there is no chance of compilation error. Scripts use weak typing and hence issues arises at runtime.<br />
<br />
67) How we can force all the validation controls to run?<br />
The Page.Validate() method is used to force all the validation controls to run and to perform validation.<br />
<div>
<br /></div>
<div>
68) What is the difference between custom controls and user controls?<br />
Custom controls are basically compiled code, i.e., DLLs. These can be easily added to the toolbox, so it can be easily used across multiple projects using a drag-and-drop approach. These controls are comparatively hard to create. But User Controls (.ascx) are just like pages (.aspx). These are comparatively easy to create but tightly coupled with respect to User Interface and code.<br />
<br />
69) What are the types of Authentication in ASP.NET?<br />
There are three types of authentication available in ASP.NET:<br />
<b>Windows Authentication</b>: This authentication method uses built-in Windows security features to authenticate a user.<br />
<b>Forms Authentication</b>: Authenticates against a customized list of users or users in a database.<br />
<b>Passport Authentication</b>: Validates against Microsoft Passport service which is basically a centralized authentication service.</div>
<div>
<br />
70) What is ASP.NET AJAX?<br />
Ajax stands for Asynchronous JavaScript and XML; in other words Ajax is the combination of various technologies such as a JavaScript, CSS, XHTML, DOM, etc.<br />
<br />
AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes. This means that it is possible to update parts of a web page, without reloading the entire page.<br />
<br />
We can also define Ajax is a combination of client side technologies that provides asynchronous communication between the user interface and the web server so that partial page rendering occurs instead of complete page post back.<br />
<br />
Ajax is platform-independent; in other words AJAX is a cross-platform technology that can be used on any Operating System since it is based on XML & JavaScript. It also supports open source implementation of other technology. It partially renders the page to the server instead of complete page post back. We use AJAX for developing faster, better and more interactive web applications.<br />
<br />
AJAX uses a HTTP request between web server & browser.<br />
<br />
With AJAX, when a user clicks a button, you can use JavaScript and DHTML to immediately update the UI, and spawn an asynchronous request to the server to fetch results.<br />
<br />
When the response is generated, you can then use JavaScript and CSS to update your UI accordingly without refreshing the entire page. While this is happening, the form on the users screen doesn't flash, blink, disappear, or stall.<br />
<br />
The power of AJAX lies in its ability to communicate with the server asynchronously, using a XMLHttpRequest object without requiring a browser refresh.<br />
<br />
Ajax essentially puts JavaScript technology and the XMLHttpRequest object between your Web form and the server.<br />
<br /></div>
<div>
71) What is the REST architecture?<br />
REST (Representational State Transfer) is an architectural style for designing applications and it dictates to use HTTP for making calls for communications instead of complex mechanism like CORBA, RPC or SOAP. </div>
<div>
<br /></div>
<div>
There are few principles associated with REST architectural style:</div>
<div>
<br /></div>
<div>
Everything is a resource i.e. File, Images, Video, WebPage etc.<br />
Every Resource is identified by a Unique Identifier.<br />
Use simple and Uniform Interfaces.<br />
Everything is done via representation (sending requests from a client to server and receiving responses from server to client).<br />
Be Stateless- Every request should be an independent request.<br />
<br />
72) Which compiler is used in ASP.NET?<br />
Answer: Roslyn is the name of the compiler used by .NET Framework.<br />
<br />
73) What are the different Session state management options available in ASP.NET?<br />
State Management in ASP.NET?<br />
A new instance of the Web page class is created each time the page is posted to the server.<br />
<br />
In traditional Web programming, all information that is associated with the page, along with the controls on the page, would be lost with each roundtrip.<br />
<br />
The Microsoft ASP.NET framework includes several options to help you preserve data on both a per-page basis and an application-wide basis.<br />
<br />
These options can be broadly divided into the following two categories:<br />
Client-Side State Management Options<br />
Server-Side State Management Options<br />
<br />
Client-Side State Management<br />
Client-based options involve storing information either in the page or on the client computer.<br />
<br />
Some client-based state management options are:<br />
Hidden fields<br />
View state<br />
Cookies<br />
Query strings<br />
<br />
Server-Side State Management<br />
There are situations where you need to store the state information on the server side.<br />
Server-side state management enables you to manage application-related and session-related information on the server.<br />
<br />
ASP.NET provides the following options to manage state at the server side:<br />
Application state<br />
Session state<br />
<br />
<img src="https://www.c-sharpcorner.com/UploadFile/8ef97c/Asp-Net-interview-questions-and-answers/Images/State%20Management.jpg" /><br />
<br />
74) What are Web Services in ASP.NET?<br />
A Web Service is a software program that uses XML to exchange information with other software via common internet protocols. In a simple sense, Web Services are a way for interacting with objects over the Internet.<br />
<br />
A web service is:<br />
Language Independent.<br />
Protocol Independent.<br />
Platform Independent.<br />
It assumes a stateless service architecture.<br />
Scalable (e.g. multiplying two numbers together to an entire customer-relationship management system).<br />
Programmable (encapsulates a task).<br />
Based on XML (open, text-based standard).<br />
Self-describing (metadata for access and use).<br />
Discoverable (search and locate in registries)- ability of applications and developers to search for and locate desired Web services through registries. This is based on UDDI.<br />
<br />
Key Web Service Technologies:<br />
XML- Describes only data. So, any application that understands XML-regardless of the application's programming language or platform-has the ability to format XML in a variety of ways (well-formed or valid).<br />
<br />
SOAP- Provides a communication mechanism between services and applications.<br />
<br />
WSDL- Offers a uniform method of describing web services to other programs.<br />
<br />
UDDI- Enables the creation of searchable Web services registries.<br />
<br />
<img src="https://www.c-sharpcorner.com/UploadFile/8ef97c/Asp-Net-interview-questions-and-answers/Images/UDDI.jpg" /></div>
<div>
75) What is the App Domain Concept in ASP.NET?<br />
ASP.NET introduces the concept of an Application Domain which is shortly known as AppDomain. It can be considered as a Lightweight process which is both a container and boundary. The .NET runtime uses an AppDomain as a container for code and data, just like the operating system uses a process as a container for code and data. As the operating system uses a process to isolate misbehaving code, the .NET runtime uses an AppDomain to isolate code inside a secure boundary.<br />
<br />
The CLR can allow the multiple .NET applications to run in a single AppDomain. Mulitple Appdomains can exist in Win32 process.<br />
<br />
How to create AppDomain: AppDomains are created using the CreateDomain method. AppDomain instances are used to load and execute assemblies (Assembly). When an AppDomain is no longer in use, it can be unloaded.<br />
<br />
<ol class="dp-c" start="1" style="background-color: white; border: none; box-sizing: border-box; color: #5c5c5c; font-family: Consolas, "Courier New", Courier, mono, serif; font-size: 16px; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1px 45px !important; padding: 0px;">
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">public</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">class</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> MyAppDomain: MarshalByRefObject </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">{ </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">public</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">string</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> GetInfo() </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> { </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">return</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> AppDomain.CurrentDomain.FriendlyName; </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">} </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">public</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">class</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> MyApp </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">{ </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> <span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">public</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">static</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> </span><span class="keyword" style="background-color: inherit; border: none; box-sizing: border-box; color: #006699; font-weight: bold; margin: 0px; padding: 0px;">void</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> Main() </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> { </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> AppDomain apd = AppDomain.CreateDomain(<span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"Rajendrs Domain"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">); </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> MyAppDomain apdinfo = (MyAppDomain) apd.CreateInstanceAndUnwrap(Assembly.GetCallingAssembly() </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> .GetName() </span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> .Name, <span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"MyAppDomain"</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;">); </span></span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> Console.WriteLine(<span class="string" style="background-color: inherit; border: none; box-sizing: border-box; color: blue; margin: 0px; padding: 0px;">"Application Name = "</span><span style="background-color: inherit; border: none; box-sizing: border-box; margin: 0px; padding: 0px;"> + apdinfo.GetInfo()); </span></span></li>
<li class="alt" style="border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; color: inherit; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;"> } </span></li>
<li class="" style="background-color: #f8f8f8; border-bottom: none; border-image: initial; border-left: 3px solid rgb(108, 226, 108); border-right: none; border-top: none; box-sizing: border-box; line-height: 18px; list-style-image: initial; list-style-position: outside !important; list-style-type: decimal-leading-zero; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="background-color: inherit; border: none; box-sizing: border-box; color: black; margin: 0px; padding: 0px;">} </span></li>
</ol>
<br />
76) What is master page in ASP.NET?<br />
The extension of MasterPage is '.master'. MasterPage cannot be directly accessed from the client because it just acts as a template for the other Content Pages. In a MasterPage we can have content either inside ContentPlaceHolder or outside it. Only content inside the ContentPlaceHolder can be customized in the Content Page. We can have multiple masters in one web application.A MasterPage can have another MasterPage as Master to it. The MasterPageFile property of a webform can be set dynamically and it should be done either in or before the Page_PreInit event of the WebForm. Page.MasterPageFile = "MasterPage.master". The dynamically set Master Page must have the ContentPlaceHolder whose content has been customized in the WebForm.<br />
<br />
<b>Some points about Master Pages:</b><br />
1. The extension of MasterPage is '.master'.<br />
<br />
2. MasterPage cannot be directly accessed from the client because it just acts as a template for the other Content Pages.<br />
<br />
3. In a MasterPage we can have content either inside ContentPlaceHolder or outside it. Only content inside the ContentPlaceHolder can be customized in the Content Page.<br />
<br />
4. We can have multiple masters in one web application.</div>
<div>
5. A MasterPage can have another MasterPage as Master to it.<br />
<br />
6. The content page content can be placed only inside the content tag.<br />
<br />
7. Controls of MasterPage can be programmed in the MasterPage and content page but a content page control will never be programmed in MasterPage.<br />
<br />
8. A master page of one web application cannot be used in another web application.<br />
<br />
9. The MasterPageFile property of a webform can be set dynamically and it should be done either in or before the Page_PreInit event of the WebForm. Page.MasterPageFile = "MasterPage.master". The dynamically set Master Page must have the ContentPlaceHolder whose content has been customized in the WebForm.<br />
<br />
10. The order in which events are raised: Load (Page) a Load (Master) a LoadComplete (Page) i.e. if we want to overwrite something already done in Load event handler of Master then it should be coded in the LoadComplete event of the page.<br />
<br />
11. Page_Load is the name of method for event handler for Load event of Master. (it's not Master_Load)<br />
<b>A master page is defined using the following code:</b><br />
<br />
<%@ master language="C#" %><br />
<br />
Adding a MasterPage to the Project<br />
<br />
1 Add a new MasterPage file (MainMaster.master) to the Web Application.<br />
<br />
2 Change the Id of ContentPlaceHolder in <head> to "cphHead" and the Id "ContentPlaceHolder1" to "cphFirst".</head><br />
<br />
3 Add one more ContentPlaceHolder (cphSecond) to Master page.<br />
<br />
4 To the master page add some header, footer and some default content for both the content place holders.<br />
<br />
<span style="font-family: "consolas" , "courier new" , "courier" , "mono" , serif;"></span><br />
<form id="form1" runat="server">
<span style="font-family: "consolas" , "courier new" , "courier" , "mono" , serif;">Header... </span></form>
<span style="font-family: "consolas" , "courier new" , "courier" , "mono" , serif;">
</span>
<span style="font-family: "consolas" , "courier new" , "courier" , "mono" , serif;"> <br /> </span><br />
<span style="font-family: "consolas" , "courier new" , "courier" , "mono" , serif;"> <asp:contentplaceholder id="cphFirst" runat="server"> This is First Content Place Holder (Default) </asp:contentplaceholder></span></div>
<span style="font-family: "consolas" , "courier new" , "courier" , "mono" , serif;"> <br /> </span><br />
<span style="font-family: "consolas" , "courier new" , "courier" , "mono" , serif;"> <asp:contentplaceholder id="cphSecond" runat="server"></asp:contentplaceholder></span><span style="font-family: "consolas" , "courier new" , "courier" , "mono" , serif; font-size: 16px;"> </span><br />
This is Second Content Place Holder (Default).<br />
<div>
<div>
<br /></div>
<div>
</div>
</div>
<br />
<div>
<br />
Footer... </div>
<div>
</div>
<div>
<br /></div>
<div>
77) What are the data controls available in ASP.NET?<br />
The Controls having DataSource Property are called Data Controls in ASP.NET. ASP.NET allows powerful feature of data binding, you can bind any server control to simple properties, collections, expressions and/or methods. When you use data binding, you have more flexibility when you use data from a database or other means.<br />
<br />
Data Bind controls are container controls.<br />
<br />
Controls -> Child Control<br />
<br />
Data Binding is binding controls to data from databases. With data binding we can bind a control to a particular column in a table from the database or we can bind the whole table to the data grid.<br />
<br />
Data binding provides simple, convenient, and powerful way to create a read/write link between the controls on a form and the data in their application.<br />
<br />
Data binding allows you to take the results of properties, collection, method calls, and database queries and integrate them with your ASP.NET code. You can combine data binding with Web control rendering to relieve much of the programming burden surrounding Web control creation. You can also use data binding with ADO.NET and Web controls to populate control contents from SQL select statements or stored procedures.<br />
<br />
Data binding uses a special syntax:<br />
<br />
<%# %><br />
<br />
The <%#, which instructs ASP.NET to evaluate the expression. The difference between a data binding tags and a regular code insertion tags <% and %> becomes apparent when the expression is evaluated. Expressions within the data binding tags are evaluated only when the DataBind method in the Page objects or Web control is called.<br />
<br />
Data Bind Control can display data in connected and disconnected model.<br />
<br />
Following are data bind controls in ASP.NET: <br />
Repeater Control<br />
DataGrid Control<br />
DataList Control<br />
GridView Control<br />
DetailsView<br />
FormView<br />
DropDownList<br />
ListBox<br />
RadioButtonList<br />
CheckBoxList<br />
BulletList etc.</div>
<div>
<br /></div>
<div>
78) What is page directives in ASP.NET?<br />
Basically Page Directives are commands. These commands are used by the compiler when the page is compiled.<br />
<br />
How to use the directives in an ASP.NET page<br />
<br />
It is not difficult to add a directive to an ASP.NET page. It is simple to add directives to an ASP.NET page. You can write directives in the following format:<br />
<br />
<%@[Directive][Attributes]%><br />
<br />
See the directive format, it starts with "<%@" and ends with "%>". The best way is to put the directive at the top of your page. But you can put a directive anywhere in a page. One more thing, you can put more than one attribute in a single directive.<br />
<br />
Here is the full list of directives:<br />
@Page<br />
@Master<br />
@Control<br />
@Import<br />
@Implements<br />
@Register<br />
@Assembly<br />
@MasterType<br />
@Output Cache<br />
@PreviousPageType<br />
@Reference<br />
<br />
79) What is HTTP Handler?<br />
Every request into an ASP.NET application is handled by a specialized component known as an HTTP handler. The HTTP handler is the most important ingredient while handling ASP.NET requests.<br />
<br />
<b>Examples:</b> ASP.NET uses different HTTP handlers to serve different file types. For example, the handler for web Page creates the page and control objects, runs your code, and renders the final HTML.<br />
<br />
<b>ASP.NET default handlers: </b><br />
1. Page Handler (.aspx) - Handles Web pages.<br />
2. User Control Handler (.ascx) - Handles Web user control pages.<br />
3. Web Service Handler (.asmx) - Handles Web service pages.<br />
4. Trace Handler (trace.axd) - Handles trace functionality.<br />
<br />
Why we need to create our own HTTP Handler: Sometime we need to avoid ASP.NET full page processing model, which saves lot of overheads, as ASP.NET web form model has to go through many steps such as creating web page objects, persisting view state etc. What we are interested into is to develop some low level interface that provides access to objects like Request and Response but doesn't use the full control based web form model discussed above.<br />
<br />
<b>Examples:</b><br />
1. Dynamic image creator - Use the System.Drawing classes to draw and size your own images.<br />
2. RSS - Create a handler that responds with RSS-formatted XML. This would allow you to add RSS feed capabilities to your sites.<br />
3. Render a custom image,<br />
4. Perform an ad hoc database query,<br />
4. Return some binary data.<br />
<br />
All HTTP handlers are defined in the <httphandlers> section of a configuration file which is nested in the <system .web=""> element.</system></httphandlers></div>
<br />
<httphandlers></httphandlers><br />
<add path="trace.axd" type="System.Web.Handlers.TraceHandler" validate="true" verb="*"> </add><br />
<add path="*.config" type="System.Web.HttpForbiddenHandler" validate="true" verb="*"> </add><br />
<add path="*.cs" type="System.Web.HttpForbiddenHandler" validate="true" verb="*"> </add><br />
<add path="*.aspx" type="System.Web.UI.PageHandlerFactory" validate="true" verb="*"> </add></div>
<br />
80) What are Differences between ASP.NET HttpHandler and HttpModule?<br />
The user requests for a resource on web server. The web server examines the file name extension of the requested file, and determines which ISAPI extension should handle the request. Then the request is passed to the appropriate ISAPI extension. For example when an .aspx page is requested it is passed to ASP.NET page handler. Then Application domain is created and after that different ASP.NET objects like Httpcontext, HttpRequest, HttpResponse are created. Then instance of HttpApplication is created and also instance of any configured modules. One can register different events of HttpApplication class like BeginRequest, AuthenticateRequest, AuthorizeRequest, ProcessRequest etc.<br />
<br />
<b>HTTP Handler</b><br />
HTTP Handler is the process which runs in response to a HTTP request. So whenever user requests a file it is processed by the handler based on the extension. So, custom http handlers are created when you need to special handling based on the file name extension. Let's consider an example to create RSS for a site. So, create a handler that generates RSS-formatted XML. Now bind the .rss extension to the custom handler.<br />
<br />
<b>HTTP Modules</b><br />
HTTP Modules are plugged into the life cycle of a request. So when a request is processed it is passed through all the modules in the pipeline of the request. So generally http modules are used for: <br />
<br />
<b>Security:</b> For authenticating a request before the request is handled.<br />
<b>Statistics and Logging:</b> Since modules are called for every request they can be used for gathering statistics and for logging information.<br />
<b>Custom header: </b>Since response can be modified, one can add custom header information to the response.<br />
<br />
81) Explain the AdRotator Control?<br />
<b>AdRotator control</b> are used to create a dynamic ads. The AdRotator Control presents ad images each time a user enters or refreshes a webpage. When the ads are clicked, it will navigate to a new Web location. The AdRotator control is used to display a sequence of ad images.The AdRotator control to work we need an Advertisement file (XML file) and some sample images.<br />
<br />
Adding the AdRotator web server control to your web application. first, select the AdRotator and drag and drop the control to your web form. Map the XML file which contains the details about each and every ad.<br />
<br />
The advertisement file is an XML file. The following are some of the elements of this XML file.<br />
1. <imageurl>: Optional. The path to the image file.<br />2. <navigateurl>: Optional. The URL to link to if the user clicks the ad.<br />3. <alternatetext>: Optional. An alternate text for the image.<br />4. <impressions>: Optional. The display rates in percent of the hits.<br /><br />XML code that has the details about the ads. The file Ads.xml looks like the code below:</impressions></alternatetext></navigateurl></imageurl><br />
<div>
<div>
<advertisements> </advertisements></div>
<div>
<ad> </ad></div>
<div>
<imageurl>adimages/2.jpg</imageurl> </div>
<div>
<navigateurl>http://cat2.com</navigateurl> </div>
<div>
<alternatetext>Cat 2</alternatetext> </div>
<div>
<impressions>30</impressions> </div>
<div>
</div>
</div>
<br />
<br />
<div>
<ad> </ad></div>
<div>
<imageurl>adimages/3.jpg</imageurl> </div>
<div>
<navigateurl>http://cat3.com</navigateurl> </div>
<div>
<alternatetext>Cat 3</alternatetext> </div>
<div>
<impressions>20</impressions> </div>
<div>
</div>
<br />
<div>
<ad> </ad></div>
<div>
<imageurl>adimages/4.jpg</imageurl> </div>
<div>
<navigateurl>http://cat4.com</navigateurl> </div>
<div>
<alternatetext>Cat 4</alternatetext> </div>
<div>
<impressions>10</impressions> </div>
<div>
</div>
<br />
<div>
</div>
<b>82) Explain GridView control in ASP.NET?</b><br />
The GridView control displays the values of a data source in a table. Each column represents a field, while each row represents a record. The GridView control supports the following features:<br />
<div>
Binding to data source controls, such as SqlDataSource.<br />
Built-in sort capabilities.<br />
Built-in update and delete capabilities.<br />
Built-in paging capabilities.<br />
Built-in row selection capabilities.<br />
Programmatic access to the GridView object model to dynamically set properties, handle events, and so on.<br />
Multiple key fields.<br />
Multiple data fields for the hyperlink columns.<br />
Customizable appearance through themes and styles.<br />
<br />
Creating a GridView<br />
<asp:gridview id="gridService" runat="server"> </asp:gridview> </div>
<div>
<br /></div>
<div>
<b>83) Explain Cookie-less Session in ASP.NET?</b><br />
By default a session uses a cookie in the background. To enable a cookie-less session, we need to change some configuration in the Web.Config file. <b>Follow these steps:</b><br />
1. Open Web.Config file.<br />
2. Add a <sessionstate> tag under <system .web=""> tag.<br />3. Add an attribute "cookieless" in the <sessionstate> tag and set its value to "AutoDetect" like below:</sessionstate></system></sessionstate><br />
<br />
<sessionstate cookieless="AutoDetect" regenerateexpiredsessionid="true"><b>The possible values for "cookieless" attribute are:</b><br />AutoDetect: Session uses background cookie if cookies are enabled. If cookies are disabled, then the URL is used to store session information.<br /><br /><b>UseCookie: </b>Session always use background cookie. This is default.<br /><br /><b>UseDeviceProfile:</b> Session uses background cookie if browser supports cookies else URL is used.<br /><br /><b>UseUri:</b> Session always use URL.<br /><br /><b>"regenerateExpiredSessionId"</b> is used to ensure that if a cookieless url is expired a new new url is created with a new session. And if the same cookieless url is being used by multiple users an the same time, they all get a new regenerated session url.</sessionstate><br />
<br />
<b>84) What is Themes in ASP.NET?</b><br />
A theme decides the look and feel of the website. It is a collection of files that define the looks of a page. It can include skin files, CSS files & images.<br />
<br />
We define themes in a special App_Themes folder. Inside this folder is one or more subfolders named Theme1, Theme2 etc. that define the actual themes. The theme property is applied late in the page's life cycle, effectively overriding any customization you may have for individual controls on your page.<br />
<br />
<b>How to apply themes</b><br />
<br />
<b>There are 3 different options to apply themes to our website:</b><br />
1. Setting the theme at the page level: the Theme attribute is added to the page directive of the page.<br />
<br />
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"Inherits="Default" Theme="Theme1"%> <br />
<br />
2. Setting the theme at the site level: to set the theme for the entire website you can set the theme in the web.config of the website. Open the web.config file and locate the <pages> element and add the theme attribute to it:<br /><br /><pages theme="Theme1"> <br /> .... <br /> .... </pages> </pages><br />
3. Setting the theme programmatically at runtime: here the theme is set at runtime through coding. It should be applied earlier in the page's life cycle ie. Page_PreInit event should be handled for setting the theme. The better option is to apply this to the Base page class of the site as every page in the site inherits from this class.<br />
<br />
Page.Theme = Theme1; <br />
<br />
<b>Uses of Themes</b><br />
<br />
1. Since themes can contain CSS files, images and skins, you can change colors, fonts, positioning and images simply by applying the desired themes.<br />
<br />
2. You can have as many themes as you want and you can switch between them by setting a single attribute in the web.config file or an individual aspx page. Also you can switch between themes programmatically.<br />
<br />
3. Setting the themes programmatically, you are offering your users a quick and easy way to change the page to their likings.<br />
<br />
4. Themes allow you to improve the usability of your site by giving users with vision problems the option to select a high contrast theme with a large font size.<br />
<br />
<b>85) What is WebParts in ASP.NET?</b><br />
ASP.NET 2.0 incorporates the concept of WEB PARTS in itself and we can code and explore that as easily as we have done with the other controls in the previous sessions.<br />
<br />
We can compose web parts pages from "web parts", which can be web controls, user controls.<br />
<br />
<b>Component of Web Parts: </b>The web parts consist of different components like: <br />
Web Part Manager<br />
Web Part Zone<br />
CatLog Part<br />
CatLog Zone<br />
Connections Zone<br />
Editor Part<br />
Editor Zone<br />
<br />
<b>Web Part Zone</b><br />
Web Part Zone can contain one or more Web Part controls.<br />
This provides the layout for the Controls it contains. A single ASPX page can contain one or more Web Part Zones.<br />
<br />
A Web Part Control can be any of the controls in the toolbox or even the customized user controls.<br />
<br />
<b>86) How can we improve the Performance of an ASP.NET Web Page?</b><br />
This is the most common question from ASP.NET forum to any interview. In this post I’m going to point out some of the important points that may help to improve the performance.<br />
<br />
Here I used the word “improve performance” in the sense to decrease the loading time of the page. There are various reasons behind. Some of them we look into from the “backend side” (Database side) and rest of them we need to take care in “front-end” ((UI) side.<br />
<br />
For illustrative purpose, you have an ASP.NET Web site, one of the aspx page take much time to load. Throughout this article, we are going to see how to decrease the loading time.<br />
<br />
<b>Back End (DB)</b><br />
<br />
1. Try to check the Query performance that is how much time the query will take to execute and pull the records from DB. Then use SQL Server Profiler and Execution plan for that query so that you can come to a conclusion in which part it took much time.<br />
<br />
2, Check in every table (who are all part of the query) Index is created properly.<br />
<br />
3. If your query involves a complex stored procedure, which in turn use lot of joins, then you should focus on every table. In some cases, sub-query perform better than the joins.<br />
<br />
4. If your web page involves paging concepts, try to move the paging concepts to SQL Server. I meant that based on the page count the SP will return the records, instead of bringing the records as a whole.<br />
<br />
<b>87) What are the major events in global.aspx?</b><br />
The Global.asax file, which is derived from the HttpApplication class, maintains a pool of HttpApplication objects, and assigns them to applications as needed. The Global.asax file contains the following events:<br />
Application_Init<br />
Application_Disposed<br />
Application_Error<br />
Application_Start<br />
Application_End<br />
Application_BeginReques</div>
<div>
<br /></div>
<div>
<b>88) What is the authentication and authorization in ASP.NET?</b><br />
<b>Authentication:</b> Prove genuineness<br />
<b>Authorization:</b> process of granting approval or permission on resources.<br />
<br />
In ASP.NET <b>Authentication </b>means to identify the user or in other words its nothing but to validate that he exists in your database and he is the proper user.<br />
<br />
<b>Authorization</b> means does he have access to a particular resource on the IIS website. A resource can be an ASP.NET web page, media files (MP4, GIF, JPEG etc), compressed file (ZIP, RAR) etc.<br />
<br />
<img src="https://www.c-sharpcorner.com/UploadFile/8ef97c/Asp-Net-interview-questions-and-answers/Images/authentication%20and%20authorization.jpg" /><br />
<br />
<b>Types of authentication and authorization in ASP.NET</b><br />
<br />
There are three ways of doing authentication and authorization in ASP.NET:<br />
<b>Windows authentication:</b> In this methodology ASP.NET web pages will use local windows users and groups to authenticate and authorize resources.<br />
<br />
<b>Forms Authentication:</b> This is a cookie based authentication where username and password are stored on client machines as cookie files or they are sent through URL for every request. Form-based authentication presents the user with an HTML-based Web page that prompts the user for credentials.<br />
<br />
<b>Passport authentication:</b> Passport authentication is based on the passport website provided by the Microsoft .So when user logins with credentials it will be reached to the passport website ( i.e. hotmail,devhood,windows live etc) where authentication will happen. If Authentication is successful it will return a token to your website.<br />
<br />
<b>Anonymous access:</b> If you do not want any kind of authentication then you will go for Anonymous access.<br />
<br />
In 'web.config' file set the authentication mode to 'Windows' as shown in the below code snippets.<br />
<br />
<authentication mode="Windows"> We also need to ensure that all users are denied except authorized users. The below code snippet inside the authorization tag that all users are denied. '?' indicates any unknown user.</authentication><br />
<authorization> <br /> <deny users="?"> </deny></authorization><br />
<br />
<b>89) Describe application state management in ASP.NET?</b><br />
Application Level State Management is used to maintain the state of all the users accessing the web forms present within the website.<br />
<br />
The value assigned for an application is considered as an object.<br />
<br />
Application object will not have any default expiration period.<br />
<br />
Whenever the webserver has been restarted or stopped then the information maintained by the application object will be lost.<br />
<br />
If any data is stored on the application object then that information will be shared upon all the users accessing the webserver.<br />
<br />
Since the information is shared among all the users, it is advisable to lock and unlock the application object as per requirement.<br />
<br />
<b>Global Application Class(Global.asax):</b><br />
<br />
It is a Class which consists of event handlers which executes the code implicitly whenever a relevant task has been performed on the web server.Design: <br />
<br />
<%@ Application Language="C#" %> <br />
<script runat="server"> <br> void Application_Start(object sender, EventArgs e) <br> { <br> // Code that runs on application startup <br> } <br> void Application_End(object sender, EventArgs e) <br> { <br> // Code that runs on application shutdown <br> } <br> void Application_Error(object sender, EventArgs e) <br> { <br> // Code that runs when an unhandled error occurs <br> } <br> void Session_Start(object sender, EventArgs e) <br> { <br> // Code that runs when a new session is started <br> } <br> void Session_End(object sender, EventArgs e) <br> { <br> // Code that runs when a session ends. <br> } <br> </script><br />
<br />
<b>90) Enterprise Library in ASP.NET?</b><br />
<b>Enterprise Library:</b> It is a collection of application blocks and core infrastructure. Enterprise library is the reusable software component designed for assisting the software developers.<br />
<br />
We use the Enterprise Library when we want to build application blocks intended for the use of developers who create complex enterprise level application.<br />
<br />
Enterprise Library Application Blocks<br />
<b><br /></b>
<b>Security Application Block</b><br />
Security Application Block provide developers to incorporate security functionality in the application. This application can use various blocks such as authenticating and authorizing users against the database.<br />
<br />
<b>Exception Handling Application Block</b><br />
This block provides the developers to create consistency for processing the error that occur throughout the layers of Enterprise Application.<br />
<br />
<b>Cryptography Application Block</b><br />
Cryptography application blocks provides developers to add encryption and hashing functionality in the applications.<br />
<br />
<b>Caching Application Block</b><br />
Caching Application Block allows developers to incorporate local cache in the applications.<br />
<br />
91) </div>
</div>
योगेश चन्द्र उप्रेतीhttp://www.blogger.com/profile/17009545633002694199noreply@blogger.com0tag:blogger.com,1999:blog-8545307170893600943.post-3915968761891582542019-10-26T00:02:00.002-07:002019-10-26T00:12:19.704-07:00OOPS Concepts - Part 1<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-size: x-large;"><u><b>OOPS Concepts – OOPS Concepts Example:</b></u></span><br />
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>What is <span style="font-size: x-large;">OOPS?</span></b> To implement the real world things or
we can say real world objects in to programming world we required programming
techniques that is called Object Oriented Programming System. OOPS is used to
write programs based on the real world objects. <o:p></o:p></div>
<div class="MsoNormal">
The states and behaviors of an objects are represented as
the member variables and methods. In oops programming programs are organized around
objects and data rather than actions and logic.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>What are the advantages of OOPS concepts?</b> Major
advantages of OOPS programming are;<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<ol start="1" style="margin-top: 0in;" type="1">
<li class="MsoNormal" style="mso-list: l2 level1 lfo1; tab-stops: list .5in;"><b>Simplicity</b>:
OOPS programming objects model real world objects, so the complexity is
reduced and the program structure is clear.<o:p></o:p></li>
<li class="MsoNormal" style="mso-list: l2 level1 lfo1; tab-stops: list .5in;"><b>Modularity</b>:
Each object forms a separate entity whose internal workings are decoupled
from other parts of the system.<o:p></o:p></li>
<li class="MsoNormal" style="mso-list: l2 level1 lfo1; tab-stops: list .5in;"><b>Modifiability</b>:
It is easy to make minor changes in the data representation or the
procedures in an OO program. Changes inside a class do not affect any
other part of a program, since the only public interface that the external
world has to a class is through the use of methods.<o:p></o:p></li>
<li class="MsoNormal" style="mso-list: l2 level1 lfo1; tab-stops: list .5in;"><b>Extensibility</b>:
Adding new features or responding to changing operating environments can
be solved by introducing a few new objects and modifying some existing
ones.<o:p></o:p></li>
<li class="MsoNormal" style="mso-list: l2 level1 lfo1; tab-stops: list .5in;"><b>Maintainability</b>:
Objects can be maintained separately, making locating and fixing problems
easier.<o:p></o:p></li>
<li class="MsoNormal" style="mso-list: l2 level1 lfo1; tab-stops: list .5in;"><b>Reusability</b>:
Objects can be reused in different programs.<o:p></o:p></li>
</ol>
<div class="MsoNormal">
<b>What is the difference between Procedural programming and
OOPS? </b>Procedural language is based on functions but object oriented
language is based on real world objects.<o:p></o:p></div>
<ol start="1" style="margin-top: 0in;" type="1">
<li class="MsoNormal" style="mso-list: l0 level1 lfo2; tab-stops: list .5in;">Procedural
language gives importance on the sequence of function execution but object
oriented language gives importance on states and behaviors of the objects.<o:p></o:p></li>
<li class="MsoNormal" style="mso-list: l0 level1 lfo2; tab-stops: list .5in;">Procedural
language exposes the data to the entire program but object oriented
language encapsulates the data.<o:p></o:p></li>
<li class="MsoNormal" style="mso-list: l0 level1 lfo2; tab-stops: list .5in;">Procedural
language follows top down programming paradigm but object oriented
language follows bottom up programming paradigm.<o:p></o:p></li>
<li class="MsoNormal" style="mso-list: l0 level1 lfo2; tab-stops: list .5in;">Procedural
language is complex in nature so it is difficult to modify, extend and
maintain but object oriented language is less complex in nature so it is
easier to modify, extend and maintain.<o:p></o:p></li>
<li class="MsoNormal" style="mso-list: l0 level1 lfo2; tab-stops: list .5in;">Procedural
language provides less scope of code reuse but object oriented language
provides more scope of code reuse.<o:p></o:p></li>
</ol>
<div class="MsoNormal">
<b>What are the core concepts of OOPS? </b>OOPS core
concepts are;<o:p></o:p></div>
<ol start="1" style="margin-top: 0in;" type="1">
<li class="MsoNormal" style="mso-list: l1 level1 lfo3; tab-stops: list .5in;">Abstraction<o:p></o:p></li>
<li class="MsoNormal" style="mso-list: l1 level1 lfo3; tab-stops: list .5in;">Encapsulation<o:p></o:p></li>
<li class="MsoNormal" style="mso-list: l1 level1 lfo3; tab-stops: list .5in;">Polymorphism<o:p></o:p></li>
<li class="MsoNormal" style="mso-list: l1 level1 lfo3; tab-stops: list .5in;">Inheritance<o:p></o:p></li>
<li class="MsoNormal" style="mso-list: l1 level1 lfo3; tab-stops: list .5in;">Composition<o:p></o:p></li>
<li class="MsoNormal" style="mso-list: l1 level1 lfo3; tab-stops: list .5in;">Association<o:p></o:p></li>
<li class="MsoNormal" style="mso-list: l1 level1 lfo3; tab-stops: list .5in;">Aggregation<o:p></o:p></li>
</ol>
<div class="MsoNormal">
<b>What is <span style="font-size: x-large;">Abstraction?</span></b> Abstraction is the concept of
hiding the internal details and describing things in simple terms. For example,
a method that adds two integers. The internal processing of the method is
hidden from the outer world. There are many ways to achieve abstraction in
object-oriented programing’s, such as encapsulation and inheritance.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Abstraction</b> main goal is to handle complexity by
hiding unnecessary details from the user. That enables the user to implement
more complex logic on top of the provided abstraction without understanding or
even thinking about all the hidden complexity. That’s a very generic concept
that’s not limited to object-oriented programming. You can find it everywhere
in the real world.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>For Example </b>Abstraction in the real world. I’m a
coffee addict. So, when I wake up in the morning, I go into my kitchen, switch
on the coffee machine and make coffee. Sounds familiar? Making coffee with a
coffee machine is a good example of abstraction.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
You need to know how to use your coffee machine to make
coffee. You need to provide water and coffee beans, switch it on and select the
kind of coffee you want to get.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
The thing you don’t need to know is how the coffee machine
is working internally to brew a fresh cup of delicious coffee. You don’t need
to know the ideal temperature of the water or the amount of ground coffee you
need to use.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Someone else worried about that and created a coffee machine
that now acts as an abstraction and hides all these details. You just interact
with a simple interface that doesn’t require any knowledge about the internal
implementation.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
You can use the same concept in object-oriented programming
languages<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>What is <span style="font-size: x-large;">Encapsulation?</span> </b>Encapsulation is the technique
used to implement abstraction in object-oriented programming. Encapsulation is
used for access restriction to class members and methods. Access modifier
keywords are used for encapsulation in object oriented programming. <o:p></o:p></div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b>For example </b>encapsulation is achieved using <b>private</b>,
<b>protected</b> and <b>public</b> keywords.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>What is <span style="font-size: x-large;">Polymorphism?</span> </b>Polymorphism is the concept
where an object behaves differently in different situations. There are two
types of polymorphism – compile time polymorphism and runtime polymorphism.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Compile-time polymorphism </b>is achieved by method <b>overloading</b>.
All the methods name is same and arguments are different. Here compiler will be
able to identify the method to invoke at compile-time, hence it’s called
compile-time polymorphism.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>Runtime polymorphism</b> is implemented when we have
“IS-A” relationship between objects. <b>This is also called a method overriding</b>
because subclass has to override the superclass method for runtime polymorphism.
If we are working in terms of the superclass, the actual implementation class
is decided at runtime. The compiler is not able to decide which class method
will be invoked. This decision is done at runtime, hence the name as runtime
polymorphism or dynamic method dispatch.<o:p></o:p></div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b>What is <span style="font-size: x-large;">Static Binding</span> and <span style="font-size: x-large;">Dynamic Binding?</span> </b>Static or
early binding is resolved at compile time. <b>Method overloading </b>is an
example of static binding. Dynamic or late or virtual binding is resolved at
run time. <b>Method overriding</b> is an example of dynamic binding.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>What is <span style="font-size: x-large;">Inheritance?</span> </b>Inheritance is the object-oriented
programming concept where an object is based on another object. Inheritance is
the <b>mechanism of</b> <b>code reuse</b>. The object that is getting inherited
is called <b>superclass</b> and the object that inherits the superclass is
called <b>subclass</b>.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>What is <span style="font-size: x-large;">Association?</span></b> Is the OOPS concept to <b>define
the relationship between objects</b>. The association defines the <b>multiplicity
between objects</b>. <b>Association</b> is a relationship between two objects
with multiplicity.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>For example</b> Teacher and Student objects. There is a <b>one-to-many
relationship</b> between a teacher and students. Similarly, a student can have
a one-to-many relationship with teacher objects. However, both student and
teacher objects are independent of each other. <o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>What is <span style="font-size: x-large;">Aggregation?</span> </b>Aggregation is a special type of
association. In aggregation, objects have their own life cycle but there is
ownership. Whenever we have “HAS-A” relationship between objects and ownership
then it’s a case of aggregation. <b>Aggregation</b> is also known as “HAS-A”
relationship. <o:p></o:p></div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b>For Example </b>When class Car has a member reference
variable of type Wheel then the relationship between the classes Car and Wheel
is known as Aggregation. Aggregation can be understood as “whole to its parts”
relationship. Car is the whole and Wheel is part. Wheel can exist without the
Car. <b>Aggregation is a weak association</b>.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>What is <span style="font-size: x-large;">Composition?</span> </b>The composition is a special
case of aggregation. The <b>composition</b> is a more restrictive form of
aggregation. When the contained object in “HAS-A” relationship can’t exist on
its own, then it’s a case of composition. Composition is a special form of Aggregation
where the part cannot exist without the whole. <b>Composition is a strong
Association.<o:p></o:p></b></div>
<div class="MsoNormal">
<b><br /></b></div>
<div class="MsoNormal">
<b>For example</b> House has-a Room. Here the room can’t
exist without the house and House cannot exists without room. Composition is
said to be better than inheritance.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>What is the <span style="font-size: x-large;">Diamond problem in inheritance? </span></b>In case
of multiple inheritance, suppose class A has two subclasses B and C, and a
class D has two super classes B and C. If a method present in A is overridden
by both B and C but not by D then from which class D will inherit that method B
or C? </div>
<div class="MsoNormal">
This problem is known as diamond problem.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>What is <span style="font-size: x-large;">multiple inheritance?</span> </b>A child class
inheriting states and behaviors from multiple parent classes is known as
multiple inheritance.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>What is <span style="font-size: x-large;">Dependency?</span> </b>When one class depends on another
because it uses that at some point in time then this relationship is known as
Dependency. One class depends on another <b>if the independent class is a
parameter variable or local variable of a method of the dependent class</b>. A
Dependency is drawn as a dotted line from the dependent class to the
independent class with an open arrowhead pointing to the independent class.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>What is the difference between <span style="font-size: x-large;">Association and
Dependency?</span> </b>The main difference between Association and Dependency is in
case of Association one class has an attribute or member variable of the other
class type but in case of Dependency a method takes an argument of the other
class type or a method has a local variable of the other class type.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>What is a <span style="font-size: x-large;">Class?</span> </b>A class is the specification or
template of an object. First of all, a class is a group of related methods and
variables. Everything in C# is built upon classes.<o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>What is an <span style="font-size: x-large;">Object?</span> Object</b> is instance of class. In
other words, an instance of a class is an object defined by that particular
class. Creating a new instance, or an object, is called <b>instantiation</b>. In
memory, you can create an object using the “new” keyword. Objects are reference
types. Objects are stored in the heap. <b>An object is a section of memory</b>
that has been configured according to the class blueprint. <o:p></o:p></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b>What is <span style="font-size: x-large;">Object attributes?</span> </b>Object attributes is the
data bundled in an instance of a class. The object attributes are called
instance variables or member fields. An instance variable is a variable defined
in a class, for which each object in the class has a separate copy.</div>
</div>
योगेश चन्द्र उप्रेतीhttp://www.blogger.com/profile/17009545633002694199noreply@blogger.com0tag:blogger.com,1999:blog-8545307170893600943.post-68606641306509037422019-09-29T03:45:00.000-07:002019-09-29T04:07:32.218-07:00GIT Commands<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-size: x-large;">Important GIT Commands:</span><br />
<br />
npm config --global set proxy http://yogesh.upreti@gmail.com@23.90.64.10:8080<br />
npm config --global set proxy http://yogesh.upreti@gmail.com:10080<br />
<br />git config --global http.proxy http://yogesh.upreti@gmail.com@proxy.domain.com:10080<br />git config --global http.proxy http://yogesh.upreti@gmail.com@23.90.64.10:8080<div>
<br /> http://dev.azure.com/YOUR_PROJECT_PATH<br />
<b><span style="font-size: large;">Step 1</span>:</b> Pull the updated data before commit<br />
<span style="font-size: x-large;">git pull</span><br />
<br />
<span style="font-size: large;"><b>Step 2:</b> </span>Add the changes made by you<br />
<span style="font-size: x-large;">git add .</span><br />
<br />
<span style="font-size: large;"><b>Step 3: </b></span>Commit the changes with the detailed comment whatever you have done if single file then use <span style="font-size: x-large;">"-m"</span><br />
while if you have made changes in multiple files then use<span style="font-size: x-large;"> -F</span><br />
<span style="font-size: x-large;">git commit -m "Changes comment"</span><br />
<br />
For multiple comment first create a text file where you want<br />
<span style="font-size: x-large;">git commit -F "Changes comment" </span><br />
<br />
To log your commit changes comments. Like this given below<br />
<span style="font-size: x-large;">git commit -F "../CommitDetails.txt"</span><br />
<br />
Step 4: Finally push the changes that are made by you using:<br />
<span style="font-size: x-large;">git push</span><br />
<br />
Check Status: Like 'Your branch is up to date with 'origin/master'.'<br />
<span style="font-size: x-large;">git status </span><br />
<br />
Check log: Checking log of the commit changes<br />
<span style="font-size: x-large;">git log</span><br />
<br />
<span style="font-size: x-large;">Changing the git remote origin url:</span><br />
<br />
D:\Yogesh\Project\MY_PROJECT_PATH>git remote set-url origin https://dev.azure.com/YOUR_PROJECT_PATH/_git/YOUR_PROJECT_PATH.Web<br />
<span style="font-size: x-large;">Check the Remote Origin URL</span><br />
<span style="font-size: large;">git remote get-url --all origin</span><br />
<br />
D:\Yogesh\Project\MY_PROJECT_PATH> git remote set-url origin https://dev.azure.com/MY_PROJECT_PATH_API/_git/MY_PROJECT_PATH_API.API<br />
<br />
<br />
D:\Yogesh\Project\MY_PROJECT_PATH> git remote get-url origin https://dev.azure.com/MY_PROJECT_PATH_API/_git/MY_PROJECT_PATH_API.API<br />
<span style="font-size: x-large;">git push </span><br />
<span style="font-size: x-large;"><br /></span>
<span style="font-size: x-large;">git stash</span><br />
<span style="font-size: x-large;"><br /></span>
<span style="font-size: x-large;">git reset --hard</span><br />
<br />
<span style="font-size: x-large;">Git Clone: Creating clone of project.</span><br />
git clone -b DEV https://dev.azure.com/MY_PROJECT_PATH_API/_git/MY_PROJECT_PATH_API.API</div>
</div>
योगेश चन्द्र उप्रेतीhttp://www.blogger.com/profile/17009545633002694199noreply@blogger.com0tag:blogger.com,1999:blog-8545307170893600943.post-21124527495892599222019-09-29T01:18:00.001-07:002019-09-29T01:18:06.656-07:00Using SQL Server Logging Exception in SQL table<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-size: x-large;">Writing l</span><span style="font-size: x-large;">ogs in </span><span style="font-size: x-large;">SQL Server:</span><br />
<br />
<span style="font-size: large;">First create a table to log the system exceptions occurred:</span><br />
<br />
CREATE TABLE [dbo].[tblExceptionLog]( <br />
<span style="white-space: pre;"> </span>[Id] [int] IDENTITY(1, 1) NOT NULL, <br />
<span style="white-space: pre;"> </span>[ErrorLine] [int] NULL, <br />
<span style="white-space: pre;"> </span>[ErrorMessage] [nvarchar](4000) NULL, <br />
<span style="white-space: pre;"> </span>[ErrorNumber] [int] NULL, <br />
<span style="white-space: pre;"> </span>[ErrorProcedure] [nvarchar](128) NULL, <br />
<span style="white-space: pre;"> </span>[ErrorSeverity] [int] NULL, <br />
<span style="white-space: pre;"> </span>[ErrorState] [int] NULL, <br />
<span style="white-space: pre;"> </span>[DateErrorRaised] [datetime] NULL <br />
) <br />
<br />
<span style="font-size: large;">Create a stored procedure: This procedure will insert the exception log in case of error will come</span><br />
<br />
Create PROC [dbo].[uspGetErrorInfo]<br />
AS<br />
BEGIN<br />
<span style="white-space: pre;"> </span>INSERT INTO tblExceptionLog( <br />
<span style="white-space: pre;"> </span>ErrorLine,<br />
<span style="white-space: pre;"> </span>ErrorMessage,<br />
<span style="white-space: pre;"> </span>ErrorNumber, <br />
<span style="white-space: pre;"> </span>ErrorProcedure,<br />
<span style="white-space: pre;"> </span>ErrorSeverity,<br />
<span style="white-space: pre;"> </span>ErrorState, <br />
<span style="white-space: pre;"> </span>DateErrorRaised <br />
<span style="white-space: pre;"> </span>) <br />
<span style="white-space: pre;"> </span>SELECT <br />
<span style="white-space: pre;"> </span>ERROR_LINE () AS ErrorLine, <br />
<span style="white-space: pre;"> </span>Error_Message() AS ErrorMessage, <br />
<span style="white-space: pre;"> </span>Error_Number() AS ErrorNumber, <br />
<span style="white-space: pre;"> </span>Error_Procedure() AS 'Proc', <br />
<span style="white-space: pre;"> </span>Error_Severity() AS ErrorSeverity, <br />
<span style="white-space: pre;"> </span>Error_State() AS ErrorState, <br />
<span style="white-space: pre;"> </span>GETDATE () AS DateErrorRaised<br />
END <br />
GO<br />
<br />
<span style="font-size: large;">Use the following approach to log the exception:</span><br />
<br />
BEGIN TRY<span style="white-space: pre;"> </span><br />
BEGIN TRAN<br />
--SQL Statements<br />
COMMIT TRAN<br />
END TRY<br />
BEGIN CATCH<br />
<span style="white-space: pre;"> </span>--Print 'Catching Exceptio Here'<br />
<span style="white-space: pre;"> </span>IF @@TRANCOUNT > 0<br />
<span style="white-space: pre;"> </span>ROLLBACK TRAN<span style="white-space: pre;"> </span><br />
<br />
<span style="white-space: pre;"> </span><b>EXEC dbo.uspGetErrorInfo</b> <span style="white-space: pre;"> </span><br />
END CATCH</div>
योगेश चन्द्र उप्रेतीhttp://www.blogger.com/profile/17009545633002694199noreply@blogger.com0tag:blogger.com,1999:blog-8545307170893600943.post-65226044847129534632019-09-29T00:58:00.000-07:002019-09-29T00:58:11.110-07:00Using SQL Server list all the tables that are not in used since a long time Unused tables list<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-size: x-large;">Create CTE for the unused tables: </span><br />
<br />
<span style="font-size: large;">Which are the tables from the sys.all_objects and not in the sys.dm_db_index_usage_stats table.</span><br />
<br />
WITH UnUsedTables (TableName , TotalRowCount, CreatedDate , LastModifiedDate )<br />
AS (<br />
SELECT DBTable.name AS TableName<br />
,PS.row_count AS TotalRowCount<br />
,DBTable.create_date AS CreatedDate<br />
,DBTable.modify_date AS LastModifiedDate<br />
FROM sys.all_objects DBTable<br />
JOIN sys.dm_db_partition_stats PS ON OBJECT_NAME(PS.object_id)=DBTable.name<br />
WHERE DBTable.type ='U'<br />
AND NOT EXISTS (SELECT OBJECT_ID <br />
FROM sys.dm_db_index_usage_stats<br />
WHERE OBJECT_ID = DBTable.object_id )<br />
)<br />
-- Select data from the CTE<br />
SELECT TableName , TotalRowCount, CreatedDate , LastModifiedDate<br />
FROM UnUsedTables<br />
ORDER BY TotalRowCount ASC</div>
योगेश चन्द्र उप्रेतीhttp://www.blogger.com/profile/17009545633002694199noreply@blogger.com0tag:blogger.com,1999:blog-8545307170893600943.post-57151617245980598392019-09-29T00:54:00.001-07:002019-09-29T00:54:34.686-07:00Using SQL Server Temporarily disabled all check constraints<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-size: large;">Temporarily disabled all check constraints:</span><br />
<span style="font-size: large;"><br /></span>
<span style="font-size: large;">Classification for disable:</span><br />
<br />
DECLARE @sql NVARCHAR(MAX) = N'';<br />
<br />
;WITH x AS<br />
(<br />
SELECT DISTINCT obj =<br />
QUOTENAME(OBJECT_SCHEMA_NAME(parent_object_id)) + '.'<br />
+ QUOTENAME(OBJECT_NAME(parent_object_id))<br />
FROM sys.foreign_keys<br />
)<br />
SELECT @sql += N'ALTER TABLE ' + obj + ' NOCHECK CONSTRAINT ALL;<br />
' FROM x;<br />
<br />
EXEC sp_executesql @sql;<br />
<br />
<span style="font-size: large;">For enabling again:</span><br />
<br />
DECLARE @sql NVARCHAR(MAX) = N'';<br />
<br />
;WITH x AS<br />
(<br />
SELECT DISTINCT obj =<br />
QUOTENAME(OBJECT_SCHEMA_NAME(parent_object_id)) + '.'<br />
+ QUOTENAME(OBJECT_NAME(parent_object_id))<br />
FROM sys.foreign_keys<br />
)<br />
SELECT @sql += N'ALTER TABLE ' + obj + ' WITH CHECK CHECK CONSTRAINT ALL;<br />
' FROM x;<br />
<br />
<br />
EXEC sp_executesql @sql;</div>
योगेश चन्द्र उप्रेतीhttp://www.blogger.com/profile/17009545633002694199noreply@blogger.com0tag:blogger.com,1999:blog-8545307170893600943.post-64180322415677898092019-09-29T00:41:00.000-07:002019-09-29T00:46:30.888-07:00Using SQL Server Validate Email ID OR Email Address <div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-size: x-large;">Different kind of input validation at SQL server level:</span><br />
<br />
<span style="font-size: large;">Check For Valid Email Id in SQL Server:</span><br />
<br />
Declare @Email NVARCHAR(MAX) = 'yogesh.upreti14@yahoo.co.in'<br />
IF @Email LIKE '%_@__%.__%' AND PATINDEX('%[^a-z,0-9,@,.,_,\-]%', @Email) = 0<br />
BEGIN<br />
----- Execute you Data manipulation operation here.<br />
END<br />
<br />
<br /></div>
योगेश चन्द्र उप्रेतीhttp://www.blogger.com/profile/17009545633002694199noreply@blogger.com0tag:blogger.com,1999:blog-8545307170893600943.post-16939840513015924642019-09-29T00:32:00.002-07:002019-09-29T00:32:24.671-07:00Using SQL Server Remove Duplicate From A Table<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-size: x-large;">Different strategies for removing duplicate records in SQL Server</span><br /><div>
<br /></div>
<span style="font-size: large;">Delete duplicate rows from a table example using temporary table:</span><br />
BEGIN TRAN<br />
<br />
--Pull distinct records in the temporary table<br />
<br />
SELECT DISTINCT * INTO #T_TEMP FROM TableName<br />
<br />
--Truncate the target table<br />
<br />
TRUNCATE TABLE TableName<br />
<br />
--Insert the distinct records from temporary table back to target table<br />
<br />
INSERT INTO TableName SELECT * FROM #T_TEMP<br />
<br />
--Drop the temporary table<br />
<br />
IF OBJECT_ID('tempdb..#T_TEMP') IS NOT NULL<br />
DROP TABLE #T_TEMP<br />
COMMIT TRAN<br />
<br />
GO<br />
<br />
SELECT * FROM TableName<br />
<br />
<span style="font-size: large;">Delete duplicate rows from a table example using CTE:</span><div>
<div>
<br /></div>
<div>
WITH cte AS (</div>
<div>
SELECT </div>
<div>
contact_id, </div>
<div>
first_name, </div>
<div>
last_name, </div>
<div>
email, </div>
<div>
ROW_NUMBER() OVER (</div>
<div>
PARTITION BY </div>
<div>
first_name, </div>
<div>
last_name, </div>
<div>
email</div>
<div>
ORDER BY </div>
<div>
first_name, </div>
<div>
last_name, </div>
<div>
email</div>
<div>
) row_num</div>
<div>
FROM </div>
<div>
sales.contacts</div>
<div>
)</div>
<div>
DELETE FROM cte</div>
<div>
WHERE row_num > 1;</div>
</div>
<div>
<br /></div>
<span style="font-size: large;">Delete duplicate rows from a table </span><span style="font-size: large;">Using correlated sub query:</span><div>
<br /></div>
<div>
<div>
--With correlated subquery</div>
<div>
<br /></div>
<div>
CREATE TABLE Employee</div>
<div>
( </div>
<div>
[ID] INT IDENTITY, </div>
<div>
[FirstName] Varchar(100), </div>
<div>
[LastName] Varchar(100), </div>
<div>
[Address] Varchar(100), </div>
<div>
) </div>
<div>
GO </div>
<div>
INSERT INTO Employee([FirstName], [LastName], [Address]) </div>
<div>
VALUES ('Linda', 'Mitchel', 'America') </div>
<div>
INSERT INTO Employee([FirstName], [LastName], [Address]) </div>
<div>
VALUES ('Linda', 'Mitchel', 'America') </div>
<div>
INSERT INTO Employee([FirstName], [LastName], [Address]) </div>
<div>
VALUES ('John', 'Albert', 'Australia') </div>
<div>
INSERT INTO Employee([FirstName], [LastName], [Address]) </div>
<div>
VALUES ('John', 'Albert', 'Australia') </div>
<div>
INSERT INTO Employee([FirstName], [LastName], [Address]) </div>
<div>
VALUES ('John', 'Albert', 'Australia') </div>
<div>
INSERT INTO Employee([FirstName], [LastName], [Address]) </div>
<div>
VALUES ('Arshad', 'Ali', 'India') </div>
<div>
INSERT INTO Employee([FirstName], [LastName], [Address]) </div>
<div>
VALUES ('Arshad', 'Ali', 'India') </div>
<div>
INSERT INTO Employee([FirstName], [LastName], [Address]) </div>
<div>
VALUES ('Arshad', 'Ali', 'India') </div>
<div>
INSERT INTO Employee([FirstName], [LastName], [Address]) </div>
<div>
VALUES ('Arshad', 'Ali', 'India') </div>
<div>
GO </div>
<div>
SELECT * FROM Employee </div>
<div>
GO </div>
<div>
--Selecting distinct records </div>
<div>
SELECT * FROM Employee E1 </div>
<div>
WHERE E1.ID = ( SELECT MAX(ID) FROM Employee E2 </div>
<div>
WHERE E2.FirstName = E1.FirstName AND E1.LastName = E2.LastName </div>
<div>
AND E1.Address = E2.Address) </div>
<div>
GO </div>
<div>
--Deleting duplicates </div>
<div>
DELETE Employee </div>
<div>
WHERE ID < ( SELECT MAX(ID) FROM Employee E2 </div>
<div>
WHERE E2.FirstName = Employee.FirstName AND E2.LastName = Employee.LastName </div>
<div>
AND E2.Address = Employee.Address) </div>
<div>
GO </div>
<div>
SELECT * FROM Employee </div>
<div>
GO </div>
<div>
<br /></div>
<span style="font-size: large;">Creating new table with distinct records and renaming it</span></div>
<div>
New table with distinct only</div>
<div>
<br /><div>
BEGIN TRAN</div>
<div>
-- Pull distinct records in a new table </div>
<div>
SELECT DISTINCT * INTO EmployeeNew </div>
<div>
FROM Employee </div>
<div>
--Drop the old target table </div>
<div>
DROP TABLE Employee </div>
<div>
--rename the new table </div>
<div>
EXEC sp_rename 'EmployeeNew', 'Employee' </div>
<div>
COMMIT TRAN</div>
<div>
GO </div>
<div>
SELECT * FROM Employee </div>
<div>
GO</div>
<br /><span style="font-size: large;">Using MERGE Statement</span><br style="font-size: x-large;" /><br />Beginning with SQL Server 2008, now you can use MERGE SQL COMMAND to perform INSERT/UPDATE/DELETE operations in a single statement. This new command is similar to the UPSERT (fusion of the words UPDATE and INSERT.) command of Oracle. It inserts rows that don't exist and updates the rows that do exist. With the introduction of the MERGE SQL command, developers can more effectively handle common data warehousing scenarios, like checking whether a row exists and then executing an insert or update or delete.<br /><br />The MERGE statement basically merges data from a source result set to a target table based on a condition that you specify and if the data from the source already exists in the target or not. The new SQL command combines the sequence of conditional INSERT, UPDATE and DELETE commands in a single atomic statement, depending on the existence of a record. With this you can make sure no duplicate records are being inserted into the target table, but rather updated if there is any change and only new records are inserted which do not already exist in the target.</div>
</div>
योगेश चन्द्र उप्रेतीhttp://www.blogger.com/profile/17009545633002694199noreply@blogger.com0tag:blogger.com,1999:blog-8545307170893600943.post-52372967481518833352019-09-28T23:49:00.000-07:002019-09-28T23:49:50.189-07:00SQL important Hierarchy Queries<div dir="ltr" style="text-align: left;" trbidi="on">
Common type expression for employee organisation hierarchy <br />
<br />
,OrgHierarchyLevel <br />
As <br />
( <br />
SELECT Id, [Name], ParentID, LevelId FROM T_Organisation WHERE Id = @OrgId <br />
UNION All <br />
SELECT O.Id, O.[Name], O.ParentID, O.LevelId FROM T_Organisation O <br />
INNER JOIN OrgHierarchyLevel OHL ON O.Id = OHL.ParentID <br />
) <br />
<br />
Common type expression for employee organisation hierarchy sequence within one row using PIVOT Query:<br />
<br />
DECLARE @OrgId INT = 100<br />
;WITH OrgHierarchyLvl<br />
<span style="white-space: pre;"> </span>As<br />
<span style="white-space: pre;"> </span>(<br />
<span style="white-space: pre;"> </span>SELECT<br />
<span style="white-space: pre;"> </span>Id, Name, ParentID, LevelId<br />
<span style="white-space: pre;"> </span>FROM<br />
<span style="white-space: pre;"> </span>Organisation WHERE Id = @OrgId<br />
<span style="white-space: pre;"> </span>UNION All<br />
<span style="white-space: pre;"> </span>SELECT<br />
<span style="white-space: pre;"> </span>ic.Id, ic.Name, ic.ParentID, ic.LevelId<br />
<span style="white-space: pre;"> </span>FROM Organisation ic<br />
<span style="white-space: pre;"> </span>INNER JOIN OrgHierarchyLvl cte ON ic.Id = cte.ParentID<br />
<span style="white-space: pre;"> </span>)<br />
,OrgSequence AS<br />
<span style="white-space: pre;"> </span>(<br />
<span style="white-space: pre;"> </span>SELECT @OrgId as OrgId,* FROM<br />
<span style="white-space: pre;"> </span>(<br />
<span style="white-space: pre;"> </span>SELECT LevelId,Name from OrgHierarchyLvl<br />
<span style="white-space: pre;"> </span>) AS SourceTable<br />
<span style="white-space: pre;"> </span>PIVOT(Max(Name) FOR [LevelId] IN ([1],[2],[3],[4],[5],[6],[7],[8])<br />
<span style="white-space: pre;"> </span>) AS PivotTable<br />
<span style="white-space: pre;"> </span>)<br />
<span style="white-space: pre;"> </span>SELECT <span style="white-space: pre;"> </span> OrgId,[1] AS [Level 1 Org], [2] AS [Level 2 Org], [3] AS [Level 3 Org], [4] AS [Level 4 Org],<br />
<span style="white-space: pre;"> </span>[5] AS [Level 5 Org], [6] AS [Level 6 Org], [7] AS [Level 7 Org], [8] AS [Level 8 Org] FROM OrgSequence</div>
योगेश चन्द्र उप्रेतीhttp://www.blogger.com/profile/17009545633002694199noreply@blogger.com0tag:blogger.com,1999:blog-8545307170893600943.post-12245457249235739612019-09-28T23:36:00.001-07:002019-09-28T23:36:23.925-07:00Using JSON in SQL server<div dir="ltr" style="text-align: left;" trbidi="on">
Selecting data using JSON in sql server:<br />
<br />
SELECT gid, email FROM tempEmails FOR json auto <br />
<br />
Selecting Column Values From JSON Format in sql server:<br />
<br />
For Example the below is the JSON format:<br />
<br />
[{"gid":"0000036285","email":"tetsuya.ono@gmail.com,tetsuya.ono@gmail.com"},{"gid":"0000047244","email":"Miyuki.Yamamoto@gmail.com"}]<br />
select JSON_Value(JSON_F52E2B61-18A1-11d1-B105-00805F49916B,'$.gid') as gid<br />
JSON_Value(JSON_F52E2B61-18A1-11d1-B105-00805F49916B,'$.email') as email<br />
<br />
;WITH CTE ([JSON_F52E2B61-18A1-11d1-B105-00805F49916B])<br />
as<br />
(<br />
SELECT TOP 2 gid, email FROM tempEmails FOR json auto<br />
)<br />
,CTE1 (JSONValue) AS<br />
(<br />
select [JSON_F52E2B61-18A1-11d1-B105-00805F49916B] JSONValue from cte<br />
)<br />
SELECT JSON_Value(JSONValue,'$[0].gid') as gid,<br />
JSON_Value(JSONValue,'$[0].email') as email FROM CTE1</div>
योगेश चन्द्र उप्रेतीhttp://www.blogger.com/profile/17009545633002694199noreply@blogger.com0tag:blogger.com,1999:blog-8545307170893600943.post-4622918292397209912019-09-28T23:15:00.000-07:002019-09-28T23:15:06.675-07:00Using SQL query Insert Data From CSV File in to the SQL server database <div dir="ltr" style="text-align: left;" trbidi="on">
drop table #test1<br />
CREATE TABLE #test1(<br />
<span style="white-space: pre;"> </span>[GID] [nvarchar](MAX) NULL,<br />
<span style="white-space: pre;"> </span>[Staff_Name] [nvarchar](MAX) NULL,<br />
<span style="white-space: pre;"> </span>[E_mail_Address] [nvarchar](MAX) NULL,<br />
<span style="white-space: pre;"> </span>[OrgLevel1] [nvarchar](MAX) NULL,<br />
<span style="white-space: pre;"> </span>[OrgLevel2] [nvarchar](MAX) NULL,<br />
<span style="white-space: pre;"> </span>[OrgLevel3] [nvarchar](MAX) NULL,<br />
<span style="white-space: pre;"> </span>[OrgLevel4] [nvarchar](MAX) NULL,<br />
<span style="white-space: pre;"> </span>[OrgLevel5] [nvarchar](MAX) NULL,<br />
<span style="white-space: pre;"> </span>[OrgLevel6] [nvarchar](MAX) NULL,<br />
<span style="white-space: pre;"> </span>[HeaOrDeputy] [nvarchar](MAX) NULL,<br />
<span style="white-space: pre;"> </span>[RoleType] [nvarchar](MAX) NULL,<br />
<span style="white-space: pre;"> </span>[OrgLevelId] [nvarchar] (MAX) NULL<br />
)<br />
<br />
--BULK INSERT #test1<br />
<br />
-- FROM 'D:\Master.csv'<br />
<br />
--BULK INSERT #test1<br />
--FROM 'D:\Master.csv'<br />
--WITH<br />
--(<br />
-- FIELDTERMINATOR = ' ' -- add this<br />
--);<br />
<br />
<br />
--BULK INSERT #test1<br />
--FROM 'D:\Master.csv'<br />
--WITH (<br />
--FIELDTERMINATOR = ',',<br />
--ROWTERMINATOR = '\n',<br />
--ERRORFILE = 'D:\myRubbishData.log'<br />
--);<br />
<br />
--BULK<br />
--INSERT #test1<br />
-- FROM 'D:\tempApproverMst.txt'<br />
--WITH<br />
--(<br />
--<span style="white-space: pre;"> </span>FIRSTROW = 1,<br />
--<span style="white-space: pre;"> </span>FIELDTERMINATOR = ' ',<br />
--<span style="white-space: pre;"> </span>ROWTERMINATOR = '\n', --0x0a<br />
--<span style="white-space: pre;"> </span>Lastrow = 30000003<br />
--)<br />
--GO<br />
<br />
INSERT INTO #test1 ([GID], [Staff_Name], [E_mail_Address], [PhoneN],OrgLevel1,OrgLevel2,OrgLevel3,OrgLevel4,OrgLevel5,OrgLevel6,HeaOrDeputy,OrgLevelId)<br />
SELECT A.[GID], A.[Staff_Name], A.[E_mail_Address], A.[PhoneN],A.OrgLevel1,A.OrgLevel2,A.OrgLevel3,A.OrgLevel4,A.OrgLevel5,A.OrgLevel6,A.HeaOrDeputy,A.OrgLevelId<br />
FROM OPENROWSET ('Microsoft.ACE.OLEDB.12.0', 'Excel 12.0; Database=D:\Master.csv', 'select * from [TableName]') AS A;<br />
<br />
SELECT * FROM #test1</div>
योगेश चन्द्र उप्रेतीhttp://www.blogger.com/profile/17009545633002694199noreply@blogger.com0tag:blogger.com,1999:blog-8545307170893600943.post-68878525217369268402019-09-27T00:48:00.001-07:002019-09-27T00:48:29.747-07:00Capitalize only the first letter of each word of each sentence in SQL Server<div dir="ltr" style="text-align: left;" trbidi="on">
BEGIN TRAN UPDATE T_MST_Skills SET Name = stuff((select ' '+upper(left(T3.V, 1))+lower(stuff(T3.V, 1, 1, ''))<br />
from (select cast(replace((select Name as '*' for xml path('')), ' ', '<x>') as xml).query('.')) as T1(X)</x><br />
cross apply T1.X.nodes('text()') as T2(X)<br />
cross apply (select T2.X.value('.', 'varchar(1000)')) as T3(V)<br />
for xml path(''), type<br />
).value('text()[1]', 'varchar(1000)'), 1, 1, '') from T_MST_Skills where [Name] not like '%(%' and [Name] not like '%.%';<br />
<br />
Rollback:<br />
<br />
select Name,stuff((<br />
select ' '+upper(left(T3.V, 1))+lower(stuff(T3.V, 1, 1, ''))<br />
from (select cast(replace((select Name as '*' for xml path('')), ' ', '<x>') as xml).query('.')) as T1(X)</x><br />
cross apply T1.X.nodes('text()') as T2(X)<br />
cross apply (select T2.X.value('.', 'varchar(1000)')) as T3(V)<br />
for xml path(''), type<br />
).value('text()[1]', 'varchar(1000)'), 1, 1, '') as [Capitalize first letter only] from T_MST_Skills where [Name] not like '%(%' and [Name] not like '%.%';</div>
योगेश चन्द्र उप्रेतीhttp://www.blogger.com/profile/17009545633002694199noreply@blogger.com0tag:blogger.com,1999:blog-8545307170893600943.post-46943337257086152362019-08-28T23:58:00.000-07:002019-08-28T23:58:16.569-07:00SQL Database Cloning (Clone an existing database to a new database) Create database replica<div dir="ltr" style="text-align: left;" trbidi="on">
<b><u><span style="font-size: large;">Duplicating SQL Database</span></u></b><br /><br />I need to essentially copy all of these tables and the data and the constraints exactly from one database to another.<br /><br /> The simplest backup command would be:<br /><br /><span style="background-color: #e69138; color: white;"> BACKUP DATABASE [db-prod] TO DISK = 'D:\dbbackup\db-prod.bak' WITH INIT;</span><br /> Now to restore this as a different database, you need to know the file names because it will try to put the same files in the same place. So if you run the following:<br /><br /><span style="background-color: #e69138;"><span style="color: white;"> EXEC [db-prod].dbo.sp_helpfile;</span></span><br />You should see output that contains the names and paths of the data and log files. When you construct your restore, you'll need to use these, but replace the paths with the name of the new database, e.g.:<br /><br /><span style="background-color: #e69138;"><span style="color: white;"> RESTORE DATABASE [db-local] FROM DISK = 'D:\dbbackup\db-prod.bak'</span></span><div>
<span style="background-color: #e69138;"><span style="color: white;"><br />WITH MOVE 'db-prod' TO 'C:\Program Files\Microsoft SQL </span></span></div>
<div>
<span style="background-color: #e69138;"><span style="color: white;"><br /></span></span></div>
<div>
<span style="background-color: #e69138;"><span style="color: white;">Server\MSSQL12.SQLEXPRESS\MSSQL\DATA\db-local.mdf',</span></span></div>
<div>
<span style="background-color: #e69138;"><span style="color: white;"><br />MOVE 'db-prod_log' TO 'C:\Program Files\Microsoft SQL </span></span></div>
<div>
<span style="background-color: #e69138;"><span style="color: white;"><br /></span></span></div>
<div>
<span style="background-color: #e69138;"><span style="color: white;">Server\MSSQL12.SQLEXPRESS\MSSQL\DATA\db-local_log.ldf';</span></span><br /><br /> You'll have to replace dbname and newname with your actual database names, and also some folder and C:\path_from_sp_helpfile_output\ with your actual paths. I can't get more specific in my answer unless I know what those are.<br /><br />Of course if the clone target (in this case DB-B) already exists, you'll want to drop it:<br /><br /><span style="background-color: #e69138;"><span style="color: white;"> USE [master];<br /><br />GO<br /><br />IF DB_ID('db-local') IS NOT NULL<br /><br />BEGIN<br /><br /> ALTER DATABASE [db-local] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;<br /><br /> DROP DATABASE [db-local];<br /><br />END<br /><br />GO</span></span><br /><br /></div>
</div>
योगेश चन्द्र उप्रेतीhttp://www.blogger.com/profile/17009545633002694199noreply@blogger.com0tag:blogger.com,1999:blog-8545307170893600943.post-18317551631644550802019-05-11T12:23:00.003-07:002019-05-11T12:23:46.393-07:00What is the use of static variable in C#? When to use it? Why can't I declare the static variable inside method?<div dir="ltr" style="text-align: left;" trbidi="on">
<h1 class="grid--cell fs-headline1 fl1 ow-break-word" itemprop="name" style="background-color: white; border: 0px; box-sizing: inherit; color: #242729; flex: 1 1 auto !important; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-size: 2.07692rem !important; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; font-weight: inherit; line-height: 1.3; margin: 0px 0px 0.5em; overflow-wrap: break-word !important; padding: 0px; vertical-align: baseline;">
S<a class="question-hyperlink" href="https://stackoverflow.com/questions/10795502/what-is-the-use-of-static-variable-in-c-when-to-use-it-why-cant-i-declare-th" style="border: 0px; box-sizing: inherit; color: #242729; cursor: pointer; font-size: 2.07692rem; font-stretch: inherit; font-style: inherit; font-variant: inherit; line-height: 1.35; margin: 0px 0px 0.5em; padding: 0px; text-decoration-line: none; vertical-align: baseline;">tatic variable in C#? </a>static variable value is shared among all instances of that class.</h1>
<div>
<div style="background-color: white; border: 0px; box-sizing: inherit; clear: both; color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-size: 15px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin-bottom: 1em; padding: 0px; vertical-align: baseline;">
A <code style="background-color: #eff0f1; border: 0px; box-sizing: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 1px 5px; vertical-align: baseline; white-space: pre-wrap;">static</code> variable shares the value of it among all instances of the class.</div>
<div style="background-color: white; border: 0px; box-sizing: inherit; clear: both; color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-size: 15px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin-bottom: 1em; padding: 0px; vertical-align: baseline;">
Example without declaring it static:</div>
<pre class="lang-cs prettyprint prettyprinted" style="background-color: #eff0f1; border-radius: 3px; border: 0px; box-sizing: inherit; color: #393318; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin-bottom: 1em; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: 12px 8px; vertical-align: baseline; width: auto;"><code style="border: 0px; box-sizing: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;"><span class="kwd" style="border: 0px; box-sizing: inherit; color: #101094; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">public</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="border: 0px; box-sizing: inherit; color: #101094; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">class</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="typ" style="border: 0px; box-sizing: inherit; color: #2b91af; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Variable</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">{</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="kwd" style="border: 0px; box-sizing: inherit; color: #101094; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">public</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="typ" style="border: 0px; box-sizing: inherit; color: #2b91af; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">int</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> i </span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="lit" style="border: 0px; box-sizing: inherit; color: #7d2727; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">5</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">;</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="kwd" style="border: 0px; box-sizing: inherit; color: #101094; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">public</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="border: 0px; box-sizing: inherit; color: #101094; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">void</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> test</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">()</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">{</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
i </span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> i </span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">+</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="lit" style="border: 0px; box-sizing: inherit; color: #7d2727; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">5</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">;</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="typ" style="border: 0px; box-sizing: inherit; color: #2b91af; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Console</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="border: 0px; box-sizing: inherit; color: #2b91af; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">WriteLine</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">i</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">);</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">}</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">}</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="kwd" style="border: 0px; box-sizing: inherit; color: #101094; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">public</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="border: 0px; box-sizing: inherit; color: #101094; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">class</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="typ" style="border: 0px; box-sizing: inherit; color: #2b91af; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Exercise</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">{</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="kwd" style="border: 0px; box-sizing: inherit; color: #101094; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">static</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="border: 0px; box-sizing: inherit; color: #101094; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">void</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="typ" style="border: 0px; box-sizing: inherit; color: #2b91af; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Main</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">()</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">{</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="typ" style="border: 0px; box-sizing: inherit; color: #2b91af; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Variable</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="border: 0px; box-sizing: inherit; color: #101094; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">var</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="border: 0px; box-sizing: inherit; color: #101094; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">new</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="typ" style="border: 0px; box-sizing: inherit; color: #2b91af; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Variable</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">();</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="kwd" style="border: 0px; box-sizing: inherit; color: #101094; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">var</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">test</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">();</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="typ" style="border: 0px; box-sizing: inherit; color: #2b91af; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Variable</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> var1 </span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="border: 0px; box-sizing: inherit; color: #101094; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">new</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="typ" style="border: 0px; box-sizing: inherit; color: #2b91af; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Variable</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">();</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
var1</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">test</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">();</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="typ" style="border: 0px; box-sizing: inherit; color: #2b91af; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Console</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="border: 0px; box-sizing: inherit; color: #2b91af; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">ReadKey</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">();</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">}</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">}</span></code></pre>
<div style="background-color: white; border: 0px; box-sizing: inherit; clear: both; color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-size: 15px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin-bottom: 1em; padding: 0px; vertical-align: baseline;">
Explanation: If you look at the above example, I just declare the <code style="background-color: #eff0f1; border: 0px; box-sizing: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 1px 5px; vertical-align: baseline; white-space: pre-wrap;">int</code> variable. When I run this code the output will be <code style="background-color: #eff0f1; border: 0px; box-sizing: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 1px 5px; vertical-align: baseline; white-space: pre-wrap;">10</code> and <code style="background-color: #eff0f1; border: 0px; box-sizing: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 1px 5px; vertical-align: baseline; white-space: pre-wrap;">10</code>. Its simple.</div>
<div style="background-color: white; border: 0px; box-sizing: inherit; clear: both; color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-size: 15px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin-bottom: 1em; padding: 0px; vertical-align: baseline;">
Now let's look at the static variable here; I am declaring the variable as a <code style="background-color: #eff0f1; border: 0px; box-sizing: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 1px 5px; vertical-align: baseline; white-space: pre-wrap;">static</code>.</div>
<div style="background-color: white; border: 0px; box-sizing: inherit; clear: both; color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-size: 15px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin-bottom: 1em; padding: 0px; vertical-align: baseline;">
Example with static variable:</div>
<pre class="lang-cs prettyprint prettyprinted" style="background-color: #eff0f1; border-radius: 3px; border: 0px; box-sizing: inherit; color: #393318; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin-bottom: 1em; max-height: 600px; overflow-wrap: normal; overflow: auto; padding: 12px 8px; vertical-align: baseline; width: auto;"><code style="border: 0px; box-sizing: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline; white-space: inherit;"><span class="kwd" style="border: 0px; box-sizing: inherit; color: #101094; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">public</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="border: 0px; box-sizing: inherit; color: #101094; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">class</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="typ" style="border: 0px; box-sizing: inherit; color: #2b91af; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Variable</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">{</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="kwd" style="border: 0px; box-sizing: inherit; color: #101094; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">public</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="border: 0px; box-sizing: inherit; color: #101094; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">static</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="typ" style="border: 0px; box-sizing: inherit; color: #2b91af; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">int</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> i </span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="lit" style="border: 0px; box-sizing: inherit; color: #7d2727; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">5</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">;</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="kwd" style="border: 0px; box-sizing: inherit; color: #101094; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">public</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="border: 0px; box-sizing: inherit; color: #101094; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">void</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> test</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">()</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">{</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
i </span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> i </span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">+</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="lit" style="border: 0px; box-sizing: inherit; color: #7d2727; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">5</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">;</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="typ" style="border: 0px; box-sizing: inherit; color: #2b91af; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Console</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="border: 0px; box-sizing: inherit; color: #2b91af; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">WriteLine</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">i</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">);</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">}</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">}</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="kwd" style="border: 0px; box-sizing: inherit; color: #101094; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">public</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="border: 0px; box-sizing: inherit; color: #101094; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">class</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="typ" style="border: 0px; box-sizing: inherit; color: #2b91af; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Exercise</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">{</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="kwd" style="border: 0px; box-sizing: inherit; color: #101094; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">static</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="border: 0px; box-sizing: inherit; color: #101094; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">void</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="typ" style="border: 0px; box-sizing: inherit; color: #2b91af; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Main</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">()</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">{</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="typ" style="border: 0px; box-sizing: inherit; color: #2b91af; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Variable</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="border: 0px; box-sizing: inherit; color: #101094; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">var</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="border: 0px; box-sizing: inherit; color: #101094; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">new</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="typ" style="border: 0px; box-sizing: inherit; color: #2b91af; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Variable</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">();</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="kwd" style="border: 0px; box-sizing: inherit; color: #101094; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">var</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">test</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">();</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="typ" style="border: 0px; box-sizing: inherit; color: #2b91af; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Variable</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> var1 </span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="border: 0px; box-sizing: inherit; color: #101094; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">new</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="typ" style="border: 0px; box-sizing: inherit; color: #2b91af; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Variable</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">();</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
var1</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">test</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">();</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="typ" style="border: 0px; box-sizing: inherit; color: #2b91af; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Console</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="border: 0px; box-sizing: inherit; color: #2b91af; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">ReadKey</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">();</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">}</span><span class="pln" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="border: 0px; box-sizing: inherit; color: #303336; font-family: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">}</span></code></pre>
<div style="background-color: white; border: 0px; box-sizing: inherit; clear: both; color: #242729; font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-size: 15px; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin-bottom: 1em; padding: 0px; vertical-align: baseline;">
Now when I run above code, the output will be <code style="background-color: #eff0f1; border: 0px; box-sizing: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 1px 5px; vertical-align: baseline; white-space: pre-wrap;">10</code> and <code style="background-color: #eff0f1; border: 0px; box-sizing: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; font-size: 13px; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 1px 5px; vertical-align: baseline; white-space: pre-wrap;">15</code>. So the static variable value is shared among all instances of that class.</div>
</div>
</div>
योगेश चन्द्र उप्रेतीhttp://www.blogger.com/profile/17009545633002694199noreply@blogger.com0tag:blogger.com,1999:blog-8545307170893600943.post-79048708020783400402019-05-04T13:26:00.002-07:002019-05-04T13:26:22.315-07:00ADO.NET<div dir="ltr" style="text-align: left;" trbidi="on">
<h1>
How to Create and Execute SqlCommand in ADO.NET</h1>
<br />
<div id="projectInfo">
<div class="section">
<div class="itemBarLong tagsContainer">
<label for="Technologies">Technologies </label>ADO.NET, Data Access, .NET Development</div>
<div class="itemBarLong tagsContainer">
<label for="Topics"><br /></label></div>
<div class="itemBarLong tagsContainer">
<label for="Topics">Topics</label></div>
<div class="itemBarLong tagsContainer">
<br /> <div id="Topics">
Stored Procedures, sql commond </div>
</div>
<div class="itemBarLong">
<br /></div>
<div class="itemBarLong">
How to Create and Execute SqlCommand in ADO.NET</div>
</div>
</div>
<div id="longDesc">
<h2>
Introduction</h2>
We can create and execute different types of SqlCommand. In this applilcation, we will demonstrate how to create and execute SqlCommand:<br />
<br />
1. Create different types of SqlCommand;<br />
2. Execute SqlCommand in different ways;<br />
3. Display the result.<br />
<h2>
Using the Code</h2>
1. <b>ExecuteNonQuery </b>method<br />
If you need modify the data (Add, Delete, Update), you can use the method to complete it.<br />
<br />
<div class="scriptcode">
<div class="pluginEditHolder" plugincommand="mceScriptCode">
<div class="title">
<b>C#</b></div>
<div class="pluginLinkHolder">
<br /></div>
<pre class="hidden">public static Int32 ExecuteNonQuery(String connectionString, String commandText,
CommandType commandType, params SqlParameter[] parameters)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(commandText, conn))
{
cmd.CommandType = commandType;
cmd.Parameters.AddRange(parameters);
conn.Open();
return cmd.ExecuteNonQuery();
}
}
}
</pre>
<pre class="hidden">
</pre>
<pre class="hidden"><b>Visual Basic</b></pre>
<pre class="hidden">
</pre>
<pre class="hidden">Public Shared Function ExecuteNonQuery(ByVal connectionString As String, ByVal commandText As String,
ByVal commandType As CommandType, ByVal ParamArray parameters() As SqlParameter) As Int32
Using conn As New SqlConnection(connectionString)
Using cmd As New SqlCommand(commandText, conn)
' There're three command types: StoredProcedure, Text, TableDirect. The TableDirect
' type is only for OLE DB.
cmd.CommandType = commandType
cmd.Parameters.AddRange(parameters)
conn.Open()
Return cmd.ExecuteNonQuery()
End Using
End Using
End Function
</pre>
<pre class="hidden">
</pre>
<div class="preview">
<pre class="csharp"><span class="cs__keyword">public </span><span class="cs__keyword">static</span> Int32 ExecuteNonQuery(String connectionString, String commandText,
CommandType commandType, <span class="cs__keyword">params</span> SqlParameter[] parameters)
{
<span class="cs__keyword">using</span> (SqlConnection conn = <span class="cs__keyword">new</span> SqlConnection(connectionString))
{
<span class="cs__keyword">using</span> (SqlCommand cmd = <span class="cs__keyword">new</span> SqlCommand(commandText, conn))
{
cmd.CommandType = commandType;
cmd.Parameters.AddRange(parameters);
conn.Open();
<span class="cs__keyword">return</span> cmd.ExecuteNonQuery();
}
}
}
</pre>
</div>
</div>
</div>
<br />
2. <b>ExecuteScalar </b>method<br />
If you only need one value (first column and first row), you can use the method to get the value, such as the statistical value.<br />
<br />
<div class="scriptcode">
<div class="pluginEditHolder" plugincommand="mceScriptCode">
<div class="title">
<b>C#</b></div>
<div class="pluginLinkHolder">
<br /></div>
<pre class="hidden">public static Object ExecuteScalar(String connectionString, String commandText,
CommandType commandType, params SqlParameter[] parameters)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(commandText, conn))
{
cmd.CommandType = commandType;
cmd.Parameters.AddRange(parameters);
conn.Open();
return cmd.ExecuteScalar();
}
}
}
</pre>
<pre class="hidden">
</pre>
<pre class="hidden"><b>Visual Basic</b></pre>
<pre class="hidden">
</pre>
<pre class="hidden">Public Shared Function ExecuteScalar(ByVal connectionString As String, ByVal commandText As String,
ByVal commandType As CommandType, ByVal ParamArray parameters() As SqlParameter) As Object
Using conn As New SqlConnection(connectionString)
Using cmd As New SqlCommand(commandText, conn)
cmd.CommandType = commandType
cmd.Parameters.AddRange(parameters)
conn.Open()
Return cmd.ExecuteScalar()
End Using
End Using
End Function
</pre>
<pre class="hidden">
</pre>
<div class="preview">
<pre class="csharp"><span class="cs__keyword">public </span><span class="cs__keyword">static</span> Object ExecuteScalar(String connectionString, String commandText,
CommandType commandType, <span class="cs__keyword">params</span> SqlParameter[] parameters)
{
<span class="cs__keyword">using</span> (SqlConnection conn = <span class="cs__keyword">new</span> SqlConnection(connectionString))
{
<span class="cs__keyword">using</span> (SqlCommand cmd = <span class="cs__keyword">new</span> SqlCommand(commandText, conn))
{
cmd.CommandType = commandType;
cmd.Parameters.AddRange(parameters);
conn.Open();
<span class="cs__keyword">return</span> cmd.ExecuteScalar();
}
}
}
</pre>
</div>
</div>
</div>
<br />
3. <b>ExecuteReader </b>method<br />
When you need the details of the data, you can use this method to return the information.<br />
<br />
<div class="scriptcode">
<div class="pluginEditHolder" plugincommand="mceScriptCode">
<div class="title">
<b>C#</b></div>
<div class="pluginLinkHolder">
<br /></div>
<pre class="hidden">public static SqlDataReader ExecuteReader(String connectionString, String commandText,
CommandType commandType, params SqlParameter[] parameters)
{
SqlConnection conn = new SqlConnection(connectionString);
using (SqlCommand cmd = new SqlCommand(commandText, conn))
{
cmd.CommandType = commandType;
cmd.Parameters.AddRange(parameters);
conn.Open();
// When using CommandBehavior.CloseConnection, the connection will be closed when the
// IDataReader is closed.
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return reader;
}
}
</pre>
<pre class="hidden">
</pre>
<pre class="hidden"><b>Visual Basic</b></pre>
<pre class="hidden">
</pre>
<pre class="hidden">Public Shared Function ExecuteReader(ByVal connectionString As String, ByVal commandText As String,
ByVal commandType As CommandType, ByVal ParamArray parameters() As SqlParameter) As SqlDataReader
Dim conn As New SqlConnection(connectionString)
Using cmd As New SqlCommand(commandText, conn)
cmd.CommandType = commandType
cmd.Parameters.AddRange(parameters)
conn.Open()
' When using CommandBehavior.CloseConnection, the connection will be closed when the
' IDataReader is closed.
Dim reader As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
Return reader
End Using
End Function
</pre>
<pre class="hidden">
</pre>
<div class="preview">
<pre class="csharp"><span class="cs__keyword">public </span><span class="cs__keyword">static</span> SqlDataReader ExecuteReader(String connectionString, String commandText,
CommandType commandType, <span class="cs__keyword">params</span> SqlParameter[] parameters)
{
SqlConnection conn = <span class="cs__keyword">new</span> SqlConnection(connectionString);
<span class="cs__keyword">using</span> (SqlCommand cmd = <span class="cs__keyword">new</span> SqlCommand(commandText, conn))
{
cmd.CommandType = commandType;
cmd.Parameters.AddRange(parameters);
conn.Open();
<span class="cs__com">// When using CommandBehavior.CloseConnection, the connection will be closed when the </span><span class="cs__com">// IDataReader is closed.</span>
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
<span class="cs__keyword">return</span> reader;
}
}
</pre>
</div>
</div>
</div>
<br />
4. <b>Parameter</b><br />
You can add several parameters to the command and set the properties of parameter, such as the value, the type and the direction. If the direction is set as Output, you can get the value of parameter after executing the command.<br />
<br />
<div class="scriptcode">
<div class="pluginEditHolder" plugincommand="mceScriptCode">
<div class="title">
<b>C#</b></div>
<div class="pluginLinkHolder">
<br /></div>
<pre class="hidden">// Specify the year of StartDate
SqlParameter parameterYear = new SqlParameter("@Year", SqlDbType.Int);
parameterYear.Value = year;
SqlParameter parameterBudget = new SqlParameter("@BudgetSum", SqlDbType.Money);
parameterBudget.Direction = ParameterDirection.Output;
</pre>
<pre class="hidden">
</pre>
<pre class="hidden"><b>Visual Basic</b></pre>
<pre class="hidden">
</pre>
<pre class="hidden">' Specify the year of StartDate
Dim parameterYear As New SqlParameter("@Year", SqlDbType.Int)
parameterYear.Value = year
Dim parameterBudget As New SqlParameter("@BudgetSum", SqlDbType.Money)
parameterBudget.Direction = ParameterDirection.Output
</pre>
<pre class="hidden">
</pre>
<div class="preview">
<pre class="csharp"><span class="cs__com">// Specify the year of StartDate</span>
SqlParameter parameterYear = <span class="cs__keyword">new</span> SqlParameter(<span class="cs__string">"@Year"</span>, SqlDbType.Int);
parameterYear.Value = year;
SqlParameter parameterBudget = <span class="cs__keyword">new</span> SqlParameter(<span class="cs__string">"@BudgetSum"</span>, SqlDbType.Money);
parameterBudget.Direction = ParameterDirection.Output;
</pre>
</div>
</div>
</div>
<br />
5.<b> Command Type.</b><br />
There're three command types: StoredProcedure, Text (Default), TableDirect. The TableDirect type is only for OLE DB.<br />
<br />
<div class="scriptcode">
<div class="pluginEditHolder" plugincommand="mceScriptCode">
<div class="title">
<b>C#</b></div>
<div class="pluginLinkHolder">
<br /></div>
<pre class="hidden">using (SqlDataReader reader = SqlHelper.ExecuteReader(connectionString, commandText,
CommandType.StoredProcedure, parameterYear, parameterBudget))
</pre>
<pre class="hidden">
</pre>
<pre class="hidden"><b style="font-family: "Times New Roman"; white-space: normal;">Visual Basic</b></pre>
<pre class="hidden"><b style="font-family: "Times New Roman"; white-space: normal;">
</b></pre>
<pre class="hidden">Using reader As SqlDataReader = SqlHelper.ExecuteReader(connectionString, commandText,
CommandType.StoredProcedure,
parameterYear, parameterBudget)
</pre>
<pre class="hidden">
</pre>
<div class="preview">
<pre class="csharp"><span class="cs__keyword">using</span> (SqlDataReader reader = SqlHelper.ExecuteReader(connectionString, commandText,
CommandType.StoredProcedure, parameterYear, parameterBudget))
</pre>
</div>
</div>
</div>
</div>
</div>
योगेश चन्द्र उप्रेतीhttp://www.blogger.com/profile/17009545633002694199noreply@blogger.com2tag:blogger.com,1999:blog-8545307170893600943.post-75193713600899008692018-12-17T09:49:00.000-08:002018-12-17T09:49:18.368-08:00Validation to Check Duplicate Records<div dir="ltr" style="text-align: left;" trbidi="on">
<div>
<h1 class="icon-rss-head icon-answered indent no-border" id="threadstatus" style="background-attachment: initial; background-clip: initial; background-image: url("../images/sprite-forums.png"); background-origin: initial; background-position: 0px -2337px; background-repeat: no-repeat; background-size: initial; border: 0px; color: #222222; font-family: "Segoe UI Light", "Segoe UI", Tahoma, Arial, Helvetica, sans-serif; font-size: 36px; font-weight: normal; line-height: 1.4em; margin: 0px 0px 13px; padding: 0px 0px 0px 38px; position: relative; text-align: left; vertical-align: baseline; z-index: 1;" title="Prevent from inserting duplicate records into the table">
Prevent from inserting duplicate records into the table</h1>
</div>
You need to check if a duplicate entry exists in table before inserting the data. I suggest you could refer to the solution given below:<div>
<br /></div>
<div>
Approach 1=></div>
<div>
<br /></div>
<div>
<pre class="prettyprint lang-cs prettyprinted" style="background: rgb(255, 255, 255); border: 1px solid rgb(225, 226, 226); color: #222222; font-family: Consolas, monospace; font-size: 13.008px; margin-bottom: 30px; margin-top: 18px; overflow: auto; padding: 5px; vertical-align: baseline;"><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">using </span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">SqlConnection</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> conn </span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="background: transparent; border: 0px; color: blue; margin: 0px; padding: 0px; vertical-align: baseline;">new</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">SqlConnection</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">System</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">Configuration</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">ConfigurationManager</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">ConnectionStrings</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">[</span><span class="str" style="background: transparent; border: 0px; color: #a31515; margin: 0px; padding: 0px; vertical-align: baseline;">"DefaultCSRConnection"</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">].</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">ConnectionString</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">))</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">{</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="com" style="background: transparent; border: 0px; color: green; margin: 0px; padding: 0px; vertical-align: baseline;">//Open your connection</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
conn</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">Open</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">();</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="com" style="background: transparent; border: 0px; color: green; margin: 0px; padding: 0px; vertical-align: baseline;">//Your Select Query</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="com" style="background: transparent; border: 0px; color: green; margin: 0px; padding: 0px; vertical-align: baseline;">//Change this as per your design and provide the primary key field here</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="kwd" style="background: transparent; border: 0px; color: blue; margin: 0px; padding: 0px; vertical-align: baseline;">string</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> selectString </span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="str" style="background: transparent; border: 0px; color: #a31515; margin: 0px; padding: 0px; vertical-align: baseline;">"SELECT COUNT(*) FROM tbl_students WHERE YourIDField = @idfield"</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">;</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="com" style="background: transparent; border: 0px; color: green; margin: 0px; padding: 0px; vertical-align: baseline;">//Create Command object</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">SqlCommand</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> myCommand </span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="background: transparent; border: 0px; color: blue; margin: 0px; padding: 0px; vertical-align: baseline;">new</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">SqlCommand</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">selectString</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> conn</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">);</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="com" style="background: transparent; border: 0px; color: green; margin: 0px; padding: 0px; vertical-align: baseline;">//Pass your parameter here</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
myCommand</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">Parameters</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">AddWithValue</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="str" style="background: transparent; border: 0px; color: #a31515; margin: 0px; padding: 0px; vertical-align: baseline;">"@id"</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> yourexistingidvalue</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">);</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="com" style="background: transparent; border: 0px; color: green; margin: 0px; padding: 0px; vertical-align: baseline;">// Get the Result query</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="kwd" style="background: transparent; border: 0px; color: blue; margin: 0px; padding: 0px; vertical-align: baseline;">var</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> idExists </span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">Int32</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">)</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">myCommand</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">ExecuteScalar</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">()</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">></span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="lit" style="background: transparent; border: 0px; color: red; margin: 0px; padding: 0px; vertical-align: baseline;">0</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">;</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="com" style="background: transparent; border: 0px; color: green; margin: 0px; padding: 0px; vertical-align: baseline;">//Check if record exists in table</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="kwd" style="background: transparent; border: 0px; color: blue; margin: 0px; padding: 0px; vertical-align: baseline;">if</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">(!</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">idExists</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">)</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">{</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="com" style="background: transparent; border: 0px; color: green; margin: 0px; padding: 0px; vertical-align: baseline;">//Insert the Record</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
using </span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">SqlCommand</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> cmd </span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> conn</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">CreateCommand</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">())</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">{</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">SqlCommand</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> cmd1 </span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="background: transparent; border: 0px; color: blue; margin: 0px; padding: 0px; vertical-align: baseline;">new</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">SqlCommand</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="str" style="background: transparent; border: 0px; color: #a31515; margin: 0px; padding: 0px; vertical-align: baseline;">"Insert into tbl_students (first_name,last_name,sex,dob,active) values(@first_name, @last_name,@sex,@dob,@active)"</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> conn</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">);</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
cmd1</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">Parameters</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">Add</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="str" style="background: transparent; border: 0px; color: #a31515; margin: 0px; padding: 0px; vertical-align: baseline;">"@first_name"</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">SqlDbType</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">NVarChar</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">).</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">Value</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> txtFname</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">Text</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">;</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
cmd1</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">Parameters</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">Add</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="str" style="background: transparent; border: 0px; color: #a31515; margin: 0px; padding: 0px; vertical-align: baseline;">"@last_name"</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">SqlDbType</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">NVarChar</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">).</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">Value</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> txtLname</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">Text</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">;</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
cmd1</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">Parameters</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">Add</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="str" style="background: transparent; border: 0px; color: #a31515; margin: 0px; padding: 0px; vertical-align: baseline;">"@sex"</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">SqlDbType</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">NVarChar</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">).</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">Value</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> ddlgender</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">SelectedValue</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">;</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
cmd1</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">Parameters</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">Add</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="str" style="background: transparent; border: 0px; color: #a31515; margin: 0px; padding: 0px; vertical-align: baseline;">"@dob"</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">SqlDbType</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">DateTime</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">).</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">Value</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> txtDateofBirth</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">Text</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">;</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
cmd1</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">Parameters</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">Add</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="str" style="background: transparent; border: 0px; color: #a31515; margin: 0px; padding: 0px; vertical-align: baseline;">"@active"</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">SqlDbType</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">Bit</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">).</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">Value</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">Convert</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">ToInt32</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">ddlActiveInactive</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">SelectedValue</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">);</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
cmd1</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">ExecuteNonQuery</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">();</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
conn</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">Close</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">();</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">ScriptManager</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">RegisterStartupScript</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="kwd" style="background: transparent; border: 0px; color: blue; margin: 0px; padding: 0px; vertical-align: baseline;">this</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="background: transparent; border: 0px; color: blue; margin: 0px; padding: 0px; vertical-align: baseline;">this</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">.</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">GetType</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">(),</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="str" style="background: transparent; border: 0px; color: #a31515; margin: 0px; padding: 0px; vertical-align: baseline;">"alert"</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="str" style="background: transparent; border: 0px; color: #a31515; margin: 0px; padding: 0px; vertical-align: baseline;">"alert('page added sucessfully');window.location ='csrstudentprofile.aspx';"</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="background: transparent; border: 0px; color: blue; margin: 0px; padding: 0px; vertical-align: baseline;">true</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">);</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">}</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">}</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">}</span></pre>
</div>
<div>
<div>
Approach 2=></div>
<div>
<br /></div>
<div>
<pre class="prettyprint prettyprinted" style="background: rgb(255, 255, 255); border: 1px solid rgb(225, 226, 226); color: #222222; font-family: Consolas, monospace; font-size: 13.008px; margin-bottom: 30px; margin-top: 18px; overflow: auto; padding: 5px; vertical-align: baseline;"><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">CREATE PROC </span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">ValidateAndInsertStudent</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="lit" style="background: transparent; border: 0px; color: red; margin: 0px; padding: 0px; vertical-align: baseline;">@first_name</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> VARCHAR</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="lit" style="background: transparent; border: 0px; color: red; margin: 0px; padding: 0px; vertical-align: baseline;">50</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">),</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="lit" style="background: transparent; border: 0px; color: red; margin: 0px; padding: 0px; vertical-align: baseline;">@last_name</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> VARCHAR</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="lit" style="background: transparent; border: 0px; color: red; margin: 0px; padding: 0px; vertical-align: baseline;">50</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">),</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="lit" style="background: transparent; border: 0px; color: red; margin: 0px; padding: 0px; vertical-align: baseline;">@sex</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> VARCHAR</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="lit" style="background: transparent; border: 0px; color: red; margin: 0px; padding: 0px; vertical-align: baseline;">50</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">),</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="lit" style="background: transparent; border: 0px; color: red; margin: 0px; padding: 0px; vertical-align: baseline;">@dob</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">DateTime</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="lit" style="background: transparent; border: 0px; color: red; margin: 0px; padding: 0px; vertical-align: baseline;">@active</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> BIT</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="lit" style="background: transparent; border: 0px; color: red; margin: 0px; padding: 0px; vertical-align: baseline;">@Done</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> BIT</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="lit" style="background: transparent; border: 0px; color: red; margin: 0px; padding: 0px; vertical-align: baseline;">@Msg</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> VARCHAR</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="lit" style="background: transparent; border: 0px; color: red; margin: 0px; padding: 0px; vertical-align: baseline;">200</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">)</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">)</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
AS
</span><span class="kwd" style="background: transparent; border: 0px; color: blue; margin: 0px; padding: 0px; vertical-align: baseline;">BEGIN</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
SET </span><span class="lit" style="background: transparent; border: 0px; color: red; margin: 0px; padding: 0px; vertical-align: baseline;">@Done</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="lit" style="background: transparent; border: 0px; color: red; margin: 0px; padding: 0px; vertical-align: baseline;">1</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
SET </span><span class="lit" style="background: transparent; border: 0px; color: red; margin: 0px; padding: 0px; vertical-align: baseline;">@Msg</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="str" style="background: transparent; border: 0px; color: #a31515; margin: 0px; padding: 0px; vertical-align: baseline;">''</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
IF </span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">SELECT COUNT</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">(*)</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> FROM tbl_students WHERE first_name</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="lit" style="background: transparent; border: 0px; color: red; margin: 0px; padding: 0px; vertical-align: baseline;">@first_name</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> AND last_name</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="lit" style="background: transparent; border: 0px; color: red; margin: 0px; padding: 0px; vertical-align: baseline;">@last_name</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> AND sex</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="lit" style="background: transparent; border: 0px; color: red; margin: 0px; padding: 0px; vertical-align: baseline;">@sex</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> AND dob</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="lit" style="background: transparent; border: 0px; color: red; margin: 0px; padding: 0px; vertical-align: baseline;">@dob</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> AND active</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="lit" style="background: transparent; border: 0px; color: red; margin: 0px; padding: 0px; vertical-align: baseline;">@active</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">)></span><span class="lit" style="background: transparent; border: 0px; color: red; margin: 0px; padding: 0px; vertical-align: baseline;">0</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="kwd" style="background: transparent; border: 0px; color: blue; margin: 0px; padding: 0px; vertical-align: baseline;">BEGIN</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
SET </span><span class="lit" style="background: transparent; border: 0px; color: red; margin: 0px; padding: 0px; vertical-align: baseline;">@Done</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="lit" style="background: transparent; border: 0px; color: red; margin: 0px; padding: 0px; vertical-align: baseline;">0</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
SET </span><span class="lit" style="background: transparent; border: 0px; color: red; margin: 0px; padding: 0px; vertical-align: baseline;">@Msg</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">=</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="str" style="background: transparent; border: 0px; color: #a31515; margin: 0px; padding: 0px; vertical-align: baseline;">'Record exists'</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
RETURN</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">;</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="kwd" style="background: transparent; border: 0px; color: blue; margin: 0px; padding: 0px; vertical-align: baseline;">END</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="typ" style="background: transparent; border: 0px; color: #2b91af; margin: 0px; padding: 0px; vertical-align: baseline;">Insert</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="kwd" style="background: transparent; border: 0px; color: blue; margin: 0px; padding: 0px; vertical-align: baseline;">into</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> tbl_students </span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">first_name</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">last_name</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">sex</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">dob</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">active</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">)</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> values</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">(</span><span class="lit" style="background: transparent; border: 0px; color: red; margin: 0px; padding: 0px; vertical-align: baseline;">@first_name</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;"> </span><span class="lit" style="background: transparent; border: 0px; color: red; margin: 0px; padding: 0px; vertical-align: baseline;">@last_name</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="lit" style="background: transparent; border: 0px; color: red; margin: 0px; padding: 0px; vertical-align: baseline;">@sex</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="lit" style="background: transparent; border: 0px; color: red; margin: 0px; padding: 0px; vertical-align: baseline;">@dob</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">,</span><span class="lit" style="background: transparent; border: 0px; color: red; margin: 0px; padding: 0px; vertical-align: baseline;">@active</span><span class="pun" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">)</span><span class="pln" style="background: transparent; border: 0px; color: black; margin: 0px; padding: 0px; vertical-align: baseline;">
</span><span class="kwd" style="background: transparent; border: 0px; color: blue; margin: 0px; padding: 0px; vertical-align: baseline;">END</span></pre>
</div>
<div>
Approach 3=> Using Output Parameter</div>
</div>
<div>
<br /></div>
<div>
<div style="box-sizing: border-box; color: #444444; font-family: "Courier New" !important; font-size: 10pt !important;">
<span style="box-sizing: border-box; font-size: 10pt !important;"><span style="box-sizing: border-box; color: blue; font-family: "courier new"; font-size: 11pt !important;">CREATE</span><span style="box-sizing: border-box; font-family: "courier new"; font-size: 11pt !important;"> <span style="box-sizing: border-box; color: blue; font-family: inherit; font-size: 11pt !important;">PROCEDURE</span> [dbo]<span style="box-sizing: border-box; color: grey; font-family: inherit; font-size: 11pt !important;">.</span>[<span style="color: #a31515;">InsertsFruitName</span>]</span></span></div>
<div style="box-sizing: border-box; color: #444444; font-family: "Courier New" !important; font-size: 10pt !important;">
<span style="box-sizing: border-box; font-family: "courier new"; font-size: 11pt !important;"><span style="box-sizing: border-box; font-family: "Courier New" !important; font-size: 10pt !important;"> @FruitId <span style="box-sizing: border-box; color: blue; font-family: inherit; font-size: 11pt !important;">INT</span><span style="box-sizing: border-box; color: grey; font-family: inherit; font-size: 11pt !important;">,</span></span></span></div>
<div style="box-sizing: border-box; color: #444444; font-family: "Courier New" !important; font-size: 10pt !important;">
<span style="box-sizing: border-box; font-family: "courier new"; font-size: 11pt !important;"><span style="box-sizing: border-box; font-family: "Courier New" !important; font-size: 10pt !important;"> @Exists <span style="box-sizing: border-box; color: blue; font-family: inherit; font-size: 11pt !important;">VARCHAR</span><span style="box-sizing: border-box; color: grey; font-family: inherit; font-size: 11pt !important;">(</span>30<span style="box-sizing: border-box; color: grey; font-family: inherit; font-size: 11pt !important;">) </span></span></span><span style="box-sizing: border-box; color: blue; font-family: inherit; font-size: 11pt !important;">OUTPUT</span><span style="color: grey; font-family: inherit; font-size: 11pt;">,</span></div>
<div style="box-sizing: border-box; color: #444444; font-family: "Courier New" !important; font-size: 10pt !important;">
<span style="box-sizing: border-box; font-family: "courier new"; font-size: 11pt !important;"><span style="box-sizing: border-box; font-family: "Courier New" !important; font-size: 10pt !important;"> </span></span>@Details <span style="box-sizing: border-box; color: blue; font-family: inherit; font-size: 11pt !important;">VARCHAR</span><span style="box-sizing: border-box; color: grey; font-family: inherit; font-size: 11pt !important;">(</span>300<span style="box-sizing: border-box; color: grey; font-family: inherit; font-size: 11pt !important;">)</span> </div>
<div style="box-sizing: border-box; color: #444444; font-family: "Courier New" !important; font-size: 10pt !important;">
<span style="box-sizing: border-box; color: blue; font-family: "courier new"; font-size: 11pt !important;"><span style="box-sizing: border-box; font-family: "Courier New" !important; font-size: 10pt !important;">AS</span></span></div>
<div style="box-sizing: border-box; color: #444444; font-family: "Courier New" !important; font-size: 10pt !important;">
<span style="box-sizing: border-box; color: blue; font-family: "courier new"; font-size: 11pt !important;"><span style="box-sizing: border-box; font-family: "Courier New" !important; font-size: 10pt !important;">BEGIN</span></span></div>
<div style="box-sizing: border-box; color: #444444; font-family: "Courier New" !important; font-size: 10pt !important;">
<span style="box-sizing: border-box; font-family: "courier new"; font-size: 11pt !important;"><span style="box-sizing: border-box; font-family: "Courier New" !important; font-size: 10pt !important;"> <span style="box-sizing: border-box; color: blue; font-family: inherit; font-size: 11pt !important;">SET</span> <span style="box-sizing: border-box; color: blue; font-family: inherit; font-size: 11pt !important;">NOCOUNT</span> <span style="box-sizing: border-box; color: blue; font-family: inherit; font-size: 11pt !important;">ON</span><span style="box-sizing: border-box; color: grey; font-family: inherit; font-size: 11pt !important;">;</span></span></span></div>
<div style="box-sizing: border-box; color: #444444; font-family: "Courier New" !important; font-size: 10pt !important;">
<span style="box-sizing: border-box; font-family: "courier new"; font-size: 11pt !important;"><span style="box-sizing: border-box; font-family: "Courier New" !important; font-size: 10pt !important;"> IF EXISTS (SELECT TOP 1 FROM </span></span>Fruits WHERE FruitId = @FruitId<span style="font-size: 10pt;">)</span></div>
<div style="box-sizing: border-box; color: #444444; font-family: "Courier New" !important; font-size: 10pt !important;">
<span style="font-size: 10pt;"> </span><span style="color: blue;">BEGIN</span></div>
<div style="box-sizing: border-box; color: #444444; font-family: "Courier New" !important; font-size: 10pt !important;">
<span style="box-sizing: border-box; font-family: "courier new"; font-size: 11pt !important;"><span style="box-sizing: border-box; font-family: "Courier New" !important; font-size: 10pt !important;"> <span style="box-sizing: border-box; color: blue; font-family: inherit; font-size: 11pt !important;">SELECT</span> @Exists <span style="box-sizing: border-box; color: grey; font-family: inherit; font-size: 11pt !important;">=</span> 'EXISTS'</span></span><span style="font-size: 10pt;"> </span></div>
<div style="box-sizing: border-box; color: #444444; font-family: "Courier New" !important; font-size: 10pt !important;">
<span style="box-sizing: border-box; font-family: "courier new"; font-size: 11pt !important;"><span style="box-sizing: border-box; font-family: "Courier New" !important; font-size: 10pt !important;"><span style="color: blue; font-family: "courier new"; font-size: 14.6667px;"> END</span></span></span></div>
<div style="box-sizing: border-box; color: #444444; font-family: "Courier New" !important; font-size: 10pt !important;">
<span style="box-sizing: border-box; font-family: "courier new"; font-size: 11pt !important;"><span style="box-sizing: border-box; font-family: "Courier New" !important; font-size: 10pt !important;"><span style="color: blue; font-family: "courier new"; font-size: 14.6667px;"> ELSE</span></span></span></div>
<div style="box-sizing: border-box; color: #444444; font-family: "Courier New" !important; font-size: 10pt !important;">
<span style="box-sizing: border-box; font-family: "courier new"; font-size: 11pt !important;"><span style="box-sizing: border-box; font-family: "Courier New" !important; font-size: 10pt !important;"><span style="color: blue; font-family: "courier new"; font-size: 14.6667px;"> BEGIN</span></span></span></div>
<div style="box-sizing: border-box; color: #444444; font-family: "Courier New" !important; font-size: 10pt !important;">
<span style="box-sizing: border-box; font-family: "courier new"; font-size: 11pt !important;"><span style="box-sizing: border-box; font-family: "Courier New" !important; font-size: 10pt !important;"><span style="color: blue; font-family: "courier new"; font-size: 14.6667px;"> </span></span></span><span style="box-sizing: border-box; font-family: "courier new"; font-size: 11pt !important;"><span style="box-sizing: border-box; font-family: "Courier New" !important; font-size: 10pt !important;"><span style="box-sizing: border-box; color: blue; font-family: inherit; font-size: 11pt !important;">SELECT</span> @Exists <span style="box-sizing: border-box; color: grey; font-family: inherit; font-size: 11pt !important;">=</span> ''</span></span><span style="font-size: 10pt;"> </span></div>
<div style="box-sizing: border-box; color: #444444; font-family: "Courier New" !important; font-size: 10pt !important;">
<span style="box-sizing: border-box; font-family: "courier new"; font-size: 11pt !important;"><span style="box-sizing: border-box; font-family: "Courier New" !important; font-size: 10pt !important;"><span style="color: blue; font-family: "courier new"; font-size: 14.6667px;"> </span></span></span><span style="box-sizing: border-box; font-family: "courier new"; font-size: 11pt !important;"><span style="box-sizing: border-box; font-family: "Courier New" !important; font-size: 10pt !important;"><span style="box-sizing: border-box; color: blue; font-family: inherit; font-size: 11pt !important;">INSERT INTO</span> </span></span>Fruits (<span style="box-sizing: border-box; font-family: "courier new"; font-size: 11pt !important;"><span style="box-sizing: border-box; font-family: "Courier New" !important; font-size: 10pt !important;">Details) VALUES (@Details)</span></span><span style="font-size: 10pt;"> </span><span style="box-sizing: border-box; color: blue; font-family: inherit; font-size: 11pt !important;">WHERE</span><span style="font-size: 10pt;"> FruitId </span><span style="box-sizing: border-box; color: grey; font-family: inherit; font-size: 11pt !important;">=</span><span style="font-size: 10pt;"> @FruitId</span></div>
<div style="box-sizing: border-box; color: #444444; font-family: "Courier New" !important; font-size: 10pt !important;">
<span style="box-sizing: border-box; font-family: "courier new"; font-size: 11pt !important;"><span style="box-sizing: border-box; font-family: "Courier New" !important; font-size: 10pt !important;"><span style="color: blue; font-family: "courier new"; font-size: 14.6667px;"> END</span></span></span></div>
<div style="box-sizing: border-box; color: #444444; font-family: "Courier New" !important; font-size: 10pt !important;">
<span style="box-sizing: border-box; font-size: 10pt !important;"><span style="box-sizing: border-box; color: blue; font-family: "courier new"; font-size: 11pt !important; line-height: 15.6933px;">END</span></span></div>
</div>
<div style="box-sizing: border-box; color: #444444; font-family: "Courier New" !important; font-size: 10pt !important;">
<span style="box-sizing: border-box; font-size: 10pt !important;"><span style="box-sizing: border-box; color: blue; font-family: "courier new"; font-size: 11pt !important; line-height: 15.6933px;"><br /></span></span></div>
Return Output parameter from Stored Procedure in ASP.Net<div>
<br /></div>
<div>
<div style="box-sizing: border-box; color: #444444; font-family: "Courier New" !important; font-size: 10pt !important;">
<span style="box-sizing: border-box; color: blue; font-family: "courier new"; font-size: 9.5pt;">protected</span><span style="box-sizing: border-box; font-family: "courier new"; font-size: 9.5pt;"> <span style="box-sizing: border-box; color: blue; font-family: inherit; font-size: 11pt !important;">void</span> Submit(<span style="box-sizing: border-box; color: blue; font-family: inherit; font-size: 11pt !important;">object</span> sender, <span style="box-sizing: border-box; color: #2b91af; font-family: inherit; font-size: 11pt !important;">EventArgs</span> e)</span></div>
<div style="box-sizing: border-box; color: #444444; font-family: "Courier New" !important; font-size: 10pt !important;">
<span style="box-sizing: border-box; font-family: "courier new"; font-size: 9.5pt;">{</span></div>
<div style="box-sizing: border-box; color: #444444; font-family: "Courier New" !important; font-size: 10pt !important;">
<span style="box-sizing: border-box; font-family: "courier new"; font-size: 9.5pt;"> <span style="box-sizing: border-box; color: blue; font-family: inherit; font-size: 11pt !important;">string</span> constring = <span style="box-sizing: border-box; color: #2b91af; font-family: inherit; font-size: 11pt !important;">ConfigurationManager</span>.ConnectionStrings[<span style="box-sizing: border-box; color: #a31515; font-family: inherit; font-size: 11pt !important;">"constr"</span>].ConnectionString;</span></div>
<div style="box-sizing: border-box; color: #444444; font-family: "Courier New" !important; font-size: 10pt !important;">
<span style="box-sizing: border-box; font-family: "courier new"; font-size: 9.5pt;"> <span style="box-sizing: border-box; color: blue; font-family: inherit; font-size: 11pt !important;">using</span> (<span style="box-sizing: border-box; color: #2b91af; font-family: inherit; font-size: 11pt !important;">SqlConnection</span> con = <span style="box-sizing: border-box; color: blue; font-family: inherit; font-size: 11pt !important;">new</span> <span style="box-sizing: border-box; color: #2b91af; font-family: inherit; font-size: 11pt !important;">SqlConnection</span>(constring))</span></div>
<div style="box-sizing: border-box; color: #444444; font-family: "Courier New" !important; font-size: 10pt !important;">
<span style="box-sizing: border-box; font-family: "courier new"; font-size: 9.5pt;"> {insert</span></div>
<div style="box-sizing: border-box; color: #444444; font-family: "Courier New" !important; font-size: 10pt !important;">
<span style="box-sizing: border-box; font-family: "courier new"; font-size: 9.5pt;"> <span style="box-sizing: border-box; color: blue; font-family: inherit; font-size: 11pt !important;">using</span> (<span style="box-sizing: border-box; color: #2b91af; font-family: inherit; font-size: 11pt !important;">SqlCommand</span> cmd = <span style="box-sizing: border-box; color: blue; font-family: inherit; font-size: 11pt !important;">new</span> <span style="box-sizing: border-box; color: #2b91af; font-family: inherit; font-size: 11pt !important;">SqlCommand</span>(<span style="box-sizing: border-box; color: #a31515; font-family: inherit; font-size: 11pt !important;">"InsertsFruitName"</span>, con))</span></div>
<div style="box-sizing: border-box; color: #444444; font-family: "Courier New" !important; font-size: 10pt !important;">
<span style="box-sizing: border-box; font-family: "courier new"; font-size: 9.5pt;"> {</span></div>
<div style="box-sizing: border-box; color: #444444; font-family: "Courier New" !important; font-size: 10pt !important;">
<span style="box-sizing: border-box; font-family: "courier new"; font-size: 9.5pt;"> cmd.CommandType = <span style="box-sizing: border-box; color: #2b91af; font-family: inherit; font-size: 11pt !important;">CommandType</span>.StoredProcedure;</span></div>
<div style="box-sizing: border-box; color: #444444; font-family: "Courier New" !important; font-size: 10pt !important;">
<span style="box-sizing: border-box; font-family: "courier new"; font-size: 9.5pt;"> cmd.Parameters.AddWithValue(<span style="box-sizing: border-box; color: #a31515; font-family: inherit; font-size: 11pt !important;">"@FruitId"</span>, <span style="box-sizing: border-box; color: blue; font-family: inherit; font-size: 11pt !important;">int</span>.Parse(txtFruitId.Text.Trim()));</span></div>
<div style="box-sizing: border-box; color: #444444; font-family: "Courier New" !important; font-size: 10pt !important;">
<span style="box-sizing: border-box; font-family: "courier new"; font-size: 9.5pt;">cmd.Parameters.AddWithValue(<span style="box-sizing: border-box; color: #a31515; font-family: inherit; font-size: 11pt !important;">"@Details"</span>, <span style="box-sizing: border-box; color: blue; font-family: inherit; font-size: 11pt !important;">int</span>.Parse(txtFruitDetails.Text.Trim()));</span></div>
<div style="box-sizing: border-box; color: #444444; font-family: "Courier New" !important; font-size: 10pt !important;">
<span style="box-sizing: border-box; font-family: "courier new"; font-size: 9.5pt;"> cmd.Parameters.Add(<span style="box-sizing: border-box; color: #a31515; font-family: inherit; font-size: 11pt !important;">"@Exists"</span>, <span style="box-sizing: border-box; color: #2b91af; font-family: inherit; font-size: 11pt !important;">SqlDbType</span>.VarChar, 30);</span></div>
<div style="box-sizing: border-box; color: #444444; font-family: "Courier New" !important; font-size: 10pt !important;">
<span style="box-sizing: border-box; font-family: "courier new"; font-size: 9.5pt;"> cmd.Parameters[<span style="box-sizing: border-box; color: #a31515; font-family: inherit; font-size: 11pt !important;">"@Exists"</span>].Direction = <span style="box-sizing: border-box; color: #2b91af; font-family: inherit; font-size: 11pt !important;">ParameterDirection</span>.Output;</span></div>
<div style="box-sizing: border-box; color: #444444; font-family: "Courier New" !important; font-size: 10pt !important;">
<span style="box-sizing: border-box; font-family: "courier new"; font-size: 9.5pt;"> con.Open();</span></div>
<div style="box-sizing: border-box; color: #444444; font-family: "Courier New" !important; font-size: 10pt !important;">
<span style="box-sizing: border-box; font-family: "courier new"; font-size: 9.5pt;"> cmd.ExecuteNonQuery();</span></div>
<div style="box-sizing: border-box; color: #444444; font-family: "Courier New" !important; font-size: 10pt !important;">
<span style="box-sizing: border-box; font-family: "courier new"; font-size: 9.5pt;"> con.Close();</span></div>
<div style="box-sizing: border-box; color: #444444;">
<span style="font-family: courier new;"><span style="font-size: 12.6667px;">IF(</span></span><span style="font-family: "courier new"; font-size: 12.6667px;">cmd.Parameters[</span><span style="box-sizing: border-box; color: #a31515; font-family: inherit; font-size: 11pt !important;">"@Exists"</span><span style="font-family: "courier new"; font-size: 12.6667px;">].Value</span><span style="font-family: "courier new"; font-size: 12.6667px;">.ToString()</span><span style="font-family: "courier new"; font-size: 12.6667px;"> == ""</span><span style="font-family: "courier new"; font-size: 12.6667px;">)</span></div>
<div style="box-sizing: border-box; color: #444444;">
<span style="font-family: "courier new"; font-size: 12.6667px;">{</span></div>
<div style="box-sizing: border-box; color: #444444; font-family: "Courier New" !important; font-size: 10pt !important;">
<span style="box-sizing: border-box; font-family: "courier new"; font-size: 9.5pt;"> lblFruitName.Text = <span style="box-sizing: border-box; color: #a31515; font-family: inherit; font-size: 11pt !important;">"Fruit Details Added Successfully."';</span></span></div>
<div style="box-sizing: border-box; color: #444444; font-family: "Courier New" !important; font-size: 10pt !important;">
<span style="box-sizing: border-box; font-family: "courier new"; font-size: 9.5pt;">}</span></div>
<div style="box-sizing: border-box; color: #444444; font-family: "Courier New" !important; font-size: 10pt !important;">
<span style="box-sizing: border-box; font-family: "courier new"; font-size: 9.5pt;">else</span></div>
<div style="box-sizing: border-box; color: #444444; font-family: "Courier New" !important; font-size: 10pt !important;">
<span style="box-sizing: border-box; font-family: "courier new"; font-size: 9.5pt;">{</span></div>
<div style="box-sizing: border-box; color: #444444; font-family: "Courier New" !important; font-size: 10pt !important;">
<span style="box-sizing: border-box; font-family: "courier new"; font-size: 9.5pt;"> lblFruitName.Text = "Fruits Details Already Exists";</span></div>
<div style="box-sizing: border-box; color: #444444;">
<span style="font-family: courier new;"><span style="font-size: 12.6667px;">}</span></span></div>
<div style="box-sizing: border-box; color: #444444; font-family: "Courier New" !important; font-size: 10pt !important;">
<span style="box-sizing: border-box; font-family: "courier new"; font-size: 9.5pt;"> }</span></div>
<div style="box-sizing: border-box; color: #444444; font-family: "Courier New" !important; font-size: 10pt !important;">
<span style="box-sizing: border-box; font-family: "courier new"; font-size: 9.5pt;"> }</span></div>
<div style="box-sizing: border-box; color: #444444; font-family: "Courier New" !important; font-size: 10pt !important;">
<span style="box-sizing: border-box; font-family: "courier new"; font-size: 9.5pt;">}</span></div>
</div>
<div style="box-sizing: border-box; color: #444444; font-family: "Courier New" !important; font-size: 10pt !important;">
<span style="box-sizing: border-box; font-family: "courier new"; font-size: 9.5pt;"><br /></span></div>
<div style="box-sizing: border-box; color: #444444; font-family: "Courier New" !important; font-size: 10pt !important;">
<span style="box-sizing: border-box; font-family: "courier new"; font-size: 9.5pt;"><br /></span></div>
<div>
<br /></div>
</div>
योगेश चन्द्र उप्रेतीhttp://www.blogger.com/profile/17009545633002694199noreply@blogger.com1tag:blogger.com,1999:blog-8545307170893600943.post-8583809671284584102018-10-29T22:57:00.003-07:002018-10-29T22:57:33.381-07:00Converting String in to CSV files in C Sharp<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<br />
<br />
<br />
<br />
<br />
Function For Creating CSV files in C#<br />
<br />
public static string FormatCSV(string input)<br />
{<br />
try<br />
{<br />
if (input == null)<br />
return string.Empty;<br />
<br />
bool containsQuote = false;<br />
bool containsComma = false;<br />
int len = input.Length;<br />
for (int i = 0; i < len && (containsComma == false || containsQuote == false); i++)<br />
{<br />
char ch = input[i];<br />
if (ch == '"')<br />
containsQuote = true;<br />
else if (ch == ',')<br />
containsComma = true;<br />
}<br />
<br />
if (containsQuote && containsComma)<br />
input = input.Replace("\"", "\"\"");<br />
<br />
if (containsComma)<br />
return "\"" + input + "\"";<br />
else<br />
return input;<br />
}<br />
catch<br />
{<br />
throw;<br />
}<br />
}</div>
योगेश चन्द्र उप्रेतीhttp://www.blogger.com/profile/17009545633002694199noreply@blogger.com0tag:blogger.com,1999:blog-8545307170893600943.post-12917704713920587062017-11-16T22:47:00.001-08:002017-11-16T22:47:07.550-08:00How to split comma separated value in sql server<div dir="ltr" style="text-align: left;" trbidi="on">
<h3 style="text-align: left;">
How to split comma separated value in SQL server :</h3>
----- Approach 1: Common Table Expression (CTE) ----- Lets call this function as Split2. here we are using<br />
CREATE FUNCTION dbo.Split2 ( @strString varchar(4000))<br />
<span style="white-space: pre;"> </span>RETURNS @Result TABLE(Value BIGINT)<br />
AS<br />
begin<br />
WITH StrCTE(start, stop) AS<br />
(<br />
SELECT 1, CHARINDEX(',' , @strString )<br />
UNION ALL<br />
SELECT stop + 1, CHARINDEX(',' ,@strString , stop + 1)<br />
FROM StrCTE<br />
WHERE stop > 0<br />
) <br />
insert into @Result<br />
SELECT SUBSTRING(@strString , start, CASE WHEN stop > 0 THEN stop-start ELSE 4000 END) AS stringValue<br />
FROM StrCTE<span style="white-space: pre;"> </span> <br />
return<br />
end<br />
GO<br />
<br />
----- Approach 2: XML (surprise) ----- XML could be applied to do some type of string parsing (see this) Let’s call this function as Split3.<br />
CREATE FUNCTION dbo.Split3 ( @strString varchar(4000))<br />
<span style="white-space: pre;"> </span>RETURNS @Result TABLE(Value BIGINT)<br />
AS<br />
BEGIN<br />
DECLARE @x XML<br />
SELECT @x = CAST('<a href="https://www.blogger.com/null">'+ REPLACE(@strString,',','</a><a href="https://www.blogger.com/null">')+ '</a>' AS XML)<span style="white-space: pre;"> </span> <br />
INSERT INTO @Result <br />
SELECT t.value('.', 'int') AS inVal<br />
FROM @x.nodes('/A') AS x(t)<br />
RETURN<br />
END <br />
GO<br />
<br />
----- Approach 3: Classic TSQL Way ----- This approach is slightly unusual but very effective. this needs you to create a table of sequential numbers called a Tally Table.<br />
SELECT TOP 11000 --equates to more than 30 years of dates <br />
IDENTITY(INT,1,1) AS N <br />
INTO dbo.Tally <br />
FROM Master.dbo.SysColumns sc1, <br />
Master.dbo.SysColumns sc2<br />
<br />
----- Lets index the table for better performance. -----<br />
ALTER TABLE dbo.Tally <br />
ADD CONSTRAINT PK_Tally_N PRIMARY KEY CLUSTERED (N)<br />
WITH FILLFACTOR = 100<br />
<br />
----- Finally out Split4 function. -----<br />
CREATE FUNCTION dbo.Split4 ( @strString varchar(4000))<br />
<span style="white-space: pre;"> </span>RETURNS @Result TABLE(Value BIGINT)<br />
AS<br />
BEGIN<br />
SET @strString = ','+@strString +','<br />
INSERT INTO @t (Value)<br />
SELECT SUBSTRING(@strString,N+1,CHARINDEX(',',@strString,N+1)-N-1) <br />
FROM dbo.Tally<br />
WHERE N < LEN(@strString) <br />
AND SUBSTRING(@strString,N,1) = ',' --Notice how we find the comma<br />
RETURN<br />
END <br />
GO<br />
<br />
----- so I changed the code slightly to test out XML function directly and copied data ia a temporary table -----<br />
--declare @str varchar(4000) = '1,2,3,4,5,6'<br />
declare @str varchar(4000) = 'A,B,C,D,E,F'<br />
declare @xml XML<br />
select @xml = cast('<a href="https://www.blogger.com/null">'+REPLACE(@str,',','</a><a href="https://www.blogger.com/null">')+'</a>' as XML)<br />
select * into tempTable from (select t.value('.', 'VARCHAR') as inVal <span style="white-space: pre;"> </span>from @xml.nodes('/A') as x(t)) as tbl<br />
select * from tempTable<br />
<br />
----- so I changed the code slightly to test out XML function directly and copied data using Table Variable Parameter -----<br />
--declare @str varchar(4000) = '1,2,3,4,5,6'<br />
declare @temp table(VALUE nvarchar(100))<br />
declare @str varchar(4000) = 'A,B,C,D,E,F'<br />
declare @xml XML<br />
select @xml = cast('<a href="https://www.blogger.com/null">'+REPLACE(@str,',','</a><a href="https://www.blogger.com/null">')+'</a>' as XML)<br />
insert into @temp select t.value('.', 'VARCHAR') as inVal <span style="white-space: pre;"> </span>from @xml.nodes('/A') as x(t)<br />
select * from @temp<br />
<br /></div>
योगेश चन्द्र उप्रेतीhttp://www.blogger.com/profile/17009545633002694199noreply@blogger.com2