Python Notes - Programiz
Python Notes - Programiz
create a class
create objects from the class
Create a class in C#
//field
string breed;
//method
public void bark() {
Note: In C#, fields and methods inside a class are called members of a class.
C# Objects
Now, the bullDog object can access the fields and methods of the Dog class.
We use the name of objects along with the “.” operator to access members of a class.
For example,
using System;
namespace ClassObject {
class Dog {
string breed;
Console.ReadLine();
}
}
}
Output:
Bull Dog
Bark Bark !!
In the above program, we have created an object named bullDog from the Dog class. Notice that
we have used the object name and the . (dot operator) to access the breed field and the bark()
method.
We can create multiple objects from the same class. For example,
using System;
namespace ClassObject {
class Employee {
string department;
Console.ReadLine();
}
}
}
Output:
Sheeran: Development
Taylor: Content Writing
In C#, we can also create an object of a class in another class. For example,
namespace ClassObject {
class Employee {
public string name;
}
}
class EmployeeDrive {
static void Main(string[] args) {
Console.WriteLine("Employee 1");
Console.ReadLine();
}
}
}
Output
Employee 1
Name: Gloria
Work: Coding