Type System Flashcards
(22 cards)
What is a type?
Type defines a blueprint for a value.
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
How is data created?
Data is created by instantiating a type.
How is a predefined type instantiated?
Simply by using a literal, such as 12 or “Hello, world”
How is a custom type instantiated?
Using “new” operator.
MyType instanceOfType = new MyType();
What comprises value types?
Most built-in types (all numeric types, char and bool types) as well as custom struct and enum types.
What comprises the reference types?
Class, array, delegate and interface types. String type is a reference type as well.
What does a reference-type variable contain?
A reference-type variable contains a reference to an object that contains the value.
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)
Are all types from System namespace predefined in C#?
No. System namespace contains many types that are not predefined by C#. For example DateTime.
Is System.DateTime type predefined in C#?
No.
List 4 type categories in C#.
- Value types.
- Reference types.
- Generic type parameters.
- Pointer types.
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.
Are methods and events allowed in anonymous types?
No. Only properties are allowed to be defined in the anonymous type.
What is accessibility level of properties in an anonymous type?
Properties of an anonymous type are read-only.
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.
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.
What information is stored in the overhead of the reference type instance?
- The key to object’s type.
- Object’s lock state.
- Whether the object has been flagged by garbage collector.
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.
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.
Which real number type is typically used for financial calculations where base-10 accurate arithmetic is required?
decimal
How is hexadecimal value denoted in C#?
Hexadecimal value is denoted with “0x” prefix. For example: long x = 0x7F;