what are classes and objects in C#?
Loading
what are classes and objects in C#?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Toufik El AzzaouiPosted Oct 11, 2022, 8:11 AM
If you mean the difference between class and object:
- Object is an instance of a class. You can say that a class is a "type". That is, when you declare an object of this class, it is like declaring a new "variable" whose type is this class.
For example:
If we define a class Person which has the attributes : Name, first name, age, and the methods : eat, speak...
public Class Person(){
private string name;
private string firstname;
private int age;
public void speak(){
}
public void eat(){
}
}
Then we can declare an object of type Person.
Person P=new Person();
We say that P is an instance (an object) of the class Person.
Translated with www.DeepL.com/Translator (free version)
Jignesh KumarPosted Sep 14, 2022, 4:06 AM
Hello,
Uday DodiyaPosted Sep 14, 2022, 3:57 AM
Classes and Objects are basic concepts of Object Oriented Programming that revolve around real life entities.
Class
1. Class is a set of object which shares common characteristics/ behavior and common properties/ attributes.
2. Class is not a real world entity. It is just a template or blueprint or prototype from which objects are created.
3. Class does not occupy memory.
4. Class is a group of variables of different data types and group of methods.
A class can contain:
• data member
• method
• constructor
• nested class and
• interface
Example :
• Animal
• Student
• Bird
• Vehicle
• Company
Object
It is a basic unit of Object-Oriented Programming and represents real life entities. A typical program creates many objects, which as you know, interact by invoking methods. An object consists of :
Example of an object: dog
Vishal YelvePosted Sep 13, 2022, 2:33 PM
C# Class
Class is the blueprint for the object.
We can think of the class as a sketch (prototype) of a house. It contains all the details about the floors, doors, windows, etc. We can build a house based on these descriptions. House is the object.
Like many houses can be made from the sketch, we can create many objects from a class.
C# Objects
An object is an instance of a class. Suppose, we have a class Dog. Bulldog, German Shepherd, and Pug are objects of the class.
Creating an Object of a class
In C#, here's how we create an object of the class.
Sachin SinghPosted Sep 13, 2022, 6:50 AM
Not only for c# but for any Object Oriented Programming will have same meaning.
The root purpose behind the discovery of OOPs was to overcome from the drawbacks of functional programming. Like C programing has the feature to include a library in the context of reusability but even the include feature copies the physical file to destination project , so it is not actually resusability. Similarly, With C programing or any other functional programing we can't add additional feature to an exisiting function,for this you will need to copy paste and modify the function with a different function name, meaning you will endup creating new function. Inheritance and polymorphism solves such issue.
Now, why all the problems were coming, because we were thinking interms of functions, the solution was to think in terms of real world entities, their struture (blueprint like attributes (phone is of red color with keypad and can be used for calls)) was termed as class while the class in action was called as object, meaning when the class get's initialize and really do something we call it object.
what is class and object:-
what you "see" is and object. What you "Say" is a class.
For example, you are currently looking at your laptop , you are saying you are looking at your laptop but actually you are looking at a specific laptop may be dell or lenovo, so Laptop is a generic term, a representative of feature (blueprint/skelton) but the laptop you are using is just and instance of that blueprint, it is a real laptop not a blueprint of laptop, in short it is an object.
So, we speak in terms of classes beacause language will always be generic until you are not a scientist, but in reality we only talk about object (speaking as a class) and instance of some class, because whatever is in sight is an object not a class. Sachin is an instance of human. Sachin is an object of type Human. Sachin : Human.
Now, it should be clear.
Rajanikant HawaldarPosted Sep 13, 2022, 6:40 AM
Hello Dpk,
Classes are:
1)Class
2)Sealed Class
3)Abstract Class
4)Nested Class
5)Static Class
6)Derived Class
https://www.c-sharpcorner.com/UploadFile/0c1bb2/types-of-classes-in-C-Sharp1/
https://www.c-sharpcorner.com/code/3685/nested-class-in-c-sharp-and-interview-question-on-nested-class.aspx
Objects :
https://www.c-sharpcorner.com/blogs/c-sharp-objects-and-classes
https://www.c-sharpcorner.com/article/classes-and-objects/
https://www.c-sharpcorner.com/Blogs/class-and-object-in-c-sharp
https://www.c-sharpcorner.com/UploadFile/e9fdcd/basics-of-C-Sharp/
https://www.c-sharpcorner.com/uploadfile/puranindia/singleton-objects-C-Sharp/