tags:

views:

50

answers:

2

I have just started learning C# and would have one question that I cannot find answer on. Maybe I am just searching something slightly different. Also on MSDN I found following:

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

I am also reading a book for it and it says:

This variable will store numeric value (integer value) which is actually particular type of data. Therefore you will need to use data type determined for storing such a data and that is called an int.

Also if I understand well so data type of any variable is just saying how the data in this variable will look like? If its int, then it will contain specific range of numbers. Right?

+1  A: 

A data type actually says two things about your data:

  • How it looks. To use your example, if it’s an int, then it will contain a number from a specific range.
  • What you can do with it. For example, you can add two int numbers together by saying 1 + 2. Or you can append a text to string (this looks similar: "a" + "b"). Or you can search for a fragment of a text in another text ("hello".IndexOf("ll") will return 2).

These two things are called the implementation and the interface of a data type, respectively.

Konrad Rudolph
Thanks, also what the book says (I know my translation was terrible) is correct. Number is just type (kind) of data so I will need to use appropriate data type in C#.
SnakeeP
A: 

True. In C#, everything derives from the object type. But, as you put different types of data in your variables, you've got to specify which specialized type of object it is (byte, string, int,...) so the .net framework knows how to interpret the data it contains.

For example, how would the framework add two (int) objects if it isn't aware that the content of each of them is int?

That's the whole point of giving a variable a type. If you do, the framework knows what methods it can allow you to execute on this objects while making the operations type-safe.

A bit more on type-safety, if you wish : http://en.wikipedia.org/wiki/Type_safety

nrocha