Variables and Standard Types

What is a type?

All variables in your code are of a 'type'. A type represents a plethora of concepts, depending on what you are using them for.

using System; public class Program...

 

We've seen in our previous example the use of the words 'string', and 'int'. These are types (or specifically type aliases - we'll come to this later),  which define a certain set of requirements, including:

  • The storage space required to store a value of the type
  • The minimum and maximum values that can be represented with the type
  • The location in memory for variables of the type
  • What operations can be permitted
  • The base type it inherits from

Each of these requirements is defined by the type, and handled at run-time by the .NET Framework's CLR.

All types in the .NET Framework inherit from Object, which represents the root of the type hierachy. All strings, numbers, dates and times can trace their inheritance back to this one type.

When you declare a variable of a type, the runtime considers all of the above in figuring it where and how to store the value.

Different types bring different services to your code. For instance, if I am working with dates, I can use DateTime.Now to give me an instance of a DateTime that represents the current date/time. When working with integers, I can use the special value int.MaxValue which represents the biggest integer that can be used in your code. If I am working with strings, I can use ToUpper() to convert my lower-case string to an UPPER-CASE string.

using System; public class Program...

 

These services will come into play as we develop out SurveryBuilder application.

Page 6 of 17