简体   繁体   中英

Are interfaces data types?

I am viewing tuts+ video titled Design Patterns in C# . In it, the instructor says:

"Interfaces are contracts. They are data types that define properties and methods that we have to implement within a class."

My question: Is this correct, that is, are interfaces really data types in C#?

From the C# programming guide :

C# is a strongly-typed language. Every variable and constant has a type, as does every expression that evaluates to a value. Every method signature specifies a type for each input parameter and for the return value.

(...)

You use the struct, class, interface, and enum constructs to create your own custom types.

So yes, interfaces are considered first class types in the .NET world. Anyway I find the term "data type" confusing in that context, I think that saying just "type" is more correct.

By the term datatype , the instructor must have hinted that Interfaces declare the type that has to be defined later.

If you consider the usage of an Interface, it does the same. It helps us to declare the methods but gives away the freedom to the developers to define it as per their need.

Hope it clears your doubts.

Interface can be considered as reference data type.

A data type in programming language is a set of data with values having predefined characteristics . In Object Oriented programming, a programmer can create new data types to suit the application requirements.

With object-oriented programming, a programmer can create new data types to meet application needs. Such an exercise as known as " data abstraction " and the result is a new class of data. Such a class can draw upon the "built-in" data types such as number integers and characters. For example, a class could be created that would abstract the characteristics of a purchase order. The purchase order data type would contain the more basic data types of numbers and characters and could also include other object defined by another class. The purchase order data type would have all of the inherent services that a programming language provided to its built-in data types.

Source

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM