C#TheLanguage Flashcards

1
Q

What is a delegate in C#? How is a delegate declared, instantiated, used?

A

A C# delegate is a type that represents a reference to .a method with a particular parameter list and return types. When instantiating a delegate you associate it to a particular method, and then you can invoke that method through the delegate.

// Declare a delegate
public delegate int PerformCalc(int x, int y);
This is a delegate that can point to a method which takes 2 ints and returns an int.
// Method to be encapsulated in delegate
public int AddMethod(int a, int b) {
  return a + b;
}
// Instantiate the delegate
PerformCalc calcHandler = new PerformCalc(AddMethod);
// Or
PerformCalc calcHandler = AddMethod;
// Call the delegate
calcHandler(1, 4) // returns 5

A delegate can have more than one method in its invocation list:
calcHandler += AnotherCalcMethod

Calling the delegate calls each of the methods in the order in which they were added. If there is a return value, it will be the value returned by the last method.

Delegates allow for things like passing methods as parameters, having callback methods etc.

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

What is a Func?

A

A predefined delegate which takes optional parameters and returns a value.

// declare func which takes 2 ints and returns a string
Func myFunc;
// the last type parameter is the return type (string in this case)
instantiate e.g. with lambda
myFunc = (num1, num2) => {
  return "hello world"
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is an Action?

A

A built-in delegate that takes optional parameters and does not return a value.

// declare 
Action myAction;
// the method to assign
public void doStuff(int a, string b) {
// do stuff
}
// instantiate action
myAction = doStuff;
// call
myAction(5, "hello world");
// with anonymous method
myAction = delegate(int c, string d) {
// do stuff
}
// with lambda
myAction = (num, word) => { //do stuff }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a Predicate?

A
A predefined delegate that takes one parameter T and returns a bool
// predicate which takes a person object and returns true if name is Claire
Predicate myPredicate = (p) => {
  return p.Name == "Claire"
}
// Then can pass to Linq I think?
List people = new List();
//...
people.Where(myPredicate);
// returns list of people matching the predicate
// this is the equivalent of doing this:
people.Where(p => p.Name == "Claire");
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a c# event and how does it relate to delegates?

A

An Event declaration adds a layer of abstraction and protection on the delegate instance. This protection prevents clients of the delegate from resetting the delegate and its invocation list and only allows adding or removing targets from the invocation list.

To have and raise an event you need 3 things:

  • A delegate definition
  • An event definition
  • A method called to raise the event (call the event/delegate)

delegate definition:
public delegate int WorkPerformedHandler(int hours);

event definition (with the delegate type defined above):
public event WorkPerformedHandler WorkPerformed;
method:
protected virtual void OnWorkPerformed(int hours) {
 // Check event/delegate is not null
// that means it has been subscribed to
  if(WorkPerformed != null) {
       WorkPerformed(hours); // this calls the delegate, e.g.  
        calls the methods that have subscribed to the event
        i.e the methods that the delegate is referencing!
  }
}
// Declare
// General form:
public event {Delegate type} {event name}
// Example:
public event EventHandler Punch;
// Can also use generic event handler
public event EventHandler Punch
// EventHandler is a built in delegate type
// can also use custom delegate
public event MyCustomEventHandler Punch;
// Method where you raise the event (good idea to make 
// virtual so can be overridden
public virtual void OnPunch() {
// Either use event directly, OR
//get the delegate from the event e.g.
MyDelegate handler = Punch as MyDelegate;
// check if not null, meaning there are subscribers
if(handler != null) {
// raise event
handler(this, EventArgs.Empty) 
// or whatever parameters accepted by the delegate
}
}
Subscribing to event (in other class):
classWithEvent = new ClassWithEvent()
classWithEvent.Punch += MyMethod
public void MyMethod(object source, EventArgs args) {
// react to event
}
// Or with anonymous method
classWithEvent.Punch += delegate(object source, EventArgs args) {
  // body
}
// Or with lambda
classWithEvent.Punch += (source, args) => {// body}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a lambda

A

As far as I understand it is syntactic sugar/concise notation for a delegate. Wherever I use a lambda it is expecting a function/action with a specific signature (return type and parameters), which is how it infers the types of the lambda parameters. So if I do something like list.Where(x => x.id == 1), that Where method is expecting a delegate (i.e. function or action) which takes whatever type x is as a parameter and returns true if it meets condition

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

Questions on asynchronous programming

A

https://docs.microsoft.com/en-us/dotnet/csharp/async
AND do pluralsight course!

AND WATCH THIS VIDEO https://www.youtube.com/watch?v=0qiB3oW_nd8

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

Expressions and expression trees

A

todo

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

C# Closures?

A

todo

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

What are generics and why use them? How can you constrain generics? How are C# generics different from Java Generics?

A

todo

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

What is the async keyword?

A

The async keyword is used to indicate that a method can contain the await keyword.

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

What is the await keyword?

A

The await keyword is used to interrupt the flow of the code until the Task being awaited has completed. It releases the thread it is running on, and when the task completes execution is returned to that point in the code.

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

What is a Task?

A

A Task represents a concurrent operation. It’s an abstraction, a wrapper around a concurrent operation. IT may or may not run on a separate thread.
You can do things with it like ContinueWith(code to run after it’s complete), check it’s status etc.

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

What is configureawait?

A

todo

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

Wait, await, configureawait and deadlocking, what gives?

A

todo

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

Why async all the way up?

A

todo

17
Q

How would you implement the Linq method Where? How about Select?

A

public static IEnumerable Where(this IEnumerable sequence, Func predicate) {
foreach(var item in sequence) {
if(predicate(item)) { yield return item};
}
}

usage:
things.Where(x => x.IsAvailable);

Amazingly, the x => x.IsAvailable is a lambda which is the Func that is passed to the Where. It is a function which takes a thing, and returns true if IsAvailable is true or false if IsAvailable is false.

So when we call predicate(item), we are calling the following:

bool IsThingAvailable(Thing thing) {
  return thing.IsAvailable;
}
18
Q

What is a stream?

A

todo

19
Q

C# dynamic keyword. What is it? How can it, for example, replace reflection?

A

Declaring something as dynamic essentially lets you switch off C#’s type checking.

20
Q

What is the CLR? And what is the DLR?

A

todo

21
Q

What is yield return and what is it good for?

A

It’s all about being lazy. Deferred execution. You can iterate through a sequence but the item is only obtained when it is requested.

22
Q

What is the Lazy keyword

A
It's like the equivalent of doing:
prop get { if prop == null prop = new prop(); return prop}

Essentially the thing is only instantiated when first accessed. You defer its instantiation until it is required. Which it might never be.

23
Q

What is partial application and currying (functional programming concepts)

A

todo