int age = 21; // Integer
float price = 10.951f // Float
bool result = true; // Boolean
string message = "Hello World!"; // String
char firstInitial; // Declaration
firstInitial = 'K'; // Initialization
/* Other data types: double, long, byte */
// Comment on a single line
/*
Block comments
Everything in here will be
treated as a comment
*/
int age = 21;
age++;
int yourAge = 34;
age = age + yourAge;
/*
+ : Addition - : Subtraction
* : Multiplication / : Division
% : Modulus Division
++ : Increase by 1 -- : Decrease by 1
*/
age += 5; // Addition assignment operator
age -= 8; // Subtraction assignment operator
if (age == 10)
{
message = "one decade old";
}
if (age > 21)
{
message = "over 21";
}
else if (age < 21)
{
message = "under 21";
}
else
{
message = "exactly 21";
}
/*
== : Equal to != : Not Equal to
> : Greater than < : Less than
>= : Greater than or equal to <= : Less than or equal to
*/
// While Loop
while (num < 100)
{
num++;
}
// For Loop
for (int i = 0; i < 100; i++)
{
sum += i; // adds all values from 0 to 99
}
// Creates an empty "scores" array that can store 20 integers
int[] scores = new int[20];
scores[0] = 92;
scores[1] = 87;
Console.WriteLine( scores[0] ); // Prints 92
// Insert values at instantiation
// Size of array automatically matches number of items
string[] friends = new string[] { "Chris", "Jill", "Alex" };
class Person {
public string name;
public int age;
private int ssn; // can't be accessed from outside instance
float wage; // fields are private by default
}
Person man = new Person();
man.name = "John";
man.age = 30;
class Person {
private int age;
public int getAge()
{
return age;
}
public int setAge(int newAge)
{
age = newAge;
}
}
Person man = new Person();
man.setAge(25);
Console.WriteLine( man.getAge() );