50 Fun Facts About C# Programming
Learn something new, then test yourself with the quiz.
Know these facts? Prove it.
Take the 50-question quizWhich game engine, launched in 2005 at an Apple developer conference, uses C# as its primary scripting language?
Unity once offered two other scripting languages, Boo and UnityScript, but retired both in favour of C#. The engine first shipped as a Mac-only product.
Which question-and-answer site for programmers, created in 2008 by Jeff Atwood and Joel Spolsky, is written in C#?
Stack Overflow runs on C# with Microsoft's SQL Server as its database. Its founders wanted a more open alternative to the question-and-answer sites that came before it.
The sharp sign in the name of C# can be read as four of which symbol, arranged two by two?
Plus signs fit the joke: C++ is C stepped up once, so four of them hint at a language one step beyond C++. The name also nods to music, where a sharp raises a note by a semitone.
James Gosling dismissed the newly announced C# as an imitation of which language, which he had created at Sun?
Java came from Sun Microsystems, whose co-founder Bill Joy made the same charge. C#'s lead architect answered that it was no clone and sat much closer to C++ in its design.
Anders Hejlsberg, lead architect of C#, later designed which Microsoft language that compiles to JavaScript?
TypeScript was released to the public in October 2012, after two years of internal development at Microsoft. Its code is translated into plain JavaScript before it runs.
Which automatic process frees C# programmers from releasing memory by hand, as they would in C or C++?
Garbage collection in .NET allocates and releases memory on the program's behalf. That removes classic bugs such as forgetting to free an object or touching memory that has already been freed.
Which method of the Console class prints Hello, World and then moves to a new line in a first C# program?
WriteLine lives in the System namespace, so its full name is System.Console.WriteLine. A directive at the top of the file lets programmers leave off the first part.
Despite the symbol in its name, C# does not begin comments with #; what starts a single-line comment instead?
Two slashes, written //, turn the rest of the line into a comment. Longer notes that run over several lines sit between the markers /* and */.
Which C# keyword declares a variable that can hold only true or false, and never the number 1?
The bool keyword is an alias for the .NET type System.Boolean. Unlike C++, C# never turns an integer into one automatically, so a number cannot stand in for a condition.
Which method name traditionally marks the entry point of a C# program, the first code to run?
Main is the entry point of a C# application, and a program can have only one entry point. Newer versions of the language let short programs leave it out by writing top-level statements instead.
Before the name C# was chosen, Microsoft's design team called the language what, a name dropped for trademark reasons?
Cool stood for C-like Object Oriented Language, the name the team used from January 1999. Microsoft had considered keeping it for the finished language.
Anders Hejlsberg, the chief designer of C#, first made his name writing which Borland compiler of the 1980s?
Turbo Pascal sold for $49.95, about a tenth of the price of comparable products, and was known for compiling very fast. Hejlsberg had written its core in Denmark before Borland licensed it.
Which punctuation mark ends almost every C# statement, a rule the language shares with C and C++?
Semicolon endings, not line breaks, tell the C# compiler where a statement stops. That is why one long statement can be spread across several lines of a file.
Which pair of symbols encloses a block of code in C#, such as the body of a class or method?
Curly braces signal both a block of code and a new scope. A variable declared between a pair stops existing at the closing one, so referring to it afterwards is an error.
C# treats the names total and Total as two different identifiers; what is this trait of a language called?
Case sensitivity applies to every C# identifier. By convention, classes and methods take PascalCase names, while local variables and method arguments are written in camelCase.
Which C# keyword, written at the top of a file before a name such as System, saves typing full type names?
The using directive lets code say Console in place of System.Console. The same keyword has a second job: it begins a statement that closes files and similar objects automatically when a block ends.
Which C# keyword tells the compiler to work out a local variable's type from the value assigned to it?
The var keyword still produces a strongly typed variable, because the compiler fills in the type. It is required for anonymous types, which have no name a programmer could write.
Which C# loop keyword visits every element of a collection in turn without needing an index variable?
The foreach statement runs its body once for each element, and skips it entirely when the collection is empty. It works on arrays, lists and any other type that can be enumerated.
Which C# keyword is written as the return type of a method that gives no value back to its caller?
The void keyword tells callers there is no result to use. C# refuses to let a variable be declared with it, since there would be nothing to store.
In a C# try statement, which block runs whether or not an exception was thrown, making it ideal for cleanup?
The finally block runs whenever control leaves the try block, however it leaves. Programmers put code there to close files and tidy up whatever else the try block had opened.
What is the C# term for converting a value type such as int to the type object, stored on the heap?
Boxing happens automatically, while getting the value back out again requires a cast. Both steps are computationally expensive compared with a simple assignment.
Assigning a C# int to a long variable needs no cast syntax; what is this kind of conversion called?
Implicit conversions always succeed, so the compiler performs them silently. Going the other way, from long to int, risks losing data and has to be requested with a cast.
When a C# double such as 9.99 is cast to int, what happens to the fractional part of the number?
It is discarded, because converting a double to an integer type rounds toward zero. So 9.99 becomes 9, and -9.99 becomes -9 rather than -10.
Which int method turns text into a number and returns false, rather than throwing, when the text is invalid?
TryParse is like the plain Parse method, except that it reports failure through its return value. The converted number comes back separately, through a second argument.
Which exception does int.Parse throw in C# when handed text such as hello that is not a valid number?
FormatException signals text in the wrong shape, such as letters where digits belong. A number too large to fit in an int triggers a different error, OverflowException.
In C#, the keyword int is simply an alias for which .NET structure in the System namespace?
Int32 is named for its size in bits, which caps it at 2,147,483,647. The keyword and the .NET name are interchangeable, so either one can declare a variable.
Which C# keyword makes integer arithmetic throw an OverflowException instead of silently wrapping round?
The checked keyword is needed because C# integer arithmetic is unchecked by default. Bits that do not fit are simply thrown away, so adding one to the maximum value wraps to the minimum.
Which method, which every C# type inherits from System.Object, turns a value such as 42 into text?
ToString is shared by every type because all of them, even plain integers, descend from System.Object. A class that does not supply its own version gets one that merely returns the type's full name.
What name is given to the C# operator written ??, which returns its right-hand value only when the left has none?
Null-coalescing saves writing an if statement: the expression gives its left side unless that is null, and then gives its right. The right-hand side is not even evaluated when the left has a value.
Which symbol, added after a C# value type such as int, lets a variable of that type also hold null?
A question mark turns int into int?, shorthand for Nullable<int>. The variable can then hold null as well as every ordinary int value, and null is where it starts by default.
Which C# modifier stops any other class from inheriting from the class it is applied to?
A class marked sealed cannot be inherited from. The modifier can also be applied to a single method, where it stops classes further down the chain from replacing that method.
A C# class may inherit from only one other class, but it can implement any number of what?
Interfaces are contracts listing the members a type promises to supply. They matter because C# has no multiple inheritance of classes, so they are how one type takes on several roles.
Which C# modifier marks a class that can never be instantiated directly and exists only to be inherited from?
An abstract class may leave some of its methods without a body, and every ordinary class that inherits from it must fill them in. Trying to create one directly is a compile error.
Which C# keyword must a child class write to replace an inherited method that was declared virtual?
An override method must have the same signature as the inherited one it replaces. The keyword cannot be used on a method that was never declared replaceable in the first place.
Which C# feature lets one class, such as List<T>, work with any element type while staying type-safe?
Generics let a programmer write one List<T> in place of separate classes for int, string and every other type. Unlike similar features elsewhere, they keep full type information at run time.
Which C# keyword marks a field that can be assigned only where it is declared or inside a constructor?
A readonly field differs from a const one, which must be settled when the program is compiled. Its value can be worked out while the program runs, such as the moment an object was created.
Which access level do the members of a C# class receive when no access modifier is written?
Members are private by default, so only code inside the same class can reach them. Marking one protected widens access to the classes that inherit from it.
Which C# keyword lets a child class call a constructor or method of the class it inherits from?
The base keyword reaches the parent's version of a method, even one the child has replaced. It also chooses which of the parent's constructors runs when an object is created.
What is the C# name for a class member that looks like a field but runs get and set accessors?
A property appears to outside code as public data, but reading or writing it actually calls a small method. For the simplest ones, the compiler writes those methods and a hidden backing field itself.
Which C# keyword defines a value type, such as a point with X and Y coordinates, in place of a class?
A struct variable holds its data directly, so passing one to a method copies the whole thing. Microsoft suggests the type for small, data-centric values with little or no behaviour.
Which C# feature, first released in 2007, lets programmers write SQL-like queries against arrays and databases?
LINQ stands for Language Integrated Query and is pronounced like the word link. The same query syntax works on XML documents, relational databases and ordinary in-memory lists.
Which C# keyword pauses a method until a task finishes, without blocking the thread it runs on?
The await operator suspends its method and hands control back to the caller, so a program stays responsive during slow work such as a download. A method that uses it is declared with the async modifier.
Which C# type holds a reference to a method, making it ideal for callbacks and event handlers?
A delegate fixes the parameter list and return type of the methods it can point to, which keeps each call type-safe. Event handlers in C# are simply methods invoked through one.
Which kind of compilation turns a C# program's intermediate code into machine code while the program is running?
Just-in-time compiling is the job of the .NET runtime, not of the C# compiler. The compiler itself produces only Intermediate Language, which no processor can run directly.
Which C# keyword marks a block of code in which pointers and direct memory access are allowed?
The unsafe keyword fences off code the runtime cannot verify, such as reading memory through a pointer. A project must also switch on a special compiler option before it will build.
Which C# keyword, added in version 9, creates a class whose objects are equal when their values match?
A record compares by value: two of them are equal when they have the same type and store the same values. Two ordinary class objects are equal only if they are the very same object in memory.
Which C# operator takes a class name written in the source code and returns the reflection object describing that class?
The typeof operator takes the name of a type, not a value. To discover the type of an object that already exists while the program runs, programmers call the GetType method.
A C# char holds one code unit of which Unicode encoding, also the basis of every .NET string?
UTF-16 code units make a char 16 bits wide, with values running from U+0000 to U+FFFF. A C# string is simply a sequence of these char values.
Which Geneva-based standards body, also home to the JavaScript specification, publishes the official C# standard?
Ecma International lists the C# specification as ECMA-334. The body began in 1961 as the European Computer Manufacturers Association, and its present name keeps the old initials.
Which codename is given to Microsoft's open-source C# compiler, notable for being written in C# itself?
Roslyn first surfaced in a 2010 blog post recruiting developers for the project, and Microsoft released its code to the public in April 2014. It also opens the compiler's analysis to other tools.
Think you know C# Programming?
Put these facts to the test with the interactive quiz.
Take the 50-question quizTeaching C# Programming?
Make a custom quiz — handy for classrooms and study groups.
Make a quiz on anything