100% found this document useful (7 votes)
20K views1 page

C# Cheat Sheet

The document provides an overview of C# language basics including working with numbers, defining new types, working with text, dates and times, conditional logic, inheritance, working with a database, comparison operators, loops, and working with XML.

Uploaded by

Addele Cruz
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (7 votes)
20K views1 page

C# Cheat Sheet

The document provides an overview of C# language basics including working with numbers, defining new types, working with text, dates and times, conditional logic, inheritance, working with a database, comparison operators, loops, and working with XML.

Uploaded by

Addele Cruz
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
  • Cheat Sheet: C# Language Basics

Visual C# 2005 Express Edition

Cheat Sheet : C# Language Basics


Working With Numbers
int i = 0; // convert from a string int i = [Link]("1"); // convert froma string and dont throw exceptions if ([Link]("1", out i)) {} i++; // increment by one i--; // decrement by one i += 10; // add 10 i -= 10; // subtract 10 i *= 10; // multiply by 10 i /= 10; // divide by 10 i = checked(i*2) // check for overow i = unchecked(i*2) // ignore overow

Dene New Types


struct MyValueType {} // denes a value type class MyClass { // denes a reference type // elds const string constName = "[Link]"; static string staticName = "[Link]"; readonly string readOnlyName; string instName; // properties public string Name { // read/write get { return instName; } set { instName = value; } } public string ReadOnlyName { get { return instName; } } // methods public void Print() { [Link](instName); } public int Add(int a, int b) { return a + b; } public static void PrintStatic() { [Link](staticName); } // constructors public MyClass() {} // default constructor public MyClass(string name) { readOnlyName = name; instName = name; } }

Working With Text


using [Link]; using [Link]; string name = "David"; string hello1 = "Hello " + name; string hello2 = [Link]("Hello {0}", name); [Link]("Hello {0}", name); StringBuilder sb = new StringBuilder("Hello "); [Link](name); [Link]("Goodbye {0}", name); [Link]([Link]()); // Create a text le using (StreamWriter w = [Link](@"c:\[Link]")) { [Link]("Bob"); [Link]("David"); } using (StreamReader r = [Link](@"c:\[Link]")) { while (![Link]) { [Link]([Link]()); } } foreach (string s in [Link](@"c:\[Link]")) { [Link](s); }

Dates and Times


DateTime now = [Link]; // date and time DateTime today = [Link]; // just the date DateTime tomorrow = [Link](1); DateTime yesterday = [Link](-1); TimeSpan time = tomorrow - today; int days = [Link]; int hours = [Link]; int minutes = [Link]; int seconds = [Link]; int milliseconds = [Link]; time += new TimeSpan(days, hours, minutes, seconds, milliseconds);

// Read from a text le

Conditional Logic

Inheritance
class MyClass : Object { // override inherited method public override string ToString() { // call base class version string s = [Link](); return "Hello " + s; } }

Working With a Database


using [Link]; using [Link]; string cs = @"Data Source=.\SQLEXPRESS;" + string cs = @"Initial Catalog=NamesDB;" + string cs = @"Integrated Security=True;"; using (SqlConnection con = new SqlConnection(cs)) { [Link](); string sql = "INSERT INTO Names(Name) VALUES(@Name)"; // insert a record SqlCommand cmd1 = new SqlCommand(sql, con); [Link]("@Name", [Link], 100); [Link]["@Name"].Value = "Bob"; [Link](); // insert a second record [Link]["@Name"].Value = "David"; [Link](); // read records sql = "SELECT * FROM Names"; SqlCommand cmd2 = new SqlCommand(sql, con); using (SqlDataReader r = [Link]()) { int iName = [Link]("Name"); while ([Link]()) { [Link]( [Link](iName)?"Null":[Link](iName) ); } } // read a single value sql = "SELECT TOP 1 Name FROM Names"; SqlCommand cmd3 = new SqlCommand(sql, con); [Link]([Link]()); }

if (i == 0) {} if (i <= 10) {} else {} switch (i) { case 0: case 1: break; case 2: break; default: break; }

Working with XML


using [Link]; using [Link]; // Create an XML le using (XmlWriter xw = [Link](@"c:\[Link]")) { [Link]("<names>"); [Link]("<name>Bob</name>"); [Link]("<name>David</name>"); [Link]("</names>"); } // Load an XML le XmlDocument doc = new XmlDocument(); [Link](@"c:\[Link]"); foreach (XmlNode node in [Link]("//name/text()")) { [Link]([Link]); }

Compare Operators
if if if if if if if if (i (i (i (i (i (i (o (o == != <= >= > < is == 0) // equal 0) // not equal 0) // less than or equal 0) // greater than or equal 0) // greater than 0) // less than MyClass) // check type of object null) // check if reference is null

Loops
for (int i=0; i<10; i++) {} while (i<10) { i++; } do { i++; } while (i<10); foreach (ListItem item in [Link]) {}

[Link]

Copyright 2006, [Link]

You might also like