Krystyna Ślusarczyk Mid Level Flashcards

1
Q

What is the use of “using” keyword?

A

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
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are global using directives?

A

Can we used in any file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is statically-typed and dynamically-types languages?

A

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#.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a dynamic keyword?

A

A variable declared with Dynamic type will bypass the static type checking.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are weakly typed and strongly typed languages?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is an expression?

A

piece code which evalutes to some value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a statement?

A

piece of code but does not evaluate to any value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is expression-bodied members?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

what is Func and Actions?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is Lambda expressions?

A

allows us to define anonymous functions.

(param1, param2) => param1 + param2;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the difference between throw and throw ex?

A

Throw preserve the stack trace while throw ex does not preserve the stack trace.

using throw ex, we loose the information about the methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a stack trace?

A

The stack trace is a trace of all methods that have been called that led to a particular situation in the code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to log stack trace?

A

Environemnt.StackTrace.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the difference between typeof anf GetType?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is checked keyword?

A

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
{

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is checked keyword?

A

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
{

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is the range of a BYTE?

A

0 to 255

18
Q

What is unchecked keyword?

A

Opposite of checked keyword. Used when we set it in the project.

19
Q

What is a silent failure?

A

The failure happens without the knowledge of a developer.

Example, arithmetic operations overflow.

20
Q

What is BigInt?

A

Bigint is limited by the application’s memeoty. It represents Gigantic number.

21
Q

What is difference between string and StringBuilder?

A

Strings are immutable. It means that once we define string it cannot be modified.

For example,

string b = “abc”;
strung add = b + “c”;

STringBuilder strBuilder = New StringBuilder();
strBuilder.Append();

StringBuilder is a utility class used for optimal concatination of strings.

When we append using string,

a. It creates a new string.
b. Points to a new string.
c. GC have to clean old string.

If we want add a string incrementally then we have to use StringBuilder.

22
Q

What is an Array?

A

Array is a most basic collection in c#.

We can think array as a collection of boxes. Each boxes has an index and hold a vlue.

Arrays are reference types in c#.

Example:
  //Array
        var arr = new int[5];

        arr[0] = 1;

        var arrWithIni = new int[] { 1, 2, 3 };

        var multiDimensionArr = new int [3,3];

        var jaggedArr = new int[2][3];

        jaggedArr[0] = new int[] { 1, 2, 3 };
23
Q

What is a jagged array?

A

Jagged array is an array of arrays which can be different sizes.

24
Q

What are advantages of arrays?

A

they are fast and easy to use.

25
Q

What are dis-advantages of arrays?

A

Arrays are of fixed size.

26
Q

What is a List?

A

List is a strongly typed generic collections of objects.

To create a List,

//List

        var listOf = new List();

        //List

We can also define Capaciity of the list in the constructor.

When we define the capacity it optimizes by not allocation new size every time we add a new item.

27
Q

Why catch(exception) is a bad idea?

A

The catch(exception) is bad idea because it catches every type of exception.When we decide to catch an exception we shoud know how to handle the exception.

28
Q

When catch(exception) is OK to use?

A

in GLobal catch block.

29
Q

What is global catch exception?

A

the global catch block is the cach block in the upper most layer of the application so that any exception in the application is handled.

30
Q

What are NuGet packages?

A

NuGet is a Microsoft supported package manager so that developers can create and share their code.

31
Q

What is the difference between Release and debug mode?

A

In Release mode, there will some optimization so that the program can run faster.

32
Q

If we want to run a piece of code only in debug mode, how to do that?

A

if DEBUG

33
Q

Does the garbage collector call the Dispose method?

A

No, GC is not aware of Dispose method. we have to call it. We can use using statement to call.

34
Q

When should be write our own destructors?

A

Never. Destructors are very tricky and we don’t have a guarantee that they will run. We shoudl use DIpose method instead.

35
Q

What is managed and unmanaged code?

A

Managed resources are managed by CLR. all objects created in C# are managed resources. GC is aware of their existence and they will clean up when those resources are not needed.

Unmanaged are not managed by C#. GC are not aware of them and GC wont clean them. Unmanaged resources are DB connections and File handlers.We have to perform the clean i

36
Q

What is the difference between Dispose and Finalize?

A

Dispose method is used to free unmanaged resources. we have to use IDisposable to use Dispose method. Example, File reader class used File handler. We need to close the file handler. We should not put this code in destructor. Instead, we have to use IDisposbale.

The Finalize method is the same thing as the destructor, so it is the method that is called on an object when cleaned up by the garbage collector. We cannot override Finalize method. We get a compiler waring. Instead, we have write a destructor.

37
Q

What is a Tuple?

A

Tuple is a small data structure used to bind many values together.

For example, if I write a function and I need to return many values, I can use tuples.

To create a tuple,

      var tuple1 = new Tuple(1, "abc");
      var tuple2 = Tuple.Create(1, 5, "this");
38
Q

How many values tuples can hold?

A

8 because Tuples are reference types.

39
Q

What is a valueTuples?

A

same as tuple but they are value type. Hence, we can store more then 8 data and == operators returns true.

In tuple we have to use .Equals to compare.

var valTuple = new ValueTuple(2, "abc");
var valTuple2 = (2, "abc");
ValueTuple valTuple3 = (number: 2, text: "hey");
40
Q

Differences between tuple and valueTuple?

A

Tuples are reference types and ValueTuples are value types.

Tuples are immutable i.e. you cant do tupleVar.item1 = 5;

Tuples can hold only 8 data.

Tuples are less performance then value tuples.

Properties are names item1 , item2…

in ValueTuple, we can name the property while declaring.