1. The compiler embeds the type information into the executable file as metadata. The common language runtime (CLR) uses that metadata at run time to further guarantee type safety when it allocates and reclaims memory.
2. If you want compiler to infer type which declaring keyword in used
a. Var
3. Two fundamental points about the type system in the .NET Framework
a. It supports the principle of inheritance. Types can derive from other types, called base types. The derived type inherits (with some restrictions) the methods, properties, and other members of the base type. The base type can in turn derive from some other type, in which case the derived type inherits the members of both base types in its inheritance hierarchy. This unified type hierarchy is called the Common Type System (CTS).
b. Each type in the CTS is defined as either a value type or a reference type. This includes all custom types in the .NET Framework class library and also your own user-defined types.
4. What are structs and classes – value type or reference type
a. Types that you define by using the struct keyword are value types; all the built-in numeric types are structs. Types that you define by using the class keyword are reference types.
5. What are Value Types:
a. Value types derive from System.ValueType, which derives from System.Object.
b. Types that derive from System.ValueType have special behavior in the CLR. Value type variables directly contain their values in stack, which means that the memory is allocated inline in whatever context the variable is declared.
c. There is no separate heap allocation or garbage collection overhead for value-type variables.
6. What are the two main categories for Value Type
a. Structs and Enums
7. What’s the base type of built in Numeric Type?
a. The built-in numeric types are structs, and they have properties and methods that you can access
8. Can you derive from System.Int32
a. Value types are sealed, which means, for example, that you cannot derive a type from System.Int32, and you cannot define a struct to inherit from any user-defined class or struct because a struct can only inherit from System.ValueType. However, a struct can implement one or more interfaces. You can cast a struct type to an interface type; this causes a boxing operation to wrap the struct inside a reference type object on the managed heap.
9. What are reference Types?
a. A type that is defined as a class, delegate, array, or interface is a reference type.
b. At run time, when you declare a variable of a reference type, the variable contains the value null until you explicitly create an instance of the object by using the new operator, or assign it an object that has been created elsewhere by using new.
10. What happens internal to memory when an object is created?
a. First the reference for the object is created in the stack.
b. When object is created with the new keyword memory is allocated in the managed heap.
c. Types on the managed heap require overhead both when they are allocated and when they are reclaimed by the automatic memory management functionality of the CLR, which is known as garbage collection. However, garbage collection is also highly optimized, and in most scenarios it does not create a performance issue.
11. Are array int[] i={1,2,3}, value type or reference type?
a. All Arrays are by default reference type, derived from System.Array.
12. How to specify a type to a literal
a. All Literals receive type from compiler. You can explicitly specify the type by suffixing it by “f” or “F”. e.g 4.56f
13. How to start an Enum from 1
a. enum Days {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri};
14. What can be underlying type of Enum.
a. Every enumeration type has an underlying type, which can be any integral type except char. The default underlying type of enumeration elements is int. To declare an enum of another integral type, such as byte, use a colon after the identifier followed by the type
b. enum Days : byte {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri};
15. Struts fall into which categories:
a. Numeric Types
b. Bool
c. User Defined Struts.
16. What are nullable Types in C#?
a. Nullable types are instances of the System.Nullable<T> struct. A nullable type can represent the correct range of values for its underlying value type, plus an additional null value.
b. The syntax T? is shorthand for Nullable<T>, where T is a value type. The two forms are interchangeable. E.g int? x = null
c. Use the ?? operator to assign a default value that will be applied when a nullable type whose current value is null is assigned to a non-nullable type, for example int? x = null; int y = x ?? -1;
17. What is Boxing and Unboxing?
a. Boxing is the process of converting a value type to the type object or to any interface type implemented by this value type. When the CLR boxes a value type, it wraps the value inside a System.Object and stores it on the managed heap. Unboxing extracts the value type from the object. Boxing is implicit; unboxing is explicit. The concept of boxing and unboxing underlies the C# unified view of the type system, in which a value of any type can be treated as an object.
18. Explain Boxing Conversion?
a.
b. Boxing a value type allocates an object instance on the heap and copies the value into the new object.
19. What in Unboxing?
a. Checking the object instance to make sure that it is a boxed value of the given value type.
b. Copying the value from the instance into the value-type variable.
c.
20. What is the new type introduced in VS 2010.
a. dynamic. The type is a static type, but an object of type dynamic bypasses static type checking. In most cases, it functions like it has type object.
21. Comments about Var:
a. var can only be used when a local variable is declared and initialized in the same statement; the variable cannot be initialized to null, or to a method group or an anonymous function.
b. var cannot be used on fields at class scope.
c. Variables declared by using var cannot be used in the initialization expression. In other words, this expression is legal: int i = (i = 20); but this expression produces a compile-time error: var i = (i = 20);
d. Multiple implicitly-typed variables cannot be initialized in the same statement.
e. If a type named var is in scope, then the var keyword will resolve to that type name and will not be treated as part of an implicitly typed local variable declaration.
1 comment:
tank u sir
Dot Net Training
institutes in Chennai
Post a Comment