C# : C#
Basics
C# is case-sensitive, including its variable and method names.
Move Line Up/Down - ALT + UP/DOWN
Copy Line - ctrl+c with nothing selected, then paste
Comments
/*
This is another style of a comment.
It allows multiple lines.
*/
// alert
MessageBox.Show("Rob is amazing.");
// labelbox
label1.Text = "Hellow World!";
Variables
int robsNumber = 3;
double robsBigNumber = 3.333333;
decimal robsDecimal = 67.83m;
string robsString = "Rob is amazing";
bool days_ok = false;
// constant
const int PI = 3.14;
// private variabe
private string name = "Rob Secrets";
// change to decimal - this doesnt work.
int wholeNumber 33;
decimal moneyNumber = 4500m;
wholeNumber = (int) moneynumber;
try catch
try
{
//code
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Parseing
//parsing string to a number
double myVar1 = double.Parse(textBox1.Text);
// if parse
if (int.TryParse(tb_input_days, out tb_input_days_int))
{
days_ok = true;
}
Hide Show Elements
pictureBox1.Visible = false;
pictureBox2.Visible = true;
// close the window
this.Close();
Label
//clear a label
label2.Text = "";
// do a calculation and output to a label
label_main_output.Text = ((weight * 703)/height).ToString();
Textbox
// store string
string lname = textBox_lname.Text;
// store number
double cost = double.Parse(tb_cost.Text);
int height = int.Parse(textBox_h.Text);
// focus
textBox_numberOfPackages.Focus();
// get string, concatenate, print
string fname = tb_fname.Text;
string lname = tb_lname.Text;
string mname = tb_mname.Text;
string title = tb_title.Text;
label_main.Text = title + " " + fname + " " + mname + " " + lname;
// get number, calculate, print
int speed = int.Parse(tb_speed.Text);
label_main_output.Text = (speed * 12) + " mph";
Shortcuts
a = a + b;
a += b;
Change Colors
messageLable.BackColor = Color.Black;
messageLable.ForeColor = Color.Yellow;
Random Number
private int randomNumber()
{
Random rand = new Random();
int number = rand.Next(100);
return number;
}