C#-1 Flashcards

(52 cards)

1
Q

Classes and Interfaces

A

In a traditional object-oriented paradigm, the only kind of type is a class. In C#, there are several other kinds of types, one of which is an interface. An interface is like a class, except that it only describes members. The implementation for those members comes from types that implement the interface. Interfaces are particularly useful in scenarios where multiple inheritance is required (unlike languages such as C++ and Eiffel, C# does not support multiple inheritance of classes.)

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

C# overview

A

C# is a general-purpose, type-safe, object-oriented programming language. The goal of the language is programmer productivity. To this end, the language balances simplicity, expressiveness, and performance. The chief architect of the language since its first version is Anders Hejlsberg (creator of Turbo Pascal and architect of Delphi). The C# language is platform-neutral, but it was written to work well with the Microsoft .NET

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

Inheritance

A

An example is a base class called Asset and subclasses called house and stock, using this syntax asset : house

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

Polymorphism

A

References are polymorphic. This means a variable of type x can refer to an object that subclasses x. For instance, consider the following method:
public static void Display (Asset asset)
{ System.Console.WriteLine (asset.Name); }

This method can display both a Stock and a House, since they are both Assets: 
Stock msft    = new Stock ... ; 
House mansion = new House ... ; 
Display (msft); 
Display (mansion); 

Polymorphism works on the basis that subclasses (Stock and House) have all the features of their base class (Asset). The converse, however, is not true. If Display was modified to accept a House, you could not pass in an Asset:

static void Main() { Display (new Asset()); }    // Compile-time error public static void Display (House house)   // Will not accept Asset {   
System.Console.WriteLine (house.Mortgage); 
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Events

A

Events are function members that simplify acting on object state

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

Type

A

Unified type system The fundamental building block in C# is an encapsulated unit of data and functions called a type. C# has a unified type system, where all types ultimately share a common base type. This means that all types, whether they represent business objects or are primitive types such as numbers, share the same basic set of functionality. For example, an instance of any type can be converted to a string by calling its ToString

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

Abstract

A

An abstract class is a special kind of class that cannot be instantiated. So the question is why we need a class that cannot be instantiated? An abstract class is only to be sub-classed (inherited from). In other words, it only allows other classes to inherit from it but cannot be instantiated. The advantage is that it enforces certain hierarchies for all the subclasses. In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.

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

Catch

A

test

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

Checked

A

test

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

Delegate (simple)

A

Delegates A delegate is an object that knows how to call a method

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

Explicit

A

test

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

Extern

A

test

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

Finally

A

test

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

Fixed

A

test

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

Implicit

A

test

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

Internal

A

test

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

Lock

A

test

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

Namespace

A

test

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

New

A

test

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

Object

A

test

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

Operator

22
Q

Override

23
Q

Out

24
Q

Params

25
Private
test
26
Protected
test
27
Public
test
28
Ref
test
29
Sealed
test
30
Sizeof
test
31
Casting
test
32
Reference conversions
test
33
Upcast
An upcast operation creates a base class reference from a subclass reference. For example: ``` Stock msft = new Stock(); Asset a = msft; // Upcast ``` After the upcast, variable a still references the same Stock object as variable msft . The object being referenced is not itself altered or converted: Console.WriteLine (a == msft); // True Although a and msft refer to the identical object, a has a more restrictive view on that object: Console.WriteLine (a.Name); // OK Console.WriteLine (a.SharesOwned); // Error: SharesOwned undefined The last line generates a compile- time error because the variable a is of type Asset , even though it refers to an object of type Stock . To get to its SharesOwned field, you must downcast the Asset to a Stock .
34
Downcast
A downcast operation creates a subclass reference from a base class reference. For example: ``` Stock msft = new Stock(); Asset a = msft; // Upcast Stock s = (Stock)a; // Downcast ``` Console.WriteLine (s.SharesOwned); // Console.WriteLine (s == a); // True Console.WriteLine (s == msft); // True As with an upcast, only references are affected not the underlying object. A downcast requires an explicit cast because it can potentially fail at runtime: ``` House h = new House(); Asset a = h; // Upcast always succeeds ``` Stock s = (Stock)a; // Downcast fails: a is not a Stock If a downcast fails, an InvalidCastException is thrown. This is an example of runtime type checking (we will elaborate on this concept in Static and Runtime Type Checking ).
35
Operator overloading
test
36
As
test
37
Is
test
38
Methods
test
39
Properties
Properties are function members that encapsulate a piece of an object’s state, such as a button’s color or a label’s
40
Indexers
test
41
Interface
An interface is like a class, except that it only describes members. The Implementation for those members comes from types that implement the interface. An interface is not a class. It is an entity that is defined by the word Interface. An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body. As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesn’t support multiple inheritance, interfaces are used to implement multiple inheritance.
42
Virtual
Virtual Function Members A function marked as virtual can be overridden by subclasses wanting to provide a specialized implementation. Methods, properties, indexers, and events can all be declared virtual : ``` public class Asset { public string Name; public virtual decimal Liability { get { return 0; } } } ``` A subclass overrides a virtual method by applying the override modifier: ``` public class Stock : Asset { public long SharesOwned; } public class House : Asset { public decimal Mortgage; public override decimal Liability { get { return Mortgage; } } } ``` By default, the Liability of an Asset is 0 . A Stock does not need to specialize this behavior. However, the House specializes the Liability property to return the value of the Mortgage : House mansion = new House { Name="McMansion", Mortgage=250000 }; Asset a = mansion; Console.WriteLine (mansion.Liability); // 250000 Console.WriteLine (a.Liability); // 250000 The signatures, return types, and accessibility of the virtual and overridden methods must be identical. An overridden method can call its base class implementation via the base keyword (we will cover this in The base Keyword ).
43
Encapsulation
Encapsulation means creating a boundary around an object, to separate its external (public) behavior from its internal (private) implementation
44
Delegate (complex)
A delegate type defines the kind of method that delegate instances can call. Specifically, it defines the method’s return type and its parameter types. The following defines a delegate type called Transformer: delegate int Transformer (int x); Transformer is compatible with any method with an int return type and a single int parameter, such as this: static int Square (int x) { return x * x; } Assigning a method to a delegate variable creates a delegate instance: Transformer t = Square; which can be invoked in the same way as a method: int answer = t(3); // answer is 9 Here’s a complete example: ``` delegate int Transformer (int x); class Test { static void Main() { Transformer t = Square; // Create delegate instance int result = t(3); // Invoke delegate Console.WriteLine (result); // 9 } ``` static int Square (int x) { return x * x; } } A delegate instance literally acts as a delegate for the caller: the caller invokes the delegate, and then the delegate calls the target method. This indirection decouples the caller from the target method. The statement: Transformer t = Square; is shorthand for: Transformer t = new Transformer (Square); Note Technically, we are specifying a method group when we refer to Square without brackets or arguments. If the method is overloaded, C# will pick the correct overload based on the signature of the delegate to which it’s being assigned. The expression: t(3) is shorthand for: t.Invoke(3) Note A delegate is similar to a callback, a general term that captures constructs such as C function
45
Multi-cast delegate
Multicast Delegates All delegate instances have multicast capability. This means that a delegate instance can reference not just a single target method, but also a list of target methods. The + and += operators combine delegate instances. For example: SomeDelegate d = SomeMethod1; d += SomeMethod2; The last line is functionally the same as: d = d + SomeMethod2; Invoking d will now call both SomeMethod1 and SomeMethod2. Delegates are invoked in the order they are added. The - and -= operators remove the right delegate operand from the left delegate operand. For example: d -= SomeMethod1; Invoking d will now cause only SomeMethod2 to be invoked. Calling + or += on a delegate variable with a null value works, and it is equivalent to assigning the variable to a new value: SomeDelegate d = null; d += SomeMethod1; // Equivalent (when d is null) to d = SomeMethod1; Similarly, calling −= on a delegate variable with a single target is equivalent to assigning null to that variable. Note Delegates are immutable, so when you call += or −=, you’re in fact creating a new delegate instance and assigning it to the existing variable. If a multicast delegate has a nonvoid return type, the caller receives the return value from the last method to be invoked. The preceding methods are still called, but their return values are discarded. In most scenarios in which multicast delegates are used, they have void return types, so this subtlety does not arise. Note All delegate types implicitly derive from System.MulticastDelegate, which inherits from System.Delegate. C# compiles +, -, +=, and -= operations made on a delegate to the static Combine and Remove methods of the System.Delegate
46
Threads and processes
Threads vs Processes A thread is analogous to the operating system process in which your application runs. Just as processes run in parallel on a computer, threads run in parallel within a single process. Processes are fully isolated from each other; threads have just a limited degree of isolation. In particular, threads share (heap) memory with other threads running in the same application. This, in part, is why threading is useful: one thread can fetch data in the background, for instance, while another thread can display the data as it arrives.
47
Properties, methods,events
Properties, methods, and events In the pure object- oriented paradigm, all functions are methods (this is the case in Smalltalk). In C#, methods are only one kind of function member , which also includes properties and events (there are others, too). Properties are function members that encapsulate a piece of an object’s state, such as a button’s color or a label’s text. Events are function members that simplify acting on object state changes.
48
Reflection
Test
49
Hiding
Hiding Inherited Members A base class and a subclass may define identical members. For example: ``` public class A { public int Counter = 1; } public class B : A { public int Counter = 2; } ``` The Counter field in class B is said to hide the Counter field in class A . Usually, this happens by accident, when a member is added to the base type after an identical member was added to the subtype. For this reason, the compiler generates a warning, and then resolves the ambiguity as follows: References to A (at compile time) bind to A.Counter . References to B (at compile time) bind to B.Counter . Occasionally, you want to hide a member deliberately, in which case you can apply the new modifier to the member in the subclass. The new modifier does nothing more than suppress the compiler warning that would otherwise result : ``` public class A { public int Counter = 1; } public class B : A { public new int Counter = 2; } ``` The new modifier communicates your intent to the compiler and other programmers that the duplicate member is not an accident. Note C# overloads the new keyword to have independent meanings in different contexts. Specifically, the new operator is different from the new member modifier .
50
New vs override
new versus override Consider the following class hierarchy: ``` public class BaseClass { public virtual void Foo() {Console.WriteLine ("BaseClass.Foo"); } } public class Overrider : BaseClass { public override void Foo() { Console.WriteLine ("Overrider.Foo"); } } public class Hider : BaseClass { public new void Foo() { Console.WriteLine ("Hider.Foo"); } } ``` The differences in behavior between Overrider and Hider are demonstrated in the following code: ``` Overrider over = new Overrider(); BaseClass b1 = over; over.Foo(); // Overrider.Foo b1.Foo(); //Overrider.Foo Hider h = new Hider(); BaseClass b2 = h; h.Foo(); // Hider.Foo b2.Foo(); // BaseClass.Foo ```
51
Casting and reference conversions
An object reference can be: ``` Implicitly upcast to a base class reference Explicitly downcast to a subclass reference ``` Upcasting and downcasting between compatible reference types performs reference conversions : a new reference is (logically) created that points to the same object. An upcast always succeeds; a downcast succeeds only if the object is suitably typed.
52
Base
The base keyword is similar to the this keyword. It serves two essential purposes: Accessing an overridden function member from the subclass Calling a base- class constructor (see the next section) In this example, House uses the base keyword to access Asset ’s implementation of Liability : public class House : Asset { ... public override decimal Liability { get { return base. Liability + Mortgage; } } } With the base keyword, we access Asset ’s Liability property nonvirtually . This means we will always access Asset ’s version of this property regardless of the instance’s actual runtime type. The same approach works if Liability is hidden rather than overridden . (You can also access hidden members by casting to the base class before invoking the function.)