Sunday, April 9, 2017

C# program to test a number is prime or not

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public static void IsPrime(int number)
  {

   if (number == 1 || number == 0)
   {
    Console.WriteLine(false);
    return;
   }

   if (number == 2)
   {
    Console.WriteLine(true);
    return;
   }
   for (int i = 2; i <= (int)Math.Sqrt(number); i++)
   {
    if (number % i == 0)
    {
     Console.WriteLine(false);
     return;
    }
   }
   Console.WriteLine(true);
  }

Friday, November 4, 2016

Common ASP.Net Web Virtual and Relative Url helper classes

List of ASP.Net Web/MVC Virtual Paths,Relative and Absolute Paths Utility helper classes
Property Input Url Return Value
HttpRequest.ApplicationPath http://localhost/testapp/home.aspx /testapp
HttpRequest.CurrentExecutionFilePath http://localhost/testapp/home.aspx /testapp/home.aspx
HttpRequest.FilePath http://localhost/testapp/home.aspx /testapp/home.aspx
HttpRequest.AppRelativeCurrentExecutionFilePath http://localhost/testapp/home.aspx ~/testapp/home.aspx
HttpRequest.Path http://localhost/testapp/home.aspx /testapp/home.aspx/PathInfo
HttpRequest.PhysicalApplicationPath http://localhost/testapp/home.aspx C:\inetpub\wwwroot\TestApp
HttpRequest.RawUrl http://localhost/testapp/home.aspx?user=1 /testapp/home.aspx?user=1
UrlHelper.Content ~/testapp/home.aspx c:\inetpub\wwwroot\testapp\home.aspx
Property Input Url Return Value
HttpContext.Current.Request.Url.Host http://localhost:8080/testapp/home.aspx?user=1&new=2 localhost
HttpContext.Current.Request.Url.Authority http://localhost:8080/testapp/home.aspx?user=1&new=2 localhost:8080
HttpContext.Current.Request.Url.AbsolutePath http://localhost:8080/testapp/home.aspx?user=1&new=2 /testapp/home.aspx
HttpContext.Current.Request.Url.AbsoluteUri http://localhost:8080/testapp/home.aspx?user=1&new=2 http://localhost:8080/testapp/home.aspx?user=1&new=2
HttpContext.Current.Request.Url.PathAndQuery http://localhost:8080/testapp/home.aspx?user=1&new=2 /testapp/home.aspx?user=1&new=2
HttpContext.Current.Request.RawUrl http://localhost:8080/testapp/home.aspx?user=1&new=2 /testapp/home.aspx?user=1&new=2
Property Input Url Return Value
VirtualPathUtility.GetFileName http://localhost/testapp/home.aspx home
VirtualPathUtility.GetExtension http://localhost/testapp/home.aspx aspx
VirtualPathUtility.GetDirectory http://localhost/testapp/home.aspx /testapp/
VirtualPathUtility.IsAbsolute http://localhost/testapp/home.aspx True
VirtualPathUtility.IsAppRelative http://localhost/testapp/home.aspx False
VirtualPathUtility.ToAppRelative http://localhost/testapp/home.aspx ~/home.aspx
VirtualPathUtility.ToAbsolute ~/home.aspx /testapp/home
VirtualPathUtility.AppendTrailingSlash http://localhost/testapp/home.aspx http://localhost/testapp/home.aspx/

Sunday, October 30, 2016

Common Csharp dotnet Format specifiers or Format Strings for display

Name Format String Example Comments
Currency Format C or c or C2 - precision Console.WriteLine(amount.ToString("C")) output depends on your current UI culture
Percent Format P or p or P2 - precision) Console.WriteLine(number.ToString("P")) output depends on your current UI culture
Padding with leading zero Format 0 var input = "123"; Console.WriteLine(input.ToString("0000")); //result will be 0123 Replaces the zero placeholder with the corresponding digit if one is present; otherwise, zero appears in the result string.
Long date Format with no time and zone info just verbose date D var date = DateTime.Now; Console.WriteLine(date.ToString("D")); result will be Sunday,October 30,2015 (output depends on your current UI culture)
Short date Format with no time and zone info just date d var date = DateTime.Now; Console.WriteLine(date.ToString("d")); result will be 10/30/2015 (output depends on your current UI culture)
Full date and time Format with no zone info f or g var date = DateTime.Now; Console.WriteLine(date.ToString("f")); result will be Sunday,October 30,2015 12:23 PM (output depends on your current UI culture)
Just time Format t or T var date = DateTime.Now; Console.WriteLine(date.ToString("t")); result will be 1:23 PM or 1:23:45 PM
Year month Format Y or y var date = DateTime.Now; Console.WriteLine(date.ToString("y")); October, 2015
Month day Format M or m var date = DateTime.Now; Console.WriteLine(date.ToString("M")); October 30 (output depends on UI culture)
UTC time Format O or o var date = DateTime.Now; Console.WriteLine(date.ToString("O")); 2015-10-30T13:45:30.0000000-07:00
Custom date and time Format MMMM dd, yyyy var date = DateTime.Now; Console.WriteLine(date.ToString("MMMM dd, yyyy")); October 30, 2015
Custom date and time Format MM/dd/yy H:mm:ss var date = DateTime.Now;Console.WriteLine(date.ToString("MM/dd/yy H:mm:ss")); 10/30/15 14:14:11
Custom Format d or dd - day of the month
ddd or dddd - abbreviated or full day of the week
M or MM - month of year
MMM or MMMM - abbreviated month or full name of the month
y or yy - two digit year
YYYY - four digit year
var date = DateTime.Now; Console.WriteLine(date.ToString("d MMMM YYYY")); 30 October 2015
Enumeration Format strings G or g Console.WriteLine(LogLevel.Verbose.ToString("G"))); Displays enum entry as a string value. Here result will be Verbose
Enumeration Format strings G or g or F or f Console.WriteLine(LogLevel.Verbose.ToString("G"))); Displays enum entry as a string value. Here result will be Verbose
Enumeration Format strings D or d Console.WriteLine(LogLevel.Verbose.ToString("D"))); Displays enum entry as a integer value. Here result will be 1

Common Csharp dotnet Regex expressions for user input validation

Name Pattern Example
US zip code regex ^\d{5}(?:[-\s]\d{4})?$ 12345 or 12345-2345
Canadian zip code regex ^([ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ])\ {0,1}(\d[ABCEGHJKLMNPRSTVWXYZ]\d)$ M4B 1B4
US phone number regex \(?\d{3}\)?-? *\d{3}-? *-?\d{4} (123) 345-234
US Date (MM/dd/YYYY) regex ^(0?[1-9]|[12][0-9]|3[01])[\/](0?[1-9]|1[012])[\/]\d{4}$ 05/31/2016
24 Hour time regex ^(?:0?[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$ 13:12
12 Hour time regex (AM/PM) ^(([0]?[0-9]|1[0-2]):[0-5][0-9][ ][aApP][mM])|((1[3-9]|2[0-3]):[0-5][0-9])$ 7:00 AM
Url regex ^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$ http://www.test.com or www.test.com
IP address regex ^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$ 127.0.0.1
US social security number rgex ^([0-9]{3}[-]*[0-9]{2}[-]*[0-9]{4})*$ 123-09-2345