Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a class and a datatype?

I have heard the following statement:

We can say class is a datatype or a datatype is one type of class.

Can anyone explain to me what exactly this means?

like image 753
Hitesh Prajapati Avatar asked Mar 02 '11 06:03

Hitesh Prajapati


People also ask

Is type and class the same thing?

The class defines object's internal state and the implementation of its operations. In contrast, an object's type only refers to its interface - a set of requests to which it can respond. An object can have many types, and objects of different classes can have the same type.

What is the difference between class and type in Java?

In this system, a class (an element that is described by an instance of Class<T> ) is any type created with the keyword class or interface , any primitive type, any array thereof, and void . Everything else is just a Type .

What is the difference between class and type in Python?

Types were built-in objects defined in C; classes were what you built when using a class statement. The two were named differently because you couldn't mix these; classes could not extend types.

Why do we use class as a data type?

The purpose of a data type class is to be used as the type of a literal property in an object class. Data type classes provide the following features: They provide for SQL and client interoperability by providing SQL logical operation, client data type, and translation information.


2 Answers

C# is a strongly typed language; therefore every variable and object must have a declared type.

A data type can be described as being either:

A built-in data type, such as an int or char, or

A user-defined data type, such as a class or interface.

Data types can also be defined as being either:

Value Types (C# Reference), which store values, or

Reference Types (C# Reference), which store references to the actual data.

** Class is a user define data type. **

like image 123
sikender Avatar answered Sep 30 '22 18:09

sikender


Classes are Reference Types.

A Data Type is a value type if it holds the data within its own memory allocation.

Reference types are allocated on the heap, and memory management is handled by the garbage collector. Value types are allocated on the stack or inline and are deallocated when they go out of scope. In general, value types are cheaper to allocate and deallocate.

Like for example

class Person
{
   string name;
}

In this the class Person is reference type and name is value type i.e data type.

struct Person
{
   string name;
}

In this the struct Person is value type and also name is value type i.e both are data type.

A data type is a value type if it holds the data within its own memory allocation. A reference type contains a pointer to another memory location that holds the data.

In reference to MSDN article on Classes and Structures and also MSDN article on Reference Type and Value Type

like image 44
Harsh Baid Avatar answered Sep 30 '22 20:09

Harsh Baid