• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/141

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

141 Cards in this Set

  • Front
  • Back

What is C#?

C# is a type-safe, managed and object oriented language, which is compiled by .Net framework for generating intermediate language (IL).

List four advantages of C#?

  • Easy to learn
  • Object oriented
  • Component oriented
  • Part of .NET framework

Explain the three types of comments in C#?

  1. Single Line Comment Eg : //
  2. Multiline Comments Eg: /* */
  3. XML Comments Eg : ///

Explain two ways sealed keyword is used in C#?

sealed is used to either:




  1. prevent the class from being inherited from other classes.
  2. prevent the methods being overridden in child classes.

Give an example of a sealed class?

class x {}


sealed class y:x {}

Give an example of a sealed method?

class ClassA {


protected virtual void First() {}


}




class ClassB : ClassA {


sealed protected override void First() {}


}




class ClassC : ClassB {


protected override void First() {} //error


}

What are two the differences between Array and ArrayList in C#?

  1. Arrays store values or elements of same data type whereas arraylists store values of different datatypes.
  2. Arrays use fixed lengths but arraylists don't.

What are the two uses of using keyword in C#?

  1. As a directive to create an alias for a namespace or to import types defined in other namespaces.
  2. As a statement to define a scope that will dispose of an object when it ends.

Explain C# namespaces?

Namespaces are containers for grouping related user-defined types.

What is the use of keyword const?

To create a constant assigned at compile-time that cannot be reassigned.

What is the difference between the const and readonly keywords?

const is assigned at compile-time.




readonly is assigned at run-time declaration or in a constructor.

What seven things can the static modifier be used to modify?


  1. classes
  2. fields
  3. methods
  4. properties
  5. operators
  6. events
  7. constructors

What three things can't the static modifier be used to modify?

  1. indexers
  2. finalizers
  3. types other than classes (eg struct, interface)

Which is the only non-static member a static class can contain?

consts; they behave just like static members.

Can a static member reference other non-static members of the same class?

Not without first creating an instance of the class.

What's the difference between dispose and finalize methods?

dispose is called explicitly by client code to clean up resources immediately.




finalize is called implicitly by the garbage collector and cannot be called explicitly by client code.

Can multiple catch blocks be executed when an exception is raised?




Explain.

No, C# executes the first exception block that handles an exception in the InnerException stack.

How can multiple exceptions be handled in one catch block?

Catch the most general exception and then loop through the InnerExceptions and handle appropriately.

What is the purpose of the finally block?

To ensure a block of code is executed regardless of the way the try block is exited.




Especially to clean up resources.

How can code exit a try block?

normal flow of code execution


break


continue


goto


return


propagation of exception

What is the difference between throw and "throw {e}" where {e} is reference to passed exception?

throw preserves the exception stack




"throw {e}" resets the exception stack and only passes the information from the point of "throw {e}"

What are the two types of error that can be raised in C#?

Compile time error


Run time error

Can we get an exception in a finally block?




If so, how can we handle such an exception?

Yes.




Let it propagate or manage with "try...catch".

Which assembly does System namespace reside?

mscorlib.dll

What two ways can the out keyword be used?

1. To modify a method parameter




2. To modify a generic type parameter as covariant (this has certain usage constraints)

What is the difference between out and ref parameters?

An out parameter may be modified by the method it is passed to but does not need to be initialised when passed to said method.




A ref parameter may be modified by the method it is passed to but is initialised before it is passed to said method.

What three ways can the ref keyword be used?




Which two of these uses are co-dependent?


  1. In a method signature and call to pass an argument by reference.
  2. In a method signature to return a value by reference.
  3. In a member body to indicate that return value from 2. is stored locally and the caller intends to modify it.

What is a jagged array?

An array of arrays of different sizes and dimensions.

Can this be used in a static method?

No.

What are the two categories of value types and what do these categories consist of?

1. Structs


a. Numeric types


b. char


c. bool


d. User defined




2. Enumerations

What are the two reference type categories and what seven types do these categories contain between them?

  • Used to declare reference types:

  1. array
  2. class
  3. delegate
  4. interface



  • Inbuilt reference types:

  1. dynamic
  2. object
  3. string

Where are value types stored?

On the stack.

Where are reference types stored?

The reference is stored on the stack and points to the data on the heap.

What is a delegate type and what are they the basis for?

A reference type that encapsulates a type-safe and secure named or anonymous method.




Delegate types are the basis for Events.




https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/delegate

What is an object type?

object is a reference type that is a C# alias for System.Object in the .NET framework.

What is a dynamic type?

A reference type that is identical to the object type but operations that contain dynamic types are not resolved or type checked at compile-time.

What is a string type?

string is an object of type System.String whose value is text that is stored as a sequential read-only collection of Char objects.




It can also be defined as a reference type that is an alias for System.String in the .NET framework.

What types can you assign to an object?
All C# types (predefined, user-defined, reference and value types) can be assigned to an "object" because they all inherit, either directly or indirectly, from Object which "object" is an alias for.
What is casting a variable to an object called?
Boxing.
What is casting an object to a value type called?
Unboxing.
What is the difference between String and string types and when would you use either one?

String is the .NET CLR type that is declared in System namespace. Use this type when declaring a public API that will be available to anyone using any language.



string is the native C# alias for String therefore does not require System and, by convention, should be used for internal implementations.
What is a "verbatim string" and how is it declared?

A string that doesn't need any escaping except double quotation marks when quotation marks need to be included.




Prefix the string with "@" to declare verbatim string:


@"c:\program files\"


notice the backslashes do not need to be escaped.

What is a "string literal"?

A string with all unicode characters represented as their literal equivalent.



e.g.


Hello


World!




would be represented as:


"\tHello\r\n\tWorld!\r\n"

What is the .NET type that the object alias refers to?

System.Object
What is the .NET type that the string alias refers to?
System.String

What is the .NET type that the bool alias refers to?

System.Boolean

What is the .NET type that the byte alias refers to?

System.Byte

What is the .NET type that the sbyte alias refers to?

System.SByte

What is the .NET type that the short alias refers to?

System.Int16

What is the .NET type that the ushort alias refers to?

System.UInt16

What is the .NET type that the int alias refers to?

System.Int32

What is the .NET type that the uint alias refers to?

System.UInt32

What is the .NET type that the long alias refers to?

System.Int64

What is the .NET type that the ulong alias refers to?

System.UInt64

What is the .NET type that the float alias refers to?

System.Single

What is the .NET type that the double alias refers to?

System.Double

What is the .NET type that the decimal alias refers to?

System.Decimal

What is the .NET type that the char alias refers to?

System.Char

What is the difference between the float, double and decimal types?

float is a simple type that stores 32-bit floating point values with range of -3.4 × 1038 to +3.4 × 1038 with a precision of 7 digits




double is a simple type that stores a 64-bit floating point values with a range of ±5.0 × 10−324 to ±1.7 × 10308 with a precision of 15-16 digits




decimal is 128-bit data type with more precision but smaller range (-7.9 x 1028 to 7.9 x 1028) / (100 to 1028) with a precision of 28-29 significant digits

What is the type of the variable if a real numeric literal is assigned to a var?


e.g.


var x = 3.5;

x is a double

What happens if you attempt to assign a real numeric literal to a variable of type float or decimal?

A compile-time error.

How is a numeric literal assigned to a variable of type double? Give an example.

By default, if the literal is a real, or by adding a "D" or "d" if the literal is an integer:


e.g.


double x = 3.5D;


or


double x = 3D;

How is a real numeric literal assigned to a variable of type float? Give an example.

By adding a suffix of "F" or "f":


e.g.


float x = 3.5F;


or


float x = 3F;

How is a numeric literal assigned to a variable of type decimal? Give an example.

By adding a suffix of "M" or "m":


e.g.


decimal x = 3.5M;


or


decimal x = 3M;

Which floating point type is most appropriate for financial calculations and why?

decimal because it has more precision and smaller range then double and float.

What sets of values can a double and a float contain? How does this differ from decimal

Positive and negative zero.


Positive and negative infinity.


NaN.


The finite set of nonzero values.


Or null if it's a nullable variable.




A decimal can only contain the finite set of nonzero values or null if it's a nullable variable.

Name the 9 integral types.

sbyte


byte


char


short


ushort


int


uint


long


ulong

What size is a sbyte and what is its range?




Quote the mnemonic.

signed 8-bit integer


-128 to 127




Die kNaVe

What size is a byte and what is its range?




Quote the mnemonic.

unsigned 8-bit integer


0 to 255




NuLL

What size is a char and what is its range?

unicode 16-bit character


U+0000 to U+ffff

What size is a short and what is its range?




Quote the mnemonic.

signed 16-bit integer


-32,768 to 32,767




MooN, CaSH-oaF

What size is a ushort and what is its range?




Quote the mnemonic.

unsigned 16-bit integer


0 to 65,535




JeweL, Low-MoLe

What size is an int and what is its range?




Quote the mnemonic.

signed 32-bit integer


-2,147,483,648 to 2,147,483,647




SuN, DRaG, eaRFoaM, JaR-oaF

What size is a uint and what is its range?




Quote the mnemonic.

unsigned 32-bit integer


0 to 4,294,967,295




SeaR, NeighBouR, PayCHeCK, NeBuLa

What size is a long and what is its range?




Quote the mnemonic.

signed 64-bit integer


-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807




ZiP, NuN Mow, MohiCaN, SMaSH, FLoweR, CaKehoLe, oaf-SoFa

What size is a ulong and what is its range?




Quote the mnemonic.

unsigned 64-bit integer


0 to18,446,744,073,709,551,615




DoVe, eaR-RaSH, CouRieR, SCuM, GaSP, LiLT, aSH-hoTeL

What is the enum keyword used to declare?

An enumeration; a distinct type that consists of a set of named constants called "the enumerator list".

Can an enum be nested inside a class or struct and accessed publicly?

Yes.

What is an enum's default underlying type?

int

What types can an enum's underlying type be?

Any integral type except char.

How do you declare an enum with an underlying type of byte?

enum Days: byte {Sat, Sun, Mon, Tue, Wed, Thu, Fri}

What value does the first enumerator in an enum have by default?

0

How do you override an enum's default values?

enum Days: byte {Sat = 1, Sun, Mon, Tue, Wed, Thu, Fri}




enum Options {black = 1, red = 7, green = 13}

Can you cast a value of an enum's integral type that doesn't explicitly exist in the enum's range?




For example:


enum Options { black = 1, red = 7, green = 13 }


option = (Options)1024

Yes

How does the FlagsAttribute class - [Flags] or [FlagsAttribute] modify an enum?

Indicates that an enumeration can be treated as a bit field (a set of flags):




[Flags] enum CarOptions { SunRoof = 0x01, Spoiler = 0x02, FogLights = 0x04, TintedWindows = 0x08 }




var options = CarOptions.SunRoof | CarOptions.FogLights




(CarOptions could be declared with 1, 2, 4 & 8 as the values, they don't have to be hex)

What would the following output:




[Flags] enum CarOptions { SunRoof = 0x01, Spoiler = 0x02, FogLights = 0x04, TintedWindows = 0x08 }




var options = CarOptions.SunRoof | CarOptions.FogLights;




Console.WriteLine(options);


Console.WriteLine((int)options);

SunRoof, FogLights


5

Explain the public access modifier.

The modified type or member can be accessed by any other code in the same assembly or assembly that references the containing assembly.

Explain the private access modifier.

The modified type or member can be accessed only by code in the same class or struct.

Explain the protected access modifier.

The modified type or member can be accessed only by code in the same class or in a class that is derived from that original class.

Why can't the protected access modifier be applied to any of a struct's members?

Because a struct is sealed and therefore cannot be inherited from.

Explain the internal access modifier.

The modified type or member cannot be accessed from another assembly but can be accessed from any code in the same assembly.

Explain the protected internal access modifier.

The modified type or member can be accessed by any code in the assembly in which it is declared or from within a derived class in another assembly.

What is the default access for a class, interface, delegate or struct declared directly within a namespace?

Internal.

What is the default access for class and struct members (including nested classes and structs)?

Private.

How can you make internal declarations visible to other assemblies?

By identifying another assembly as a "friend assembly" using the "InternalsVisibleToAttribute":




[assembly: InternalsVisibleTo("AssemblyB")]

How can an internal member have a more public access level than the type that contains it?

A public member of an internal class might be accessible outside the assembly if the member is part of the class's implementation of publicly accessible interface methods or overridden virtual methods defined in a public base class.

What is the minimum access level of the type of a field, property or event?

At least as accessible as the member itself.

What access level must user-define operators be declared at?

Public.

Can finalizers have an access modifier assigned to them?

No.

Which access modifiers can be applied to an interface's members and why?

None, they are public by default and design.

What is the default access level of enumeration members and what access modifiers can be applied to them?

Public by default and design; no access modifier can be applied to an enumeration member.

What is an InnerException?

InnerException is a property of the Exception class and gives detail about the exception including child exception detail.

Will a finally block execute if return statement is reached first?

Yes.

When implementing ref return with ref local, is the referred member modified with the ref keyword or only the return signature of the method and the local variable that stores the result?

No, the referred member within the called type is not modified with the ref keyword.

Explain why the StringBuilder class exists in C#

StringBuilder is used to operate on a mutable string of characters because a string is immutable and therefore expensive when its value needs to be manipuated.

What is a StringBuilder's default capacity and default MaxCapacity?

Default capacity = 16 chars




Default MaxCapacity = Int32.MaxValue (2,147,483,647)

When is it faster to initialise a StringBuilder instance with a specified capacity?




Why is it faster to do so?

When it is known that the final string will be larger than a doubling sequence value (ie: larger than 16, 32, 64, 128 etc).




It is faster to initialise to a known capacity because otherwise a new buffer is created when a doubling value is reached and the old buffer is copied in.

When is it advisable to initialise a StringBuilder instance with a smaller MaxCapacity than the default?

When the app will be running on a device with limited memory capacity.

Is a C# string null terminated?

No.

Can a C# string contain embedded null characters ('\0')?

Yes.

What does the Length property of a string represent?




And, conversely what is it not safe to assume that the Length property of a string represents (that might be natural to assume it does represent), and why? Give two examples.

The number of Char objects it contains.




It does not, necessarily, represent the number of Unicode characters the string contains because one character may be represented by multiple Char objects.




Two examples of a Char not representing a Unicode character are when a character is encoded as a base character and


  1. is part of a surrogate pair
  2. is part of a combining character sequence

How can individual Unicode points in a string be accessed?

By using the StringInfo object.

What are the five ways a string can be created?

  1. By assigning a string literal to a string variable.
  2. By calling a string class constructor.
  3. By using the string concatenation operator on any combination of string instances or literals.
  4. Retrieving a property or calling a method that returns a string.
  5. By calling a formatting method to convert a value or object to its string representation.

What is "Boxing"?

Casting a variable to an object.

What is "Unboxing"?

Casting an object to a value type.

What is a textual surrogate pair?




How can you handle a surrogate pair in C#?

When a character is represented in UTF-32 by a 21-bit value that includes a plane.




A surrogate pair is handled by converting it to a string using Char.ConvertFromUtf32(int utf32)

What Char static method returns the character type ( e.g: UppercaseLetter, MathSymbol, ConnectorPunctuation etc) the passed character represents.

System.Globilisation.UnicodeCategory Char.GetUnicodeCategory(char c);

How can you ensure a string doesn't contain any combining characters and that they have all been converted to a single UTF-16 encoded code units?

string.Normalize();

How can you discover the number of characters in a string if the string contains surrogate pairs?

Instantiate a new StringInfo instance and call StringInfo.LengthInTextElements:




StringInfo si = new StringInfo(stringVar);


Console.WriteLine(si.LengthInTextElements);

What is the difference between:


Char.Equals(object obj);


Char.CompareTo(char value);

Equals returns boolean indicating whehter the passed obj is an instance of Char and equals the value of this instance.




CompareTo indicates whether this instance precedes, follows or is in the same position in the sort order as the specified Char by returning an int.

Define ^ (XOR) operator

This is an eXclusive OR.




The result of a logical ^ (XOR) is true if, and only if, exactly one of its operands is true.




The result of a bitwise ^ (XOR) compares each bit and outputs 1 whenever the inputs do not match and 0 when they do.

Define | (OR) operator

The result of a logical | (OR) is false if, and only if, both operands are false.




The result of a bitwise | (OR) compares each bit and outputs 0 if both inputs are false otherwise the output is 1.

Define || (logical-conditional-OR) operator

If the first operand of a || (logical-conditional-OR) evaluates to true, the second operand isn't evaluated. If the first operand evaluates to false the second operand determines the result.

Define & (AND) operator

The result of a logical & (AND) is true if, and only if, both operands are true.




The result of a bitwise & (AND) compares each bit and outputs 1 if both inputs are 1, otherwise the output is 0.

Define && (logical-conditional-AND) operator

If the first operand evaluates to false, the second operand isn't evaluated. If the first operand evaluates to true the second operand determines the result.

What is the default entry point of a C# application?

The Main method.

What is the default return type of the Main method of a C# application?




What other type can a Main method return?

void




An int

Which two ways can a finalizer be overridden in C#?

By declaring a finalizer with the name of the class prefix by a tilde:




~ClassAName() {


//clean up code


}




By declaring an expression body definition:


~ClassAName() => Console.WriteLine($"destroying");

When should a finalizer be declared?

When the class instantiates unmanaged resources such as windows, files or network connections.

If a class is using a particularly expensive unmanaged resource which method should be implemented and why?




Which method should also be implemented?

Dispose() from IDisposable because it gives the client code the means to dispose of the resource immediately without relying on the garbage collector to call the finalizer.




A finalizer should still be implemented to ensure the resource is released when the client doesn't explicitly call Dispose().

What seven kinds of functions exist in C#?

  1. Methods
  2. Operators
  3. Constructors
  4. Properties
  5. Events
  6. Indexers
  7. Finalizers

What is the name of the C# compiler?

csc.exe

How is a C# application compiled using the C# compiler without Visual Studio for:


  • an executable?
  • a library?

call from the command line:




csc MyProgramFile.cs




or




csc /target:library MyProgramFile.cs

What parameter does a Main method accept?

An optional array of strings.

What must a C# identifier begin with?

An underscore or a letter.

By convention, which three identifiers are camel case?

  1. parameters
  2. local variables
  3. private fields

Which case should all identifiers be in except for those that are camel case by convention?

Pascal case.

How can you use a reserved word as an identifier?

By qualifying it with the @ prefix:




class @class {...}

Which keywords can be used as identifiers and why?

Contextual keywords because ambiguity can not arise.

What are C#'s two categories of predefined types and what types do they contain?

Predefined value types:


  • all integral types
  • all floating point types
  • bool



Predefined reference types:


  • object
  • string

What is another name for predefined types?

Built-in types.