Krystyna Ślusarczyk Mid Level Flashcards
What is the use of “using” keyword?
Two main uses:
a. The using directive which allows using types from another name spaces, and to create alias.
Example:
using System.Diagnostics;
namespace justFOrLearning { class @using { Stopwatch sw = Stopwatch.StartNew(); } }
We can also make the using as global in C# 10 onwards.
Another example is that if we have conflicts, then we can create the alias.
In file1,.cs
namespace File1 { class Person{} }
In file2,.cs
namespace File1 { class Person{} }
when we do
using file1;
using file2;
Person p = new Person(); //this creats conflicts.
so we can create alias:
using file1Person = file1.person;
using file2Person = file2.person;
Next is using staement ,that defines a scope in which an IDisposable will be used, and disposed at this scpre’s end.
var fileStream = File.Open(“path”, FileMode.Open);
try
{
}
catch
{
filestream.Dispoase;
}
using(var fileStream = File.Open("path", FileMode.Open) { //some operations }
What are global using directives?
Can we used in any file.
What is statically-typed and dynamically-types languages?
In statically-typed languages, type checks happen at the compile time.
In Dynamically-typed languages, type checks happen at the runtime time.
Example,JS\
let v = 10; v= "hey";
No errors here.
this wont happen in C#.
What is a dynamic keyword?
A variable declared with Dynamic type will bypass the static type checking.
What are weakly typed and strongly typed languages?
res = “2” + 3;
2 will be converted to other type.
In weakly types languages the variable is converted form one type to another. I strongly types languages, this is not the case.
What is an expression?
piece code which evalutes to some value.
What is a statement?
piece of code but does not evaluate to any value.
What is expression-bodied members?
expression-bodied members are members deinfed with expression body instead of the regular body with braces.
class string {
public override string ToSTtring(string str) { //some operations }
can be written as
public override string ToSTtring(string str) = //some operations
only one line can be added.
what is Func and Actions?
in C#, we can treat functions like any pther types- assign them to variables or pass them as parameters.
Func and Actions allws us to do that.
Ex:
Func func1;
The last parameter of Func is always a return type.
Action is used to represent void functions.
What is Lambda expressions?
allows us to define anonymous functions.
(param1, param2) => param1 + param2;
What is the difference between throw and throw ex?
Throw preserve the stack trace while throw ex does not preserve the stack trace.
using throw ex, we loose the information about the methods.
What is a stack trace?
The stack trace is a trace of all methods that have been called that led to a particular situation in the code.
How to log stack trace?
Environemnt.StackTrace.
What is the difference between typeof anf GetType?
Both typeof and GetType returns the Type object which holds the information about the type.The inforaon such as properties methods an d constructors.
The GetType is called upon an object so it gets resolved in runtime. It is a aprt of System.Object.
Example:
Class Base
{}
Class Derived : Base
{}
var type = typeof Base;
derivedObj = new Base(); var type = derivedObj.GetType();
GetType always return the actual type.
What is checked keyword?
The checked keyword is sued to define a scope in which arithmetic operations will be checked for overflow.
It checks just the scope in which it is defined and not the inner code. I mean if a method is called inside the scope, the code inside the method will not be checked for the overflow.
Checked keyword reduces the performace.
Ex:
checked
{
}
What is checked keyword?
The checked keyword is sued to define a scope in which arithmetic operations will be checked for overflow.
It checks just the scope in which it is defined and not the inner code. I mean if a method is called inside the scope, the code inside the method will not be checked for the overflow.
Checked keyword reduces the performance.
Ex:
checked
{
}