Write the UTF-8 and ASCII encoded byte arrays


   
 

using System;
using System.IO;
using System.Text;

class Test
{
    public static void Main() 
    {        
        using (StreamWriter output = new StreamWriter("practice.txt")) 
        {
            // Create and write a string containing the symbol for Pi.
            string srcString = "Area = u03A0r^2";

            // Convert the UTF-16 encoded source string to UTF-8 and ASCII.
            byte[] utf8String = Encoding.UTF8.GetBytes(srcString);
            byte[] asciiString = Encoding.ASCII.GetBytes(srcString);
            
            // Write the UTF-8 and ASCII encoded byte arrays. 
            output.WriteLine("UTF-8  Bytes: {0}", BitConverter.ToString(utf8String));
            output.WriteLine("ASCII  Bytes: {0}", BitConverter.ToString(asciiString));
            
            Console.WriteLine(BitConverter.ToString(utf8String));
            Console.WriteLine(BitConverter.ToString(asciiString));
        }
    }
}



           
         
     


Write the UTF-16 encoded bytes of the source string


   
 
using System;
using System.IO;
using System.Text;

class Test
{
    public static void Main() 
    {        
        using (StreamWriter output = new StreamWriter("practice.txt")) 
        {
            // Create and write a string containing the symbol for Pi.
            string srcString = "Area = u03A0r^2";

            // Write the UTF-16 encoded bytes of the source string.
            byte[] utf16String = Encoding.Unicode.GetBytes(srcString);
            output.WriteLine("UTF-16 Bytes: {0}", BitConverter.ToString(utf16String));
            Console.WriteLine(BitConverter.ToString(utf16String));
        }
    }
}




           
         
     


Trace.WriteLine

using System;
using System.Data;
using System.Data.SqlClient;
using System.Threading;
using System.Diagnostics;
class MainClass {

static void Main() {
Trace.WriteLine(“Entered Main()”);

for (int i = 0; i < 6; i++) Trace.WriteLine(i); Trace.WriteLine("Exiting from Main()"); } } [/csharp]

Use Trace.Fail to alert a fail

   
 


using System;
using System.Data;
using System.Data.SqlClient;
using System.Threading;
using System.Diagnostics;


class Class1 {
    [STAThread]
    static void Main(string[] args) {
        SqlConnection dbConn = new SqlConnection("server=.;database=pubs;uid=sa;pwd=");

        SqlCommand dbComm = new SqlCommand("SELECT * FROM " + "authors", dbConn);
        SqlDataReader dr = null;
        Trace.WriteLine(DateTime.Now + " - Executing SQL statement");
        try {
            dbConn.Open();
            Trace.Assert(dbConn.State == ConnectionState.Open,"Error", "Connection failed...");
            dr = dbComm.ExecuteReader(CommandBehavior.CloseConnection);
            Trace.Assert(dr != null, "Error","The SqlDataReader is null!");

            while (dr.Read()) {
            }
        } catch {
            Trace.Fail("An error occurred in database access");
        } finally {
            if ((dr.IsClosed == false) &amp;&amp; (dr != null))
                dr.Close();
        }
    }
}

    


Week refers to the week of the month, with “5” meaning the last week.

   
 

using System;
using System.Globalization;
public class MainClass {
    public static void Main() {
        TimeZoneInfo wa = TimeZoneInfo.FindSystemTimeZoneById("W. Australia Standard Time");

        foreach (TimeZoneInfo.AdjustmentRule rule in wa.GetAdjustmentRules()) {
            Console.WriteLine("Rule: applies from " + rule.DateStart +
                                              " to " + rule.DateEnd);

            Console.WriteLine("   Delta: " + rule.DaylightDelta);

            Console.WriteLine("   Start: " + FormatTransitionTime
                                             (rule.DaylightTransitionStart, false));

            Console.WriteLine("   End:   " + FormatTransitionTime
                                             (rule.DaylightTransitionEnd, true));
            Console.WriteLine();
        }
    }
    static string FormatTransitionTime(TimeZoneInfo.TransitionTime tt,
                                    bool endTime) {
        if (endTime &amp;&amp; tt.IsFixedDateRule
                    &amp;&amp; tt.Day == 1 &amp;&amp; tt.Month == 1
                    &amp;&amp; tt.TimeOfDay == DateTime.MinValue)
            return "-";

        string s;
        if (tt.IsFixedDateRule)
            s = tt.Day.ToString();
        else
            s = "The first second third fourth last".Split()[tt.Week - 1] +
                " " + tt.DayOfWeek + " in";

        return s + " " + DateTimeFormatInfo.CurrentInfo.MonthNames[tt.Month - 1]
                 + " at " + tt.TimeOfDay.TimeOfDay;
    }
}

    


TimeZoneInfo also provides IsDaylightSavingTime and GetUtcOffset methods–the difference is that they accept either a DateTime or DateTimeOffset.

   
 

using System;
public class MainClass {
    public static void Main() {

        TimeZoneInfo wa = TimeZoneInfo.FindSystemTimeZoneById("W. Australia Standard Time");

        Console.WriteLine(wa.Id);
        Console.WriteLine(wa.DisplayName);
        Console.WriteLine(wa.BaseUtcOffset);
        Console.WriteLine(wa.SupportsDaylightSavingTime);
    }
}