using System;
using System.Collections.Generic;
namespace MyProject
{
public class MyClass
{
...
}
}
<type> <name>;
int age;
const int x = 0;
const string productName = "Visual C#";
const double x = 1.0, y = 2.0, z = 3.0;
// Declaration statement. int counter; // Assignment statement. counter = 1; // foreach statement block that contains multiple statements. foreach (int radius in radii){}
Console.WriteLine("Hello World")
y = x+5
myString = "Hi!";
if ( age == 15)
Type |
Alias For |
Allowed Values |
byte |
System.Byte |
Integer between 0 and 255 |
int |
System.Int32 |
Integer between −2147483648 and 2147483647 |
bool |
System.Boolean |
Boolean value, true or false |
string |
System.String |
A sequence of characters
etc... |
enum Days { Sun, Mon, Tue, Wed, Thu, Fri, Sat }; //Starts from 0
//OR
enum Days { Sun = 1, Mon, Tue, Wed, Thu, Fri, Sat }; //Starts from 1
static void Main()
{
int x = (int)Days.Sun;
int y = (int)Days.Fri;
Console.WriteLine("Sun = {0}", x);
Console.WriteLine("Fri = {0}", y);
}
type[] arrayName;
// Declare a single-dimensional array
int[] array1 = new int[5];
// Declare and set array element values
int[] array2 = new int[] { 1, 3, 5, 7, 9 };
// Alternative syntax
int[] array3 = { 1, 2, 3, 4, 5, 6 };
// Declare a two dimensional array
int[,] multiDimensionalArray1 = new int[2, 3];
// Declare and set array element values
int[,] multiDimensionalArray2 = { { 1, 2, 3 }, { 4, 5, 6 } };
// Create a list of strings.
var salmons = new List<string>();
salmons.Add("chinook");
salmons.Add("coho");
salmons.Add("pink");
salmons.Add("sockeye");
// Create a list of strings by using a
// collection initializer.
var salmons = new List<string> { "chinook", "coho", "pink", "sockeye" };
if (<test>)
{
<code executed if <test> is true>;
}
else
{
<code executed if <test> is false>;
}
switch (<testVar>)
{
case <comparisonVal1>:
<code to execute if <testVar> == <comparisonVal1> >
break;
case <comparisonVal2>:
<code to execute if <testVar> == <comparisonVal2> >
break;
…
default:
<code to execute if <testVar> != comparisonVals>
break;
}
int i = 1;
while (i <= 10)
{
Console.WriteLine(″{0}″, i++);
}
int i = 1;
do
{
Console.WriteLine(″{0}″, i++);
} while (i <= 10);
int i;
for (i = 1; i <= 10; ++i)
{
Console.WriteLine(″{0}″, i);
}
var salmons = new List<string> { "chinook", "coho", "pink", "sockeye" };
foreach (var salmon in salmons)
{
Console.Write(salmon + " ");
}
break—Causes the loop to end immediately
continue—Causes the current loop cycle to end immediately (execution continues with the next loop cycle)
goto—Allows jumping out of a loop to a labeled position (not recommended if you want your code to be easy to read and understand)
return—Jumps out of the loop and its containing function
namespace ProgrammingGuide
{
// Class definition.
public class MyCustomClass
{
// Class members:
// Property.
public int Number { get; set; }
// Method.
public int Multiply(int num)
{
return num * Number;
}
// Instance Constructor.
public MyCustomClass()
{
Number = 0;
}
}
}
public string FullName { get; set; }
public int Age { get; set; }
public string Country { get; set; }
// private field private DateTime date; // public field (Generally not recommended.) public string day;
public class Taxi
{
public bool isInitialized;
public Taxi()
{
isInitialized = true;
}
}
class TestTaxi
{
static void Main()
{
Taxi t = new Taxi();
Console.WriteLine(t.isInitialized);
}
}
public class Employee
{
public int salary;
public Employee(int annualSalary)
{
salary = annualSalary;
}
public Employee(int weeklySalary, int numberOfWeeks)
{
salary = weeklySalary * numberOfWeeks;
}
}
//Usage
Employee e1 = new Employee(30000);
Employee e2 = new Employee(500, 52);
// public class:
public class Tricycle
{
// protected method:
protected void Pedal() { }
// private field:
private int wheels = 3;
// protected internal property:
protected internal int Wheels
{
get { return wheels; }
}
}
public void Caller()
{
int numA = 4;
// Call with an int variable.
int productA = Square(numA);
int numB = 32;
// Call with another int variable.
int productB = Square(numB);
// Call with an integer literal.
int productC = Square(12);
// Call with an expression that evaulates to int.
productC = Square(productA * 3);
}
int Square(int i)
{
// Store input argument in a local variable.
int input = i;
return input * input;
}