Code Snippets

This is just a list of the small code samples that I always seem to forget, and I use this page as a personal reference for those little code snippets.

string.Split

string[] values = delimited.Split(new string[] { “/” }, StringSplitOptions.RemoveEmptyEntries);

DateTime.ToString()

These are the custom date time formats that I like to use and always forget..

DateTime.Now.ToString(“dd-MMM-yyyy hh:mm:ss.fff tt”);

  • MMM – Three digit month
  • hh – 12 hour time
  • HH – 24 hour time
  • mm – lower case m’s for minutes
  • fff – milliseconds
  • tt – AM / PM

When using a date for a log entry timestamp I use:
DateTime.Now.ToString(“yyyy-MM-dd hh:mm:ss”);

Access the Registry

[csharp]
using Microsoft.Win32;

// This creates or opens
RegistryKey key = Registry.LocalMachine.CreateSubKey("\Sofware\YourApplication\YourKey");
[/csharp]

SQL methods

  • newid() – generates a new uniqueidentifier (uuid or guid)
  • charindex(searchcriteria, stringtoSearch, [startPosition])
  • substring(stringToCheck, startingPos, Length)
  • replace( stringToCheck , searchValue , replacementValue )

C# basics

Decimal Literal – 123.45m

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.