Friday, September 20, 2019

Difference between Html.RenderBody() and Html.RenderSection() in ASP.NET MVC

The following table lists the differences between the RenderBody() and RenderSection() methods.
RenderBody()RenderSection()
The RenderBody() method must be present in the layout view.The RenderSection() method is optional.
RenderBody() renders all the content of the child view which is not wrapped in the named section.RenderSection() renders only a part of the child view which is wrapped under the named section.
Multiple RenderBody() methods are NOT allowed in a single layout view.Multiple RenderSection() methods are allowed in a single layout view.
The RenderBody() method does not include any parameter.The RenderSection() method includes boolean parameter "required", which makes the section optional or mandatory. If the required parameter is true, then the child view must contain the section.

Friday, September 13, 2019

Reversing strings in c# using linq

A common requirement is reversing strings in c#.There are few different ways by which we can reverse strings.

Using the for loop for reversing strings

We iterate over the entire string and then just assign the characters to a different string.We loop through the original string in reverse order.

 Using the Reverse method of the array class

Reverse is a static method of the Array class.We can pass char array to this method and it will reverse the order of the characters.Once we get the characters in reverse order we can pass them to the string constructor to create the reversed string.

 Reversing strings in c# using LINQ

We can use the query operators in LINQ to reverse the strings.For reversing strings in c# using linq we use the ForEach() method which iterates over the entire collection.In this method we pass the logic to reverse the string.
By using the ForEach method we can avoid the for statement and directly loop over the string.

How to Reverse a Number and String in C#?

The first example shows how to reverse a number in C#. The second example shows how to reverse a string in C#.
 

Reverse a Number in C#

 
Here is the code sample that shows you to reverse a number in C#. 
  1. using System;  
  2.   
  3. namespace ReverseNo  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             Console.WriteLine("Enter a No. to reverse");  
  10.             int Number = int.Parse(Console.ReadLine());  
  11.             int Reverse = 0;  
  12.             while(Number>0)  
  13.             {  
  14.                 int remainder = Number % 10;  
  15.                 Reverse = (Reverse * 10) + remainder;  
  16.                 Number = Number / 10;  
  17.             }  
  18.             Console.WriteLine("Reverse No. is {0}",Reverse);  
  19.             Console.ReadLine();  
  20.         }  
  21.     }  
  22. }  
Preview
 
 

Reverse a String in C#

 
The following code sample shows how to reverse a string in C#.
  1. using System;  
  2.   
  3. namespace reverseString  
  4. {  
  5.     class Program  
  6.     {  
  7.         static void Main(string[] args)  
  8.         {  
  9.             string str = "", reverse = "";  
  10.             int Length = 0;  
  11.             Console.WriteLine("Enter a Word");  
  12.             //Getting String(word) from Console  
  13.             str = Console.ReadLine();  
  14.             //Calculate length of string str  
  15.             Length = str.Length - 1;  
  16.             while(Length>=0)  
  17.             {  
  18.                 reverse = reverse + str[Length];  
  19.                 Length--;  
  20.             }  
  21.             //Displaying the reverse word  
  22.             Console.WriteLine("Reverse word is {0}",reverse);  
  23.             Console.ReadLine();  
  24.         }  
  25.     }  
  26. }  
Preview
 

Get max value for identity column without a table scan

  You can use   IDENT_CURRENT   to look up the last identity value to be inserted, e.g. IDENT_CURRENT( 'MyTable' ) However, be caut...