Reputation: 12562
In his book Pro C# 2008 and the .NET 3.5 Platform, Andrew Toelsen says, "In the world of .NET, type is simply a general term used to refer to a member from the set {class, interface, structure, enumeration, delegate}.
What do these all have in common that they would be classed together as types? What would be a formal definition of type that would cover all of these under one umbrella term like that???
Upvotes: 1
Views: 97
Reputation: 20054
Short answer: you can declare variables of these - a type defines the "kind of" or "structure" of a variable.
Technically, when you declare a variable of a certain type (for example, a class, an integral type etc.), you can only assign values of this specific type (or a compatible one, or one where an implicit type conversion exists) to the variable. In a strongly typed language like C#, in most cases the compiler will tell you if you break that rule.
Upvotes: 2
Reputation: 19347
A type is anything that can describe a variable. For insstance, I can have a variable 'a' of type 'int' (int is a struct). I can also have 'b' of type 'MyClass' (MyClass is a class).
Basically, all type are used (more or less) in this way (to declare a variable):
TypeName variableName;
The newly declared variable automatically has some sort of internal structure, for example, if it is of a class type, it can have methods and properties. If is of a delegate type, then it is designed to hold a reference to a method.
Basically, a type is anything that you use to declare a variable. A type gives the variable its meaning, as well as restricts the variable to being used in a way which is suited to its existence.
Upvotes: 3
Reputation: 210785
They're all types because they represent all the possible types of a variable. Simple! :)
Upvotes: 0