enum Department
{
Marketing,
Development,
HR
};
Department empTom = Department.Development;
= Department.Development;
----> The employee tom as been initialise to the development department. if (empTom == Department.Development)
{
Console.WriteLine("Tom is in the development department");
}
else if (empTom == Department.Marketing)
{
Console.WriteLine("Tom 's department is marketing.");
}
Console.ReadLine();
1. Type:
const string _cs = ".cs";
const
----> the const keyword is followed by_cs = "";
----> Overwriting the constant variable will produce a compile-time error:Console.WriteLine(_cs);
----> Accessing a constant variablestring sentence = "I am Batman!";
string[] words = sentance.Split(' ');
foreach (string word in words)
{
Console.WriteLine(word);
}
do
{
Console.WriteLine("1 is greater than 2");
Console.ReadLine();
}
do{ }
----> The impossible statement to be executed is enclosed in curly brackets after the do keyword.while (1 > 2);
while (1 > 2);
----> the condition is stated afterwards.public static void MyMethod()
{
Console.WriteLine("no parameter");
}
public static void MyMethod(string name)
{
Console.WriteLine(name);
}
MyMethod();
MyMethod("Tom");
try
{
int[] myArray = new int[3] { 1, 2, 3 };
Console.WriteLine(myArray[4]);
Console.ReadLine();
}
try{}
the statement which can cause an error is enclosed within curly brackets. catch(Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine();
}
catch(Exception ex)
----> the catch method takes the exception class as parameter using which we can get information about the error that occurred.ex.Message
----> get the error message in a readable format.class RocketBunny<T>
{
T value;
class RocketBunny<T>
----> When we specify a generic class the <T> characters should be added.T value;
----> Notice that the datatype of the value is genericspublic RocketBunny(T parameter)
{
value = parameter;
}
T parameter
----> the method's parameter is also of type generic that is its data type customizable.value = parameter;
----> The value of the variable "value" can be of any data type since both the variable "value" and the parameter being supplied are of the generic data type. public void Write()
{
Console.WriteLine(value);
}
}
RocketBunny<int> objPen = new RocketBunny<int>(1);
RocketBunny<int> objPen = new RocketBunny<int>
----> Instantiating the generic object objPen as an integer. (1)
----> initialising the objPen value to 1.objPen.Write();
static class Island
{
public static string Name { get; set; }
static public void Write()
{
Console.WriteLine(Name);
}
}
static void Main(string[] args)
{
Island.Name = "Mauritius";
Island.Name
----> Static properties requires no instantiation, thus we cannot have more than 1 instance of the Island class.Island.Write();
Console.ReadLine();
}
interface Channelr
{
void Write();
}
void Write();
----> the class that inherites the interface ChannelR needs to have a method named Write() in it to function.class myClass : Channelr
{
public void Write()
{
Console.WriteLine("Hello World");
}
}
Channelr objChannelR = new myClass();
objChannelR.Write();