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?
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.
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 .
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.
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.
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. **
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With