delegate void myDelegate(string msg);
delegate void myDelegate(string message);
----> Declaring a delegate of Name "myDelegate"void
----> return type. this delegate will only take methods with the void return type as value. delegate
----> keyword used to create a delegate.(string msg)
----> the delegate will require methods with 1 single string parameter to function.static void showMsg1(string message)
{
Console.WriteLine("Hello " + message);
}
static void showMsg2(string message)
{
Console.WriteLine("Welcome abord the Delegate ship. " + message);
}
MyDelegate objD = new MyDelegate(showMsg1);
MyDelegate objD = new MyDelegate
----> Instantiating the delegate object "objD". Notice that delegates can be instantiated just like classes.(showMsg1)
----> Initializing the showMsg1 method to the delegate.objD += showMsg2;
myFunc += showMsg2;
----> Initialize the method "showMsg2" to the delegate myFunc +=
----> myFunc = myFunc + showMsg2;objD("Tom");
Console.ReadLine();
class Person
{
public event EventHandler OnPropertyChange;
event
----> Keyword used to declare an event.EventHandler
----> Delegate; used when we got no information about the event. string name ="";
public string Name
{
get
{
return name;
}
set
{
name = value;
OnPropertyChange(this, new EventArgs());
}
onPropertyChange(this, new EventArgs()); ---->
Run event once property's value is changed.this
----> In the current context, the this keyword is used to reference the Person class object.new EventArgs()
----> the EventArgs() class
provides a value to use for events that do not contains event information.}
Person objP = new Person();
objP.OnPropertyChanged += objP_OnPropertyChange;
+= objP_OnPropertyChange
----> Add method named objP_OnPropertyChange to the event.objP_OnPropertyChange method,
after the +=
press: tab
twice.objP.Name = "Tony Stark";
static void objP_OnPropertyChange(object sender, EventArgs e)
{
object sender
----> Object from which the event came from (in this case , the event is raised on the change "name" property object from the Person class)EventArgs e
----> Information about the event argumentsConsole.WriteLine("Sometimes you gotta run before you can walk.");
Console.ReadLine();
}
namespace TeamA
{
class Test
{
public static void Go()
{
}
}
}
namespace TeamB
{
class Test
{
public static void Go()
{
}
}
}
TeamA.Test.Go();
TeamA.Test.Go();
----> Accessing the test class method from the namespace Team Ausing TeamA
Test.Go();
class Person
{
public string Name;
public void myMethod()
{
Console.WriteLine(Name);
Console.ReadLine();
}
}
class Employee : Person
{
public string department;
}
class Employee : Person
----> Inheritance; a class cannot inherite from more than 1 classEmployee : Person
----> Class Employee is inheriting from class person.Employee objEmp = new Employee();
objEmp.Name = "Tom";
objEmp.Name = "Tom";
----> Notice that the Class Employee, now contains the variables,methods and properties of class Person.objEmp.department = "Software Engineering";
objEmp.department = "Software Engineering";
----> While inheriting from class Person, the class Employee does not loss any of its own variables,methods and properties.objEmp.myMethod();
class Person
{
protected string Name;
}
Program objP = new Program();
objP.Name = "Jake Roper";
objP.Name = "Jake Roper";
----> Logical error "ApplicatioName.Person.Name is inaccessible due to its protection level".class Program : Person
Modifier | Class | Subclass |
---|---|---|
Public | ✓ | ✓ |
No modifier | ✓ | ✗ |
Private | ✓ | ✗ |
public class Employee
{
public int ID { get; set; }
public string FullName { get; set; }
}
Using System.Reflection;
Type myType = Type.GetType("MyReflection.Employee");
Type myType = Type.GetType("MyReflection.Employee");
---->
Get information about the Employee class properties and store it in the myType variableType
----> Class .GetType
----> the method requires the Namespace.ClassName as a parameter to return information about the class's properties.PropertyInfo[] myProperties = myType.GetProperties();
PropertyInfo[]
----> Store the Employee class's properties in an array of PropertyInfo data type.myType.GetProperties();
----> the GetProperties method is used to retrive the properties of the Employee class.foreach (PropertyInfo item in myProperties)
{
Console.WriteLine(item.PropertyType + " " + item.Name);
}
Console.ReadLine();
abstract class myClass
{
public static string MyName { get; set; }
public static void Hello()
{
Console.WriteLine("Hello World");
Console.ReadLine();
}
}
myClass A = new myClass();
myClass.Hello();
myClass A = new myClass();
Using System.Threading
static void WriteX()
{
for (int i = 0; i < 2000; i++) Console.Write(" X");
}
Thread t = new Thread(WriteX);
Thread t
----> Declare a new threadnew Thread(WriteX);
----> Initialize the thread with the WriteX method t.Start();
t.Start();
---->Execute the WriteX() method for (int i = 0; i < 2000; i++) Console.Write(" O");
Console.ReadLine();