Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!
63. Can a value-type variable ever hold a value different than its compile-time type?
Quote:
What about a reference type? No, value type-variables can never hold values of a type different than their compile-time types. For reference types it is possible.
How does a reference type hold a different value after compilation?
I rate users. Its just not having much impact as my rating is low...
51.In the following example, which Method does MyMethod call in FurtherDerived?
Quote: It calls Base.MyMethod() since Derived.MyMethod() is declared with the new keyword which means that the hiding of the method from the base class only extends to the scope of the method from the derived class. Since the method is private, the scope doesn’t extend to MyNewMethod.
Please explain.
I rate users. Its just not having much impact as my rating is low...
Greetings everyone. Please look over the following answers and make sure it matches with yours. If it doesn't and you're confused about why, please post your questions here.
What do you call an assembly with an entry point?
It's called an application.
Is it possible to run multiple instantiations of an application on the same machine at the same time? Do they share or have a separate application domain? Yes, it's possible. Each has their own application domain.
When does application setup occur? When the execution environment calls a designated method, which is referred to as the application's entry point.
The entry point is always named what? Main
What are the possible signatures of the main entry point method? static void Main() static void Main(string[] args) static int Main() static int Main(string[] args)
True or false, the optional parameter in the entry point function may have any name, but must be of type string[]? True.
What is contained in the optional parameter, if one is requested? The command-line arguments that were specified when the application was started.
Is it possible for a single class within an application to have more than one method which qualifies as an entry-point method? No.
Is it possible for more than one class within an application to have a method which qualifies as an entry-point method, what would happen in such cases? Yes. In such cases an external mechanism such as command-line compiler option must be used to select one of the entry-point methods to use.
True or False, in C# every method must be defined as a member of a class or struct? True.
What is the purpose of an application's termination status code? The purpose of this code is to allow communication of success or failure to the execution environment.
If the application's entry-point method is defined to have a return type of "void", what's the application's termination status code always going to be? 0
What are type declarations used for in C#? They are used to define classes, structs, interfaces, enums, and delegates
What kinds of members are permitted in class declarations? Constants, Fields, methods, properties, events, indexers, operators, instance constructors, static constructors, destructors, and nested types.
Is it ever possible for a class to contain different kinds of members (ie. field, event, method, etc...) with the same name? No. Although it's possible to have more than one method (overloading) with the same name, you cannot have members of different types with the same name.
What are the 5 types of declaration spaces, how are they differentiated? 1. Global declaration space - any type or namespace not within a namespace is in the global declaration space 2. All types or namespaces declared within a namespace is within that namespace's declaration space 3. Each class, struct, or interface declaration creates a new declaration space 4. Each enumeration declaration creates a new declaration space 5. Within a block, switch-block, for, foreach, and using statement a new declaration space is created.
What is true of two namespace declarations with the same fully qualified name, with regards to declaration space? They contribute to the same declaration space.
When a class is derived from a base class, what is inherited? Everything except constructors and destructors, regardless of member accessibility.
In specific, are private members inherited? Yes. Private members are inherited but their accessibility prevents derived classes from accessing the members internally.
Is it possible to give namespaces access restrictions? (ie. private, protected, public namespaces) No. Namespaces are always publicly accessible.
True or false, all of the simple types such as int, char, ulong, bool, etc...are actually aliases for similarly named structs? True.
What constitutes the members of an interface? All members declared in the interface, as well as any inherited from base interfaces.
What are the members of an Array? The members inherited from class System.Array
What are the members of a delegate? The members in inherited from class System.Delegate
What are the possible declared accessibilities of a member, what do they mean? public - access is not limited protected - access is limited to the containing class or types derived from the containing class internal - access limited to this program protected internal - access limited to types in this program which are either the containing class, or derived from it Private - Access limited to the containing type
Do types declared within a namespace default to public or internal accessibility? internal
What accessibility do class members default to? private
What accessibility do struct members default to? private
What accessibility do interface members default to? Public
What accessibility do enum members default to? Public
What is a member's accessibility domain? A member's accessibility domain consists of the possibly disjoint sections of program text in which access to the member is permitted.
What 4 member types are characterized by their signatures? Methods, Instance constructors, indexers, and operators
What is contained in a method's signature? The name of the method, and the type and kind of each of its formal parameters, in order from left to right.
Does a method's signature include it's return type? No.
Signatures are the enabling mechanism for what feature in methods, instance constructors, indexers, and operators? Overloading
Are 'ref' and 'out' considered part of a member's signature? Yes.
Does "void MyFunc(ref int myInt)" have a different signature than "void MyFunc(out int myInt)"? Why or why not? No. Although out and ref are part of a signature, a method's signature cannot differ by just exchanging out and ref keywords, because the CLI treats them as the same.
What is the definition of an identifier's scope? The region of program text within which it is possible to refer to an entity declared by that name without qualification of the name.
Can scopes be nested? Yes.
What happens when an inner scope re-declares an identifier with the same name as an outer scope identifier. The outer scope identifier is said to be hidden, and must be fully qualified.
What is the scope of a parameter declared in a method declaration? The method's body
Within the scope of a class or struct is it possible to refer to a member in a textual position before the member's declaration? Yes.
Within the scope of a local variable, is it possible to refer to the variable in a textual position before the variable's declaration? No.
When does name hiding occur? When scopes overlap through nesting and when scopes overlap through inheritance.
When can name hiding occur as a result of nesting? When a namespace is nested within a namespace, when a type is nested within a namespace, or as a result of a local variable or parameter declaration in a method.
When a name in an inner scope hides a name in an outer scope, what happens if that outer scope identifier is overloaded? The inner scope name hides ALL overloaded identifiers.
Does name hiding as a result of nesting cause a warning or error? If so, how can this be dealt with. No. No need to deal with it.
When can name hiding occur as a result of inheritance? When classes or structs redeclare names that were inherited from base classes.
Is it possible for a derived class to declare an overloaded operator with the same signature as an operator in a base class? What is this mean for name hiding? No. It is not possible to hide the name of an operator.
Does name hiding as a result of inheritance cause a warning or error? Yes. It causes a warning. It can be dealt with by using the "new" keyword to indicate an intentional name-hiding.
In the following example, which Method does MyMethod call in FurtherDerived?
// Base class, provides a method "MyMethod" with an empty implementationclass Base{ public void MyMethod() {}}// Derived class, which inherits the base class's MyMethod, but then hides it by creating a new oneclass Derived : Base{ new private void MyMethod() {}}// A class deriving from Derived, and thus being further derived. It does not implement its// own 'MyMethod' so which MyMethod does it call Base's or Derived'sClass FurtherDerived : Derived{ public void MyNewMethod() { MyMethod(); } }
Base.MyMethod
What is a fully qualified name? What does it contain? A name which uniquely identifies the namespace or type amongst all others. It contains the complete hierarchical path of the identifier starting from the global namespace.
What construct implements the automatic memory management policies in C#? The Garbage Collector
Does C# require that objects be destructed as soon as an object becomes available for destruction? No.
Does C# require that memory be reclaimed as soon as an object has been destructed? No.
Does C# require that destructors be run in any order, or in any particular thread? No.
To avoid confusion and unexpected behavior it is generally a good idea for destructors to only perform cleanup on what? What should they not do? Data stored in their object's own fields. They should not perform any actions on referenced objects or static fields.
What are the two main categories of types in C#, how do they differ? Reference types and value types. Value types directly contain their data, while reference types store references to their data.
What is the third type, and when is it available? Pointers. Only in unsafe code.
Is it possible for operations on two value-type variables to effect the same memory? How about with reference types? Not with value types. Yes with reference types.
What is the fully qualified name of the ultimate base class of all types in C#. System.Object
Is it possible for a value-type variable to have a value of null? What about a reference variable? No. Value-type variables cannot be null. Reference-type variables can be null.
Can a value-type variable ever hold a value different than its compile-time type? What about a reference type? Value-type: no. Reference-type: Yes. It can contain values of a more derived type.
What is different about assignment to a value-type variable vs. assignment to a reference-type variable? With value-type assignment a copy of the data is made. With reference-type assignment a copy of the reference to the data is made.
What is the base class of all value types? System.ValueType
Is it possible to declare a parameterless constructor for value types? Why or why not? No. Because the compiler automatically creates an implicit parameterless constructor for all value-types.
What types of members can be declared within a struct? constants, fields, methods, properties, indexers, operators, instance constructors, static constructors, and nested types.
True for false, all simple types including int, float, char, long, and double have members? True.
True for false, when all of the operands of an expression are simple type constants, it is possible for the compiler to evaluate the expression at compile-time? True.
Is it possible to assign the 'const' keyword to user-defined structs? If not, how can this be resolved? No. Use 'static readonly' instead.
What are the 9 integral types provided by C#? sbyte, byte, short, ushort, int, uint, long, ulong, and char
What are the checked and unchecked operators used for? They are used to control overflow checking.
What happens if there's an overflow in a "checked" context? A compile-time error is created, or a System.OverflowException is thrown at run-time, depending on whether the value can be computed at compile-time.
What does NaN stand for and what does it mean? It stands for Not a Number, and it means that a variable holds an invalid floating-point value.
True or false, decimal values have greater precision but smaller range than floating point values? What does this imply about converting back and forth from decimal to float? True. It means conversion from decimal to float can result in a loss of precision, and conversion from float to decimal can cause an overflow exception.
Is there an implicit conversion from Decimal to float or double? No.
What are the possible values of a Boolean variable? true and false
What standard conversions exist between bool and other types? None.
What are the possible underlying types of an enum? byte, sbyte, short, ushort, int, uint, long, or ulong
Are variables of an enumeration type restricted to the values of the named constants? No. The values of an enumeration variable can be any acceptable value for the underlying type.
What are the available reference types? Class, interface, array, and delegate
What's another name for an instance of a reference type? An object
Which reference types can the value 'null' be used with? All reference types.
What is a delegate? A delegate is a structure that refers to one or more methods, and for instance methods, also refers to their corresponding object instance.
Why is boxing important in C#? It provides a bridge between value-types and reference-type by permitting any value of a value-type to be converted to and from type object.
Is the boxing conversion from a value-type to an object implicit or explicit? It's implicit.
True or False. A boxing conversion implies making a copy of the value being boxed? True.
Is the unboxing conversion from a reference-type to a value-type implicit or explicit? It's explicit
In order for an unboxing conversion to a given value-type to succeed at run-time, what must be true? The value of the source operand must be a reference to an object that was previously created by boxing a value of that value-type
What do variables represent? Memory storage locations
What does it mean to say C# is a type-safe language? The C# compiler guarantees that values stored in variables are always of the appropriate type
If a variable is considered initially assigned can it be used without assigning it a value? Yes.
What are the 7 categories of variables in C#? Static variables, instance variables, array elements, value parameters, reference parameters, output parameters, and local variables.
When does a static variable come into existence? Are they considered initially assigned or unassigned? Before the execution the static constructor. They are considered initially assigned
When does an instance variable come into existence? Are they initially assigned, or unassigned? When a new instance of a class is created, before the constructor is called. They are initially assigned.
When does a value parameter come into existence? Is it initially assigned, or unassigned? It comes into existence upon invocation of the function. Value parameters are initially assigned
Are reference parameters initially assigned or unassigned? Assigned.
Are output parameters initially assigned or unassigned? unassigned.
Are local variables initially assigned or unassigned? unassigned.
Are local variables created in foreach statements or catch clauses initially assigned or unassigned? Assigned
Which categories of variables are automatically initialized to their default values? Static Variables, Instance variables, and array elements.
What does it mean for a variable to be definitely assigned? It means the that at a given location in code, the compiler can prove, by a static-flow analysis, that the variable has been automatically initialized or has been the target of at least one assignment.
Reads and writes of which types are atomic? bool, char, byte, sbyte, short, ushort, uint, int, float, valid enums of the previous types, and all reference types.
Which types are not guaranteed to be atomic? long, ulong, double, decimal, as well as all user-defined types.
What does a conversion enable? It enables an expression of one type to be treated as another type?
What are the two types of conversions, and what do they mean. Implicit and Explicit. Implicit means the programmer needs no formal declaration of intent to treat one type as another - The compiler recognizes the intention by the current context. Explicit means the programmer must state at compile-time, their intent to treat one type as another.
True for false, the pre-defined implicit conversions always succeed and never cause exceptions to be thrown? True.
Which types implicitly convert to the 'char' type? None.
What is required for an implicit reference conversion? The compiler must be able to determine at compile-time that a conversion between two reference types will always succeed.
A conversion from long to ulong is an implicit conversion, provided the value of the variable being converted is not....what? Negative.
Is it legal to explicitly cast a type to a type which there is an implicit conversion for? Why or why not? Yes. Because the set of explicit conversions includes all implicit conversions.
When converting from an enum to another type, how does the compiler treat the enum? The compiler treats the enum as being that of the underlying type, not as a user-defined type.