views:

172

answers:

1

How can literals be explained? Why should we use it in a language such as C# and ...?

Literals are very common in C#. I want to know the philosophy and history of literals.

+11  A: 

Literals are very common in C#. I want to know the philosophy and history of literals.

Funnily enough I am on the bus right now writing a blog entry about that exact topic. Look to my blog in the next couple of weeks for the whole article (assuming that I finish it.) But for now, here's a sneak peak.

The principal philosophical impact of string literals in programming languages is that they allow the developer to make a distinction between the mention and the use of a programming language structure. For example:

string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Console.WriteLine(alphabet.Length); // 26
Console.WriteLine("alphabet".Length); // 8

The quotation marks make a big difference. They mean "do not treat the thing inside the quotation marks as part of the program but rather as mere text that has no impact on the program qua the program".

To describe the full philosophical impact of the use-mention distinction would take many hundreds of pages; I recommend that you read "Godel Escher Bach" by Douglas Hofstadter; it is all about these sorts of philosophical explorations.

What is more interesting than string literals are what we might think of as "program literals"; that is "literals" that capture more complex structures than mere string values. Expression tree lambdas in C# 3 are such structures (though they are not usually referred to as "literals", really they logically are.) The history of the lambda literal goes back to the early days of Lisp, with its "quote" operator. I'll be discussing that at more length in my blog, so stay tuned.

Eric Lippert
Very nice Answer . Thanks a lot
Eve
Oh the sweet irony of a language designer caught in a grammatical error. You meant *principal* (meaning primary) philosophical impact, anyway *principle* can only be a noun. +1 anyway.
Ben Voigt