Part-10-C# Tutorial Class & object
Class & Object
Some Key points about classes
class Demo
{
//method
public void add()
{
Console.WriteLine("This is inside class method");
}
}
Class & Object
- Class are created by using class "Keyword" declaration.A class declaration begin with attributes ,modifiers and accessibility of a class.
- Class are the user defined data types that represent the state and behaviour of an object. State represents the properties and behaviour is the action that objects can perform.
- The name of the class ex:- Program,Test,Demo..etc.
- The declaration is followed by the body of the class that contains member definitions between the curly braces “{“ and “}”.
Syantax :-
public class Program
{//code
}
- Class can be declared by using the following access specifiers that limit the accessibility of class to other classes, however some classes does not require any access modifiers.
Some Key points about classes
- Classes are reference types that hold the object created dynamically in a heap memory.
- All classes have a base type of System.Object.
- The default access modifier of a class is Internal.
- The default access modifier of methods and variables is Private.
Types of class to be discuss later.
- Abstract class
- Partial class
- Sealed class
- Static class
Class Employee{
int empId = 24;
string empName ="chandan";
string job = "Software Developer";
}
A class can also have methods. A method defines the operations that can be performed inside a class.
{
//method
public void add()
{
Console.WriteLine("This is inside class method");
}
}
class Program
{
static void Main(string[] args)
{
Demo demo = new Demo();
demo.add();
Console.Read();
}
}
Demo demo = new Demo();
new "keyword" create reference type, new means memory allocate.
{
static void Main(string[] args)
{
Demo demo = new Demo();
demo.add();
Console.Read();
}
}
Thus, in the above program, the Demo class, objects create in the main class As the Demo class contains all method of a base class implicitly, we able to create Object.
Object :
An instance of an object is created by using the “new” operator. It is denoted by using the class name for which the instance is being created followed by a variable to store the reference of the instance, then demo class name Demo with reference name ex-demo “=” equals to sign followed by the “new” keyword and then again the name of the class with both open and closed brackets “()” and end with semicolon.
new "keyword" create reference type, new means memory allocate.
please comment if you have any doubt & query regarding to this topic and for another new tutorial topic just drop me a comment.