71 terms

C# Programming

Set of interview questions and general info about C# programming
What is C#
General purpose, type safe, object oriented, platform neutral programming language. Works best with the windows .Net framework and is the most up to date Visual Studio language
What does the 2.0 version of C# include and when was it released?
The 2.0 version of C# includes such new features as generics, partial classes, and iterators and was released in late 2005.
What is the .NET Framework?
a software framework developed by Microsoft that runs primarily on Microsoft Windows. It includes a large library and provides language interoperability (each language can use code written in other languages) across several programming languages. Programs written for the .NET Framework execute in a software environment (as contrasted to hardware environment), known as the Common Language Runtime (CLR), an application virtual machine that provides important services such as security, memory management, and exception handling. The class library and the CLR together constitute the .NET Framework.
What is the CLR?
Common Language Runtime: the virtual machine component of Microsoft's .NET framework and is responsible for managing the execution of .NET programs. In a process known as just-in-time (JIT) compilation, the CLR compiles the intermediate language code known as Common Intermediate Language (CIL) into the machine instructions that in turn are executed by the computer's CPU. The CLR provides additional services including memory management, type safety and exception handling. All programs written for the .NET framework, regardless of programming language, are executed by the CLR. It provides exception handling, garbage collection and thread management.
Does C# support multiple-inheritance?
No. C# limits classes to single-inheritance, meaning each classes inherits from a single parent class. This is done to prevent ambiguity. Interfaces are the way around this.
What is JIT (just in time compiling)?
a method to improve the runtime performance of computer programs. JIT compilers represent a hybrid approach, translation occurring continuously, as with interpreters, but with caching of translated code to minimize performance degradation. also offers other advantages over statically compiled code at development time, such as handling of late-bound data types and the ability to enforce security guarantees. JIT builds upon two earlier ideas in run-time environments: bytecode compilation and dynamic compilation. It converts code at runtime prior to executing it natively, for example bytecode into native machine code.
What is the goal of JIT?
to reach or surpass the performance of static compilation, while maintaining the advantages of bytecode interpretation: Much of the "heavy lifting" of parsing the original source code and performing basic optimization is often handled at compile time, prior to deployment: compilation from bytecode to machine code is much faster than compiling from source.
What is static compilation?
a compiled version of a program which has been statically linked against libraries; no dynamic linking occurs: all the bindings have been done at compile time.
What are the two advantages of dynamic compilation?
1) Often-used libraries (for example the standard system libraries) need to be stored in only one location, not duplicated in every single binary.
2) If a library is upgraded or replaced, all programs using it dynamically will immediately benefit from the corrections. Static builds would have to be re-linked first.
What are the 5 visibility levels (access modifiers)?
1) public: available to anyone anywhere
2) protected: can be accessed by the class itself and by any class derived from that class, an not any other class outside itself and derived class
3) internal: accessible only in the same assembly and invisible outside it.
4) protected internal: available to classes that are within the same assembly and derived from the specified base class.
5) private: only the class or struct declaring the member is allowed access to that member.
Are private class-level variables inherited?
Yes, but they are not accessible. Although they are not visible or accessible via the class interface, they are inherited.
What does the term immutable mean?
The data value MAY NOT be changed. Note: The variable value may be changed, but the original immutable data value was discarded and a new data value was created in memory. Think StringBuilder
What is the difference between System.String and System.Text.StringBuilder classes?
System.String is immutable. System.StringBuilder was designed with the purpose of having a mutable string where a variety of operations can be performed.
Whats the advantage of using System.Text.StringBuilder over System.String?
StringBuilder is more efficient in cases where there is a large amount of string manipulation. Strings are immutable so each time a string is changed, a new instance is created in memory.
What is the difference between a shallow copy and a deep copy?
A shallow copy means that the contents (each array item) contains references to the same object as the elements in the original array. A deep copy would create a new instance of each element's object, resulting in a different, yet identical object.
What is the .NET collection class that allows an element to be accessed using a unique key?
What is the C# syntax to catch any possible exception?
a catch block that catches the exception of type System.Exception. You can also omit the parameter data type in this case and just write catch{}.
Explain how a finally block works.
A try/catch block can be optionally followed by a finally block that will be executed whether or not the code in the try or catch blocks executes successfully.
Can multiple catch blocks be executed for a single try statement?
No. Once the proper catch block is executed, control is passed to the finally block (if there is any).
What does the keyword 'sealed' mean?
will prevent class from being inherited.
Can you allow a class to be inherited, but prevent a method from being overridden?
Yes. Just leave the class public and make the method sealed.
What is an abstract class?
class that cannot be instantiated; a class that must be inherited and have the methods overridden; basically a blueprint for a class without ANY implementation within methods or properties. Abstract classes can have methods which have functionality
When do you absolutely have to declare a class as abstract?
1) When the class itself is inherited from an abstract class
2) When at least one of the methods in the class is abstract.
What is an interface?
defines a set of properties, methods, and events; does not provide implementation. They are implemented by classes or other interfaces, and defined as separate entities from classes; represents a contract between an object and a user of the object; cannot be directly instantiated as an object. No data members can be defined in an interface. Useful way to group certain classes together based on functionality.
What is the difference between an interface and an abstract class?
In an interface, all methods are abstract; there is no implementation at all. In an abstract class, some methods can be concrete. In an interface, no accessibility modifiers are allowed, everything is public. An abstract class may have accessibility modifiers.
Can you specify the accessibility modifier for methods inside of an interface?
No. All methods must be public.
Can you inherit multiple interfaces?
Yes, .NET supports multiple interface inheritance.
What is the difference between a struct and a class?
Structs are value-type variables and are thus saved on the stack, additional overhead, but faster retrieval. Also structs cannot inherit.
What is the implicit name of the parameter that gets passed into the set method/property of a class?
The data type of the "value" parameter is defined by whatever data type the property is declared as.
What does the keyword virtual mean?
The method or property can be overridden and most importantly, the run-time type of the object determines which method is to be invoked.
Can you declare an override method to be static if the original method is not static?
No. The signature of the virtual method must remain the same. Only the keyword virtual is replaced by the keyword override.
What are the different ways a method can be overloaded?
A method can be overloaded with different parameter types, a different number of parameters, and different order of parameters.
If a base class has a number of overloaded constructors, and an inheriting class has a number of overloaded constructors; can you enforce a call from an inherited constructor to a specific base constructor?
Yes, just place a colon, and then keyword base (parameter list to invoke the appropriate constructor) in the overloaded constructor definition inside the inherited class. For example, public MyDerivedClass (int y) : base (y+1) where the parent class has a constructor that takes an int as a parameter.
What is a delegate?
a form of type-safe function pointer used by the .NET Framework.; provides level of indirection; single method interface; delegate instance is an object implementing that interface; specify a method to call and optionally an object to call the method on. They are used, among other things, to implement callbacks and event listeners; forms the basis of event handling.
What is a multicast delegate?
derives from System.Delegate; its a delegate that points to several methods. Multicast delegation is a mechanism that provides functionality to execute more than one method. There is a list of delegates maintained internally, and when the multicast delegate is invoked, the list of delegates is executed.
What are the four things you need in order to use a delegate?
1) delegate type needs to be declared
2) there must be a method containing the code to execute
3) delegate instance must be created
4) delegate instance must be invoked
Will a delegate instance prevent its target from being garbage collected?
yes; if the delegate instance itself cant be collected; leads to memory leaks when short-lived object subscribes to an event in a long-lived object using itself as a target; the long-lived object indirectly holds a reference to the short-lived one prolonging its lifetime.
Are delegates immutable?
yes; once you've created a delegate instance, nothing about it can be changed; makes it safe to pass around references to delegate instances and combine them with others without worrying about consistency, thread safety or anyone trying to change their actions
What does the Dispose() method do with the connection object?
Closes the connection and deletes the object from memory.
How is the DLL Hell problem solved in .NET?
Assembly versioning allows the application to specify not only the library it needs to run (which was available under Win32) but also the version of the assembly.
What is a satellite assembly?
When you write a multilingual or multicultural application in .NET, and want to distribute the core application separately from the localized modules, the localized assemblies that modify the core application are called satellite assemblies.
What namespaces are necessary to create a localized application?
System.Globalization and System.Resources.
What is the smallest unit of execution in .NET?
an assembly.
When should you call the garbage collector in .NET?
As a good rule, you should NOT call the garbage collector. However, you could call the garbage collector when you are done using a large object (or sets of objects) to force the garbage collector to dispose of those very large objects from memory. This is usually not good practice tho
How do you convert a value-type to a reference-type?
What happens in memory when you box and unbox a value-type?
Boxing converts a value-type to a reference-type, thus storing the object on the heap. Unboxing converts a reference-type to a value-type, thus storing the value on the stack.
What is the heap?
Block of memory where objects live. Whenever a new object is created it is allowed onto the heap. Old objects are regularly deallocate from the heap to save available memory
What is the stack?
Block of memory for storing local variables and parameters. Automatically grows and shrinks as function is entered and exited.
What is a namespace?
Groups classes together so that they have a unique identifier
What is encapsulation?
process of providing access to an object only through it's messages while keeping the details PRIVATE; controls the use of a class.
What is a primitive data type?
basic type is a data type provided by a programming language as a basic building block; most common are: Int, Long, Float, Double, Decimal, String, Char and Bool.
What is reflection?
provides objects (of type Type) that describe assemblies, modules and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties. If you are using attributes in your code, reflection enables you to access them.
When is reflection useful?
1) When you have to access attributes in your program's metadata.
2) For examining and instantiating types in an assembly
3) For building new types at runtime.
4) For performing late binding, accessing methods on types created at run time
What is a 'using' statement?
defines a scope at the end of which an object will be disposed. The object you instantiate must implement the System.IDisposable interface.
What are class properties?
class members that expose functionality of methods using the syntax of fields. They simplify the syntax of calling traditional get and set methods (a.k.a. accessor methods). Like methods, they can be static or instance.
What are partial classes?
class definitions can be split up across multiple physical files. To the compiler, this does not make a difference as all the fragments of the partial class are grouped and the compiler treats it as a single class.
What are generics?
they are classes, structures, interfaces, and methods that have placeholders (type parameters) for one or more of the types that they store or use. they let you tailor a method, class, structure, or interface to the precise data type it acts upon. most frequently used with collections and the methods. can also create custom generic types and methods to provide your own generalized solutions and design patterns.
What are predicates?
delegate function that returns true or false; convenient way of basically testing if something is true
What is func?
same as a System.Predicate<T> delegate; but takes one parameter (still returns true or false).
What are lambdas?
Lambda expressions provide a concise way to write first-class anonymous function values.
ex: listOfFoo.Where(x => x.Size && > 10);
lambda expressions are merely short-hand syntax for anonymous delegates with type inference for parameters and return type
What are extension methods?
Extension methods are a form of syntactic sugar providing the illusion of adding new methods to the existing class outside its definition. In practice, an extension method is a static method that is callable as if it were an instance method; the receiver of the call is bound to the first parameter of the method, decorated with keyword this. Extension methods allow developers to add new methods to the public contract of an existing CLR type, without having to sub-class it or recompile the original type.
What are the three things necessary for creating extension methods?
1) need to be defined in a static class
2) need to be defined as a static method
3) need to take at least one parameter defined as follows:
this (keyword) string (type) parameterName
What is the significance of Application.Idle?
it's how message processing is done on the application's UI thread; has become a convenient place to take care of application housekeeping chores asynchronously within the primary operations of the app without getting involved with multiple threads; menus and buttons are typically enabled or disabled to match their corresponding command states when the application goes idle
What is a constraint?
When you define a generic class, you can apply restrictions to the kinds of types that client code can use for type arguments when it instantiates your class. If client code tries to instantiate your class by using a type that is not allowed by a constraint, the result is a compile-time error. Constraints are specified by using the where contextual keyword.
What is the significance of the 'where' keyword?
public abstract class ObjectMapperBase<T> where T : new()
internal abstract bool UpdateObject(T plainObjectOrginal, T plainObjectNew, WebMethod fwm, IDbTransaction transaction);

The new() constraint tells the compiler that all objects
of type T must have a public parameterless constructor
What are the different permutations of how to use the 'where' keyword?
1) where T: struct
2) where T : class
3) where T : new()
4) where T : <base class name>
5) where T : <interface name>
6) where T : U
What is lazy initialization
tactic of delaying the creation of an object, the calculation of a value, or some other expensive process until the first time it is needed.
What does the checked keyword mean?
used to properly check to see if an overflow exception has occurred
What is a destructor?
function (much like constructor) which is called when the object is explicitly deleted by code, or when the object passes out of scope, which can happen when the program exits a function; has the same name as the class, but with a tilde prefix.
What are attributes?
an object that represents data you want to associate with an element in your program; the element to which you attach an attribute is referred to as the target; custom attributes can be a powerful tool when combined with reflection; convention is to append the word Attribute to your attribute name; compiler supports calling the attribute with the shorter version of the name (without the 'Attribute')
What are the four things reflection is used for?
1) viewing metadata
2) performing type discovery
3) late binding to methods and properties
4) creating types at runtime