Type System Flashcards Preview

C# > Type System > Flashcards

Flashcards in Type System Deck (22):
1

What is a type?

Type defines a blueprint for a value.

2

What is a predefined type?
What are some predefined types?

Predefined is a type that is specially supported by the compiler. In C#, predefined types are recognized with a C# keyword.
Examples of predefined types are: int, string, bool
https://stackoverflow.com/questions/13464641/c-sharp-predefined-types-vs-primitive-types/46866599#46866599

3

How is data created?

Data is created by instantiating a type.

4

How is a predefined type instantiated?

Simply by using a literal, such as 12 or "Hello, world"

5

How is a custom type instantiated?

Using "new" operator.

MyType instanceOfType = new MyType();

6

What comprises value types?

Most built-in types (all numeric types, char and bool types) as well as custom struct and enum types.

7

What comprises the reference types?

Class, array, delegate and interface types. String type is a reference type as well.

8

What does a reference-type variable contain?

A reference-type variable contains a reference to an object that contains the value.

9

What is the difference between reference and value types in regards to them containing null value?

Reference types can be assigned null value which means that they point to no object. Value types cannot ordinarily have a null value. (Exception: nullable value types)

10

Are all types from System namespace predefined in C#?

No. System namespace contains many types that are not predefined by C#. For example DateTime.

11

Is System.DateTime type predefined in C#?

No.

12

List 4 type categories in C#.

1. Value types.
2. Reference types.
3. Generic type parameters.
4. Pointer types.

13

How is anonymous type created?

To create an anonymous type use "new" operator with the object initializer.

var v = new { Amount = 108, Message = "Hello" };

In this case "Amount" and "Message" are user defined object properties and their type will be derived by the compiler.

14

Are methods and events allowed in anonymous types?

No. Only properties are allowed to be defined in the anonymous type.

15

What is accessibility level of properties in an anonymous type?

Properties of an anonymous type are read-only.

16

Can a static class contain instance variables? Here is an example:

static class MyClass
{
int myIntVar = 3;
}

No.
All data and function members of a static class must be static.

17

How many bytes will the following Struct occupy in memory?

struct MyStruct{
int x;
int y;
}

8 bytes. 4 bytes for each integer and no overhead. Value type instances occupy precisely the memory required for their fields.

18

What information is stored in the overhead of the reference type instance?

1. The key to object's type.
2. Object's lock state.
3. Whether the object has been flagged by garbage collector.

19

What type does a compiler infer a variable to be if it is a number with a decimal point?

The number with a decimal point is always inferred to be a double type by a compiler.

20

What is the largest number that int can hold in C#?

2^31 - 1. int has 4 bytes to store its value but 1 bit is reserved for a sign value of a number.

21

Which real number type is typically used for financial calculations where base-10 accurate arithmetic is required?

decimal

22

How is hexadecimal value denoted in C#?

Hexadecimal value is denoted with “0x” prefix. For example: long x = 0x7F;