tag:blogger.com,1999:blog-348626244642912382024-03-07T09:05:56.140+05:30CodeFariCodefarihttp://www.blogger.com/profile/14275737213961436106noreply@blogger.comBlogger336125tag:blogger.com,1999:blog-34862624464291238.post-3958609632282897652023-08-02T23:20:00.001+05:302023-08-02T23:20:19.978+05:30What is the Use of isNaN Function in JavaScript? A Comprehensive Explanation for Effective Input Validation<p><span style="font-family: Verdana, "sans-serif"; font-size: 11.5pt;">In the world of JavaScript, input validation is a
critical aspect of ensuring that user-provided data is processed correctly. One
indispensable tool for this purpose is the <b>`isNaN()` </b>function. This built-in
function plays a vital role in determining whether a value is a valid number or
not. In this interview answer, we will explore the significance of the
`isNaN()` function, its syntax, practical examples, and its role in ensuring
error-free and efficient code execution.</span></p>
<h3 style="text-align: left;">Q1. Can you explain what the `isNaN()` function is used for in JavaScript?</h3>
<p class="MsoNormal"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;">Certainly! The `isNaN()` function in JavaScript is
designed to identify whether a given value is "Not a Number" (NaN) or
can be converted into a valid number. It returns a Boolean value, true if the
input is not a number, and false if the input is a valid number or can be
converted into one.</span></p>
<h3 style="text-align: left;">Q2. How does the `isNaN()` function work?</h3>
<p class="MsoNormal"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;">A: The `isNaN()` function internally tries to convert
the provided value into a number. If the conversion results in NaN, it
indicates that the input is not a valid number. Conversely, if the conversion
is successful and yields a valid number, the function returns false.</span><span style="font-family: Verdana, "sans-serif"; font-size: 11.5pt;"> </span></p>
<h3 style="text-align: left;">Q3. What are some practical examples of using the `isNaN()` function?</h3>
<p class="MsoNormal"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;">A: Let's take a look at some examples to understand its
usage better:<o:p></o:p></span></p>
<p class="MsoNormal"><b style="mso-bidi-font-weight: normal;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;">1.
Valid Number:</span></b><b><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;"><o:p> </o:p></span></b></p>
<table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="background: black; border-collapse: collapse; border: none; mso-border-alt: double windowtext 1.5pt; mso-border-insideh: 1.5pt double windowtext; mso-border-insidev: 1.5pt double windowtext; mso-padding-alt: 0in 5.4pt 0in 5.4pt; mso-pattern: solid auto; mso-shading-themecolor: text1; mso-shading-themetint: 242; mso-shading: #0D0D0D; mso-yfti-tbllook: 1184;">
<tbody><tr style="mso-yfti-firstrow: yes; mso-yfti-irow: 0; mso-yfti-lastrow: yes;">
<td style="border: double windowtext 1.5pt; padding: 0in 5.4pt 0in 5.4pt; width: 6.65in;" valign="top" width="638">
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #df3079; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";"><o:p> </o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #df3079; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">console</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">.</span><span style="color: #f22c3d; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">log</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">(</span><span style="color: #e9950c; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">isNaN</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">(</span><span style="color: #df3079; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">42</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">));<span style="mso-spacerun: yes;"> </span></span><span style="font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">// Output: false (42 is a valid number)</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #df3079; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">console</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">.</span><span style="color: #f22c3d; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">log</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">(</span><span style="color: #e9950c; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">isNaN</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">(</span><span style="color: #df3079; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">3.14</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">));<span style="mso-spacerun: yes;"> </span></span><span style="font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">// Output: false (3.14 is a valid number)</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #df3079; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">console</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">.</span><span style="color: #f22c3d; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">log</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">(</span><span style="color: #e9950c; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">isNaN</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">(-</span><span style="color: #df3079; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">10</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">));<span style="mso-spacerun: yes;"> </span></span><span style="font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">// Output: false (-10 is a valid number)<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 12.0pt;"><o:p> </o:p></span></p>
</td>
</tr>
</tbody></table>
<p class="MsoNormal"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></p>
<p class="MsoNormal"><b style="mso-bidi-font-weight: normal;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;">2.
Invalid Number (NaN):<o:p></o:p></span></b></p>
<table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="background: black; border-collapse: collapse; border: none; mso-border-alt: double windowtext 1.5pt; mso-border-insideh: 1.5pt double windowtext; mso-border-insidev: 1.5pt double windowtext; mso-padding-alt: 0in 5.4pt 0in 5.4pt; mso-pattern: solid auto; mso-shading-themecolor: text1; mso-shading-themetint: 242; mso-shading: #0D0D0D; mso-yfti-tbllook: 1184;">
<tbody><tr style="mso-yfti-firstrow: yes; mso-yfti-irow: 0; mso-yfti-lastrow: yes;">
<td style="border: double windowtext 1.5pt; padding: 0in 5.4pt 0in 5.4pt; width: 6.65in;" valign="top" width="638">
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #2e95d3; font-family: "Courier New"; font-size: 12.0pt; mso-fareast-font-family: "Times New Roman";"><o:p> </o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #df3079; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">console</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">.</span><span style="color: #f22c3d; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">log</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">(</span><span style="color: #e9950c; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">isNaN</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">(</span><span style="color: #00a67d; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">"Hello"</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">)); </span><span style="font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">// Output: true ("Hello"
cannot be converted into a number)<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";"><o:p> </o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #df3079; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">console</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">.</span><span style="color: #f22c3d; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">log</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">(</span><span style="color: #e9950c; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">isNaN</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">(</span><span style="color: #f22c3d; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">NaN</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">));<span style="mso-spacerun: yes;"> </span></span><span style="font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">// Output: true (NaN is not a valid number)</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #df3079; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";"><o:p> </o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #df3079; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">console</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">.</span><span style="color: #f22c3d; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">log</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">(</span><span style="color: #e9950c; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">isNaN</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">(</span><span style="color: #2e95d3; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">undefined</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">)); </span><span style="font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">// Output: true (undefined cannot
be converted into a number)<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 12.0pt;"><o:p> </o:p></span></p>
</td>
</tr>
</tbody></table>
<p class="MsoNormal"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></p>
<p class="MsoNormal"><b style="mso-bidi-font-weight: normal;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;">3.
Conversion:</span></b><span style="font-family: Verdana, "sans-serif"; font-size: 11.5pt;"> </span></p>
<table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="background: black; border-collapse: collapse; border: none; mso-border-alt: double windowtext 1.5pt; mso-border-insideh: 1.5pt double windowtext; mso-border-insidev: 1.5pt double windowtext; mso-padding-alt: 0in 5.4pt 0in 5.4pt; mso-pattern: solid auto; mso-shading-themecolor: text1; mso-shading-themetint: 242; mso-shading: #0D0D0D; mso-yfti-tbllook: 1184;">
<tbody><tr style="mso-yfti-firstrow: yes; mso-yfti-irow: 0; mso-yfti-lastrow: yes;">
<td style="border: double windowtext 1.5pt; padding: 0in 5.4pt 0in 5.4pt; width: 6.65in;" valign="top" width="638">
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #2e95d3; font-family: "Courier New"; font-size: 12.0pt; mso-fareast-font-family: "Times New Roman";"><o:p> </o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #df3079; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">console</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">.</span><span style="color: #f22c3d; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">log</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">(</span><span style="color: #e9950c; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">isNaN</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">(</span><span style="color: #00a67d; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">"42"</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">));<span style="mso-spacerun: yes;"> </span></span><span style="font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">// Output: false (The string "42" can be
converted into the number 42)</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #df3079; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";"><o:p> </o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #df3079; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">console</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">.</span><span style="color: #f22c3d; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">log</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">(</span><span style="color: #e9950c; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">isNaN</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">(</span><span style="color: #00a67d; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">"3.14"</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">));<span style="mso-spacerun: yes;"> </span></span><span style="font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">// Output: false (The string "3.14" can be
converted into the number 3.14)</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #df3079; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";"><o:p> </o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #df3079; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">console</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">.</span><span style="color: #f22c3d; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">log</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">(</span><span style="color: #e9950c; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">isNaN</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">(</span><span style="color: #00a67d; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">"ABC"</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">));<span style="mso-spacerun: yes;"> </span></span><span style="font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">// Output: true ("ABC" cannot be converted into
a number)<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 12.0pt;"><o:p> </o:p></span></p>
</td>
</tr>
</tbody></table>
<p class="MsoNormal"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;"><o:p> </o:p></span></p>
<h3 style="text-align: left;">Q4. What is the importance of the `isNaN()` function in JavaScript?</h3>
<p class="MsoNormal"><span style="font-family: Verdana, "sans-serif"; font-size: 11.5pt;">The `isNaN()` function is particularly important for
input validation when dealing with user-provided data. When users submit data
through forms or other input methods, it is essential to verify whether the
data is a valid number before performing any calculations or operations that
require numeric inputs. Using `isNaN()` helps ensure that the provided data is
appropriate for processing, reducing the risk of errors and unexpected behavior
in your JavaScript code.</span></p>
<p class="MsoNormal"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;"><o:p> </o:p></span></p>
<h3 style="text-align: left;">Q5. Can you provide an example of how the `isNaN()` function can be used
for input validation? </h3>
<p class="MsoNormal"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;">Certainly! Let's say we have a simple form where a user
enters their age, and we want to validate if the entered value is a valid
number.<o:p></o:p></span></p>
<p class="MsoNormal"><b style="mso-bidi-font-weight: normal;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;"><o:p> </o:p></span></b></p>
<table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="background: black; border-collapse: collapse; border: none; mso-border-alt: double windowtext 1.5pt; mso-border-insideh: 1.5pt double windowtext; mso-border-insidev: 1.5pt double windowtext; mso-padding-alt: 0in 5.4pt 0in 5.4pt; mso-pattern: solid auto; mso-shading-themecolor: text1; mso-shading-themetint: 242; mso-shading: #0D0D0D; mso-yfti-tbllook: 1184;">
<tbody><tr style="mso-yfti-firstrow: yes; mso-yfti-irow: 0; mso-yfti-lastrow: yes;">
<td style="border: double windowtext 1.5pt; padding: 0in 5.4pt 0in 5.4pt; width: 6.65in;" valign="top" width="638">
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #2e95d3; font-family: "Courier New"; font-size: 12.0pt; mso-fareast-font-family: "Times New Roman";"><o:p> </o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: white; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";"><input
</span><span style="color: #df3079; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">type</span><span style="color: white; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">=</span><span style="color: #00a67d; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">"text"</span><span style="color: white; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";"> </span><span style="color: #df3079; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">id</span><span style="color: white; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">=</span><span style="color: #00a67d; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">"ageInput"</span><span style="color: white; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";"> </span><span style="color: #df3079; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">placeholder</span><span style="color: white; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">=</span><span style="color: #00a67d; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">"Enter your
age"</span><span style="color: white; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">></span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: white; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";"><button
</span><span style="color: #df3079; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">onclick</span><span style="color: white; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">=</span><span style="color: #00a67d; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">"validateAge()"</span><span style="color: white; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">></span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">Submit</span><span style="color: white; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";"></button><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: white; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";"><o:p> </o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: white; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">//Javascript
code<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #2e95d3; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";"><o:p> </o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #2e95d3; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">function</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";"> </span><span style="color: #f22c3d; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">validateAge</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">() {<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2e95d3; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">let</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";"> ageInput = </span><span style="color: #df3079; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">document</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">.</span><span style="color: #f22c3d; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">getElementById</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">(</span><span style="color: #00a67d; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">"ageInput"</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">).</span><span style="color: white; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">value</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">;<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2e95d3; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">if</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";"> (</span><span style="color: #e9950c; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">isNaN</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">(ageInput)) {<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span></span><span style="color: #f22c3d; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">alert</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">(</span><span style="color: #00a67d; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">"Please enter a valid age."</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">);<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span>} </span><span style="color: #2e95d3; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">else</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";"> {<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span></span><span style="font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">// Perform calculations or operations with the valid
ageInput</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span></span><span style="font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">// ...</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="background: black; color: white; font-family: "Courier New"; font-size: 10.5pt; mso-fareast-font-family: "Times New Roman";">}</span><span style="color: white; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 12.0pt;"><o:p> </o:p></span></p>
</td>
</tr>
</tbody></table>
<p class="MsoNormal"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;">In this example, the `isNaN()` function checks if the
user's input is a valid number. If the input is not a number (e.g., contains
characters), an alert is displayed asking the user to enter a valid age.</span><span style="font-family: Verdana, "sans-serif"; font-size: 11.5pt;"> </span></p>
<h4 style="text-align: left;">Conclusion: </h4>
<p class="MsoNormal"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;">The <b>`isNaN()`</b> function is a valuable tool in
JavaScript, empowering developers to ensure effective input validation when
dealing with user-provided data. By distinguishing valid numbers from invalid
ones, it enables developers to create error-free and efficient code, especially
when processing numeric values. Embracing the <b>`isNaN()`</b> function in your
JavaScript projects contributes to better user experiences, enhanced data
accuracy, and overall code reliability. So, leverage the power of <b>`isNaN()`</b> for
seamless input validation and make your web applications even more robust and
user-friendly!<o:p></o:p></span></p>Codefarihttp://www.blogger.com/profile/14275737213961436106noreply@blogger.com0tag:blogger.com,1999:blog-34862624464291238.post-61965977793162026332023-08-02T23:00:00.008+05:302023-08-02T23:02:39.968+05:30What are JavaScript Data Types? A Beginner's Guide<p class="MsoNormal"><b><span style="color: #1f1f1f; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";">Interviewer: Can you explain what data types are in JavaScript?</span></b></p>
<p class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .25in; margin-left: 0in; margin-right: 0in; margin-top: .25in;"><b><span style="color: #1f1f1f; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";">Interviewee</span></b><span style="color: #1f1f1f; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";">: Sure. Data types are the different ways that JavaScript
can represent data. There are eight different data types in JavaScript:<o:p></o:p></span></p>
<p class="MsoNormal" style="background: white; line-height: normal; margin-bottom: 7.5pt; mso-margin-top-alt: auto;"><b><span style="color: #1f1f1f; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";">String:</span></b><span style="color: #1f1f1f; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";"> A string is a
sequence of characters. Strings are enclosed in quotes, either single or
double. For example, "Hello, world!" is a string.<o:p></o:p></span></p>
<p class="MsoNormal"><b><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;">Example:<o:p></o:p></span></b></p>
<table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="background: black; border-collapse: collapse; border: none; mso-border-alt: double windowtext 1.5pt; mso-border-insideh: 1.5pt double windowtext; mso-border-insidev: 1.5pt double windowtext; mso-padding-alt: 0in 5.4pt 0in 5.4pt; mso-pattern: solid auto; mso-shading-themecolor: text1; mso-shading-themetint: 242; mso-shading: #0D0D0D; mso-yfti-tbllook: 1184;">
<tbody><tr>
<td style="border: double windowtext 1.5pt; padding: 0in 5.4pt 0in 5.4pt; width: 6.65in;" valign="top" width="638">
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #2e95d3; font-family: "Courier New"; font-size: 12.0pt; mso-fareast-font-family: "Times New Roman";"> </span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #2e95d3; font-family: "Courier New"; font-size: 12.0pt; mso-fareast-font-family: "Times New Roman";">let</span><span style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: white; font-family: "Courier New"; font-size: 12pt;"> name = </span><span style="color: #00a67d; font-family: "Courier New"; font-size: 12.0pt; mso-fareast-font-family: "Times New Roman";">"John Doe"</span><span style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: white; font-family: "Courier New"; font-size: 12pt;">;<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #2e95d3; font-family: "Courier New"; font-size: 12.0pt; mso-fareast-font-family: "Times New Roman";">let</span><span style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: white; font-family: "Courier New"; font-size: 12pt;"> message = </span><span style="color: #00a67d; font-family: "Courier New"; font-size: 12.0pt; mso-fareast-font-family: "Times New Roman";">'Welcome to our website!'</span><span style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: white; font-family: "Courier New"; font-size: 12pt;">;<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 12.0pt;"> </span></p>
</td>
</tr>
</tbody></table>
<p class="MsoNormal"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;"> </span></p>
<p class="MsoNormal" style="background: white; line-height: normal; margin-bottom: 7.5pt; mso-margin-top-alt: auto;"><b><span style="color: #1f1f1f; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";">Number:</span></b><span style="color: #1f1f1f; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";"> A number is a
numerical value. Numbers can be integers (whole numbers) or floating-point
numbers (numbers with decimal points). For example, 123 is an integer, and 3.14
is a floating-point number.<o:p></o:p></span></p>
<p class="MsoNormal"><b><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;">Example:<o:p></o:p></span></b></p>
<table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="background: black; border-collapse: collapse; border: none; mso-border-alt: double windowtext 1.5pt; mso-border-insideh: 1.5pt double windowtext; mso-border-insidev: 1.5pt double windowtext; mso-padding-alt: 0in 5.4pt 0in 5.4pt; mso-pattern: solid auto; mso-shading-themecolor: text1; mso-shading-themetint: 242; mso-shading: #0D0D0D; mso-yfti-tbllook: 1184;">
<tbody><tr>
<td style="border: double windowtext 1.5pt; padding: 0in 5.4pt 0in 5.4pt; width: 6.65in;" valign="top" width="638">
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #2e95d3; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";"> </span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #2e95d3; font-family: "Courier New"; font-size: 12.0pt; mso-fareast-font-family: "Times New Roman";">let</span><span style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: white; font-family: "Courier New"; font-size: 12pt;"> age = </span><span style="color: #df3079; font-family: "Courier New"; font-size: 12.0pt; mso-fareast-font-family: "Times New Roman";">25</span><span style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: white; font-family: "Courier New"; font-size: 12pt;">;<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #2e95d3; font-family: "Courier New"; font-size: 12.0pt; mso-fareast-font-family: "Times New Roman";">let</span><span style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: white; font-family: "Courier New"; font-size: 12pt;"> price = </span><span style="color: #df3079; font-family: "Courier New"; font-size: 12.0pt; mso-fareast-font-family: "Times New Roman";">9.99</span><span style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: white; font-family: "Courier New"; font-size: 12pt;">;<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt;"> </span></p>
</td>
</tr>
</tbody></table>
<p class="MsoNormal"><b><span style="color: #1f1f1f; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";">BigInt:</span></b><span style="color: #1f1f1f; font-family: Verdana, "sans-serif"; font-size: 11.5pt;"> A BigInt is a
special type of number that can represent very large integers. BigInts were
introduced in JavaScript ES2020.</span></p>
<p class="MsoNormal" style="background: white; line-height: normal; margin-bottom: 7.5pt; mso-margin-top-alt: auto;"><b><span style="color: #1f1f1f; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";">Boolean:</span></b><span style="color: #1f1f1f; font-family: Verdana, "sans-serif"; font-size: 11.5pt;"> A boolean is
a value that can be either true or false. Boolean values are often used to
represent the state of something, such as whether a button is checked or not.</span></p>
<p class="MsoNormal"><b><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;">Example:
<o:p></o:p></span></b></p>
<table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="background: black; border-collapse: collapse; border: none; mso-border-alt: double windowtext 1.5pt; mso-border-insideh: 1.5pt double windowtext; mso-border-insidev: 1.5pt double windowtext; mso-padding-alt: 0in 5.4pt 0in 5.4pt; mso-pattern: solid auto; mso-shading-themecolor: text1; mso-shading-themetint: 242; mso-shading: #0D0D0D; mso-yfti-tbllook: 1184;">
<tbody><tr>
<td style="border: double windowtext 1.5pt; padding: 0in 5.4pt 0in 5.4pt; width: 6.65in;" valign="top" width="638">
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #2e95d3; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";"> </span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #2e95d3; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">let</span><span style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: white; font-family: "Courier New"; font-size: 10.5pt;"> isStudent = </span><span style="color: #2e95d3; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">true</span><span style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: white; font-family: "Courier New"; font-size: 10.5pt;">;<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #2e95d3; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">let</span><span style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: white; font-family: "Courier New"; font-size: 10.5pt;"> isLoggedOut = </span><span style="color: #2e95d3; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";">false</span><span style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: white; font-family: "Courier New"; font-size: 10.5pt;">;<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 12.0pt;"> </span></p>
</td>
</tr>
</tbody></table>
<p class="MsoNormal" style="background: white; line-height: normal; margin-bottom: 7.5pt; mso-margin-top-alt: auto;"><b><span style="color: #1f1f1f; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";">Undefined:</span></b><span style="color: #1f1f1f; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";"> The undefined
value is used to represent a value that has not been assigned yet. For example,
if you create a variable and then don't assign it a value, the variable will be
undefined.<o:p></o:p></span></p>
<p class="MsoNormal"><b><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;">Example:
<o:p></o:p></span></b></p>
<table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="background: black; border-collapse: collapse; border: none; mso-border-alt: double windowtext 1.5pt; mso-border-insideh: 1.5pt double windowtext; mso-border-insidev: 1.5pt double windowtext; mso-padding-alt: 0in 5.4pt 0in 5.4pt; mso-pattern: solid auto; mso-shading-themecolor: text1; mso-shading-themetint: 242; mso-shading: #0D0D0D; mso-yfti-tbllook: 1184;">
<tbody><tr>
<td style="border: double windowtext 1.5pt; padding: 0in 5.4pt 0in 5.4pt; width: 6.65in;" valign="top" width="638">
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #2e95d3; font-family: "Courier New"; font-size: 12.0pt; mso-fareast-font-family: "Times New Roman";"> </span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #2e95d3; font-family: "Courier New"; font-size: 12.0pt; mso-fareast-font-family: "Times New Roman";">let</span><span style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: white; font-family: "Courier New"; font-size: 12pt;"> address;<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #df3079; font-family: "Courier New"; font-size: 12.0pt; mso-fareast-font-family: "Times New Roman";">console</span><span style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: white; font-family: "Courier New"; font-size: 12pt;">.</span><span style="color: #f22c3d; font-family: "Courier New"; font-size: 12.0pt; mso-fareast-font-family: "Times New Roman";">log</span><span style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: white; font-family: "Courier New"; font-size: 12pt;">(address);
</span><span style="font-family: "Courier New"; font-size: 12.0pt; mso-fareast-font-family: "Times New Roman";">// Output: undefined<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 12.0pt;"> </span></p>
</td>
</tr>
</tbody></table>
<p class="MsoNormal"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;"> <o:p></o:p></span></p>
<p class="MsoNormal"><b><span style="color: #1f1f1f; font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";">Null:</span></b><span style="color: #1f1f1f; font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";"> The null value is used to
represent a value that is intentionally set to nothing. For example, if you
want to create a variable that represents a missing value, you can set it to
null.<o:p></o:p></span></p>
<p class="MsoNormal"><b><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;">Example:
<o:p></o:p></span></b></p>
<table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="background: black; border-collapse: collapse; border: none; mso-border-alt: double windowtext 1.5pt; mso-border-insideh: 1.5pt double windowtext; mso-border-insidev: 1.5pt double windowtext; mso-padding-alt: 0in 5.4pt 0in 5.4pt; mso-pattern: solid auto; mso-shading-themecolor: text1; mso-shading-themetint: 242; mso-shading: #0D0D0D; mso-yfti-tbllook: 1184;">
<tbody><tr>
<td style="border: double windowtext 1.5pt; padding: 0in 5.4pt 0in 5.4pt; width: 6.65in;" valign="top" width="638">
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #2e95d3; font-family: "Courier New"; font-size: 12.0pt; mso-fareast-font-family: "Times New Roman";"> </span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span class="hljs-keyword"><span style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 1pt solid rgb(217, 217, 227); color: #2e95d3; font-family: "Courier New"; font-size: 12pt; padding: 0in;">let</span></span><span style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: white; font-family: "Courier New"; font-size: 12pt;"> userID = </span><span class="hljs-literal"><span style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 1pt solid rgb(217, 217, 227); color: #2e95d3; font-family: "Courier New"; font-size: 12pt; padding: 0in;"><span style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; box-sizing: border-box;">null</span></span></span><span style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: white; font-family: "Courier New"; font-size: 12pt;">;<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 12.0pt;"> </span></p>
</td>
</tr>
</tbody></table>
<p class="MsoNormal" style="background: white; line-height: normal; margin-bottom: 7.5pt; mso-margin-top-alt: auto;"><b><span style="color: #1f1f1f; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";">Symbol:</span></b><span style="color: #1f1f1f; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";"> A symbol is a
special type of value that is used to represent unique identifiers. Symbols
were introduced in JavaScript ES6.<o:p></o:p></span></p>
<p class="MsoNormal"><b><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;">Example:
<o:p></o:p></span></b></p>
<table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="background: black; border-collapse: collapse; border: none; mso-border-alt: double windowtext 1.5pt; mso-border-insideh: 1.5pt double windowtext; mso-border-insidev: 1.5pt double windowtext; mso-padding-alt: 0in 5.4pt 0in 5.4pt; mso-pattern: solid auto; mso-shading-themecolor: text1; mso-shading-themetint: 242; mso-shading: #0D0D0D; mso-yfti-tbllook: 1184;">
<tbody><tr>
<td style="border: double windowtext 1.5pt; padding: 0in 5.4pt 0in 5.4pt; width: 6.65in;" valign="top" width="638">
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #2e95d3; font-family: "Courier New"; font-size: 12.0pt; mso-fareast-font-family: "Times New Roman";"> </span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span class="hljs-keyword"><span style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 1pt solid rgb(217, 217, 227); color: #2e95d3; font-family: "Courier New"; font-size: 12pt; padding: 0in;">const</span></span><span style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: white; font-family: "Courier New"; font-size: 12pt;"> sym = </span><span class="hljs-title"><span style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 1pt solid rgb(217, 217, 227); color: #f22c3d; font-family: "Courier New"; font-size: 12pt; padding: 0in;"><span style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; box-sizing: border-box;">Symbol</span></span></span><span style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: white; font-family: "Courier New"; font-size: 12pt;">(</span><span class="hljs-string"><span style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 1pt solid rgb(217, 217, 227); color: #00a67d; font-family: "Courier New"; font-size: 12pt; padding: 0in;"><span style="--tw-border-spacing-x: 0; --tw-border-spacing-y: 0; --tw-ring-color: rgba(69,89,164,.5); --tw-ring-offset-color: #fff; --tw-ring-offset-shadow: 0 0 transparent; --tw-ring-offset-width: 0px; --tw-ring-shadow: 0 0 transparent; --tw-rotate: 0; --tw-scale-x: 1; --tw-scale-y: 1; --tw-scroll-snap-strictness: proximity; --tw-shadow-colored: 0 0 transparent; --tw-shadow: 0 0 transparent; --tw-skew-x: 0; --tw-skew-y: 0; --tw-translate-x: 0; --tw-translate-y: 0; box-sizing: border-box;">'uniqueSymbol'</span></span></span><span style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: white; font-family: "Courier New"; font-size: 12pt;">);<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 12.0pt;"> </span></p>
</td>
</tr>
</tbody></table>
<p class="MsoNormal" style="background: white; line-height: normal; margin-bottom: 7.5pt; mso-margin-top-alt: auto;"><b><span style="color: #1f1f1f; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";">Object:</span></b><span style="color: #1f1f1f; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";"> An object is
a collection of data and methods. Objects are the most complex data type in
JavaScript, and they are used to represent real-world entities such as cars,
people, and businesses.<o:p></o:p></span></p>
<p class="MsoNormal"><b><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;">Example:
<o:p></o:p></span></b></p>
<table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="background: black; border-collapse: collapse; border: none; mso-border-alt: double windowtext 1.5pt; mso-border-insideh: 1.5pt double windowtext; mso-border-insidev: 1.5pt double windowtext; mso-padding-alt: 0in 5.4pt 0in 5.4pt; mso-pattern: solid auto; mso-shading-themecolor: text1; mso-shading-themetint: 242; mso-shading: #0D0D0D; mso-yfti-tbllook: 1184;">
<tbody><tr>
<td style="border: double windowtext 1.5pt; padding: 0in 5.4pt 0in 5.4pt; width: 6.65in;" valign="top" width="638">
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #2e95d3; font-family: "Courier New"; font-size: 12.0pt; mso-fareast-font-family: "Times New Roman";"> </span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span class="hljs-keyword"><span style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 1pt solid rgb(217, 217, 227); color: #2e95d3; font-family: "Courier New"; font-size: 12pt; padding: 0in;">Var obj
= {<o:p></o:p></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span class="hljs-keyword"><span style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 1pt solid rgb(217, 217, 227); color: #2e95d3; font-family: "Courier New"; font-size: 12pt; padding: 0in;">“Name”:”Dilip”,<o:p></o:p></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span class="hljs-keyword"><span style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 1pt solid rgb(217, 217, 227); color: #2e95d3; font-family: "Courier New"; font-size: 12pt; padding: 0in;">“Age”:”35”<o:p></o:p></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span class="hljs-keyword"><span style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 1pt solid rgb(217, 217, 227); color: #2e95d3; font-family: "Courier New"; font-size: 12pt; padding: 0in;">}</span></span><span style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; color: white; font-family: "Courier New"; font-size: 12pt;"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 12.0pt;"> </span></p>
</td>
</tr>
</tbody></table>
<p class="MsoNormal" style="background: white; line-height: normal; margin-bottom: 7.5pt; mso-margin-top-alt: auto;"><span style="color: #1f1f1f; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";"> </span></p>
<p class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .25in; margin-left: 0in; margin-right: 0in; margin-top: .25in;"><b><span style="color: #1f1f1f; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";">Interviewer:</span></b><span style="color: #1f1f1f; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";"> <b>What are the
different ways that JavaScript can represent data types?<o:p></o:p></b></span></p>
<p class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .25in; margin-left: 0in; margin-right: 0in; margin-top: .25in;"><b><span style="color: #1f1f1f; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";">Interviewee:</span></b><span style="color: #1f1f1f; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";"> There are two ways that JavaScript can represent data
types:<o:p></o:p></span></p>
<p class="MsoNormal" style="background: white; line-height: normal; margin-bottom: 7.5pt; mso-margin-top-alt: auto;"><span style="color: #1f1f1f; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";"><b>Literals:</b> Literals are values that are written
directly into the code. For example, the string "Hello, world!" is a
literal.<o:p></o:p></span></p>
<p class="MsoNormal" style="background: white; line-height: normal; margin-bottom: 7.5pt; mso-margin-top-alt: auto;"><span style="color: #1f1f1f; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";"><b>Variables:</b> Variables are named references to values.
Variables can be used to store data of any type. For example, the following
code creates a variable named </span><span style="color: #1f1f1f; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: "Courier New"; mso-fareast-font-family: "Times New Roman";">name</span><span style="color: #1f1f1f; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";"> and assigns
it the value "John Doe":<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; tab-stops: 45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: "Courier New"; mso-fareast-font-family: "Times New Roman";"><b>var name = "John
Doe";</b><o:p></o:p></span></p>
<p class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .25in; margin-left: 0in; margin-right: 0in; margin-top: .25in;"><b><span style="color: #1f1f1f; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";">Interviewer: What are the benefits of using data types in
JavaScript?<o:p></o:p></span></b></p>
<p class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .25in; margin-left: 0in; margin-right: 0in; margin-top: .25in;"><b><span style="color: #1f1f1f; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";">Interviewee:</span></b><span style="color: #1f1f1f; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";"> There are several benefits to using data types in
JavaScript:<o:p></o:p></span></p>
<p class="MsoNormal" style="background: white; line-height: normal; margin-bottom: 7.5pt; mso-margin-top-alt: auto;"></p><ul style="text-align: left;"><li><span style="color: #1f1f1f; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";">Data types make code more readable and maintainable. When
you use data types, you can be sure that the values you are working with are of
the correct type. This makes your code more readable and easier to understand.</span></li><li><span style="color: #1f1f1f; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";">Data types help to prevent errors. If you try to use
a value of the wrong type, JavaScript will throw an error. This helps to
prevent errors in your code.</span></li><li><span style="color: #1f1f1f; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";">Data types can be used to optimize performance. JavaScript
engines can optimize code that uses data types more efficiently. This can lead
to better performance of your JavaScript code.</span></li></ul><p></p>
<p class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .25in; margin-left: 0in; margin-right: 0in; margin-top: .25in;"><b><span style="color: #1f1f1f; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";">Interviewer: What are some common mistakes that people make when
using data types in JavaScript?<o:p></o:p></span></b></p>
<p class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .25in; margin-left: 0in; margin-right: 0in; margin-top: .25in;"><b><span style="color: #1f1f1f; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";">Interviewee:</span></b><span style="color: #1f1f1f; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";"> Some common mistakes that people make when using data
types in JavaScript include:<o:p></o:p></span></p>
<p class="MsoNormal" style="background: white; line-height: normal; margin-bottom: 7.5pt; mso-margin-top-alt: auto;"></p><ul style="text-align: left;"><li><span style="color: #1f1f1f; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";">Not using data types at all. This can lead to errors
and make your code less readable.</span></li><li><span style="color: #1f1f1f; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";">Using the wrong data type. For example, using a
string where a number is expected. This can also lead to errors.</span></li><li><span style="color: #1f1f1f; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";">Not assigning values to variables. This can cause
variables to be undefined, which can also lead to errors.</span></li></ul><p></p>
<p class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .25in; margin-left: 0in; margin-right: 0in; margin-top: .25in;"><b><span style="color: #1f1f1f; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";">Interviewer: What are some tips for using data types in
JavaScript effectively?<o:p></o:p></span></b></p>
<p class="MsoNormal" style="background: white; line-height: normal; margin-bottom: .25in; margin-left: 0in; margin-right: 0in; margin-top: .25in;"><b><span style="color: #1f1f1f; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";">Interviewee:</span></b><span style="color: #1f1f1f; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";"> Here are some tips for using data types in JavaScript
effectively:<o:p></o:p></span></p>
<p class="MsoNormal" style="background: white; line-height: normal; margin-bottom: 7.5pt; mso-margin-top-alt: auto;"></p><ul style="text-align: left;"><li><span style="color: #1f1f1f; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";">Always use data types. This will make your code more
readable, maintainable, and error-free.</span></li><li><span style="color: #1f1f1f; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";">Be careful about the type of data you are using. Make
sure that you are using the correct data type for the task at hand.</span></li><li><span style="color: #1f1f1f; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: Arial; mso-fareast-font-family: "Times New Roman";">Assign values to variables. This will prevent
variables from being undefined.</span></li><li><span style="background-color: white; color: #1f1f1f; font-family: Verdana, "sans-serif"; font-size: 11.5pt;">Use the typeof operator to check the type of a value. This
can be helpful for debugging and error handling.</span> </li></ul><p></p>
Codefarihttp://www.blogger.com/profile/14275737213961436106noreply@blogger.com0tag:blogger.com,1999:blog-34862624464291238.post-66437760366556389552023-08-02T20:28:00.004+05:302023-08-02T23:01:35.879+05:30What is JavaScript? A Beginner's Guide to Understanding the Fundamentals of Web Development<p style="text-align: justify;"><span style="font-family: Verdana, "sans-serif"; font-size: 11.5pt;">In the vast world of web development, JavaScript is a
foundational programming language that plays a critical role in creating
dynamic and interactive websites. If you've ever wondered how websites respond
to your clicks, display animations, or fetch data without reloading the page,
the answer lies in JavaScript. In this beginner's guide, we will demystify the
essence of JavaScript, its importance, and how it empowers developers to craft
engaging web experiences.</span></p>
<h2 style="text-align: justify;">Understanding JavaScript:</h2>
<p class="MsoNormal" style="text-align: justify;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;">JavaScript is a versatile and lightweight programming
language that allows developers to add interactivity and functionality to web
pages. Unlike HTML and CSS, which primarily deal with content and presentation,
respectively, JavaScript handles the behavior and logic of a website.<o:p></o:p></span></p>
<h2 style="text-align: justify;">The Language of the Web:</h2>
<p class="MsoNormal" style="text-align: justify;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;">JavaScript is the language of the web, and it operates
directly within your web browser. This means that you don't need any special
setup or plugins to use it; it's natively supported by all modern browsers.
When a web page loads, the browser interprets the JavaScript code and executes
it on the user's device, making it a powerful client-side scripting language.<o:p></o:p></span></p>
<h2 style="text-align: justify;">Essential Features of JavaScript:</h2>
<p class="MsoNormal" style="text-align: justify;"><b style="mso-bidi-font-weight: normal;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;">1.
Variables and Data Types:</span></b><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;"> JavaScript allows you to store and
manipulate data using variables. It supports various data types like numbers,
strings, booleans, arrays, objects, and more.<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: justify;"><b style="mso-bidi-font-weight: normal;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;">Example:
<o:p></o:p></span></b></p>
<table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="background: black; border-collapse: collapse; border: none; text-align: justify;">
<tbody><tr style="mso-yfti-firstrow: yes; mso-yfti-irow: 0; mso-yfti-lastrow: yes;">
<td style="border: double windowtext 1.5pt; padding: 0in 5.4pt 0in 5.4pt; width: 6.65in;" valign="top" width="638">
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #2e95d3; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";"><o:p> </o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #2e95d3; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">let</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";"> age = </span><span style="color: #df3079; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">25</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">;<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #2e95d3; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">let</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";"> name = </span><span style="color: #00a67d; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">"John Doe"</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">;<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #2e95d3; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">let</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";"> isStudent = </span><span style="color: #2e95d3; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">true</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">;<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #2e95d3; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">let</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";"> hobbies = [</span><span style="color: #00a67d; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">"reading"</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">, </span><span style="color: #00a67d; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">"gaming"</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">, </span><span style="color: #00a67d; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">"cooking"</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">];<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt;"><o:p> </o:p></span></p>
</td>
</tr>
</tbody></table>
<p class="MsoNormal" style="text-align: justify;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;"><o:p> </o:p></span></p>
<p class="MsoNormal" style="text-align: justify;"><b style="mso-bidi-font-weight: normal;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;">2.
Functions:</span></b><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;"> Functions are blocks of code that can be defined and
reused to perform specific tasks.<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: justify;"><b style="mso-bidi-font-weight: normal;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;">Example:<o:p></o:p></span></b></p>
<p class="MsoNormal" style="text-align: justify;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;"><span style="mso-spacerun: yes;"> </span><b style="mso-bidi-font-weight: normal;"><o:p></o:p></b></span></p>
<table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="background: black; border-collapse: collapse; border: none; text-align: justify;">
<tbody><tr style="mso-yfti-firstrow: yes; mso-yfti-irow: 0; mso-yfti-lastrow: yes;">
<td style="border: double windowtext 1.5pt; padding: 0in 5.4pt 0in 5.4pt; width: 6.65in;" valign="top" width="638">
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #2e95d3; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";"><o:p> </o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #2e95d3; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">function</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";"> </span><span style="color: #f22c3d; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">greet</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">(</span><span style="color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">name<span style="background: black;">) {<o:p></o:p></span></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span></span><span style="color: #2e95d3; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">return</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";"> </span><span style="color: #00a67d; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">"Hello, "</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";"> + name + </span><span style="color: #00a67d; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">"!"</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">;<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">}<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #df3079; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">console</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">.</span><span style="color: #f22c3d; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">log</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">(</span><span style="color: #f22c3d; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">greet</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">(</span><span style="color: #00a67d; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">"Alice"</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">)); </span><span style="font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">//
Output: Hello, Alice!<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt;"><o:p> </o:p></span></p>
</td>
</tr>
</tbody></table>
<p class="MsoNormal" style="text-align: justify;"><b style="mso-bidi-font-weight: normal;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;"><o:p> </o:p></span></b></p>
<p class="MsoNormal" style="text-align: justify;"><b style="mso-bidi-font-weight: normal;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;">3.
Conditional Statements:</span></b><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;"> JavaScript includes conditional
statements to make decisions in code execution.<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: justify;"><b style="mso-bidi-font-weight: normal;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;">Example:<o:p></o:p></span></b></p>
<table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="background: black; border-collapse: collapse; border: none; text-align: justify;">
<tbody><tr style="mso-yfti-firstrow: yes; mso-yfti-irow: 0; mso-yfti-lastrow: yes;">
<td style="border: double windowtext 1.5pt; padding: 0in 5.4pt 0in 5.4pt; width: 6.65in;" valign="top" width="638">
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #2e95d3; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";"><o:p> </o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #2e95d3; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">let</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";"> temperature = </span><span style="color: #df3079; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">30</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">;<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #2e95d3; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">if</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";"> (temperature > </span><span style="color: #df3079; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">25</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">) {<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span></span><span style="color: #df3079; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">console</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">.</span><span style="color: #f22c3d; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">log</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">(</span><span style="color: #00a67d; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">"It's a hot day!"</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">);<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">} </span><span style="color: #2e95d3; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">else</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";"> {<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span></span><span style="color: #df3079; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">console</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">.</span><span style="color: #f22c3d; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">log</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">(</span><span style="color: #00a67d; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">"It's a pleasant day!"</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">);<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">}<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt;"><o:p> </o:p></span></p>
</td>
</tr>
</tbody></table>
<p class="MsoNormal" style="text-align: justify;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;"><span style="mso-spacerun: yes;"> </span><o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: justify;"><b style="mso-bidi-font-weight: normal;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;">4.
Loops:</span></b><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;"> Loops help execute a block of code repeatedly until a
certain condition is met.<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: justify;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;"><b>Example:</b></span><b><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;"><o:p> </o:p></span></b></p>
<table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="background: black; border-collapse: collapse; border: none; text-align: justify;">
<tbody><tr style="mso-yfti-firstrow: yes; mso-yfti-irow: 0; mso-yfti-lastrow: yes;">
<td style="border: double windowtext 1.5pt; padding: 0in 5.4pt 0in 5.4pt; width: 6.65in;" valign="top" width="638">
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #2e95d3; font-family: "Courier New"; font-size: 10.5pt; mso-bidi-font-size: 11.0pt; mso-fareast-font-family: "Times New Roman";"><o:p> </o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="color: #2e95d3; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">for</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";"> (</span><span style="color: #2e95d3; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">let</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";"> i = </span><span style="color: #df3079; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">0</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">; i < </span><span style="color: #df3079; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">5</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">;
i++) {<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";"><span style="mso-spacerun: yes;"> </span></span><span style="color: #df3079; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">console</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">.</span><span style="color: #f22c3d; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">log</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">(</span><span style="color: #00a67d; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">"Number: "</span><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";"> + i);<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="background: black; color: white; font-family: "Courier New"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";">}<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt;"><o:p> </o:p></span></p>
</td>
</tr>
</tbody></table>
<p class="MsoNormal" style="text-align: justify;"><br /></p>
<h2 style="text-align: justify;">Advantages of JavaScript:</h2>
<p class="MsoNormal" style="text-align: justify;"><b style="mso-bidi-font-weight: normal;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;">1.
Enhanced User Experience:</span></b><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;"> JavaScript enables developers to
create interactive elements, such as forms, sliders, and pop-ups, improving the
overall user experience.<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: justify;"><b style="mso-bidi-font-weight: normal;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;">2.
Real-time Updates:</span></b><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;"> With JavaScript, you can update specific
parts of a web page without reloading the entire content, leading to faster and
more responsive applications.<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: justify;"><b style="mso-bidi-font-weight: normal;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;">3.
Browser Compatibility:</span></b><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;"> As a widely supported language,
JavaScript ensures that your web applications function consistently across
different browsers.<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: justify;"><b style="mso-bidi-font-weight: normal;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;">4.
Third-party Libraries and Frameworks:</span></b><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;"> The JavaScript ecosystem
boasts a plethora of libraries and frameworks like React, Angular, and jQuery,
making development more efficient and reducing code complexity.<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: justify;"><b style="mso-bidi-font-weight: normal;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;">Conclusion:<o:p></o:p></span></b></p>
<p class="MsoNormal" style="text-align: justify;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 107%;">JavaScript is the heart and soul of modern web
development, empowering developers to craft immersive and user-friendly
websites. From simple interactions to complex applications, JavaScript
continues to evolve and adapt to the ever-changing landscape of web
development. As you embark on your journey to become a web developer,
understanding JavaScript's fundamentals will undoubtedly open doors to a world
of creative possibilities. So, embrace the power of JavaScript and watch your
web projects come to life with interactivity and dynamism!<o:p></o:p></span></p>Codefarihttp://www.blogger.com/profile/14275737213961436106noreply@blogger.com0tag:blogger.com,1999:blog-34862624464291238.post-47468067089535700542023-08-02T11:37:00.005+05:302023-08-02T12:29:39.237+05:30SQL SERVER – Introduction to Dynamic Data Masking<h3 style="text-align: justify;"><span style="font-size: medium;">Introduction:</span></h3><p style="text-align: justify;"><span style="font-size: medium;">In today's data-driven world, protecting sensitive information is of paramount importance. As businesses collect and store vast amounts of data in their SQL Server databases, ensuring data security becomes a critical task. SQL Server, a popular relational database management system, provides several features to enhance data security, one of which is Dynamic Data Masking (DDM). In this blog, we will explore what Dynamic Data Masking is and how it can help safeguard sensitive data in your SQL Server databases.</span></p><h3 style="text-align: justify;"><span style="font-size: medium;">Understanding Dynamic Data Masking</span></h3><h4 style="text-align: justify;"><span style="font-size: medium;">Description:</span></h4><p style="text-align: justify;"><span style="font-size: medium;">Dynamic Data Masking is a data security feature introduced in SQL Server 2016 to protect sensitive data from unauthorized access. It allows you to control the exposure of sensitive information at the database level, ensuring that only authorized users can see the original data while concealing it from others. DDM does not modify the actual data in the database; instead, it dynamically masks the data at runtime based on the user's permissions.</span></p><h3 style="text-align: justify;"><span style="font-size: medium;">Implementing Dynamic Data Masking</span></h3><h4 style="text-align: justify;"><span style="font-size: medium;">Description:</span></h4><p style="text-align: justify;"><span style="font-size: medium;">Implementing Dynamic Data Masking in SQL Server is straightforward. First, you need to identify the sensitive columns in your database that require protection, such as credit card numbers, social security numbers, or personal email addresses. Once the sensitive columns are identified, you can define masking rules to determine how the data should be masked.</span></p><h3 style="text-align: justify;"><span style="font-size: medium;">SQL Server offers four masking functions for this purpose:</span></h3><p style="text-align: justify;"><span style="font-size: medium;"><b>1. Default:</b> This function replaces the sensitive data with a predefined string. For example, a credit card number "1234-5678-9876-5432" could be masked as "xxxx-xxxx-xxxx-5432."</span></p><p style="text-align: justify;"><span style="font-size: medium;"><b>2. Random:</b> The Random function replaces the sensitive data with random characters. This ensures that even if users have access to masked data, they cannot deduce the original information.</span></p><p style="text-align: justify;"><span style="font-size: medium;"><b>3. Email: </b>The Email function masks email addresses, revealing only the first letter of the user's name followed by "xxx@example.com."</span></p><p style="text-align: justify;"><span style="font-size: medium;"><b>4. Custom String: </b>This function allows you to define a custom masking string using Transact-SQL functions. It provides flexibility in determining the masking format according to your specific requirements.</span></p><h3 style="text-align: justify;"><span style="font-size: medium;">Managing Access Permissions</span></h3><h4 style="text-align: justify;"><span style="font-size: medium;">Description:</span></h4><p style="text-align: justify;"><span style="font-size: medium;">A crucial aspect of Dynamic Data Masking is controlling access permissions. While DDM conceals sensitive data from unauthorized users, it is essential to grant appropriate permissions to those who require access to the original data. SQL Server provides various user-defined roles, such as `MASKED` and `UNMASKED`, that can be assigned to users to control their access to masked data.</span></p><p style="text-align: justify;"><span style="font-size: medium;">By defining the appropriate access permissions and roles, you can ensure that only privileged users, such as administrators or data analysts, have the authority to view the unmasked data, while other users, like front-end application users or customers, can only see the masked data.</span></p><h3 style="text-align: justify;"><span style="font-size: medium;">Limitations and Considerations</span></h3><h4 style="text-align: justify;"><span style="font-size: medium;">Description:</span></h4><p style="text-align: justify;"><span style="font-size: medium;">While Dynamic Data Masking is a powerful tool for enhancing data security, it is essential to be aware of its limitations and considerations. DDM only provides a layer of security against unauthorized access within the database. It does not protect against direct access to the database files or data breaches at the application level. Therefore, it should be used in conjunction with other security measures, such as encryption and access control.</span></p><p style="text-align: justify;"><span style="font-size: medium;">Additionally, DDM is not suitable for all types of sensitive data. Data that needs to be used for complex calculations or joins may not be ideal for masking, as the masked data might interfere with the results. Careful consideration of the data and its use cases is essential when implementing Dynamic Data Masking.</span></p><h3 style="text-align: justify;"><span style="font-size: medium;">Conclusion:</span></h3><p style="text-align: justify;"><span style="font-size: medium;">Dynamic Data Masking is a valuable feature in SQL Server that provides an additional layer of data security by dynamically masking sensitive information. By concealing sensitive data from unauthorized users, DDM helps organizations comply with data privacy regulations and maintain the confidentiality of their critical information. However, it is essential to combine DDM with other security measures and carefully plan its implementation to ensure comprehensive data protection in your SQL Server databases.</span></p>Codefarihttp://www.blogger.com/profile/14275737213961436106noreply@blogger.com0tag:blogger.com,1999:blog-34862624464291238.post-81584673000749524742023-08-01T18:41:00.004+05:302023-08-02T12:32:54.215+05:30What is garbage collection in .Net C#?<p style="text-align: justify;"><span style="font-size: medium;">In .NET and C#, garbage collection is the process of automatically managing memory by identifying and reclaiming memory that is no longer in use. It is a key component of the .NET Common Language Runtime (CLR) and is responsible for ensuring efficient memory management in managed code.</span></p><p style="text-align: justify;"><span style="font-size: medium;">In managed languages like C#, objects are allocated memory on the managed heap when they are created. The garbage collector periodically scans the managed heap to identify objects that are no longer reachable (i.e., objects that are not referenced by any active part of the program). Once an object is identified as unreachable, it is considered garbage and can be safely removed from memory. This process is called garbage collection.</span></p><p style="text-align: justify;"><span style="font-size: medium;">Here are some key points about garbage collection in .NET C#:</span></p><p style="text-align: justify;"><span style="font-size: medium;"><b>1. Automatic Memory Management:</b> Developers do not need to explicitly allocate or deallocate memory for most objects. The garbage collector automatically determines when objects are no longer needed and reclaims the memory they occupied.</span></p><p style="text-align: justify;"><span style="font-size: medium;"><b>2. Non-Deterministic:</b> Garbage collection in .NET is non-deterministic, meaning that you do not have control over when the garbage collector will run. It operates based on certain heuristics and memory pressure.</span></p><p style="text-align: justify;"><span style="font-size: medium;"><b>3. Generation-Based Collection:</b> The .NET garbage collector uses a generational approach to garbage collection. Objects are divided into three generations (0, 1, and 2) based on their age and survivability. Most objects start in Generation 0, and if they survive a garbage collection, they get promoted to Generation 1. Objects that survive multiple garbage collections in Generation 1 get promoted to Generation 2. The garbage collector prioritizes collecting objects in the higher generations less frequently, as they tend to have longer lifetimes.</span></p><p style="text-align: justify;"><span style="font-size: medium;"><b>4. Garbage Collection Pauses:</b> During garbage collection, the execution of the application is briefly paused to allow the garbage collector to perform its tasks. These pauses are usually short, but they can be optimized and fine-tuned to minimize their impact on application performance.</span></p><p style="text-align: justify;"><span style="font-size: medium;"><b>5. Finalization:</b> .NET provides a mechanism for objects to perform cleanup tasks before they are garbage collected. This mechanism is called finalization and involves implementing a finalizer method (`~ClassName`) in a class. However, finalization is not guaranteed to run promptly, and it is recommended to use the `IDisposable` pattern for deterministic cleanup.</span></p><p style="text-align: justify;"><span style="font-size: medium;"><b>6. Tuning and Configuration:</b> .NET provides options to configure and tune the behavior of the garbage collector based on the application's requirements. Developers can use configuration settings, such as server vs. workstation garbage collection, or interact with the garbage collector through the `GC` class to request garbage collection or retrieve information about the memory usage.</span></p><p style="text-align: justify;"><span style="font-size: medium;">Garbage collection in .NET C# allows developers to focus on writing code and building applications without worrying about low-level memory management. It is an essential feature that contributes to the reliability and robustness of managed applications.</span></p>Codefarihttp://www.blogger.com/profile/14275737213961436106noreply@blogger.com0tag:blogger.com,1999:blog-34862624464291238.post-77667312869856671432023-08-01T18:31:00.005+05:302023-08-02T12:33:56.910+05:30What is difference between Out and Ref Keywords in C#<p style="text-align: justify;"><span face="Arial, sans-serif" style="font-size: medium; white-space-collapse: preserve;">In C#, both the `out` and `ref` keywords are used to pass arguments to methods by reference, which means that any changes made to the parameters inside the method will also affect the original variables passed as arguments. However, there is a crucial difference between the two:</span></p><span id="docs-internal-guid-a7b82f9f-7fff-f5fa-7cc3-cbb40a193e47"><h2 dir="ltr" style="line-height: 1.38; margin-bottom: 6pt; margin-top: 18pt; text-align: justify;"><span face="Arial, sans-serif" style="font-size: medium; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-weight: 400; vertical-align: baseline; white-space-collapse: preserve;">1. `ref` keyword:</span></h2><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"></p><ul style="text-align: left;"><li style="text-align: justify;"><span face="Arial, sans-serif" style="font-size: medium; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space-collapse: preserve;">When using `ref` keyword, the variable passed as an argument must be initialized before calling the method. The method can read and modify the value of the variable.</span></li><li style="text-align: justify;"><span face="Arial, sans-serif" style="font-size: medium; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space-collapse: preserve;">The variable passed as `ref` must be initialized because the method might not assign a new value to it before returning.</span></li><li style="text-align: justify;"><span face="Arial, sans-serif" style="font-size: medium; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space-collapse: preserve;">It allows two-way communication between the caller and the method.</span></li></ul><p></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;"><span face="Arial, sans-serif" style="font-size: medium; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space-collapse: preserve;">Example of using `ref` keyword:</span></p><p dir="ltr" style="background-color: white; line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;"><span style="font-size: medium;"><br /></span></p><div dir="ltr" style="margin-left: 0pt; text-align: left;"><table style="border-collapse: collapse; border: none; text-align: justify;"><colgroup><col width="591"></col></colgroup><tbody><tr style="height: 34.5pt;"><td style="background-color: #f2f2f2; border-bottom: solid #d6e3bc 0.75pt; border-color: rgb(214, 227, 188); border-left: solid #d6e3bc 0.75pt; border-right: solid #d6e3bc 0.75pt; border-style: solid; border-top: solid #d6e3bc 0.75pt; border-width: 0.75pt; overflow-wrap: break-word; overflow: hidden; padding: 0pt 5pt; vertical-align: top;"><span style="font-size: medium;"><br /></span><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span face="Arial,sans-serif" style="background-color: transparent; color: black; font-size: medium; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;">void ModifyValue(ref int x)</span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span face="Arial,sans-serif" style="background-color: transparent; color: black; font-size: medium; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;">{</span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span face="Arial,sans-serif" style="background-color: transparent; color: black; font-size: medium; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;"> x = x * 2;</span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span face="Arial,sans-serif" style="background-color: transparent; color: black; font-size: medium; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;">}</span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><b id="docs-internal-guid-58f3cf39-7fff-0719-2f0f-c745e1b36235" style="font-weight: normal;"><span style="font-size: medium;"><br /></span></b></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span face="Arial,sans-serif" style="background-color: transparent; color: black; font-size: medium; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;">int number = 5;</span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span face="Arial,sans-serif" style="background-color: transparent; color: black; font-size: medium; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;">ModifyValue(ref number);</span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span face="Arial,sans-serif" style="background-color: transparent; color: black; font-size: medium; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre;">Console.WriteLine(number); // Output: 10</span></p><div><span style="font-size: medium;"><br /></span></div></td></tr></tbody></table></div><p dir="ltr" style="background-color: white; line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;"><span style="font-size: medium;"><br /></span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;"><span face="Arial, sans-serif" style="font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space-collapse: preserve;"><span style="border: none; display: inline-block; font-size: medium; height: 371px; overflow: hidden; width: 592px;"><img height="371" src="https://lh5.googleusercontent.com/Z48oaHYjw9TZKuZXG_JItqnfSn3PeGoDfmaeNhAPKX-dcwz66QEre4ekcqt3fgoiMSW3bAui09vu99GkDMlWTq9Ti4f4NxPcNB2mcAKMSgHnFejWLp8YaSK5iAFePYcDIDweNVWiK4C9dcglt2lHmcs" style="margin-left: 0px; margin-top: 0px;" width="592" /></span></span></p><span style="font-size: medium;"><div style="text-align: justify;"><br /></div></span><h2 dir="ltr" style="line-height: 1.38; margin-bottom: 6pt; margin-top: 18pt; text-align: justify;"><span face="Arial, sans-serif" style="font-size: medium; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-weight: 400; vertical-align: baseline; white-space-collapse: preserve;">2. `out` keyword:</span></h2><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"></p><ul style="text-align: left;"><li style="text-align: justify;"><span face="Arial, sans-serif" style="font-size: medium; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space-collapse: preserve;">The `out` keyword is used when a method wants to return multiple values. Like the `ref` keyword, the `out` parameter must also be passed as an argument to the method.</span></li><li style="text-align: justify;"><span face="Arial, sans-serif" style="font-size: medium; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space-collapse: preserve;"> The key difference is that with the `out` keyword, the variable passed as an argument does not need to be initialized before calling the method. The method must assign a value to the `out` parameter before it returns.</span></li><li style="text-align: justify;"><span face="Arial, sans-serif" style="font-size: medium; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space-collapse: preserve;">The method is obligated to assign a value to the `out` parameter; otherwise, it will result in a compilation error.</span></li></ul><p></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;"><span face="Arial, sans-serif" style="font-size: medium; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space-collapse: preserve;">Example of using `out` keyword:</span></p><p dir="ltr" style="background-color: white; line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;"><span style="font-size: medium;"><br /></span></p><div dir="ltr" style="margin-left: 0pt; text-align: left;"><table style="border-collapse: collapse; border: none; text-align: justify;"><colgroup><col width="591"></col></colgroup><tbody><tr style="height: 34.5pt;"><td style="background-color: #f2f2f2; border-bottom: solid #d6e3bc 0.75pt; border-color: rgb(214, 227, 188); border-left: solid #d6e3bc 0.75pt; border-right: solid #d6e3bc 0.75pt; border-style: solid; border-top: solid #d6e3bc 0.75pt; border-width: 0.75pt; overflow-wrap: break-word; overflow: hidden; padding: 0pt 5pt; vertical-align: top;"><span style="font-size: medium;"><br /></span><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span face="Arial, sans-serif" style="background-color: transparent; font-size: medium; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space-collapse: preserve;">void GetValues(out int a, out int b)</span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span face="Arial, sans-serif" style="background-color: transparent; font-size: medium; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space-collapse: preserve;">{</span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span face="Arial, sans-serif" style="background-color: transparent; font-size: medium; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space-collapse: preserve;"> a = 10;</span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span face="Arial, sans-serif" style="background-color: transparent; font-size: medium; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space-collapse: preserve;"> b = 20;</span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span face="Arial, sans-serif" style="background-color: transparent; font-size: medium; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space-collapse: preserve;">}</span></p><span style="font-size: medium;"><br /></span><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span face="Arial, sans-serif" style="background-color: transparent; font-size: medium; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space-collapse: preserve;">int number1, number2;</span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span face="Arial, sans-serif" style="background-color: transparent; font-size: medium; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space-collapse: preserve;">GetValues(out number1, out number2);</span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span face="Arial, sans-serif" style="background-color: transparent; font-size: medium; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space-collapse: preserve;">Console.WriteLine(number1); // Output: 10</span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span face="Arial, sans-serif" style="background-color: transparent; font-size: medium; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space-collapse: preserve;">Console.WriteLine(number2); // Output: 20</span></p><span style="font-size: medium;"><br /></span></td></tr></tbody></table></div><span style="font-size: medium;"><div style="text-align: justify;"><br /></div></span><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;"><span face="Arial, sans-serif" style="font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space-collapse: preserve;"><span style="border: none; display: inline-block; font-size: medium; height: 312px; overflow: hidden; width: 597px;"><img height="312" src="https://lh4.googleusercontent.com/gojD2pCVPpKks7oYb43tsPsavBBN-JxVuyb9MMvTzKbliYwDum_kETPcMOYd1NiSLhr0r79qCFvOFNbUzTXF31e-mxTeVXXNu0rOGchRX9LAecNenkXJLwtIKL77H3mB_2PA3vPE65_lPYFOSCuWdGU" style="margin-left: 0px; margin-top: 0px;" width="597" /></span></span></p><span style="font-size: medium;"><div style="text-align: justify;"><br /></div></span><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt; text-align: justify;"><span face="Arial, sans-serif" style="font-size: medium; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space-collapse: preserve;">In summary, the main difference between `out` and `ref` in C# is that `ref` requires the variable to be initialized before passing it to the method, and it allows two-way communication between the method and the caller, while `out` does not require initialization before passing and is intended for methods that return multiple values.</span></p><span style="font-size: medium;"><div style="text-align: justify;"><br /></div></span><div style="text-align: justify;"><br /></div></span>Codefarihttp://www.blogger.com/profile/14275737213961436106noreply@blogger.com0tag:blogger.com,1999:blog-34862624464291238.post-88070253487592858402023-07-10T21:03:00.001+05:302023-07-10T21:03:38.004+05:30What is delegate in C#? Why we need delegate?<p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11.5pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;">In C#, a delegate is a type that represents references to methods with a specific signature. It is essentially a function pointer or a callback mechanism that allows you to encapsulate and pass around methods as objects. Delegates provide a way to achieve callback functionality, event handling, and implementing the observer pattern.</span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11.5pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;">Delegates are especially useful in scenarios where you want to decouple the invoker of a method from the actual method implementation. They allow you to define a contract or a template for a method and then pass the responsibility of implementing that method to another class or object.</span></p><p><span style="font-family: Verdana; font-size: 16pt; white-space-collapse: preserve;">Here are some reasons why delegates are used in C#:</span></p><p><span style="font-family: Verdana; font-size: 11.5pt; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-weight: 700; vertical-align: baseline; white-space-collapse: preserve;">1. Callbacks:</span><span style="font-family: Verdana; font-size: 11.5pt; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space-collapse: preserve;"> Delegates can be used to define callbacks. You can pass a delegate as a parameter to a method, and that method can then call the delegate to invoke a specific method when a certain condition is met.</span></p><p><span style="font-family: Verdana; font-size: 11.5pt; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-weight: 700; vertical-align: baseline; white-space-collapse: preserve;">2. Event handling:</span><span style="font-family: Verdana; font-size: 11.5pt; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space-collapse: preserve;"> Delegates are commonly used to implement event handling in C#. Events allow objects to notify other objects when something significant happens. By using delegates, you can register event handlers to be called when the event occurs.</span></p><p><span style="font-family: Verdana; font-size: 11.5pt; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-weight: 700; vertical-align: baseline; white-space-collapse: preserve;">3. Extensibility:</span><span style="font-family: Verdana; font-size: 11.5pt; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space-collapse: preserve;"> Delegates provide a way to extend the behavior of a class without modifying its code. You can define a delegate in a base class or interface, and derived classes can provide their own implementations for that delegate, allowing for flexible and customizable behavior.</span></p><p><span style="font-family: Verdana; font-size: 11.5pt; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; font-weight: 700; vertical-align: baseline; white-space-collapse: preserve;">4. Asynchronous programming:</span><span style="font-family: Verdana; font-size: 11.5pt; font-variant-alternates: normal; font-variant-east-asian: normal; font-variant-numeric: normal; vertical-align: baseline; white-space-collapse: preserve;"> Delegates can be used in asynchronous programming scenarios, such as invoking a method on a separate thread or using the `async` and `await` keywords. Delegates can represent an asynchronous operation and be used to handle the results or completion of that operation.</span></p><p><span style="font-family: Verdana; font-size: 11.5pt; white-space-collapse: preserve;">Overall, delegates in C# offer a powerful mechanism for achieving flexibility, decoupling, and extensibility in your code by treating methods as first-class objects that can be passed and executed at runtime.</span></p><h3 dir="ltr" style="line-height: 1.38; margin-bottom: 4pt; margin-top: 16pt;"><span style="background-color: transparent; color: #434343; font-family: Verdana; font-size: 13.999999999999998pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;">Sure! Here's an example that demonstrates the usage of delegates in C#:</span></h3><div align="left" dir="ltr" style="margin-left: 0pt;"><table style="border-collapse: collapse; border: none;"><colgroup><col width="591"></col></colgroup><tbody><tr style="height: 51.75pt;"><td style="background-color: #dbe5f1; border-bottom: solid #b8cce4 0.75pt; border-left: solid #b8cce4 0.75pt; border-right: solid #b8cce4 0.75pt; border-top: solid #b8cce4 0.75pt; overflow-wrap: break-word; overflow: hidden; padding: 0pt 5pt 0pt 5pt; vertical-align: top;"><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 10pt;"><span style="background-color: transparent; color: #666666; font-family: Verdana; font-size: 11.5pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"> </span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 10pt;"><span style="background-color: transparent; color: #666666; font-family: Verdana; font-size: 11.5pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"> </span><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;">using System;</span></p><br /><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;">// Step 1: Declare a delegate</span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;">delegate void PrintDelegate(string message);</span></p><br /><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;">class Program</span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;">{</span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"> static void Main()</span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"> {</span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"> // Step 2: Create delegate instances and associate them with methods</span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"> PrintDelegate printDelegate1 = PrintMessage;</span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"> PrintDelegate printDelegate2 = PrintGreetings;</span></p><br /><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"> // Step 3: Invoke the delegate instances</span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"> printDelegate1("Hello");</span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"> printDelegate2("Welcome");</span></p><br /><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"> // Step 4: Combining delegates</span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"> PrintDelegate printDelegate3 = printDelegate1 + printDelegate2;</span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"> printDelegate3("Combined");</span></p><br /><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"> // Step 5: Removing delegates</span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"> printDelegate3 -= printDelegate2;</span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"> printDelegate3("After removal");</span></p><br /><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"> // Step 6: Using built-in delegates</span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"> Action<string> actionDelegate = PrintMessage;</span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"> actionDelegate("Using Action<>");</span></p><br /><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"> Func<int, int, int> addDelegate = AddNumbers;</span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"> int result = addDelegate(3, 5);</span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"> Console.WriteLine("Result: " + result);</span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"> }</span></p><br /><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"> // Step 7: Define methods that match the delegate's signature</span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"> static void PrintMessage(string message)</span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"> {</span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"> Console.WriteLine("Message: " + message);</span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"> }</span></p><br /><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"> static void PrintGreetings(string greeting)</span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"> {</span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"> Console.WriteLine("Greeting: " + greeting);</span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"> }</span></p><br /><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"> static int AddNumbers(int a, int b)</span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"> {</span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"> return a + b;</span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"> }</span></p><p dir="ltr" style="line-height: 1.38; margin-bottom: 0pt; margin-top: 0pt;"><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;">}</span></p><br /></td></tr></tbody></table></div><p><span style="font-family: Verdana; font-size: 11.5pt; white-space-collapse: preserve;">In this example:</span></p><p></p><ol style="text-align: left;"><li><span style="font-family: Verdana; font-size: 11.5pt; white-space-collapse: preserve;">We declare a delegate named `PrintDelegate` that takes a string parameter and has a `void` return type.</span></li><li><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11.5pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;"> In the `Main` method, we create delegate instances `printDelegate1` and `printDelegate2`, associating them with the methods `PrintMessage` and `PrintGreetings` respectively.</span></li><li><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11.5pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;">We invoke the delegate instances using the syntax `delegateInstance(arguments)`.</span></li><li><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11.5pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;">We demonstrate combining delegates using the `+` operator and removing delegates using the `-` operator.</span></li><li><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11.5pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;">We show the usage of built-in delegates `Action<>` and `Func<>`. `Action<>` is a delegate type that takes parameters but doesn't return a value, while `Func<>` is a delegate type that takes parameters and returns a value.</span></li><li><span style="background-color: transparent; color: black; font-family: Verdana; font-size: 11.5pt; font-style: normal; font-variant: normal; font-weight: 400; text-decoration: none; vertical-align: baseline; white-space: pre-wrap; white-space: pre;">We define methods that match the delegate's signature (`PrintMessage`, `PrintGreetings`, and `AddNumbers`), which are invoked by the delegate instances.</span></li></ol><p></p><p><span style="font-family: Verdana; font-size: 11.5pt; white-space-collapse: preserve;">When you run the code, you'll see the respective messages printed to the console, demonstrating the execution of the methods through delegates.</span></p><p><br /><br /></p>Codefarihttp://www.blogger.com/profile/14275737213961436106noreply@blogger.com0tag:blogger.com,1999:blog-34862624464291238.post-33873054261287266182023-05-18T22:59:00.002+05:302023-05-18T22:59:08.649+05:30how to use coalesce in PostgreSQL?<p class="MsoNormal"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 115%;">To use the `COALESCE` function in PostgreSQL, follow
these steps:<o:p></o:p></span></p><p class="MsoNormal"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 115%;">1. Start by writing a SELECT statement or any other
query where you want to handle null values.<o:p></o:p></span></p><p class="MsoNormal"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 115%;">2. Use the `COALESCE` function and provide the
expressions or columns you want to evaluate for null values. The function will
return the first non-null expression or column value.</span></p><p class="MsoNormal"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 115%;"></span></p><table border="0" cellpadding="0" cellspacing="0" class="MsoNormalTable" style="background: #DBE5F1; border-collapse: collapse; mso-padding-alt: 0cm 0cm 0cm 0cm; mso-yfti-tbllook: 1184;">
<tbody><tr>
<td style="border: solid #B8CCE4 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 478.8pt;" valign="top" width="638">
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-margin-top-alt: auto;"><span style="color: #666666; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: "Times New Roman"; mso-fareast-font-family: "Times New Roman";"> </span><span style="color: #666666; font-family: "Times New Roman","serif"; font-size: 12.0pt; mso-fareast-font-family: "Times New Roman";"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-margin-top-alt: auto;"><span style="color: #666666; font-family: "Times New Roman","serif"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";"> </span><span style="font-family: Verdana, "sans-serif"; font-size: 15.3333px;">Syntax: `COALESCE(expression1, expression2, ...)</span><span style="color: #666666; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: "Times New Roman"; mso-fareast-font-family: "Times New Roman";"><br />
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--></span><span style="color: #666666; font-family: "Times New Roman","serif"; font-size: 12.0pt; mso-fareast-font-family: "Times New Roman";"><o:p></o:p></span></p>
</td>
</tr>
</tbody></table><p class="MsoNormal"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 115%;"> For example,
suppose you have a table called "products" with columns
"name", "description", and "price". You want to
display the name and description, but if the description is null, you want to
show the text "No description available". You can use `COALESCE` as
follows:</span></p><p class="MsoNormal"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 115%;"></span></p><table border="0" cellpadding="0" cellspacing="0" class="MsoNormalTable" style="background: #DBE5F1; border-collapse: collapse; mso-padding-alt: 0cm 0cm 0cm 0cm; mso-yfti-tbllook: 1184;">
<tbody><tr>
<td style="border: solid #B8CCE4 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 478.8pt;" valign="top" width="638">
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-margin-top-alt: auto;"><span style="color: #666666; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: "Times New Roman"; mso-fareast-font-family: "Times New Roman";"> </span><span style="color: #666666; font-family: "Times New Roman","serif"; font-size: 12.0pt; mso-fareast-font-family: "Times New Roman";"><o:p></o:p></span></p>
<div style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-margin-top-alt: auto;"><span style="color: #666666; font-family: "Times New Roman","serif"; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman";"> </span><span style="font-size: x-small;"><span style="color: #666666; font-family: "Times New Roman", "serif";"> </span><span style="font-family: Verdana, "sans-serif";">SELECT name, COALESCE(description, 'No description available') AS description</span></span></div><div style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-margin-top-alt: auto;"><span style="font-size: x-small;"><span style="font-family: Verdana, "sans-serif";"><br /></span><span style="font-family: Verdana, "sans-serif"; line-height: 17.6333px;"><span> </span><span> </span>FROM products;</span></span></div><p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-margin-top-alt: auto;"><span style="color: #666666; font-family: "Verdana","sans-serif"; font-size: 11.5pt; mso-bidi-font-family: "Times New Roman"; mso-fareast-font-family: "Times New Roman";">
<!--[if !supportLineBreakNewLine]--><br />
<!--[endif]--></span><span style="color: #666666; font-family: "Times New Roman","serif"; font-size: 12.0pt; mso-fareast-font-family: "Times New Roman";"><o:p></o:p></span></p>
</td>
</tr>
</tbody></table><p class="MsoNormal"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 115%;">In this example, if the description is null, the
`COALESCE` function will return the alternative value of 'No description
available'.<o:p></o:p></span></p><p class="MsoNormal"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 115%;">3. Execute the query, and the `COALESCE` function will
handle null values based on your specified logic.<o:p></o:p></span></p><p>
</p><p class="MsoNormal"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 115%;">By using `COALESCE`, you can easily handle and replace
null values with default or alternative values in your queries.<o:p></o:p></span></p>Codefarihttp://www.blogger.com/profile/14275737213961436106noreply@blogger.com0tag:blogger.com,1999:blog-34862624464291238.post-89468962991036496042023-05-01T21:52:00.003+05:302023-05-01T21:53:31.258+05:30Catch multiple exceptions at once in C#?<p style="text-align: justify;"><span face="Verdana, "sans-serif"" style="font-size: 11.5pt;">In C#, you can catch multiple exceptions at once using
a single catch block that includes multiple exception types separated by the
pipe `|` symbol. For example:</span></p><p class="MsoNormal"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt; line-height: 115%;"></span></p><table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="background: rgb(219, 229, 241); border-collapse: collapse; border: none; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-border-alt: solid #B8CCE4 .5pt; mso-border-insideh-themecolor: accent1; mso-border-insideh-themetint: 102; mso-border-insideh: .5pt solid #B8CCE4; mso-border-insidev-themecolor: accent1; mso-border-insidev-themetint: 102; mso-border-insidev: .5pt solid #B8CCE4; mso-border-themecolor: accent1; mso-border-themetint: 102; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-yfti-tbllook: 1184; text-align: justify;">
<tbody><tr>
<td style="border: 1pt solid rgb(184, 204, 228); mso-border-alt: solid #B8CCE4 .5pt; mso-border-themecolor: accent1; mso-border-themetint: 102; padding: 0cm 5.4pt; width: 478.8pt;" valign="top" width="638">
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm;"><span face=""Verdana","sans-serif"" style="font-size: 11.5pt;"> </span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt;">try</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"><o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">{<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> </span><span style="color: green; font-family: "Cascadia Mono"; font-size: 9.5pt;">// Some code that may throw exceptions</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"><o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">}<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt;">catch</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> (IOException | ArgumentException ex)<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">{<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> </span><span style="color: green; font-family: "Cascadia Mono"; font-size: 9.5pt;">// Handle IOException or ArgumentException</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"><o:p></o:p></span></p><p class="MsoNormal"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt; line-height: 14.5667px;">}</span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm;"><span face=""Verdana","sans-serif"" style="font-size: 11.5pt;"><br /></span></p>
</td>
</tr>
</tbody></table>
<p class="MsoNormal" style="text-align: justify;"><span face=""Verdana","sans-serif"" style="font-size: 11.5pt; line-height: 115%;">In this example, the catch block will handle both
`IOException` and `ArgumentException` exceptions. You can add as many exception
types as you need, separated by the `|` symbol.<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: justify;"><span face=""Verdana","sans-serif"" style="font-size: 11.5pt; line-height: 115%;">It's also possible to have multiple catch blocks, each
handling a different exception type. In this case, the catch blocks will be
executed in order from top to bottom, and the first catch block that handles
the exception will be executed. For example:</span></p><p class="MsoNormal"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt; line-height: 115%;"></span></p><table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="background: rgb(219, 229, 241); border-collapse: collapse; border: none; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-border-alt: solid #B8CCE4 .5pt; mso-border-insideh-themecolor: accent1; mso-border-insideh-themetint: 102; mso-border-insideh: .5pt solid #B8CCE4; mso-border-insidev-themecolor: accent1; mso-border-insidev-themetint: 102; mso-border-insidev: .5pt solid #B8CCE4; mso-border-themecolor: accent1; mso-border-themetint: 102; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-yfti-tbllook: 1184; text-align: justify;">
<tbody><tr>
<td style="border: 1pt solid rgb(184, 204, 228); mso-border-alt: solid #B8CCE4 .5pt; mso-border-themecolor: accent1; mso-border-themetint: 102; padding: 0cm 5.4pt; width: 478.8pt;" valign="top" width="638">
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm;"><span face=""Verdana","sans-serif"" style="font-size: 11.5pt;"> </span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt;">try</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"><o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">{<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> </span><span style="color: green; font-family: "Cascadia Mono"; font-size: 9.5pt;">// Some code that may throw exceptions</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"><o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">}<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt;">catch</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> (IOException ex)<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">{<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> </span><span style="color: green; font-family: "Cascadia Mono"; font-size: 9.5pt;">// Handle IOException</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"><o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">}<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt;">catch</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> (ArgumentException ex)<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">{<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> </span><span style="color: green; font-family: "Cascadia Mono"; font-size: 9.5pt;">// Handle ArgumentException</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"><o:p></o:p></span></p><p class="MsoNormal"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt; line-height: 14.5667px;">}</span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm;"><span face=""Verdana","sans-serif"" style="font-size: 11.5pt;"><br /></span></p>
</td>
</tr>
</tbody></table>
<p class="MsoNormal" style="text-align: justify;"><span face=""Verdana","sans-serif"" style="font-size: 11.5pt; line-height: 115%;">In this example, if an `IOException` is thrown, the
first catch block will handle it, and the second catch block will be skipped.
If an `ArgumentException` is thrown, the first catch block will be skipped, and
the second catch block will handle it.<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: justify;"><span face=""Verdana","sans-serif"" style="font-size: 11.5pt; line-height: 115%;">When catching multiple exceptions, it's important to be
careful not to catch exceptions that you don't intend to handle, as this can
lead to unexpected behavior in your application.<o:p></o:p></span></p>Codefarihttp://www.blogger.com/profile/14275737213961436106noreply@blogger.com0tag:blogger.com,1999:blog-34862624464291238.post-43857668343806186252023-05-01T21:43:00.001+05:302023-05-01T21:56:12.743+05:30Deep cloning objects in c#<p><span face="Verdana, "sans-serif"" style="font-size: 11.5pt;">In C#, there are several
ways to perform deep cloning of objects. Here are some approaches:</span></p>
<h3 style="text-align: left;"><span face=""Verdana","sans-serif"" style="font-size: 11.5pt; line-height: 115%;">Using serialization and
deserialization: </span></h3><p class="MsoNormal"><span face=""Verdana","sans-serif"" style="font-size: 11.5pt; line-height: 115%;">Serialize the object into a memory stream and then deserialize
the object from the memory stream to create a new instance. This approach
requires the object to be serializable, meaning it must have the
`[Serializable]` attribute.</span></p><p class="MsoNormal"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt; line-height: 115%;"></span></p><table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="background: #DBE5F1; border-collapse: collapse; border: none; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-border-alt: solid #B8CCE4 .5pt; mso-border-insideh-themecolor: accent1; mso-border-insideh-themetint: 102; mso-border-insideh: .5pt solid #B8CCE4; mso-border-insidev-themecolor: accent1; mso-border-insidev-themetint: 102; mso-border-insidev: .5pt solid #B8CCE4; mso-border-themecolor: accent1; mso-border-themetint: 102; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-yfti-tbllook: 1184;">
<tbody><tr>
<td style="border: solid #B8CCE4 1.0pt; mso-border-alt: solid #B8CCE4 .5pt; mso-border-themecolor: accent1; mso-border-themecolor: accent1; mso-border-themetint: 102; mso-border-themetint: 102; padding: 0cm 5.4pt 0cm 5.4pt; width: 478.8pt;" valign="top" width="638">
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt;"> </span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span face=""Verdana","sans-serif"" style="font-size: 11.5pt;"> </span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt; mso-bidi-font-family: "Cascadia Mono";">public</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt; mso-bidi-font-family: "Cascadia Mono";">static</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> T DeepClone<</span><span style="color: #2b91af; font-family: "Cascadia Mono"; font-size: 9.5pt; mso-bidi-font-family: "Cascadia Mono";">T</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">>(T obj)<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> {<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt; mso-bidi-font-family: "Cascadia Mono";">using</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> (</span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt; mso-bidi-font-family: "Cascadia Mono";">var</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> ms = </span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt; mso-bidi-font-family: "Cascadia Mono";">new</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> MemoryStream())<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> {<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt; mso-bidi-font-family: "Cascadia Mono";">var</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> formatter = </span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt; mso-bidi-font-family: "Cascadia Mono";">new</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> BinaryFormatter();<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> formatter.Serialize(ms, obj);<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> ms.Position = 0;<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt; mso-bidi-font-family: "Cascadia Mono";">return</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> (T)formatter.Deserialize(ms);<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> }<o:p></o:p></span></p><p class="MsoNormal"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt; line-height: 14.5667px;"> }</span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt;"><br /></span></p>
</td>
</tr>
</tbody></table><h3 style="text-align: left;"><span face="Verdana, "sans-serif"" style="font-size: 11.5pt;">Using a copy
constructor: </span></h3><p class="MsoNormal"><span face=""Verdana","sans-serif"" style="font-size: 11.5pt; line-height: 115%;">Create a new instance of the object using a copy constructor that
takes an instance of the same type as a parameter. This approach requires the
class to have a copy constructor.</span><span style="font-size: 11.5pt;"> </span></p><p class="MsoNormal"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt; line-height: 115%;"></span></p><table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="background: #DBE5F1; border-collapse: collapse; border: none; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-border-alt: solid #B8CCE4 .5pt; mso-border-insideh-themecolor: accent1; mso-border-insideh-themetint: 102; mso-border-insideh: .5pt solid #B8CCE4; mso-border-insidev-themecolor: accent1; mso-border-insidev-themetint: 102; mso-border-insidev: .5pt solid #B8CCE4; mso-border-themecolor: accent1; mso-border-themetint: 102; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-yfti-tbllook: 1184;">
<tbody><tr>
<td style="border: solid #B8CCE4 1.0pt; mso-border-alt: solid #B8CCE4 .5pt; mso-border-themecolor: accent1; mso-border-themecolor: accent1; mso-border-themetint: 102; mso-border-themetint: 102; padding: 0cm 5.4pt 0cm 5.4pt; width: 478.8pt;" valign="top" width="638">
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt;"> </span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt; mso-bidi-font-family: "Cascadia Mono";"><span> </span><span> </span>public</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt; mso-bidi-font-family: "Cascadia Mono";">class</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "Cascadia Mono"; font-size: 9.5pt; mso-bidi-font-family: "Cascadia Mono";">MyClass</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"><o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> {<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt; mso-bidi-font-family: "Cascadia Mono";">public</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt; mso-bidi-font-family: "Cascadia Mono";">int</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> MyProperty { </span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt; mso-bidi-font-family: "Cascadia Mono";">get</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">; </span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt; mso-bidi-font-family: "Cascadia Mono";">set</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">; }<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> </span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt; mso-bidi-font-family: "Cascadia Mono";">public</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "Cascadia Mono"; font-size: 9.5pt; mso-bidi-font-family: "Cascadia Mono";">MyClass</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">(MyClass other)<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> {<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> MyProperty = other.MyProperty;<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> }<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> }<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> </span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt; mso-bidi-font-family: "Cascadia Mono";">public</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt; mso-bidi-font-family: "Cascadia Mono";">static</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> MyClass DeepClone(MyClass obj)<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> {<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt; mso-bidi-font-family: "Cascadia Mono";">return</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt; mso-bidi-font-family: "Cascadia Mono";">new</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> MyClass(obj);<o:p></o:p></span></p><p class="MsoNormal"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt; line-height: 14.5667px;"> }</span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt;"><br /></span></p>
</td>
</tr>
</tbody></table>
<h3 style="text-align: left;"><span face=""Verdana","sans-serif"" style="font-size: 11.5pt; line-height: 115%;">Using reflection: </span></h3><p class="MsoNormal"><span face=""Verdana","sans-serif"" style="font-size: 11.5pt; line-height: 115%;">Create a new instance of the object and set the properties or fields of the new
instance to the values of the original instance. This approach requires the
object to have a parameterless constructor.</span><span style="font-size: 11.5pt;"> </span></p><p class="MsoNormal"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt; line-height: 115%;"></span></p><table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="background: #DBE5F1; border-collapse: collapse; border: none; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-border-alt: solid #B8CCE4 .5pt; mso-border-insideh-themecolor: accent1; mso-border-insideh-themetint: 102; mso-border-insideh: .5pt solid #B8CCE4; mso-border-insidev-themecolor: accent1; mso-border-insidev-themetint: 102; mso-border-insidev: .5pt solid #B8CCE4; mso-border-themecolor: accent1; mso-border-themetint: 102; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-yfti-tbllook: 1184;">
<tbody><tr>
<td style="border: solid #B8CCE4 1.0pt; mso-border-alt: solid #B8CCE4 .5pt; mso-border-themecolor: accent1; mso-border-themecolor: accent1; mso-border-themetint: 102; mso-border-themetint: 102; padding: 0cm 5.4pt 0cm 5.4pt; width: 478.8pt;" valign="top" width="638">
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt;"> </span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt; mso-bidi-font-family: "Cascadia Mono";">public</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt; mso-bidi-font-family: "Cascadia Mono";">static</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> T DeepClone<</span><span style="color: #2b91af; font-family: "Cascadia Mono"; font-size: 9.5pt; mso-bidi-font-family: "Cascadia Mono";">T</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">>(T obj)<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> {<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt; mso-bidi-font-family: "Cascadia Mono";">var</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> type = obj.GetType();<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt; mso-bidi-font-family: "Cascadia Mono";">var</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> clone = Activator.CreateInstance(type);<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt; mso-bidi-font-family: "Cascadia Mono";">foreach</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> (var field </span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt; mso-bidi-font-family: "Cascadia Mono";">in</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> type.GetFields(BindingFlags.Instance | <span> </span><span> </span>BindingFlags.NonPublic | BindingFlags.Public))<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> {<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> field.SetValue(clone, field.GetValue(obj));<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> }<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt; mso-bidi-font-family: "Cascadia Mono";">foreach</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> (var property </span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt; mso-bidi-font-family: "Cascadia Mono";">in</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> type.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> {<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt; mso-bidi-font-family: "Cascadia Mono";">if</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> (property.CanWrite)<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> {<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> property.SetValue(clone, property.GetValue(obj));<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> }<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> }<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt; mso-bidi-font-family: "Cascadia Mono";">return</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> (T)clone;<o:p></o:p></span></p><p class="MsoNormal"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt; line-height: 14.5667px;"> }</span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt;"><br /></span></p>
</td>
</tr>
</tbody></table>
<p class="MsoNormal"><span face=""Verdana","sans-serif"" style="font-size: 11.5pt; line-height: 115%;">Note that deep cloning can
be a complex and error-prone process, especially for complex object graphs with
circular references. It's important to thoroughly test your implementation to
ensure that all objects are cloned correctly.</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt; line-height: 115%;"> <o:p></o:p></span></p>
<p class="MsoNormal"><span face=""Verdana","sans-serif"" style="font-size: 11.5pt; line-height: 115%;">In this example, we use a `for` loop to iterate over
the keys and values in the dictionary and print each key-value pair to the
console. We use the `Keys` and `Values` properties of the dictionary to get the
keys and values as `IEnumerable` collections, and then use the `ElementAt`
method to get the key and value at each index.<o:p></o:p></span></p>Codefarihttp://www.blogger.com/profile/14275737213961436106noreply@blogger.com0tag:blogger.com,1999:blog-34862624464291238.post-62246496515897775632023-05-01T19:25:00.007+05:302023-08-02T19:59:30.745+05:30Exploring Dictionary Iteration in C#: Step-by-Step Guide<p><span style="font-family: Verdana, "sans-serif"; font-size: 11.5pt;">To iterate through a dictionary in C#, you can use a
`foreach` loop or a `for` loop.</span></p>
<p class="MsoNormal"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 115%;">Here's an example of using a `foreach` loop to iterate
through a dictionary:</span></p><p class="MsoNormal"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt; line-height: 115%;"></span></p><table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="background: #DBE5F1; border-collapse: collapse; border: none; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-border-alt: solid #B8CCE4 .5pt; mso-border-insideh-themecolor: accent1; mso-border-insideh-themetint: 102; mso-border-insideh: .5pt solid #B8CCE4; mso-border-insidev-themecolor: accent1; mso-border-insidev-themetint: 102; mso-border-insidev: .5pt solid #B8CCE4; mso-border-themecolor: accent1; mso-border-themetint: 102; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-yfti-tbllook: 1184;">
<tbody><tr>
<td style="border: solid #B8CCE4 1.0pt; mso-border-alt: solid #B8CCE4 .5pt; mso-border-themecolor: accent1; mso-border-themecolor: accent1; mso-border-themetint: 102; mso-border-themetint: 102; padding: 0cm 5.4pt 0cm 5.4pt; width: 478.8pt;" valign="top" width="638">
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"><br /></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">Dictionary<</span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt;">string</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">, </span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt;">int</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">> dict = </span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt;">new</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> Dictionary<</span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt;">string</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">, </span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt;">int</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">>();</span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">dict.Add(</span><span style="color: #a31515; font-family: "Cascadia Mono"; font-size: 9.5pt;">"apple"</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">, 3);<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">dict.Add(</span><span style="color: #a31515; font-family: "Cascadia Mono"; font-size: 9.5pt;">"banana"</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">, 2);<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">dict.Add(</span><span style="color: #a31515; font-family: "Cascadia Mono"; font-size: 9.5pt;">"cherry"</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">, 5);<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt;">foreach</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> (KeyValuePair<</span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt;">string</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">, </span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt;">int</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">> pair </span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt;">in</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> dict)<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">{<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> Console.WriteLine(</span><span style="color: #a31515; font-family: "Cascadia Mono"; font-size: 9.5pt;">"{0}: {1}"</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">, pair.Key, pair.Value);<o:p></o:p></span></p><p class="MsoNormal"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt; line-height: 14.5667px;">}</span></p><p class="MsoNormal"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt; line-height: 14.5667px;"><br /></span></p>
</td>
</tr>
</tbody></table>
<p class="MsoNormal"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 115%;">In this example, we have a dictionary that maps strings
to integers. We use a `foreach` loop to iterate over the key-value pairs in the
dictionary and print each key-value pair to the console.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 115%;">Alternatively, you can use a `for` loop to iterate
through the keys or values of the dictionary:</span></p><p class="MsoNormal"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt; line-height: 115%;"></span></p><table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="background: #DBE5F1; border-collapse: collapse; border: none; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-border-alt: solid #B8CCE4 .5pt; mso-border-insideh-themecolor: accent1; mso-border-insideh-themetint: 102; mso-border-insideh: .5pt solid #B8CCE4; mso-border-insidev-themecolor: accent1; mso-border-insidev-themetint: 102; mso-border-insidev: .5pt solid #B8CCE4; mso-border-themecolor: accent1; mso-border-themetint: 102; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-yfti-tbllook: 1184;">
<tbody><tr>
<td style="border: solid #B8CCE4 1.0pt; mso-border-alt: solid #B8CCE4 .5pt; mso-border-themecolor: accent1; mso-border-themecolor: accent1; mso-border-themetint: 102; mso-border-themetint: 102; padding: 0cm 5.4pt 0cm 5.4pt; width: 478.8pt;" valign="top" width="638">
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"><br /></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">Dictionary<</span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt;">string</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">, </span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt;">int</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">> dict = </span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt;">new</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> Dictionary<</span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt;">string</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">, </span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt;">int</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">>();</span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> dict.Add(</span><span style="color: #a31515; font-family: "Cascadia Mono"; font-size: 9.5pt;">"apple"</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">, 3);<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">dict.Add(</span><span style="color: #a31515; font-family: "Cascadia Mono"; font-size: 9.5pt;">"banana"</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">, 2);<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">dict.Add(</span><span style="color: #a31515; font-family: "Cascadia Mono"; font-size: 9.5pt;">"cherry"</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">, 5);<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> </span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt;">for</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> (</span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt;">int</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> i = 0; i<dict.Count; i++)<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">{<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt;">string</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> key = dict.Keys.ElementAt(i);<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> </span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt;">int</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> value = dict.Values.ElementAt(i);<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> Console.WriteLine(</span><span style="color: #a31515; font-family: "Cascadia Mono"; font-size: 9.5pt;">"{0}: {1}"</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">, key, value);<o:p></o:p></span></p><p class="MsoNormal"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt; line-height: 14.5667px;">}</span></p><p class="MsoNormal"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt; line-height: 14.5667px;"><br /></span></p>
</td>
</tr>
</tbody></table>
<p class="MsoNormal"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 115%;">In this example, we use a `for` loop to iterate over
the keys and values in the dictionary and print each key-value pair to the
console. We use the `Keys` and `Values` properties of the dictionary to get the
keys and values as `IEnumerable` collections, and then use the `ElementAt`
method to get the key and value at each index.<o:p></o:p></span></p>Codefarihttp://www.blogger.com/profile/14275737213961436106noreply@blogger.com0tag:blogger.com,1999:blog-34862624464291238.post-76840319003979820932023-05-01T18:54:00.004+05:302023-05-01T19:00:13.829+05:30How do I cast int to enum in C#?<p style="text-align: justify;"> <span style="font-family: Verdana, "sans-serif"; font-size: 11.5pt;">To cast an integer to an enum in C#, you can use the
`Enum.Parse` method or the `Enum.TryParse` method.</span></p>
<p class="MsoNormal" style="text-align: justify;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 115%;">Here's an example of using the `Enum.Parse` method to
convert an integer value to an enum:</span></p><p class="MsoNormal"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt; line-height: 115%;"></span></p><table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="background: #DBE5F1; border-collapse: collapse; border: none; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-border-alt: solid #B8CCE4 .5pt; mso-border-insideh-themecolor: accent1; mso-border-insideh-themetint: 102; mso-border-insideh: .5pt solid #B8CCE4; mso-border-insidev-themecolor: accent1; mso-border-insidev-themetint: 102; mso-border-insidev: .5pt solid #B8CCE4; mso-border-themecolor: accent1; mso-border-themetint: 102; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-yfti-tbllook: 1184;">
<tbody><tr>
<td style="border: solid #B8CCE4 1.0pt; mso-border-alt: solid #B8CCE4 .5pt; mso-border-themecolor: accent1; mso-border-themecolor: accent1; mso-border-themetint: 102; mso-border-themetint: 102; padding: 0cm 5.4pt 0cm 5.4pt; width: 478.8pt;" valign="top" width="638">
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt;"> </span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt;">int</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> intValue = 2;<o:p></o:p></span></p><p class="MsoNormal"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt; line-height: 14.5667px;">DaysOfWeek day = (DaysOfWeek)Enum.Parse(</span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt; line-height: 14.5667px;">typeof</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt; line-height: 14.5667px;">(DaysOfWeek), intValue.ToString());</span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt;"><br /></span></p>
</td>
</tr>
</tbody></table>
<p class="MsoNormal" style="text-align: justify;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 115%;">In this example, we have an integer value of 2 that we
want to convert to an enum of type `DaysOfWeek`. We first convert the integer
value to a string using the `ToString()` method, and then use the `Enum.Parse`
method to convert the string to the enum value. The resulting `day` variable
will have the value `Wednesday`.<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: justify;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 115%;">Alternatively, you can use the `Enum.TryParse` method
to perform the conversion and avoid throwing an exception if the integer value
cannot be converted to the specified enum:</span></p><p class="MsoNormal"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt; line-height: 115%;"></span></p><table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="background: #DBE5F1; border-collapse: collapse; border: none; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-border-alt: solid #B8CCE4 .5pt; mso-border-insideh-themecolor: accent1; mso-border-insideh-themetint: 102; mso-border-insideh: .5pt solid #B8CCE4; mso-border-insidev-themecolor: accent1; mso-border-insidev-themetint: 102; mso-border-insidev: .5pt solid #B8CCE4; mso-border-themecolor: accent1; mso-border-themetint: 102; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-yfti-tbllook: 1184;">
<tbody><tr>
<td style="border: solid #B8CCE4 1.0pt; mso-border-alt: solid #B8CCE4 .5pt; mso-border-themecolor: accent1; mso-border-themecolor: accent1; mso-border-themetint: 102; mso-border-themetint: 102; padding: 0cm 5.4pt 0cm 5.4pt; width: 478.8pt;" valign="top" width="638">
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt;"> </span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt;">int</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> intValue = 2;<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> DaysOfWeek day;<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> </span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt;">if</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> (Enum.TryParse(intValue.ToString(), </span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt;">out</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> day))<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">{<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> Console.WriteLine(day);<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">}<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt;">else</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"><o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">{<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> Console.WriteLine(</span><span style="color: #a31515; font-family: "Cascadia Mono"; font-size: 9.5pt;">"Invalid enum value."</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">);<o:p></o:p></span></p><p class="MsoNormal"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt; line-height: 14.5667px;">}</span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt;"><br /></span></p>
</td>
</tr>
</tbody></table>
<p class="MsoNormal" style="text-align: justify;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 115%;">In this example, we attempt to parse the integer value
using `Enum.TryParse` and output the resulting `day` value if the conversion is
successful. If the conversion fails, we output an error message instead.<o:p></o:p></span></p>Codefarihttp://www.blogger.com/profile/14275737213961436106noreply@blogger.com0tag:blogger.com,1999:blog-34862624464291238.post-41980877567658882172023-05-01T16:42:00.004+05:302023-05-01T18:59:47.800+05:30What is the procedure for iterating over an enum in C#?<p> <span style="font-family: Verdana, "sans-serif"; font-size: 11.5pt;">To iterate through an enum in C#, you can use the
`foreach` loop or the `Enum.GetValues` method.</span></p>
<p class="MsoNormal"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 115%;">Here's an example of using a `foreach` loop to iterate
through an enum:</span></p><p class="MsoNormal"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt; line-height: 115%;"></span></p><table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="background: #DBE5F1; border-collapse: collapse; border: none; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-border-alt: solid #B8CCE4 .5pt; mso-border-insideh-themecolor: accent1; mso-border-insideh-themetint: 102; mso-border-insideh: .5pt solid #B8CCE4; mso-border-insidev-themecolor: accent1; mso-border-insidev-themetint: 102; mso-border-insidev: .5pt solid #B8CCE4; mso-border-themecolor: accent1; mso-border-themetint: 102; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-yfti-tbllook: 1184;">
<tbody><tr>
<td style="border: solid #B8CCE4 1.0pt; mso-border-alt: solid #B8CCE4 .5pt; mso-border-themecolor: accent1; mso-border-themecolor: accent1; mso-border-themetint: 102; mso-border-themetint: 102; padding: 0cm 5.4pt 0cm 5.4pt; width: 478.8pt;" valign="top" width="638">
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt;"><br /></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt;">enum</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> </span><span style="color: #2b91af; font-family: "Cascadia Mono"; font-size: 9.5pt;">DaysOfWeek</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday };<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt;">foreach</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> (DaysOfWeek day </span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt;">in</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> Enum.GetValues(</span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt;">typeof</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">(DaysOfWeek)))<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">{<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> Console.WriteLine(day);<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">}</span><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt;"> </span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt;"><br /></span></p>
</td>
</tr>
</tbody></table>
<p class="MsoNormal"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 115%;">This will output all the values of the `DaysOfWeek`
enum: `Monday`, `Tuesday`, `Wednesday`, `Thursday`, `Friday`, `Saturday`, and
`Sunday`.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 115%;">Alternatively, you can use the `Enum.GetValues` method
to get an array of all the enum values and iterate through it using a `for`
loop or a `foreach` loop:</span></p><p class="MsoNormal"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt; line-height: 115%;"></span></p><table border="1" cellpadding="0" cellspacing="0" class="MsoTableGrid" style="background: #DBE5F1; border-collapse: collapse; border: none; mso-background-themecolor: accent1; mso-background-themetint: 51; mso-border-alt: solid #B8CCE4 .5pt; mso-border-insideh-themecolor: accent1; mso-border-insideh-themetint: 102; mso-border-insideh: .5pt solid #B8CCE4; mso-border-insidev-themecolor: accent1; mso-border-insidev-themetint: 102; mso-border-insidev: .5pt solid #B8CCE4; mso-border-themecolor: accent1; mso-border-themetint: 102; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt; mso-yfti-tbllook: 1184;">
<tbody><tr>
<td style="border: solid #B8CCE4 1.0pt; mso-border-alt: solid #B8CCE4 .5pt; mso-border-themecolor: accent1; mso-border-themecolor: accent1; mso-border-themetint: 102; mso-border-themetint: 102; padding: 0cm 5.4pt 0cm 5.4pt; width: 478.8pt;" valign="top" width="638">
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"><br /></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">DaysOfWeek[] values = (DaysOfWeek[])Enum.GetValues(</span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt;">typeof</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">(DaysOfWeek));</span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt;">foreach</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> (DaysOfWeek day </span><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt;">in</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> values)<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">{<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> Console.WriteLine(day);<o:p></o:p></span></p><p class="MsoNormal"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt; line-height: 14.5667px;">}</span></p><p class="MsoNormal"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt; line-height: 14.5667px;"><br /></span></p>
</td>
</tr>
</tbody></table>
<p class="MsoNormal"><span style="font-family: "Verdana","sans-serif"; font-size: 11.5pt; line-height: 115%;">This will also output all the values of the
`DaysOfWeek` enum in the same order.<o:p></o:p></span></p>Codefarihttp://www.blogger.com/profile/14275737213961436106noreply@blogger.com0tag:blogger.com,1999:blog-34862624464291238.post-46721705749993622602023-05-01T16:24:00.001+05:302023-05-01T16:25:18.884+05:30How does the data type String differ from string in C#?<p><span style="font-family: Verdana, "sans-serif"; font-size: 11.5pt;">What
distinguishes these two and which one is more appropriate for use in C#?<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt;"><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt; mso-bidi-font-family: "Cascadia Mono";">string</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> s = </span><span style="color: #a31515; font-family: "Cascadia Mono"; font-size: 9.5pt; mso-bidi-font-family: "Cascadia Mono";">"Codefari"</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">;<o:p></o:p></span></p><h2><span style="font-family: "Cascadia Mono"; font-size: 9.5pt; line-height: 115%;">String s = </span><span style="color: #a31515; font-family: "Cascadia Mono"; font-size: 9.5pt; line-height: 115%; mso-bidi-font-family: "Cascadia Mono";">"Codefari"</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt; line-height: 115%;">;<o:p></o:p></span></h2><h2>Solution: </h2><p><span style="font-family: Verdana, "sans-serif"; font-size: 11.5pt;">The
distinction between these two statements is the case of the data type used to
declare the variable. In C#, "string" with a lowercase "s"
is a keyword that represents the built-in string data type. On the other hand,
"String" with an uppercase "S" is a class from the .NET
Framework that is used to create string objects.<o:p></o:p></span></p><p><span style="font-family: Verdana, "sans-serif"; font-size: 11.5pt;">In practice, both "string" and "String" can be
used interchangeably in C#, as they are aliases of each other. However, it is a
convention in C# to use "string" rather than "String" when
declaring variables of string type.<o:p></o:p></span></p><p>
</p><p><span style="font-family: Verdana, "sans-serif"; font-size: 11.5pt;">Therefore, it is more appropriate to use "string s =
"Codefari";" when declaring a variable of string type in C#.<o:p></o:p></span></p>Codefarihttp://www.blogger.com/profile/14275737213961436106noreply@blogger.com0tag:blogger.com,1999:blog-34862624464291238.post-38702129905866012852023-02-28T20:24:00.004+05:302023-02-28T20:24:45.059+05:30How to drop a PostgreSQL database if there are active connections to it?<p class="MsoNormal"><span style="font-size: 11.5pt; line-height: 107%;">In PostgreSQL, you cannot drop a
database if there are active connections to it. You must first terminate all
connections to the database before you can drop it. Here are the steps you can
follow to drop a PostgreSQL database with active connections:<o:p></o:p></span></p><p class="MsoNormal"></p><span style="font-size: 11.5pt;">1- Id</span><span style="font-size: 11.5pt;">entify the active connections to the
database you want to drop. You can use the following SQL query to list all
active connections:</span><br /><p></p><table border="0" cellpadding="0" cellspacing="0" class="MsoNormalTable" style="background: #F2F2F2; border-collapse: collapse; mso-padding-alt: 0cm 0cm 0cm 0cm; mso-yfti-tbllook: 1184;">
<tbody><tr>
<td style="border: solid #D6E3BC 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 478.8pt;" valign="top" width="638">
<p class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-line-height-alt: 2.5pt; mso-margin-top-alt: auto;"><span style="color: #666666; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-IN;"> <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">SELECT</span><span style="font-size: 11.5pt;">
pg_terminate_backend</span><span style="color: grey; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">(</span><span style="font-size: 11.5pt;">pg_stat_activity</span><span style="color: grey; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">.</span><span style="font-size: 11.5pt;">pid</span><span style="color: grey; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">)</span><span style="font-size: 11.5pt;"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">FROM</span><span style="font-size: 11.5pt;"> pg_stat_activity<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">WHERE</span><span style="font-size: 11.5pt;"> pg_stat_activity</span><span style="color: grey; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">.</span><span style="font-size: 11.5pt;">datname </span><span style="color: grey; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">=</span><span style="font-size: 11.5pt;"> </span><span style="color: red; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">'your_database_name'</span><span style="font-size: 11.5pt;"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 11.5pt;"> </span><span style="color: grey; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">AND</span><span style="font-size: 11.5pt;"> pid </span><span style="color: grey; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><></span><span style="font-size: 11.5pt;"> pg_backend_pid</span><span style="color: grey; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">();</span><span style="font-size: 11.5pt;"><o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: grey; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><br /></span></p>
</td>
</tr>
</tbody></table><p class="MsoNormal"><span style="font-size: 11.5pt; line-height: 107%;"> </span><span style="font-size: 11.5pt;">This query will terminate all
connections to the database except for the current connection.</span></p><p class="MsoNormal"><span style="font-size: 11.5pt; line-height: 107%;">2- After running the above query, try to
drop the database using the following SQL command:<o:p></o:p></span></p><table border="0" cellpadding="0" cellspacing="0" class="MsoNormalTable" style="background: #F2F2F2; border-collapse: collapse; mso-padding-alt: 0cm 0cm 0cm 0cm; mso-yfti-tbllook: 1184;">
<tbody><tr>
<td style="border: solid #D6E3BC 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 450.3pt;" valign="top" width="600">
<p class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-line-height-alt: 2.5pt; mso-margin-top-alt: auto;"><span style="color: #666666; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-IN;"> </span></p><p class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-line-height-alt: 2.5pt; mso-margin-top-alt: auto;"><span style="font-size: 11.5pt;">DROP DATABASE your_database_name;</span></p><p class="MsoNormal"><span style="font-size: 11.5pt; line-height: 107%;"><br /></span></p>
</td>
</tr>
</tbody></table><p class="MsoNormal"><span style="font-size: 11.5pt; line-height: 107%;"> </span><span style="font-size: 11.5pt;">If there are no more active connections
to the database, this command will drop the database.</span></p><p class="MsoNormal"><span style="font-size: 11.5pt; line-height: 107%;">If there are still active connections
to the database, you will get an error message indicating that the database
cannot be dropped because there are still active connections. In this case,
repeat step 1 to ensure all connections have been terminated, and then try
again to drop the database.<o:p></o:p></span></p><p>
</p><p class="MsoNormal"><span style="font-size: 11.5pt; line-height: 107%;">Note that dropping a database will
permanently delete all data in the database. Make sure to take appropriate
backups and verify that you are dropping the correct database before
proceeding.</span><span style="font-size: 11.5pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><o:p></o:p></span></p>Codefarihttp://www.blogger.com/profile/14275737213961436106noreply@blogger.com0tag:blogger.com,1999:blog-34862624464291238.post-56415404337062126182023-02-28T20:13:00.005+05:302023-02-28T20:13:54.056+05:30Finding the maximum difference between columns from different rows in Postgresql<p><span style="font-size: 11.5pt;">To find the maximum difference between
columns from different rows in PostgreSQL, you can use a combination of window
functions and subqueries. Here is an example query:</span></p>
<table border="0" cellpadding="0" cellspacing="0" class="MsoNormalTable" style="background: rgb(242, 242, 242); border-collapse: collapse; mso-padding-alt: 0cm 0cm 0cm 0cm; mso-yfti-tbllook: 1184;">
<tbody><tr>
<td style="border: 1pt solid rgb(214, 227, 188); padding: 0cm 5.4pt; width: 478.8pt;" valign="top" width="638">
<p class="MsoNormal" style="margin-bottom: 0cm; mso-line-height-alt: 2.5pt; mso-margin-top-alt: auto;"><span style="color: #666666; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-IN;"> </span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">SELECT</span><span style="font-size: 11.5pt;"> </span><span style="color: magenta; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">MAX</span><span style="color: grey; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">(</span><span style="color: magenta; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">abs</span><span style="color: grey; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">(</span><span style="font-size: 11.5pt;">t1</span><span style="color: grey; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">.</span><span style="font-size: 11.5pt;">col1 </span><span style="color: grey; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">-</span><span style="font-size: 11.5pt;"> t2</span><span style="color: grey; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">.</span><span style="font-size: 11.5pt;">col1</span><span style="color: grey; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">))</span><span style="font-size: 11.5pt;"> </span><span style="color: blue; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">AS</span><span style="font-size: 11.5pt;"> max_diff<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">FROM </span><span style="color: grey; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">(</span><span style="font-size: 11.5pt;"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 11.5pt;"> </span><span style="color: blue; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">SELECT</span><span style="font-size: 11.5pt;">
col1</span><span style="color: grey; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">,</span><span style="font-size: 11.5pt;"> </span><span style="color: magenta; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">ROW_NUMBER</span><span style="color: grey; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">()</span><span style="font-size: 11.5pt;"> </span><span style="color: blue; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">OVER </span><span style="color: grey; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">(</span><span style="color: blue; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">ORDER</span><span style="font-size: 11.5pt;"> </span><span style="color: blue; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">BY</span><span style="font-size: 11.5pt;"> id</span><span style="color: grey; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">)</span><span style="font-size: 11.5pt;"> </span><span style="color: blue; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">AS</span><span style="font-size: 11.5pt;"> row_num<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 11.5pt;"> </span><span style="color: blue; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">FROM</span><span style="font-size: 11.5pt;"> table_name<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: grey; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">)</span><span style="font-size: 11.5pt;"> </span><span style="color: blue; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">AS</span><span style="font-size: 11.5pt;"> t1<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: grey; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">JOIN</span><span style="color: blue; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"> </span><span style="color: grey; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">(</span><span style="font-size: 11.5pt;"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 11.5pt;"> </span><span style="color: blue; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">SELECT</span><span style="font-size: 11.5pt;">
col1</span><span style="color: grey; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">,</span><span style="font-size: 11.5pt;"> </span><span style="color: magenta; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">ROW_NUMBER</span><span style="color: grey; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">()</span><span style="font-size: 11.5pt;"> </span><span style="color: blue; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">OVER </span><span style="color: grey; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">(</span><span style="color: blue; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">ORDER</span><span style="font-size: 11.5pt;"> </span><span style="color: blue; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">BY</span><span style="font-size: 11.5pt;"> id</span><span style="color: grey; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">)</span><span style="font-size: 11.5pt;"> </span><span style="color: blue; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">AS</span><span style="font-size: 11.5pt;"> row_num<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-size: 11.5pt;"> </span><span style="color: blue; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">FROM</span><span style="font-size: 11.5pt;"> table_name<o:p></o:p></span></p>
<p class="MsoNormal" style="margin-bottom: 0cm; mso-line-height-alt: 2.5pt; mso-margin-top-alt: auto;"><span style="color: grey; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">)</span><span style="font-size: 11.5pt;"> </span><span style="color: blue; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">AS</span><span style="font-size: 11.5pt;"> t2 </span><span style="color: blue; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">ON</span><span style="font-size: 11.5pt;"> t1</span><span style="color: grey; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">.</span><span style="font-size: 11.5pt;">row_num </span><span style="color: grey; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><</span><span style="font-size: 11.5pt;"> t2</span><span style="color: grey; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">.</span><span style="font-size: 11.5pt;">row_num</span><span style="color: #666666; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-IN;"> <o:p></o:p></span></p><p class="MsoNormal" style="margin-bottom: 0cm; mso-line-height-alt: 2.5pt; mso-margin-top-alt: auto;"><span style="color: #666666; font-size: 11.5pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-IN;"><br /></span></p>
</td>
</tr>
</tbody></table>
<p class="MsoNormal"><span style="color: #666666; font-size: 11.5pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-IN;"> </span><span style="font-size: 11.5pt;">This query will calculate the absolute
difference between col1 values from all possible pairs of rows in the table and then return the maximum difference.</span></p>
<p class="MsoNormal"><span style="font-size: 11.5pt; line-height: 107%;">Here's a breakdown of how the query
works:<o:p></o:p></span></p>
<p class="MsoNormal"></p><ul style="text-align: left;"><li><span style="font-size: 11.5pt; line-height: 107%;">We use two subqueries (t1 and t2) to
generate row numbers for each row in the table, using the ROW_NUMBER() window
function. We order the rows by the primary key column (id) to ensure consistent ordering.</span></li><li><span style="font-size: 11.5pt; line-height: 107%;">We join the two subqueries on the row
numbers, with t1.row_num < t2.row_num to ensure that we only compare each
row to all subsequent rows.</span></li><li><span style="font-size: 11.5pt; line-height: 107%;">We calculate the absolute difference
between the col1 values from t1 and t2 using abs(t1.col1 - t2.col1).</span></li><li><span style="font-size: 11.5pt; line-height: 107%;">Finally, we use MAX() to return the
maximum difference across all pairs of rows.</span></li></ul><p></p>
<p class="MsoNormal"><span style="font-size: 11.5pt; line-height: 107%;">Note that this query only considers one
column (col1) for simplicity, but you can modify it to include additional
columns as needed.</span><span face=""Calibri Light",sans-serif" style="mso-ascii-theme-font: major-latin; mso-bidi-theme-font: major-latin; mso-hansi-theme-font: major-latin;"><o:p></o:p></span></p>Codefarihttp://www.blogger.com/profile/14275737213961436106noreply@blogger.com0tag:blogger.com,1999:blog-34862624464291238.post-13335152273783689532023-02-28T20:05:00.000+05:302023-02-28T20:05:04.995+05:30How do I force Postgres to respect the configuration file on service start? Or how can I assign shared_buffer and max_connections on boot?<p> <span style="font-family: Calibri, sans-serif; font-size: 11.5pt;">By default, Postgres will use the values specified in its
configuration file when it starts up. However, there are a few things you can
do to ensure that it respects the configuration file and uses the values you
specify.</span></p>
<p><span style="font-family: Calibri, sans-serif; font-size: 11.5pt;">First, make sure that the configuration file is in the correct
location and has the correct name. By default, the configuration file is named
postgresql.conf and is located in the data directory. You can specify a
different configuration file using the -D and -c options when starting the
Postgres service.<o:p></o:p></span></p>
<p><span style="font-family: Calibri, sans-serif; font-size: 11.5pt;">Second, check the output of the Postgres service startup logs to
see if there are any errors or warnings related to the configuration file. If
there are errors or warnings, you will need to address those issues before the
service will start using the values specified in the configuration file.<o:p></o:p></span></p>
<p><span style="font-family: Calibri, sans-serif; font-size: 11.5pt;">If you still have trouble getting Postgres to use the values
specified in the configuration file, you can try explicitly setting the values
using the -c option when starting the service. For example, to set the
shared_buffers and max_connections parameters, you can use the following
command:</span><span style="color: #666666; font-family: "Arial",sans-serif; font-size: 10.0pt;"><o:p></o:p></span></p>
<table border="0" cellpadding="0" cellspacing="0" class="MsoNormalTable" style="background: #F2F2F2; border-collapse: collapse; mso-padding-alt: 0cm 0cm 0cm 0cm; mso-yfti-tbllook: 1184;">
<tbody><tr>
<td style="border: solid #D6E3BC 1.0pt; padding: 0cm 5.4pt 0cm 5.4pt; width: 478.8pt;" valign="top" width="638">
<p class="MsoNormal" style="margin-bottom: .0001pt; margin-bottom: 0cm; mso-line-height-alt: 2.5pt; mso-margin-top-alt: auto;"><span style="color: #666666; font-family: "Arial",sans-serif; font-size: 10.0pt; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-IN;"> <o:p></o:p></span></p>
<p><span style="font-family: Calibri, sans-serif; font-size: 11.5pt;">sudo systemctl start postgresql -c
"shared_buffers=512MB" -c "max_connections=100"<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: justify;"><span style="color: #666666; font-family: "Times New Roman",serif; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-IN;"> </span><span style="color: #666666; font-family: "Arial",sans-serif; font-size: 10.0pt; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-IN;"><o:p></o:p></span></p>
</td>
</tr>
</tbody></table>
<p><span style="font-family: Calibri, sans-serif; font-size: 11.5pt;">This will start the Postgres service with shared_buffers set to
512MB and max_connections set to 100. Note that this approach will override any
values specified in the configuration file, so use it with caution.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 11.5pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"> </span></p>Codefarihttp://www.blogger.com/profile/14275737213961436106noreply@blogger.com0tag:blogger.com,1999:blog-34862624464291238.post-23830471575994477102023-02-25T05:01:00.003+05:302023-02-25T05:03:27.659+05:30What is data annotation?<p class="MsoNormal" style="text-align: justify;"><span style="font-size: 11.5pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Data
annotation is a way to define metadata for model properties in .NET
applications. This metadata can be used for various purposes, such as
validation, formatting, and documentation. In .NET Core 6.0, data annotations
are implemented using attributes that can be applied to model properties.<o:p></o:p></span></p>
<h2 style="text-align: justify;"><span face=""Calibri",sans-serif" style="font-size: 18pt; line-height: 107%; mso-ascii-theme-font: minor-latin; mso-bidi-theme-font: minor-latin; mso-hansi-theme-font: minor-latin;">Why would I use Data Annotation?<o:p></o:p></span></h2>
<p class="MsoNormal" style="text-align: justify;"><b><span style="font-size: 11.5pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Here are some reasons why you might
want to use data annotations in your .NET application:<o:p></o:p></span></b></p>
<p class="MsoNormal" style="text-align: justify;"></p><ul><li><span style="font-size: 11.5pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><b>Validation:
</b>Data annotations can be used to validate input data and ensure that it meets
specific criteria. For example, you can use the [Required] attribute to ensure
that a property is not null or empty, or the [RegularExpression] attribute to
ensure that a property matches a specific pattern.</span></li><li><span style="font-size: 11.5pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><b>Formatting:</b>
Data annotations can be used to format property values in a specific way. For
example, you can use the [DataType(DataType.Date)] attribute to ensure that a
date property is displayed in a specific format.</span></li><li><span style="font-size: 11.5pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><b>Documentation:</b>
Data annotations can be used to provide additional information about a model
property, which can be used for documentation purposes. For example, you can
use the [Display(Name = "First Name")] attribute to specify the
display name for a property.</span></li><li><span style="font-size: 11.5pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><b>Simplify
Code:</b> Using data annotations can help simplify code by reducing the amount of
validation code that needs to be written. Instead of writing custom validation
logic, you can use data annotations to perform common validation tasks.</span></li></ul><p></p>
<p class="MsoNormal" style="text-align: justify;"><span style="font-size: 11.5pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Here's
an example of how to use data annotations in .NET Core 6.0:<o:p></o:p></span></p>
<table border="0" cellpadding="0" cellspacing="0" class="MsoNormalTable" style="background: rgb(242, 242, 242); border-collapse: collapse; mso-padding-alt: 0cm 0cm 0cm 0cm; mso-yfti-tbllook: 1184;">
<tbody><tr>
<td style="border: 1pt solid rgb(214, 227, 188); padding: 0cm 5.4pt; width: 478.8pt;" valign="top" width="638">
<p class="MsoNormal" style="line-height: 25%; margin-bottom: 0cm;"><span face=""Arial",sans-serif" style="color: #666666; font-size: 10pt; line-height: 25%; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-IN;"> </span></p>
<p class="MsoNormal" style="line-height: 25%; margin-bottom: 0cm; mso-margin-top-alt: auto; text-align: justify;"><span style="color: blue; font-family: "Times New Roman",serif; font-size: 11.5pt; line-height: 25%; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-IN;">using</span><span style="font-family: "Times New Roman", serif; font-size: 11.5pt; line-height: 25%;"> System.ComponentModel.DataAnnotations;</span><span style="font-family: "Times New Roman", serif; font-size: 13.5pt; line-height: 25%;"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 25%; margin-bottom: 0cm; mso-margin-top-alt: auto; text-align: justify;"><span style="color: blue; font-family: "Times New Roman",serif; font-size: 11.5pt; line-height: 25%; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-IN;">public</span><span style="font-family: "Times New Roman", serif; font-size: 11.5pt; line-height: 25%;"> </span><span style="color: blue; font-family: "Times New Roman",serif; font-size: 11.5pt; line-height: 25%; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-IN;">class</span><span style="font-family: "Times New Roman", serif; font-size: 11.5pt; line-height: 25%;"> </span><span style="color: #2b91af; font-family: "Times New Roman",serif; font-size: 11.5pt; line-height: 25%; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-IN;">Person</span><span style="font-family: "Times New Roman", serif; font-size: 13.5pt; line-height: 25%;"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 25%; margin-bottom: 0cm; mso-margin-top-alt: auto; text-align: justify;"><span style="font-family: "Times New Roman", serif; font-size: 11.5pt; line-height: 25%;">{</span><span style="font-family: "Times New Roman", serif; font-size: 13.5pt; line-height: 25%;"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 25%; margin-bottom: 0cm; mso-margin-top-alt: auto; text-align: justify;"><span style="font-family: "Times New Roman", serif; font-size: 11.5pt; line-height: 25%;"> [Required(ErrorMessage = </span><span style="color: #a31515; font-family: "Times New Roman",serif; font-size: 11.5pt; line-height: 25%; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-IN;">"Name is required"</span><span style="font-family: "Times New Roman", serif; font-size: 11.5pt; line-height: 25%;">)]</span><span style="font-family: "Times New Roman", serif; font-size: 13.5pt; line-height: 25%;"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 25%; margin-bottom: 0cm; mso-margin-top-alt: auto; text-align: justify;"><span style="font-family: "Times New Roman", serif; font-size: 11.5pt; line-height: 25%;"> </span><span style="color: blue; font-family: "Times New Roman",serif; font-size: 11.5pt; line-height: 25%; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-IN;">public</span><span style="font-family: "Times New Roman", serif; font-size: 11.5pt; line-height: 25%;"> </span><span style="color: blue; font-family: "Times New Roman",serif; font-size: 11.5pt; line-height: 25%; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-IN;">string</span><span style="font-family: "Times New Roman", serif; font-size: 11.5pt; line-height: 25%;"> Name { </span><span style="color: blue; font-family: "Times New Roman",serif; font-size: 11.5pt; line-height: 25%; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-IN;">get</span><span style="font-family: "Times New Roman", serif; font-size: 11.5pt; line-height: 25%;">; </span><span style="color: blue; font-family: "Times New Roman",serif; font-size: 11.5pt; line-height: 25%; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-IN;">set</span><span style="font-family: "Times New Roman", serif; font-size: 11.5pt; line-height: 25%;">; }</span><span style="font-family: "Times New Roman", serif; font-size: 13.5pt; line-height: 25%;"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 25%; margin-bottom: 0cm; mso-margin-top-alt: auto; text-align: justify;"><span style="font-family: "Times New Roman", serif; font-size: 11.5pt; line-height: 25%;"> </span><span style="font-family: "Times New Roman", serif; font-size: 13.5pt; line-height: 25%;"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 25%; margin-bottom: 0cm; mso-margin-top-alt: auto; text-align: justify;"><span style="font-family: "Times New Roman", serif; font-size: 11.5pt; line-height: 25%;"> [EmailAddress(ErrorMessage = </span><span style="color: #a31515; font-family: "Times New Roman",serif; font-size: 11.5pt; line-height: 25%; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-IN;">"Invalid email address"</span><span style="font-family: "Times New Roman", serif; font-size: 11.5pt; line-height: 25%;">)]</span><span style="font-family: "Times New Roman", serif; font-size: 13.5pt; line-height: 25%;"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 25%; margin-bottom: 0cm; mso-margin-top-alt: auto; text-align: justify;"><span style="font-family: "Times New Roman", serif; font-size: 11.5pt; line-height: 25%;"> </span><span style="color: blue; font-family: "Times New Roman",serif; font-size: 11.5pt; line-height: 25%; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-IN;">public</span><span style="font-family: "Times New Roman", serif; font-size: 11.5pt; line-height: 25%;"> </span><span style="color: blue; font-family: "Times New Roman",serif; font-size: 11.5pt; line-height: 25%; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-IN;">string</span><span style="font-family: "Times New Roman", serif; font-size: 11.5pt; line-height: 25%;"> Email { </span><span style="color: blue; font-family: "Times New Roman",serif; font-size: 11.5pt; line-height: 25%; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-IN;">get</span><span style="font-family: "Times New Roman", serif; font-size: 11.5pt; line-height: 25%;">; </span><span style="color: blue; font-family: "Times New Roman",serif; font-size: 11.5pt; line-height: 25%; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-IN;">set</span><span style="font-family: "Times New Roman", serif; font-size: 11.5pt; line-height: 25%;">; }</span><span style="font-family: "Times New Roman", serif; font-size: 13.5pt; line-height: 25%;"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 25%; margin-bottom: 0cm; mso-margin-top-alt: auto; text-align: justify;"><span style="font-family: "Times New Roman", serif; font-size: 11.5pt; line-height: 25%;"> </span><span style="font-family: "Times New Roman", serif; font-size: 13.5pt; line-height: 25%;"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 25%; margin-bottom: 0cm; mso-margin-top-alt: auto; text-align: justify;"><span style="font-family: "Times New Roman", serif; font-size: 11.5pt; line-height: 25%;"> [Range(18, 99, ErrorMessage = </span><span style="color: #a31515; font-family: "Times New Roman",serif; font-size: 11.5pt; line-height: 25%; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-IN;">"Age must be between 18 and 99"</span><span style="font-family: "Times New Roman", serif; font-size: 11.5pt; line-height: 25%;">)]</span><span style="font-family: "Times New Roman", serif; font-size: 13.5pt; line-height: 25%;"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 25%; margin-bottom: 0cm; mso-margin-top-alt: auto; text-align: justify;"><span style="font-family: "Times New Roman", serif; font-size: 11.5pt; line-height: 25%;"> </span><span style="color: blue; font-family: "Times New Roman",serif; font-size: 11.5pt; line-height: 25%; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-IN;">public</span><span style="font-family: "Times New Roman", serif; font-size: 11.5pt; line-height: 25%;"> </span><span style="color: blue; font-family: "Times New Roman",serif; font-size: 11.5pt; line-height: 25%; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-IN;">int</span><span style="font-family: "Times New Roman", serif; font-size: 11.5pt; line-height: 25%;"> Age { </span><span style="color: blue; font-family: "Times New Roman",serif; font-size: 11.5pt; line-height: 25%; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-IN;">get</span><span style="font-family: "Times New Roman", serif; font-size: 11.5pt; line-height: 25%;">; </span><span style="color: blue; font-family: "Times New Roman",serif; font-size: 11.5pt; line-height: 25%; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-IN;">set</span><span style="font-family: "Times New Roman", serif; font-size: 11.5pt; line-height: 25%;">; }</span><span style="font-family: "Times New Roman", serif; font-size: 13.5pt; line-height: 25%;"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 25%; margin-bottom: 0cm; mso-margin-top-alt: auto; text-align: justify;"><span style="font-family: "Times New Roman", serif; font-size: 11.5pt; line-height: 25%;">}</span><span style="font-family: "Times New Roman", serif; font-size: 13.5pt; line-height: 25%;"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 25%; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; text-align: justify;"><span style="font-family: "Times New Roman", serif; font-size: 11.5pt; line-height: 25%;"> </span><span style="font-family: "Times New Roman", serif; font-size: 13.5pt; line-height: 25%;"><o:p></o:p></span></p>
</td>
</tr>
</tbody></table>
<p class="MsoNormal" style="text-align: justify;"><span style="font-size: 11.5pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">In
this example, we have defined a Person class with three properties: Name,
Email, and Age. We have used data annotations to define metadata for each
property.<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: justify;"><span style="font-size: 11.5pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">The
[Required] attribute specifies that the Name property is required and will
generate an error message if it is not present. The [EmailAddress] attribute
specifies that the Email property must be a valid email address. The [Range]
attribute specifies that the Age property must be between 18 and 99.<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: justify;"><span style="font-size: 11.5pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Data
annotations can be used in conjunction with model binding and validation in
ASP.NET Core controllers to ensure that the data being submitted by users is
valid and meets the required criteria.</span></p><table border="0" cellpadding="0" cellspacing="0" class="MsoNormalTable" style="background: rgb(242, 242, 242); border-collapse: collapse; mso-padding-alt: 0cm 0cm 0cm 0cm; mso-yfti-tbllook: 1184;">
<tbody><tr>
<td style="border: 1pt solid rgb(214, 227, 188); padding: 0cm 5.4pt; width: 478.8pt;" valign="top" width="638">
<p class="MsoNormal" style="margin-bottom: 0cm; mso-line-height-alt: .25pt;"><span face=""Arial",sans-serif" style="color: #666666; font-size: 10pt; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-IN;"> </span></p>
<p class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; margin-bottom: 0.0001pt; text-align: justify;"><span style="font-family: "Times New Roman", serif; font-size: 11.5pt;">[HttpPost]</span><span style="font-family: "Times New Roman", serif; font-size: 13.5pt;"><o:p></o:p></span></p>
<p class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; margin-bottom: 0.0001pt; text-align: justify;"><span style="color: blue; font-family: "Times New Roman",serif; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-IN;">public</span><span style="font-family: "Times New Roman", serif; font-size: 11.5pt;"> IActionResult Create(Person person)</span><span style="font-family: "Times New Roman", serif; font-size: 13.5pt;"><o:p></o:p></span></p>
<p class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; margin-bottom: 0.0001pt; text-align: justify;"><span style="font-family: "Times New Roman", serif; font-size: 11.5pt;">{</span><span style="font-family: "Times New Roman", serif; font-size: 13.5pt;"><o:p></o:p></span></p>
<p class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; margin-bottom: 0.0001pt; text-align: justify;"><span style="font-family: "Times New Roman", serif; font-size: 11.5pt;"> </span><span style="color: blue; font-family: "Times New Roman",serif; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-IN;">if</span><span style="font-family: "Times New Roman", serif; font-size: 11.5pt;"> (!ModelState.IsValid)</span><span style="font-family: "Times New Roman", serif; font-size: 13.5pt;"><o:p></o:p></span></p>
<p class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; margin-bottom: 0.0001pt; text-align: justify;"><span style="font-family: "Times New Roman", serif; font-size: 11.5pt;"> {</span><span style="font-family: "Times New Roman", serif; font-size: 13.5pt;"><o:p></o:p></span></p>
<p class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; margin-bottom: 0.0001pt; text-align: justify;"><span style="font-family: "Times New Roman", serif; font-size: 11.5pt;"> </span><span style="color: blue; font-family: "Times New Roman",serif; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-IN;">return</span><span style="font-family: "Times New Roman", serif; font-size: 11.5pt;"> BadRequest(ModelState);</span><span style="font-family: "Times New Roman", serif; font-size: 13.5pt;"><o:p></o:p></span></p>
<p class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; margin-bottom: 0.0001pt; text-align: justify;"><span style="font-family: "Times New Roman", serif; font-size: 11.5pt;"> }</span><span style="font-family: "Times New Roman", serif; font-size: 13.5pt;"><o:p></o:p></span></p>
<p class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; margin-bottom: 0.0001pt; text-align: justify;"><span style="font-family: "Times New Roman", serif; font-size: 11.5pt;"> </span><span style="color: blue; font-family: "Times New Roman",serif; font-size: 11.5pt; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-IN;">return</span><span style="font-family: "Times New Roman", serif; font-size: 11.5pt;"> Ok();</span><span style="font-family: "Times New Roman", serif; font-size: 13.5pt;"><o:p></o:p></span></p>
<p class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; margin-bottom: 0.0001pt;"><span style="font-family: "Times New Roman", serif; font-size: 11.5pt;">}</span><span face=""Arial",sans-serif" style="color: #666666; font-size: 10pt; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-IN;"> </span></p><p class="MsoNormal" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; margin-bottom: 0.0001pt;"><span style="font-family: "Times New Roman", serif; font-size: 11.5pt;"> </span></p>
</td>
</tr>
</tbody></table><p class="MsoNormal" style="text-align: justify;"><span style="font-size: 11.5pt;">In
this example, we have defined a Create action that accepts a Person object as
input. We have used ModelState.IsValid to check whether the data being
submitted is valid, and if not, we return a BadRequest response with the error
messages generated by the data annotations.</span></p><p><span style="font-size: 11.5pt; text-align: justify;">Overall,
data annotations provide a convenient way to define metadata for model
properties in .NET Core 6.0 applications, and can be used for a variety of
purposes such as validation, formatting, and documentation.</span> </p>Codefarihttp://www.blogger.com/profile/14275737213961436106noreply@blogger.com0tag:blogger.com,1999:blog-34862624464291238.post-38110224575100518792023-02-22T23:42:00.004+05:302023-02-28T20:06:20.377+05:30What is new in .Net 6.0?<p><span style="font-family: Verdana, sans-serif; font-size: 11.5pt;">.NET Core 6.0 is a major release of the .NET Core
framework that was released in November 2021. It is an open-source,
cross-platform framework for building modern applications, including web
applications, microservices, desktop applications, and more.</span></p>
<p class="MsoNormal"><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 107%;">Some of the key features and improvements in .NET Core
6.0 include:<o:p></o:p></span></p>
<p class="MsoNormal"><b><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 107%;">Performance
improvements -</span></b><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 107%;"> .NET Core 6.0 includes various runtime and
framework performance improvements, making it faster and more efficient than
previous versions.<o:p></o:p></span></p>
<p class="MsoNormal"><b><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 107%;">ASP.NET
Core updates - </span></b><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 107%;">This release includes new features in ASP.NET
Core such as Web API improvements, better Blazor support, and more.<o:p></o:p></span></p>
<p class="MsoNormal"><b><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 107%;">Improved
productivity - </span></b><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 107%;">.NET 6.0 includes many new features designed
to improve developer productivity, including support for C# 10, top-level
statements, file-scoped namespaces, and more.<o:p></o:p></span></p>
<p class="MsoNormal"><b><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 107%;">Better
support for cloud-native applications - </span></b><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 107%;">.NET 6.0 includes better
support for building and running cloud-native applications, including
containerization, Kubernetes, and Azure Functions.<o:p></o:p></span></p>
<p class="MsoNormal"><b><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 107%;">New
APIs and libraries - </span></b><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 107%;">.NET 6.0 introduces new APIs and libraries,
including new APIs for HTTP/3 and gRPC, as well as new libraries for JSON, XML,
and more.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 107%;">Overall, .NET Core 6.0 is a major release that brings
many new features and improvements to the framework, making it a great choice
for building modern, high-performance applications.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: "Verdana",sans-serif; mso-bidi-font-family: Arial;"> </span></p>
<h3><span style="font-family: "Verdana",sans-serif; mso-bidi-font-family: Arial;">Some
fundamental things we should know when we started to work with .Net Core 6.0<o:p></o:p></span></h3>
<p class="MsoNormal"><span style="font-family: "Verdana",sans-serif;"> </span></p>
<h2><span style="font-family: "Verdana",sans-serif;">Hot Reload <o:p></o:p></span></h2>
<p class="MsoNormal" style="text-align: justify;"><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 107%;">Hot Reload is a feature
introduced in .NET 6.0 that allows you to make changes to your code while your
application is running and see the changes immediately, without having to stop
and restart the application. This can be a huge productivity boost for
developers, as it enables a faster feedback loop when making changes to the
code.<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: justify;"><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 107%;">Hot Reload in .NET 6.0 is
supported in Visual Studio 2022, Visual Studio for Mac 2022, and in the .NET
CLI. The feature supports several scenarios, including adding or removing code,
changing method signatures, and editing XAML or Razor files.<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: justify;"><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 107%;">To use Hot Reload in Visual
Studio, you can simply set a breakpoint in your code and then make changes to
your code while your application is running. When you save your changes, the
code will be reloaded automatically, and your breakpoint will be hit again. You
can also make changes to XAML or Razor files and see the changes in real-time.<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: justify;"><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 107%;">To use Hot Reload in the
.NET CLI, you can use the dotnet watch command to automatically reload your
application when changes are detected. For example, you can run dotnet watch
run to start your application and watch for changes.<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: justify;"><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 107%;">Hot Reload in .NET 6.0 is a
powerful feature that can help boost developer productivity and streamline the
development process. It allows you to make changes to your code in real-time
and see the results immediately, making it easier to debug and test your code.<o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<h2><span style="font-family: "Verdana",sans-serif;">Project file<o:p></o:p></span></h2>
<p class="MsoNormal" style="text-align: justify;"><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 107%;">In .NET Core 6.0, the
project file format has been updated to a simplified format called
"SDK-style" project files. The new project file format is designed to
be more concise and easier to read, with a focus on the project's dependencies
and configuration.<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: justify;"><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 107%;">The SDK-style project file
format eliminates the need for separate project files for different project
types, such as class library, console app, and web app projects. Instead, all
project types are supported within a single project file.<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: justify;"><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 107%;">Here is an example of an
SDK-style project file for a console application in .NET 6.0: <o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"><Project
Sdk="Microsoft.NET.Sdk"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> <PropertyGroup><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> <OutputType>Exe</OutputType><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> <TargetFramework>net6.0</TargetFramework><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> </PropertyGroup><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> </span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> <ItemGroup><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> <PackageReference
Include="Microsoft.Extensions.Logging.Console"
Version="6.0.0" /><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> <PackageReference
Include="Newtonsoft.Json" Version="13.0.1" /><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> </ItemGroup><o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt; line-height: 107%;"></Project></span><o:p></o:p></p>
<p class="MsoNormal" style="text-align: justify;"><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 107%;">As you can see, the project
file starts with a <Project> element that specifies the SDK version to
use. The <PropertyGroup> element contains configuration settings for the
project, such as the output type and target framework. The <ItemGroup>
element lists the project's dependencies, which are defined as package
references.<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: justify;"><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 107%;">The new project file format
in .NET 6.0 makes it easier to manage and configure projects, with a simplified
structure that makes it easier to read and understand.<o:p></o:p></span></p>
<h2><span style="font-family: "Verdana",sans-serif;">Launch Setting Json<o:p></o:p></span></h2>
<p class="MsoNormal" style="text-align: justify;"><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 107%;">In .NET Core 6.0, the launchSettings.json
file is used to configure how the application is launched when running in
Visual Studio or using the dotnet run command. The launchSettings.json file can
be found in the Properties folder of a project, and it contains a set of
profiles that define different launch configurations for the application.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 11.5pt; line-height: 107%;"> </span></p>
<p class="MsoNormal"><span style="font-size: 11.5pt; line-height: 107%;">Here's an
example of a launchSettings.json file in .NET 6.0:<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 11.5pt; line-height: 107%;">{<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 11.5pt; line-height: 107%;"> "profiles": {<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 11.5pt; line-height: 107%;"> "MyApp": {<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 11.5pt; line-height: 107%;"> "commandName":
"Project",<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 11.5pt; line-height: 107%;"> "workingDirectory":
"$(ProjectDir)",<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 11.5pt; line-height: 107%;"> "environmentVariables": {<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 11.5pt; line-height: 107%;"> "ASPNETCORE_ENVIRONMENT":
"Development"<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 11.5pt; line-height: 107%;"> },<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 11.5pt; line-height: 107%;"> "applicationUrl":
"https://localhost:5001;http://localhost:5000"<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 11.5pt; line-height: 107%;"> }<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 11.5pt; line-height: 107%;"> }<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 11.5pt; line-height: 107%;">}<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 11.5pt; line-height: 107%;"> </span></p>
<p class="MsoNormal" style="text-align: justify;"><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 107%;">In this example, there is
one profile named "MyApp". The commandName property specifies that
the project should be launched as a project. The workingDirectory property
specifies the project's root directory. The environmentVariables property sets
the ASPNETCORE_ENVIRONMENT variable to "Development". The
applicationUrl property specifies the URLs to use when launching the
application.<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: justify;"><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 107%;">You can add additional
profiles to the launchSettings.json file to define different launch
configurations for the application. For example, you might have one profile for
running the application locally and another profile for running the application
in a production environment.<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: justify;"><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 107%;">Overall, the
launchSettings.json file in .NET 6.0 provides a convenient way to configure and
manage the launch settings for your application, making it easier to launch and
debug your application in different environments. </span><span style="font-family: "Verdana",sans-serif;"><o:p></o:p></span></p>
<h2><span style="font-family: "Verdana",sans-serif;">Appsetting.json<o:p></o:p></span></h2>
<p class="MsoNormal" style="text-align: justify;"><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 107%;">In .NET Core 6.0, the
appsettings.json file is used to store configuration data for your application.
The file contains key-value pairs that can be used to configure various aspects
of the application, such as database connections, logging, and other
application settings.<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: justify;"><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 107%;">Here's an example of an
appsettings.json file in .NET 6.0:<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 11.5pt; line-height: 107%;">{<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 11.5pt; line-height: 107%;"> "ConnectionStrings": {<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 11.5pt; line-height: 107%;"> "MyDatabase":
"Server=myserver;Database=mydatabase;User
Id=myuser;Password=mypassword;"<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 11.5pt; line-height: 107%;"> },<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 11.5pt; line-height: 107%;"> "Logging": {<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 11.5pt; line-height: 107%;"> "LogLevel": {<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 11.5pt; line-height: 107%;"> "Default":
"Information",<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 11.5pt; line-height: 107%;"> "Microsoft": "Warning",<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 11.5pt; line-height: 107%;"> "System": "Error"<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 11.5pt; line-height: 107%;"> }<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 11.5pt; line-height: 107%;"> },<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 11.5pt; line-height: 107%;"> "MyAppSettings": {<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 11.5pt; line-height: 107%;"> "SomeSetting":
"SomeValue",<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 11.5pt; line-height: 107%;"> "AnotherSetting":
"AnotherValue"<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 11.5pt; line-height: 107%;"> }<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size: 11.5pt; line-height: 107%;">}<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: justify;"><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 107%;"> </span></p>
<p class="MsoNormal" style="text-align: justify;"><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 107%;">In this example, the file
contains three sections: ConnectionStrings, Logging, and MyAppSettings. The
ConnectionStrings section contains a connection string that can be used to
connect to a database. The Logging section specifies the log levels for various
parts of the application. The MyAppSettings section contains custom settings
for the application.<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: justify;"><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 107%;">You can access the values in
the appsettings.json file from your application code using the Configuration
object. The Configuration object is created by the ASP.NET Core host and is
available throughout the application.<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: justify;"><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 107%;">Here's an example of how to
access the ConnectionStrings section in the appsettings.json file:<o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt;">var</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> builder =
WebApplication.CreateBuilder(args);<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> </span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: green; font-family: "Cascadia Mono"; font-size: 9.5pt;">// Add services to the
container.</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> </span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">builder.Services.AddControllers();<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt;">var</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> connectionString =
builder.Configuration.GetConnectionString(</span><span style="color: #a31515; font-family: "Cascadia Mono"; font-size: 9.5pt;">"DBConnectionString"</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">);<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt; line-height: 107%;">builder.Services.AddDbContext<SQLDBContext>(options
=> options.UseSqlServer(connectionString));<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: Verdana, sans-serif; font-size: 11.5pt; text-align: justify;">Overall, the
appsettings.json file in .NET 6.0 provides a convenient way to store and manage
configuration data for your application, making it easier to configure and
customize your application's behavior.</span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<h2>Program.cs<o:p></o:p></h2>
<p class="MsoNormal"><span style="font-size: 11.5pt; line-height: 107%;">Program and
Startup files are unified into Program.cs file. Here you can see how new
Program.cs file’s code looks like.<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt;">var</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> builder =
WebApplication.CreateBuilder(args);<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: green; font-family: "Cascadia Mono"; font-size: 9.5pt;">// Add services to the
container.</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">builder.Services.AddRazorPages();<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt;">var</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> app = builder.Build();<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: green; font-family: "Cascadia Mono"; font-size: 9.5pt;">// Configure the HTTP request
pipeline.</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-family: "Cascadia Mono"; font-size: 9.5pt;">if</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">
(!app.Environment.IsDevelopment())<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">{<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> app.UseExceptionHandler(</span><span style="color: #a31515; font-family: "Cascadia Mono"; font-size: 9.5pt;">"/Error"</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">);<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> </span><span style="color: green; font-family: "Cascadia Mono"; font-size: 9.5pt;">// The default HSTS value is 30 days.
You may want to change this for production scenarios, see
https://aka.ms/aspnetcore-hsts.</span><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;"> app.UseHsts();<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">}<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">app.UseHttpsRedirection();<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">app.UseStaticFiles();<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">app.UseRouting();<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">app.UseAuthorization();<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt;">app.MapRazorPages();<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt; line-height: 107%;">app.Run();<o:p></o:p></span></p>
<p class="MsoNormal" style="text-align: justify;"><span style="font-family: "Cascadia Mono"; font-size: 9.5pt; line-height: 107%;"> </span></p>
<p class="MsoNormal" style="text-align: justify;"></p><ul><li><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 107%;">ConfigureServices is
replaced with WebApplication.Services.</span></li><li><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 107%;">builder.Build() returns a
configured WebApplication to the variable app. Configure is replaced with
configuration calls to same services using app.</span></li></ul><p></p>
<p class="MsoNormal"><o:p> </o:p></p>Codefarihttp://www.blogger.com/profile/14275737213961436106noreply@blogger.com0tag:blogger.com,1999:blog-34862624464291238.post-80765836392866236052023-02-14T00:31:00.002+05:302023-02-14T00:31:42.621+05:30SQL Server: Query to remove special characters from string <p><span style="font-family: Verdana, sans-serif; font-size: 11.5pt;">The following query can be
used to remove special characters from a string using the STUFF function:</span></p>
<p class="MsoNormal" style="line-height: 150%; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-family: "Verdana",sans-serif; font-size: 10.0pt; line-height: 150%; mso-bidi-font-family: Consolas;">DECLARE</span><span style="font-family: Verdana, sans-serif; font-size: 10pt; line-height: 150%;">
@String </span><span style="color: blue; font-family: "Verdana",sans-serif; font-size: 10.0pt; line-height: 150%; mso-bidi-font-family: Consolas;">VARCHAR</span><span style="color: grey; font-family: "Verdana",sans-serif; font-size: 10.0pt; line-height: 150%; mso-bidi-font-family: Consolas;">(</span><span style="font-family: Verdana, sans-serif; font-size: 10pt; line-height: 150%;">100</span><span style="color: grey; font-family: "Verdana",sans-serif; font-size: 10.0pt; line-height: 150%; mso-bidi-font-family: Consolas;">)</span><span style="font-family: Verdana, sans-serif; font-size: 10pt; line-height: 150%;"> </span><span style="color: grey; font-family: "Verdana",sans-serif; font-size: 10.0pt; line-height: 150%; mso-bidi-font-family: Consolas;">=</span><span style="font-family: Verdana, sans-serif; font-size: 10pt; line-height: 150%;"> </span><span style="color: red; font-family: "Verdana",sans-serif; font-size: 10.0pt; line-height: 150%; mso-bidi-font-family: Consolas;">'Hello@!#$%^&*()_+ World'</span><span style="font-family: Verdana, sans-serif; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 150%; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-family: "Verdana",sans-serif; font-size: 10.0pt; line-height: 150%; mso-bidi-font-family: Consolas;">SELECT</span><span style="font-family: Verdana, sans-serif; font-size: 10pt; line-height: 150%;">
</span><span style="color: magenta; font-family: "Verdana",sans-serif; font-size: 10.0pt; line-height: 150%; mso-bidi-font-family: Consolas;">STUFF</span><span style="color: grey; font-family: "Verdana",sans-serif; font-size: 10.0pt; line-height: 150%; mso-bidi-font-family: Consolas;">(</span><span style="font-family: Verdana, sans-serif; font-size: 10pt; line-height: 150%;">@String</span><span style="color: grey; font-family: "Verdana",sans-serif; font-size: 10.0pt; line-height: 150%; mso-bidi-font-family: Consolas;">,</span><span style="font-family: Verdana, sans-serif; font-size: 10pt; line-height: 150%;"> </span><span style="color: magenta; font-family: "Verdana",sans-serif; font-size: 10.0pt; line-height: 150%; mso-bidi-font-family: Consolas;">PATINDEX</span><span style="color: grey; font-family: "Verdana",sans-serif; font-size: 10.0pt; line-height: 150%; mso-bidi-font-family: Consolas;">(</span><span style="color: red; font-family: "Verdana",sans-serif; font-size: 10.0pt; line-height: 150%; mso-bidi-font-family: Consolas;">'%[^a-zA-Z0-9 ]%'</span><span style="color: grey; font-family: "Verdana",sans-serif; font-size: 10.0pt; line-height: 150%; mso-bidi-font-family: Consolas;">,</span><span style="font-family: Verdana, sans-serif; font-size: 10pt; line-height: 150%;"> @String</span><span style="color: grey; font-family: "Verdana",sans-serif; font-size: 10.0pt; line-height: 150%; mso-bidi-font-family: Consolas;">),</span><span style="font-family: Verdana, sans-serif; font-size: 10pt; line-height: 150%;"> 1</span><span style="color: grey; font-family: "Verdana",sans-serif; font-size: 10.0pt; line-height: 150%; mso-bidi-font-family: Consolas;">,</span><span style="font-family: Verdana, sans-serif; font-size: 10pt; line-height: 150%;"> </span><span style="color: red; font-family: "Verdana",sans-serif; font-size: 10.0pt; line-height: 150%; mso-bidi-font-family: Consolas;">''</span><span style="color: grey; font-family: "Verdana",sans-serif; font-size: 10.0pt; line-height: 150%; mso-bidi-font-family: Consolas;">)</span><span style="font-family: Verdana, sans-serif; font-size: 10pt; line-height: 150%;"> </span><span style="color: blue; font-family: "Verdana",sans-serif; font-size: 10.0pt; line-height: 150%; mso-bidi-font-family: Consolas;">AS</span><span style="font-family: Verdana, sans-serif; font-size: 10pt; line-height: 150%;"> [CleanString]<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 150%; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-family: "Verdana",sans-serif; font-size: 10.0pt; line-height: 150%; mso-bidi-font-family: Consolas;">WHILE</span><span style="font-family: Verdana, sans-serif; font-size: 10pt; line-height: 150%;">
</span><span style="color: magenta; font-family: "Verdana",sans-serif; font-size: 10.0pt; line-height: 150%; mso-bidi-font-family: Consolas;">PATINDEX</span><span style="color: grey; font-family: "Verdana",sans-serif; font-size: 10.0pt; line-height: 150%; mso-bidi-font-family: Consolas;">(</span><span style="color: red; font-family: "Verdana",sans-serif; font-size: 10.0pt; line-height: 150%; mso-bidi-font-family: Consolas;">'%[^a-zA-Z0-9 ]%'</span><span style="color: grey; font-family: "Verdana",sans-serif; font-size: 10.0pt; line-height: 150%; mso-bidi-font-family: Consolas;">,</span><span style="font-family: Verdana, sans-serif; font-size: 10pt; line-height: 150%;"> @String</span><span style="color: grey; font-family: "Verdana",sans-serif; font-size: 10.0pt; line-height: 150%; mso-bidi-font-family: Consolas;">)</span><span style="font-family: Verdana, sans-serif; font-size: 10pt; line-height: 150%;"> </span><span style="color: grey; font-family: "Verdana",sans-serif; font-size: 10.0pt; line-height: 150%; mso-bidi-font-family: Consolas;">></span><span style="font-family: Verdana, sans-serif; font-size: 10pt; line-height: 150%;"> 0<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 150%; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-family: "Verdana",sans-serif; font-size: 10.0pt; line-height: 150%; mso-bidi-font-family: Consolas;">BEGIN</span><span style="font-family: Verdana, sans-serif; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 150%; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Verdana, sans-serif; font-size: 10pt; line-height: 150%;"> </span><span style="color: blue; font-family: "Verdana",sans-serif; font-size: 10.0pt; line-height: 150%; mso-bidi-font-family: Consolas;">SET</span><span style="font-family: Verdana, sans-serif; font-size: 10pt; line-height: 150%;"> @String </span><span style="color: grey; font-family: "Verdana",sans-serif; font-size: 10.0pt; line-height: 150%; mso-bidi-font-family: Consolas;">=</span><span style="font-family: Verdana, sans-serif; font-size: 10pt; line-height: 150%;"> </span><span style="color: magenta; font-family: "Verdana",sans-serif; font-size: 10.0pt; line-height: 150%; mso-bidi-font-family: Consolas;">STUFF</span><span style="color: grey; font-family: "Verdana",sans-serif; font-size: 10.0pt; line-height: 150%; mso-bidi-font-family: Consolas;">(</span><span style="font-family: Verdana, sans-serif; font-size: 10pt; line-height: 150%;">@String</span><span style="color: grey; font-family: "Verdana",sans-serif; font-size: 10.0pt; line-height: 150%; mso-bidi-font-family: Consolas;">,</span><span style="font-family: Verdana, sans-serif; font-size: 10pt; line-height: 150%;"> </span><span style="color: magenta; font-family: "Verdana",sans-serif; font-size: 10.0pt; line-height: 150%; mso-bidi-font-family: Consolas;">PATINDEX</span><span style="color: grey; font-family: "Verdana",sans-serif; font-size: 10.0pt; line-height: 150%; mso-bidi-font-family: Consolas;">(</span><span style="color: red; font-family: "Verdana",sans-serif; font-size: 10.0pt; line-height: 150%; mso-bidi-font-family: Consolas;">'%[^a-zA-Z0-9 ]%'</span><span style="color: grey; font-family: "Verdana",sans-serif; font-size: 10.0pt; line-height: 150%; mso-bidi-font-family: Consolas;">,</span><span style="font-family: Verdana, sans-serif; font-size: 10pt; line-height: 150%;"> @String</span><span style="color: grey; font-family: "Verdana",sans-serif; font-size: 10.0pt; line-height: 150%; mso-bidi-font-family: Consolas;">),</span><span style="font-family: Verdana, sans-serif; font-size: 10pt; line-height: 150%;"> 1</span><span style="color: grey; font-family: "Verdana",sans-serif; font-size: 10.0pt; line-height: 150%; mso-bidi-font-family: Consolas;">,</span><span style="font-family: Verdana, sans-serif; font-size: 10pt; line-height: 150%;"> </span><span style="color: red; font-family: "Verdana",sans-serif; font-size: 10.0pt; line-height: 150%; mso-bidi-font-family: Consolas;">''</span><span style="color: grey; font-family: "Verdana",sans-serif; font-size: 10.0pt; line-height: 150%; mso-bidi-font-family: Consolas;">)</span><span style="font-family: Verdana, sans-serif; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 150%; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-family: "Verdana",sans-serif; font-size: 10.0pt; line-height: 150%; mso-bidi-font-family: Consolas;">END</span><span style="font-family: Verdana, sans-serif; font-size: 10pt; line-height: 150%;"><o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 150%; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-family: "Verdana",sans-serif; font-size: 10.0pt; line-height: 150%; mso-bidi-font-family: Consolas;">SELECT</span><span style="font-family: Verdana, sans-serif; font-size: 10pt; line-height: 150%;">
@String </span><span style="color: blue; font-family: "Verdana",sans-serif; font-size: 10.0pt; line-height: 150%; mso-bidi-font-family: Consolas;">AS</span><span style="font-family: Verdana, sans-serif; font-size: 10pt; line-height: 150%;"> [CleanString]<o:p></o:p></span></p>
<p class="MsoNormal" style="line-height: 150%;"><span style="font-family: Verdana, sans-serif; font-size: 11.5pt;">This query uses the PATINDEX
function to search for any characters that are not letters, numbers, or spaces.
The STUFF function then replaces these characters with an empty string. The
process is repeated until all special characters are removed. The final result
is stored in the @String variable.</span></p>Codefarihttp://www.blogger.com/profile/14275737213961436106noreply@blogger.com0tag:blogger.com,1999:blog-34862624464291238.post-23916973493461408602023-02-12T22:46:00.002+05:302023-02-12T22:46:28.521+05:30What is the difference between OLAP and OLTP?<p class="MsoNormal" style="line-height: 150%;"><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 150%;">OLAP (Online Analytical
Processing) and OLTP (Online Transaction Processing) are two different
approaches to organizing and managing data in a database.</span><span style="font-family: Verdana, sans-serif; font-size: 11.5pt;"> </span></p>
<p class="MsoNormal" style="line-height: 150%;"><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 150%;">OLTP is designed to manage
transaction-oriented applications, which require fast query performance and
efficient data modification operations. It is optimized for insert, update, and
delete operations and focuses on maintaining data accuracy and consistency
through transactions. In an OLTP system, data is stored in a normalized fashion
to minimize redundancy and improve data integrity.</span><span style="font-family: Verdana, sans-serif; font-size: 11.5pt;"> </span></p>
<p class="MsoNormal" style="line-height: 150%;"><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 150%;">On the other hand, OLAP is
designed for data analysis and business intelligence tasks, which require fast
access to aggregated data. It is optimized for complex queries and aggregations
and focuses on storing data in a denormalized fashion to improve query
performance. In an OLAP system, data is often pre-calculated and stored in a
multidimensional data model, such as a cube, to allow for quick access to
aggregated data.</span><span style="font-family: Verdana, sans-serif; font-size: 11.5pt;"> </span></p>
<p class="MsoNormal" style="line-height: 150%;"><o:p></o:p></p><p><span style="font-family: Verdana, sans-serif; font-size: 11.5pt;">In summary, OLTP is
optimized for transactional systems, while OLAP is optimized for data analysis.</span> </p>Codefarihttp://www.blogger.com/profile/14275737213961436106noreply@blogger.com0tag:blogger.com,1999:blog-34862624464291238.post-36773087430527282712023-02-12T22:40:00.000+05:302023-02-12T22:40:02.313+05:30Why do we always prefer using parameters in SQL statements?<p class="MsoNormal"><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 107%;">There are several reasons why it is preferred to use
parameters in SQL statements:<o:p></o:p></span></p>
<p class="MsoNormal"></p><ol style="text-align: left;"><li><b><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 107%;">Security</span></b><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 107%;">:
Using parameters in SQL statements helps prevent SQL injection attacks. When
you use parameters, the values are treated as data, not as part of the SQL
code, so an attacker cannot modify the SQL code itself.<o:p></o:p></span></li><li><b><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 107%;">Performance</span></b><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 107%;">:
When you use parameters in SQL statements, the database can cache the execution
plan for the statement, which can lead to better performance when executing the
same statement multiple times with different values.<o:p></o:p></span></li><li><b><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 107%;">Reusability</span></b><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 107%;">:
When you use parameters in SQL statements, you can reuse the same statement
multiple times with different values, reducing the amount of code you need to
write and maintain.<o:p></o:p></span></li><li><b><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 107%;">Readability</span></b><span style="font-family: "Verdana",sans-serif; font-size: 11.5pt; line-height: 107%;">:
Using parameters in SQL statements can make the code easier to read and
understand, especially when the same value is used in multiple places in the
same statement.<o:p></o:p></span></li></ol><p></p>
<p><span style="font-family: Verdana, sans-serif; font-size: 11.5pt;">Overall, using parameters in SQL statements is a best
practice that can improve the security, performance, reusability, and
readability of your code.</span> </p>Codefarihttp://www.blogger.com/profile/14275737213961436106noreply@blogger.com0tag:blogger.com,1999:blog-34862624464291238.post-78459076133453940712023-02-12T22:27:00.003+05:302023-02-12T22:27:20.860+05:30How to concatenate text from multiple rows into a single text string in SQL Server<p>In SQL Server, you can concatenate text from multiple rows into a single text string using the FOR XML PATH clause and the STUFF function. Here's an example:</p><p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-family: Consolas; mso-bidi-font-family: Consolas;">DECLARE</span><span style="font-family: Consolas;">
@concatenatedString </span><span style="color: blue; font-family: Consolas; mso-bidi-font-family: Consolas;">VARCHAR</span><span style="color: grey; font-family: Consolas; mso-bidi-font-family: Consolas;">(</span><span style="color: magenta; font-family: Consolas; mso-bidi-font-family: Consolas;">MAX</span><span style="color: grey; font-family: Consolas; mso-bidi-font-family: Consolas;">)</span><span style="font-family: Consolas;"><o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-family: Consolas; mso-bidi-font-family: Consolas;">SELECT</span><span style="font-family: Consolas;">
@concatenatedString </span><span style="color: grey; font-family: Consolas; mso-bidi-font-family: Consolas;">=</span><span style="font-family: Consolas;"> </span><span style="color: magenta; font-family: Consolas; mso-bidi-font-family: Consolas;">STUFF</span><span style="color: grey; font-family: Consolas; mso-bidi-font-family: Consolas;">((</span><span style="color: blue; font-family: Consolas; mso-bidi-font-family: Consolas;">SELECT</span><span style="font-family: Consolas;"> </span><span style="color: red; font-family: Consolas; mso-bidi-font-family: Consolas;">', '</span><span style="font-family: Consolas;"> </span><span style="color: grey; font-family: Consolas; mso-bidi-font-family: Consolas;">+</span><span style="font-family: Consolas;">
column_name<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-family: Consolas; mso-bidi-font-family: Consolas;">FROM</span><span style="font-family: Consolas;">
table_name<o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-family: Consolas; mso-bidi-font-family: Consolas;">FOR</span><span style="font-family: Consolas;"> </span><span style="color: blue; font-family: Consolas; mso-bidi-font-family: Consolas;">XML</span><span style="font-family: Consolas;"> </span><span style="color: blue; font-family: Consolas; mso-bidi-font-family: Consolas;">PATH</span><span style="color: grey; font-family: Consolas; mso-bidi-font-family: Consolas;">(</span><span style="color: red; font-family: Consolas; mso-bidi-font-family: Consolas;">''</span><span style="color: grey; font-family: Consolas; mso-bidi-font-family: Consolas;">)),</span><span style="font-family: Consolas;"> 1</span><span style="color: grey; font-family: Consolas; mso-bidi-font-family: Consolas;">,</span><span style="font-family: Consolas;"> 2</span><span style="color: grey; font-family: Consolas; mso-bidi-font-family: Consolas;">,</span><span style="font-family: Consolas;"> </span><span style="color: red; font-family: Consolas; mso-bidi-font-family: Consolas;">''</span><span style="color: grey; font-family: Consolas; mso-bidi-font-family: Consolas;">)</span><span style="font-family: Consolas;"><o:p></o:p></span></p><p>
</p><p class="MsoNormal"><span style="color: blue; font-family: Consolas; mso-bidi-font-family: Consolas;">SELECT</span><span style="font-family: Consolas;"> @concatenatedString</span><o:p></o:p></p><p>In this example, the SELECT statement inside the STUFF function concatenates the values from the column_name in the table_name table, separated by a comma and a space. The FOR XML PATH('') clause is used to concatenate the values into a single string. The STUFF function then removes the first two characters (the comma and the space) from the string. Finally, the result is stored in the @concatenatedString variable.</p><p>For the SQL Server 2017 or higher version you can use the STRING_AGG() function, here is the example;</p><p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="color: blue; font-family: Consolas; mso-bidi-font-family: Consolas;">SELECT</span><span style="font-family: Consolas;"><o:p></o:p></span></p><p class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0cm; mso-layout-grid-align: none; text-autospace: none;"><span style="font-family: Consolas;"> </span><span style="color: magenta; font-family: Consolas; mso-bidi-font-family: Consolas;">STRING_AGG</span><span style="color: grey; font-family: Consolas; mso-bidi-font-family: Consolas;">(</span><span style="font-family: Consolas;">Coulmn_Name</span><span style="color: grey; font-family: Consolas; mso-bidi-font-family: Consolas;">,</span><span style="color: red; font-family: Consolas; mso-bidi-font-family: Consolas;">','</span><span style="color: grey; font-family: Consolas; mso-bidi-font-family: Consolas;">)</span><span style="font-family: Consolas;"> col_nm<o:p></o:p></span></p><p>
</p><p class="MsoNormal"><span style="color: blue; font-family: Consolas; mso-bidi-font-family: Consolas;">FROM</span><span style="font-family: Consolas;"> Table1</span><o:p></o:p></p><p>Where Coulmn_Name is the column you want to concatenate value in a single text.</p>Codefarihttp://www.blogger.com/profile/14275737213961436106noreply@blogger.com0tag:blogger.com,1999:blog-34862624464291238.post-51511397477440432842023-02-11T23:41:00.002+05:302023-02-11T23:42:18.685+05:30How to synchronize SQL Server CE from different clients to SQL Server on a LAN<p>Synchronizing data between SQL Server Compact Edition (SQL CE) clients and a SQL Server database on a LAN can be done using various techniques. One common approach is to use a middleware application, such as a web service, to transfer data between the clients and the server. Here is a general outline of the steps involved:</p><p></p><ul style="text-align: left;"><li>Create a web service that will act as a bridge between the SQL CE clients and the SQL Server database.</li><li>On each SQL CE client, modify the data access code to call the web service to transfer data to the server. You can use ADO.NET or LINQ to SQL to create the data access code.</li><li>On the server, create a database table to store the data from the clients.</li><li>On the web service, write the logic to transfer the data from the SQL CE clients to the SQL Server database. This may involve copying the data from the SQL CE client to a data transfer object (DTO), which can then be sent to the server.</li><li>On the server, write the logic to receive the DTO from the web service and insert the data into the SQL Server database.</li><li>Repeat this process in the opposite direction to transfer data from the SQL Server database back to the SQL CE clients.</li><li>Test the synchronization process by making changes on one or more SQL CE clients and verifying that the changes are properly transferred to the SQL Server database, and vice versa.</li><li>This is just one approach to synchronizing data between SQL CE clients and a SQL Server database on a LAN, and there are many other ways to accomplish this task, depending on your specific requirements and constraints.</li></ul><p></p><p><b>Another Approach</b></p><p></p><ul style="text-align: left;"><li>Create a SQL Server database: Create a database on the central SQL Server instance that will serve as the central repository for data.</li><li>Create SQL CE databases: Create SQL CE databases on each client that will be used to store data locally.</li><li>Create Sync Providers: Using the Sync Framework, create sync providers for both the SQL Server database and the SQL CE databases. A sync provider is responsible for communicating with the data source and handling the synchronization process.</li><li>Define the synchronization scope: Define the scope of the synchronization, including the tables and columns that will be synchronized, as well as any filters or relationships that will be applied to the data.</li><li>Configure the synchronization process: Configure the synchronization process, including the frequency of synchronization and any conflict resolution rules.</li><li>Initialize the synchronization: Trigger the initial synchronization between the SQL CE databases and the central SQL Server database. This can be done manually, or it can be automated to occur at regular intervals.</li><li>Monitor the synchronization: Monitor the synchronization process to ensure that data is being correctly transferred between the SQL CE databases and the central SQL Server database.</li><li>The exact steps to implement the synchronization may vary depending on your specific requirements, but the above steps should provide a general outline of the process.</li></ul><p></p><div><br /></div>Codefarihttp://www.blogger.com/profile/14275737213961436106noreply@blogger.com0tag:blogger.com,1999:blog-34862624464291238.post-14791575377905397962023-02-11T23:32:00.002+05:302023-02-11T23:32:27.168+05:30What is a columnstore index and how is different from clustered and non-clustered indexes?<div>A columnstore index is a type of index in a relational database management system that organizes data by columns instead of by rows. In a traditional row-based storage system, the data is stored in rows, and each row is treated as a single unit. With a columnstore index, the data is instead stored in columns, and each column contains the values for a single attribute or field.</div><div><br /></div><div>The main advantage of using a columnstore index is improved query performance. Because the data is stored in columns, the database management system can access and process only the columns that are required for a particular query, instead of reading the entire row. This can result in much faster query times, especially for large data sets.</div><div><br /></div><div>A clustered index is an index that physically reorders the rows in a table to match the order of the index. In other words, the data is stored in the same order as the index, and the index contains a pointer to the actual data row. There can only be one clustered index per table.</div><div><br /></div><div>A non-clustered index, on the other hand, is a separate structure that contains a subset of the data in the table, along with a pointer to the location of the actual data row. Unlike clustered indexes, non-clustered indexes do not physically reorder the data in the table. Multiple non-clustered indexes can be created on a single table.</div><div><br /></div><div>In summary, columnstore indexes, clustered indexes, and non-clustered indexes are different ways of organizing data in a relational database, and each type has its own advantages and use cases. Columnstore indexes are designed to improve query performance for large data sets, while clustered indexes physically reorder the data to match the order of the index, and non-clustered indexes provide an additional layer of organization that can help speed up queries.</div>Codefarihttp://www.blogger.com/profile/14275737213961436106noreply@blogger.com0