Encapsulation is hiding the complexity, abstraction is focus on the essential characteristic.
Encapsulation is implemented by using access specifiers.
public |
There are no restrictions on accessing public members. |
private |
Access is limited to within the class definition. This is the default
access modifier type if none is formally specified |
protected |
Access is limited to within the class definition and any class that
inherits from the class |
internal |
Access is limited exclusively to classes defined within the current
project assembly |
protected internal |
Access is limited to the current assembly and types derived from the
containing class. All members in the current project and all members in
derived class can access the variables. |
private protected |
Access is limited to the containing class or types derived from the
containing class within the current assembly. |
Polymorphism is the ability of
different objects to respond in a unique way to the same message.
An abstract class is a special type of class that cannot be
instantiated. An abstract class is designed to be inherited by subclasses that
either implement or override its methods. ... You can have functionality in
your abstract class—the methods in an abstract class can be both abstract and
concrete.
When a class is declared sealed, it cannot be inherited, abstract
classes cannot be declared sealed.
A data type is a value type if it holds a data value within its
own memory space. It means variables of these data types directly contain their
values.
Unlike value types, a reference type doesn't store its value
directly. Instead, it stores the address where the value is being stored. In
other words, a reference type contains a pointer to another memory location
that holds the data.
The following data types are of reference type:
• String
• All arrays, even if
their elements are value types
• Class
• Delegates
The process of converting from a value type to a reference type is
called boxing. The process of converting from a reference type to a
value type is called unboxing.
Class |
Struct |
Reference Type |
Value Type |
Support Inheritance |
No Inheritance |
The reference can be null |
Cannot have a null reference (unless Nullable is
used) |
Interfaces are still different
from Abstract classes.
Interfaces support
multiple inheritance
Interfaces can NOT
provide implementation of an abstract class
Interface methods are
by default public, while in abstract class one may have private and protected
methods also
Const is a variable which has to be assigned a value at
compile time. By default, a const is static and we cannot change the value of a
const variable throughout the entire program.
Readonly is the keyword whose
value we can change during runtime or we can assign it at run time but only
through the non-static constructor. No need to assign a value at compile time.
A Static Readonly type variable's value can be assigned at
runtime or assigned at compile time and changed at runtime. But this variable's
value can only be changed in the static constructor. And cannot be changed
further. It can change only once at runtime.
ref and out keywords
By default, the value type variable is passed by value, and the
reference type variable is passed by reference from one method to another
method in C#.C# includes ref and out are keywords, which help us to pass the
value type variables to another function by the reference.
•The ref keyword passes arguments by reference. It means any changes
made to this argument in the method will be reflected in that variable when
control returns to the calling method.
The out parameters are always passed by reference for both, the value
type and the reference type data types.
•A variable must be assigned a value before passing as an argument with
the ref keyword. But in out declare a variable without initializing.
•In the called method, may or may not assign a value for ref parameter.
For out variable, called method must specify the value.
•We must specify the ref keyword when passing to the method. Otherwise,
it will give a compile-time error. For out, it's Out key word
Both ref and out are treated differently at run time and they are
treated the same at compile time. So doesn't support overloading if 2 method
one has ref and other has out, the compiler throws exception.
Properties are not variables, therefore it cannot be passed as an out or
ref parameter.
Convert.ToInt32() calls int.Parse()
internally.Except for one thing Convert.ToInt32() returns 0 when argument is
null. TryParse(), Never throws an
exception. Returns false if cannot parse to an integer. It must use out
parameter.
Can “this” be used within a static method ? No, exception is
Extension method.
Properties or accessors : Can be read/write,
read only(only get), write only (only set).
A Non Static class can have static method. But Static class
should have Static method and Data member only.
Dispose vs Finalize
1.
Dispose can be called
explicitly by code and Finalize is a non-deterministic method called by GC when
it feels.
2.
Dispose free unmanaged
resources almost immediately , Finalize cleans unmanaged resources when it goes
out of scope.
3.
Dispose is defined in
Idisposable interface, Finalize defined in object class.
4.
In dispose, we have to
write GC.SuppressFinalized()
String vs StringBuilder : String is immutable, so every time when its
value changed it will occupy a new memory.
Sealed classes are used to restrict
the inheritance feature of object-oriented programming. Once a class is defined
as a sealed class, the class cannot be inherited.
A partial class is only used to split
the definition of a class in two or more classes in the same source code file
or more than one source file.
Early Binding : Compile time
Polymorphism, Late Binding : Run time Polymorphism
Unlike arrays, an ArrayList can hold data of multiple data types.
Elements in the ArrayList are accessed via an integer index. A dynamic array
does not have a predefined size using Array.Resize .
Constructor Chaining is an approach where
a constructor calls another constructor in the same or base class. This is very
handy when we have a class that defines multiple constructors. Example : https://www.codeproject.com/Articles/271582/Constructor-Chaining-in-Csharp-2
CopyTo |
Clone |
The CopyTo() method
copies the elements into another existing array |
The Clone() method
returns a new array (a shallow copy) object containing all the elements in
the original array. |
CopyTo require to have a
destination array |
when Clone return a new array |
Does Shallow Copy(i.e the contents
(each array element) contains references to the same object as the elements
in the original array) |
Does Shallow copy as well |
throw ex vs throw
throw ex resets the stack trace (so your errors would appear to
originate from HandleException)
throw doesn't - the original offender would be preserved.
Difference between the Equality Operator (==) and Equals()
Both the == Operator and the Equals() method are used to compare two
value type data items or reference type data items. The Equality Operator (==)
is the comparison operator and the Equals() method compares the contents of a
string. The == Operator compares the reference identity while the Equals()
method compares only contents. Let’s see with some examples.
In this example, we assigned a string variable to another variable. A
string is a reference type and in the following example, a string variable is
assigned to another string variable so they are referring to the same identity
in the heap and both have the same content so you get True output for both the
== Operator and the Equals() method.
using
System;
namespace
ComparisionExample {
class Program {
static void Main(string[] args) {
string name =
"sandeep";
string myName = name;
Console.WriteLine("== operator result
is {0}", name == myName);
Console.WriteLine("Equals
method result is {0}", name.Equals(myName));
Console.ReadKey();
}
}
}
Is vs As Operator
In C# language, we use the "is" operator to check the object
type. If two objects are of the same type, it returns true, else it returns
false. The "as" operator behaves in a similar way as the
"is" operator. The only difference is it returns the object if both
are compatible with that type. Else it returns a null.
Console.WriteLine(o2 is P1);
string str1 = o[q] as string;
Var vs dynamic
var is a statically typed variable. So the data type of these variables
are inferred at compile time. dynamic are dynamically typed variables. type is
inferred at run-time and not the compile time. var does not allow the type of
value assigned to be changed after it is assigned to.
Serialization in C# is the process
of converting an object into a stream of bytes to store the object to memory, a
database, or a file. Its main purpose is to save the state of an object in
order to be able to recreate it when needed.
Anonymous types allow us to create
new types without defining them. This is a way of defining read-only properties
in a single object without having to define each type explicitly. Here, Type is
generated by the compiler and is accessible only for the current block of code.
The type of properties is also inferred by the compiler.
We can create anonymous types by using “new” keyword together with the
object initializer. It's used in
Select clause of LINQ
Example
var anonymousData =
new
{
ForeName = "Jignesh",
SurName = "Trivedi"
};
Console.WriteLine("First
Name : " + anonymousData.ForeName);
Generally, the speed of a database system is measured by the transaction throughput, expressed as a number of transactions per second.
Azure Cosmos DB supports many APIs, such as SQL, MongoDB, Cassandra, Gremlin, and Table. Each API has its own set of database operations. These operations range from simple point reads and writes to complex queries. Each database operation consumes system resources based on the complexity of the operation.
The cost of all database operations is normalized by Azure Cosmos DB and is expressed by Request Units (or RUs, for short). Request unit is a performance currency abstracting the system resources such as CPU, IOPS, and memory that are required to perform the database operations supported by Azure Cosmos DB.
Provisioned throughput mode: In this mode, you provision the number of RUs for your application on a per-second basis in increments of 100 RUs per second. To scale the provisioned throughput for your application, you can increase or decrease the number of RUs at any time in increments or decrements of 100 RUs.
IEnumerable vs IQueryable
If you are use IEnumerable when querying data from in-memory collections like List, Array collection etc And when querying data from out-memory (like remote database, service) collections so you are use Iqueryable
IQueryable and IEnumerable represent two different things. Think of a IQueryable as a "question", it does not have any results itself. A IEnumerable is an "answer", it only has data connected to it but you can't tell what generated that data.