3

Currently, I'm reading C# 5.0 in a Nutshell and in the Type Basics (located at Chapter 2) subtopic the term symmetry of predefined types and custom types is introduced...

...why the author talks about symmetry between those types? What are the argument to say that types are symmetric?

This is the original paragraph:

A beautiful aspect of C# is that predefined types and custom types have few differences. The predefined int type serves as a blueprint for integers. It holds data -32 bits- and provides function members that use that data, such as ToString. Similarly, our custom UnitConverter type acts as a blueprint for unit conversions. It holds data -the ratio- and provides function members to use that data.

Camilo Martin
  • 37,236
  • 20
  • 111
  • 154
InfZero
  • 2,944
  • 4
  • 24
  • 36

4 Answers4

4

The meaning of the text is that all of the types in the language "behave like objects". For example, you can do this:

int i = 42;
Console.WriteLine(i.ToString()); // called a method on an int

as easily as you can do this:

DateTime d = DateTime.Now;
Console.WriteLine(d.ToString()); // called a method on a DateTime

That is, if you did not know the types of i and d you would not be able to deduce anything by just reading the code that calls .ToString().

Contrast this with a language like C++:

std::string s = "hello";
std::cout << s.length();

int i = 42;
// ...but you cannot invoke any method on i; "ints are special"

Here the fact that s.length() compiles tells us that s is not an int, or a pointer, or any other primitive type.

This uniformity does not buy you anything from a technical standpoint, but it is nevertheless a welcome feature -- especially if you are just learning the language.

Jon
  • 428,835
  • 81
  • 738
  • 806
2

I believe the author's point is that built-in types and custom types are both meant to serve similar purposes; hold data and provide mechanisms for using that data. I believe there is also a veiled reference in there that both built-in objects and custom objects inherit from System.Object, thus they share similar abilities (i.e. ToString()).

I do not necessarily agree with term 'symmetry', but that is my two cents on the topic.

Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
1

There are languages where you can't create type that behave similarly to something that is built in like int. In C# one can create type with all properties of 'int' (which is synonym of System.Int32) - value type with +/-/=operation.

I.e. in JavaScript one can't create another Array type or string type. In Java value types are (were?) not present, so type equivalent to 'int' can't be written by programmer ( Does Java make distinction between value type and reference type ).

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
0

I am reading the latest edition of this book for C# 10 and had the same question. I like all of the answers so far provided, but have one additional point: this is a reference to the utility of C#'s unified type system, which the author mentions briefly in Chapter 1 under the section 'Object Orientation':

Unified type system The fundamental building block in C# is an encapsulated unit of data and functions called a type. C# has a unified type system in which all types ultimately share a common base type. This means that all types, whether they represent business objects or are primitive types such as numbers, share the same basic functionality. For example, an instance of any type can be converted to a string by calling its ToString() method.

To distill the author's writing and the comments from others down into a few points:

  • The type system of C# is unified, in that all types inherit from the common System.Object type.
  • Logically, type components can be logically grouped into two components: data members and function members.
  • Practically, because types behave like objects (they all have some data and/or behavior); developers can expect a minimum set of functionality from all types when using C#, such as the ability to call .ToString(), among other things.

These three points seem to be what the author had in mind when he referred to the C# type system having a 'symmetric beauty'.