You are on page 1of 1

Visual C# 2005 Express Edition

Cheat Sheet : C# Language Basics


Working With Numbers Define New Types Working With Text
int i = 0; struct MyValueType {} // defines a value type using System.Text;
// convert from a string class MyClass { // defines a reference type using System.IO;
int i = int.Parse("1"); // fields
// convert froma string and don’t throw exceptions const string constName = "LearnVisualStudio.NET"; string name = "David";
if (int.TryParse("1", out i)) {} static string staticName = "LearnVisualStudio.NET"; string hello1 = "Hello " + name;
i++; // increment by one readonly string readOnlyName; string hello2 = string.Format("Hello {0}", name);
i--; // decrement by one string instName; Console.WriteLine("Hello {0}", name);
i += 10; // add 10 // properties StringBuilder sb = new StringBuilder("Hello ");
i -= 10; // subtract 10 public string Name sb.AppendLine(name);
i *= 10; // multiply by 10 { // read/write sb.AppendFormat("Goodbye {0}", name);
i /= 10; // divide by 10 get { return instName; } Console.WriteLine(sb.ToString());
i = checked(i*2) // check for overflow set { instName = value; }
i = unchecked(i*2) // ignore overflow } // Create a text file
public string ReadOnlyName { get { return instName; } } using (StreamWriter w = File.CreateText(@"c:\names.txt")) {
// methods
Dates and Times public void Print() { Console.WriteLine(instName); }
w.WriteLine("Bob");
w.WriteLine("David");
DateTime now = DateTime.Now; // date and time public int Add(int a, int b) { return a + b; } }
DateTime today = DateTime.Today; // just the date public static void PrintStatic() {
DateTime tomorrow = today.AddDays(1); Console.WriteLine(staticName); } // Read from a text file
DateTime yesterday = today.AddDays(-1); // constructors using (StreamReader r = File.OpenText(@"c:\names.txt")) {
TimeSpan time = tomorrow - today; public MyClass() {} // default constructor while (!r.EndOfStream) {
int days = time.Days; public MyClass(string name) Console.WriteLine(r.ReadLine());
int hours = time.Hours; { }
int minutes = time.Minutes; readOnlyName = name; }
int seconds = time.Seconds; instName = name; foreach (string s in File.ReadAllLines(@"c:\names.txt")) {
int milliseconds = time.Milliseconds; } Console.WriteLine(s);
time += new TimeSpan(days, hours, minutes, seconds, } }
milliseconds);
Inheritance Working With a Database
Conditional Logic class MyClass : Object { using System.Data;
if (i == 0) {} // override inherited method using System.Data.SqlClient;
if (i <= 10) {} else {} public override string ToString() {
switch (i) { // call base class version string cs = @"Data Source=.\SQLEXPRESS;" +
case 0: string s = base.ToString(); string cs = @"Initial Catalog=NamesDB;" +
case 1: return "Hello " + s; string cs = @"Integrated Security=True;";
break; } using (SqlConnection con = new SqlConnection(cs)) {
case 2: } con.Open();
break;
string sql = "INSERT INTO Names(Name) VALUES(@Name)";
default: Working with XML // insert a record
break;
using System.Xml; SqlCommand cmd1 = new SqlCommand(sql, con);
}
using System.IO; cmd1.Parameters.Add("@Name", SqlDbType.NVarChar, 100);
cmd1.Parameters["@Name"].Value = "Bob";
Compare Operators // Create an XML file cmd1.ExecuteNonQuery();
if (i == 0) // equal using (XmlWriter xw = XmlWriter.Create(@"c:\names.xml")) // insert a second record
if (i != 0) // not equal { cmd1.Parameters["@Name"].Value = "David";
if (i <= 0) // less than or equal xw.WriteRaw("<names>"); cmd1.ExecuteNonQuery();
if (i >= 0) // greater than or equal xw.WriteRaw("<name>Bob</name>"); // read records
if (i > 0) // greater than xw.WriteRaw("<name>David</name>"); sql = "SELECT * FROM Names";
if (i < 0) // less than xw.WriteRaw("</names>"); SqlCommand cmd2 = new SqlCommand(sql, con);
if (o is MyClass) // check type of object } using (SqlDataReader r = cmd2.ExecuteReader()) {
if (o == null) // check if reference is null int iName = r.GetOrdinal("Name");
// Load an XML file while (r.Read()) {
Console.WriteLine(
Loops XmlDocument doc = new XmlDocument();
r.IsDBNull(iName)?"Null":r.GetString(iName)
doc.Load(@"c:\names.xml");
for (int i=0; i<10; i++) {} );
foreach (XmlNode node in doc.SelectNodes("//name/text()"))
while (i<10) { i++; } }
{
do { i++; } while (i<10); }
Console.WriteLine(node.Value);
foreach (ListItem item in list.Items) {} // read a single value
}
sql = "SELECT TOP 1 Name FROM Names";
SqlCommand cmd3 = new SqlCommand(sql, con);
Console.WriteLine(cmd3.ExecuteScalar());
www.LearnVisualStudio.NET }
Copyright © 2006, LearnVisualStudio.NET

You might also like