C# and some OOP Flashcards
BaseClass Vs Abstract Vs Interface vs Virtual
Base class - You can inherit from a baseclass and get all its methods and properties.
Abstract class - All the methods are abstract. The inheriting class needs to implement them.
Virtual Methods - Base class can have its own implementation that may be overriden by the inheriting class. So your base class can have methods that are avalailable and its own implementation and the inheriting class can use the regular methods in the base class but needs to override the virtual methods.
Use an abstract class if you have some functionality that you want it’s subclasses to have. For instance, if you have a set of functions that you want all of the base abstract class’s subclasses to have.
You can’t have a non abstract class with abstract methods.
What is static
and why use it?
Static means there is only one copy of a static member.
it does not need to be instantiated. So if you have a class with static variable you can use it like
MyClass.StaticVariableExample
If the static keyword is applied to a class, all the members of the class must be static.
Static variables are used to keep track of information that relates logically to an entire class, as opposed to information that varies from instance to instance.
Static functions are helpful as they do not rely on an instantiated member of whatever class they are attached to. So helper funtion type stuff.
Purists might argue that static is an abomination. Pragmatists might argue that it fills a gaping hole in the “everything is an object” abstraction, allowing you to call utility methods for which it doesn’t make sense to instantiate a new object just to call them. The canonical example of this is the System.Math class. (This is a static class)
In short what is a singleton and why is it used?
A singleton is a class which only allows one instance of itself to be created - and gives simple, easy access to said instance. The singleton premise is a pattern across software development.
It’s useful in many scenarios such as creating dialog boxes, handling registry settings, managing thread pools, logging, caching, configuration settings, and managing database connections
https://csharpindepth.com/Articles/Singleton
What are the differences between Singleton and Static implementations.
The big difference between a singleton and a bunch of static methods is that singletons can implement interfaces (or derive from useful base classes, although that’s less common, in my experience), so you can pass around the singleton as if it were “just another” implementation.
Because Singletons can be instantiated, and static not:
- Singletons can implement interfaces
- Singletons can be passed as parameters
- Singletons can have their instances swapped out (such as for testing purposes)
- Singletons can be handled polymorphically, so there may exist multiple implementations
**.net 7 does support static interfaces **
What is the protected
access modifier and why use it?
Access is limited to the containing class or types derived from the containing class.
Struct members cannot be protected because the struct cannot be inherited.
Meaning, you cannot access the protected member by creating an instance of the base class and accessing it. You will get an error.
While elements declared as private can be accessed only by the class in which they’re declared, the protected keyword allows access from sub-classes and members of the same package.
By using the protected keyword, we make decisions about which methods and fields should be considered internals of a package or class hierarchy, and which are exposed to outside code.
What, why and how of singleton.
What?
1. A singleton is a class which only allows a single instance of itself to be created
2. Singletons don’t allow any parameters to be specified when creating the instance - otherwise a second request for an instance but with a different parameter could be problematic!
Why?
1. Common use-cases for the singleton design pattern include factories, builders, and objects that hold program state.
2. Its commonly used for something like a logger class.
3. It’s used for configuration, management, caches, or other things that you’re not going to have multiple of.
Why is it considered bad?
https://stackoverflow.com/questions/137975/what-are-drawbacks-or-disadvantages-of-singleton-pattern/138012#138012
http://misko.hevery.com/2008/08/17/singletons-are-pathological-liars/
How?
1. A single constructor, which is private and parameterless. This prevents other classes from instantiating it (which would be a violation of the pattern). Note that it also prevents subclassing - if a singleton can be subclassed once, it can be subclassed twice, and if each of those subclasses can create an instance, the pattern is violated. The factory pattern can be used if you need a single instance of a base type, but the exact type isn’t known until runtime.
2. The class is sealed. This is unnecessary, strictly speaking, due to the above point, but may help the JIT to optimise things more.
3. A static variable which holds a reference to the single created instance, if any.
4. A public static means of getting the reference to the single created instance, creating one if necessary.
Fully lazy instantiation
Here, instantiation is triggered by the first reference to the static member of the nested class, which only occurs in Instance. This means the implementation is fully lazy, but has all the performance benefits of the previous ones. Note that although nested classes have access to the enclosing class’s private members, the reverse is not true, hence the need for instance to be internal here. That doesn’t raise any other problems, though, as the class itself is private. The code is a bit more complicated in order to make the instantiation lazy, however.
public sealed class Singleton { private Singleton() { } public static Singleton Instance { get { return Nested.instance; } } private class Nested { // Explicit static constructor to tell C# compiler // not to mark type as beforefieldinit static Nested() { } internal static readonly Singleton instance = new Singleton(); } }
Lazy<T>
If you’re using .NET 4 (or higher), you can use the System.Lazy<T>
type to make the laziness really simple. All you need to do is pass a delegate to the constructor which calls the Singleton constructor - which is done most easily with a lambda expression.
public sealed class Singleton { private static readonly Lazy<Singleton> lazy = new Lazy<Singleton>(() => new Singleton()); public static Singleton Instance { get { return lazy.Value; } } private Singleton() { } }
In Java
~~~
// Java code to explain double check locking
public class GFG
{
// private instance, so that it can be
// accessed by only by getInstance() method
private static GFG instance;
private GFG()
{
// private constructor
}
public static GFG getInstance()
{
if (instance == null)
{
//synchronized block to remove overhead
synchronized (GFG.class)
{
if(instance==null)
{
// if instance is null, initialize
instance = new GFG();
}
} } return instance; } } ~~~
What are global variables and how do they relate to a Singleton?
- In computer programming, a global variable is a variable with global scope, meaning that it is visible (hence accessible) throughout the program
- In C# you cannot define true global variables (in the sense that they don’t belong to any class). This being said, the simplest approach that I know to mimic this feature consists in using a static class.
- In general having public static variables is bad practice - it is a shared global resource and if you change it you need to synchronize access to it. Having global state is something you want to avoid as much as possible.
What is a sealed
class?
Sealed
classes are used to restrict the users from inheriting the class
What is your experience in terms of dependecy in jection vs singleton?
What is the lifecycle of a .net api and its objects?
What is the protected internal access modifier
?
The protected internal keyword combination is a member access modifier. A protected internal member is accessible from the current assembly or from types that are derived from the containing class.
What is an assemply in C# and how does it differ from a namespace?
Namespaces affect name resolution only. Namespaces do not imply any sort of storage, nor do namespaces determine which DLLs contain your code. Namespaces allow you to group related things together under a logical name even though they may physically reside in different DLLs.
An assembly is basically just a DLL or EXE file. It contains IL code and type information that describes the code in that DLL or EXE. It can contain a lot of other stuff too, but for starters just think of it as a DLL.
You put your code into a particular assembly by compiling your code into a project (csproj) that produces the DLL or EXE.
C# is a compiled language. This means that when a developer creates C# source files, those files need to be compiled before they can run. You can not write a C# file, and then run that file or combination of files like you can with say a JavaScript or PHP file. This is where assemblies come into play for a compiled language. Assembly files are the result of the C# compiler reading in human readable C# source files, and then producing a file the application can use. In fact a C# program will often consist of many assembly files working together.
Say whether each of these access modifiers are accessbile in the same or other assemply by declard, other or derived classes.
1. Private
2. Public
3. Protected
4. Internal
5. Protected Internal
6. Private Protected
What is mscorelib.dll
These types of files are used often for code reuse. The .NET Framework is a great example of code reuse. Common classes like Console and DateTime in .NET live inside an assembly the .NET framework provides named mscorlib.dll.
What is the difference between a .exe and .dll
These types of files are used often for code reuse. The .NET Framework is a great example of code reuse. Common classes like Console and DateTime in .NET live inside an assembly the .NET framework provides named mscorlib.dll.