Study your flashcards anywhere!

Download the official Cram app for free >

  • 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

How to study your flashcards.

Right/Left arrow keys: Navigate between flashcards.right arrow keyleft arrow key

Up/Down arrow keys: Flip the card between the front and back.down keyup key

H key: Show hint (3rd side).h key

A key: Read text to speech.a key

image

Play button

image

Play button

image

Progress

1/134

Click to flip

134 Cards in this Set

  • Front
  • Back
Is it possible to inline assembly or IL in C# code?
No
Is it possible to have different access modifiers on the get/set methods of a property?
No. The access modifier on a property applies to both its get and set accessors. What you need to do if you want them to be different is make the property read-only (by only providing a get accessor) and create a private/internal set method that is separate from the property.
Is it possible to have a static indexer in C#?
No. Static indexers are not allowed in C#.
If I return out of a try/finally in C#, does the code in the finally-clause run?
Yes. The code in the finally always runs. If you return out of the try block, or even if you do a “goto” out of the try, the finally block always runs
I was trying to use an “out int” parameter in one of my functions. How should I declare the variable that I am passing to it?
You should declare the variable as an int, but when you pass it in you must specify it as ‘out’, like the following: int i; foo(out i); where foo is declared as follows: [return-type] foo(out int o) { }
How does one compare strings in C#?
In the past, you had to call .ToString() on the strings when using the == or != operators to compare the strings’ values. That will still work, but the C# compiler now automatically compares the values instead of the references when the == or != operators are used on string types. If you actually do want to compare references, it can be done as follows: if ((object) str1 == (object) str2) { … } Here’s an example showing how string compares work:

using System;
public class StringTest
{
public static void Main(string[] args)
{
Object nullObj = null; Object realObj = new StringTest();
int i = 10;
Console.WriteLine("Null Object is [" + nullObj + "]n"
+ "Real Object is [" + realObj + "]n"
+ "i is [" + i + "]n");
// Show string equality operators
string str1 = "foo";
string str2 = "bar";
string str3 = "bar";
Console.WriteLine("{0} == {1} ? {2}", str1, str2, str1 == str2 );
Console.WriteLine("{0} == {1} ? {2}", str2, str3, str2 == str3 );
}
}

Output:

Null Object is []
Real Object is [StringTest]
i is [10]
foo == bar ? False
bar == bar ? True
How do you specify a custom attribute for the entire assembly (rather than for a class)?
Global attributes must appear after any top-level using clauses and before the first type or namespace declarations. An example of this is as follows:

using System;
[assembly : MyAttributeClass] class X {}

Note that in an IDE-created project, by convention, these attributes are placed in AssemblyInfo.cs.
How do you mark a method obsolete?
[Obsolete] public int Foo() {...}

or

[Obsolete("This is a message describing why this method is obsolete")] public int Foo() {...}

Note: The O in Obsolete is always capitalized.
How do you implement thread synchronization (Object.Wait, Notify,and CriticalSection) in C#?
You want the lock statement, which is the same as Monitor Enter/Exit:

lock(obj) { // code }

translates to

try {
CriticalSection.Enter(obj);
// code
}
finally
{
CriticalSection.Exit(obj);
}
Name 10 C# keywords.
abstract, event, new, struct, explicit, null, base, extern, object, this
What is public accessibility?
There are no access restrictions.
What is protected accessibility?
Access is restricted to types derived from the containing class.
What is internal accessibility?
A member marked internal is only accessible from files within the same assembly.
What is protected internal accessibility?
Access is restricted to types derived from the containing class or from files within the same assembly.
What is private accessibility?
Access is restricted to within the containing class.
What is the default accessibility for a class?
internal for a top level class, private for a nested one
What is the default accessibility for members of an interface?
public
What is the default accessibility for members of a struct?
private
Can the members of an interface be private?
No
Methods must declare a return type, what is the keyword used when nothing is returned from the method?
void
Class methods to should be marked with what keyword?
static
Write some code using interfaces, virtual methods, and an abstract class.
using System;

public interface Iexample1{
int MyMethod1();
}

public interface Iexample2
{
int MyMethod2();
}

public abstract class ABSExample : Iexample1, Iexample2{
public ABSExample(){
System.Console.WriteLine("ABSExample constructor");
}
public int MyMethod1(){
return 1;
}
public int MyMethod2(){
return 2;
}

public abstract void MyABSMethod();
}

public class VIRTExample : ABSExample{
public VIRTExample(){
System.Console.WriteLine("VIRTExample constructor");
}
public override void MyABSMethod(){
System.Console.WriteLine("Abstract method made concrete");
}

public virtual void VIRTMethod1(){
System.Console.WriteLine("VIRTMethod1 has NOT been overridden");
}
public virtual void VIRTMethod2(){
System.Console.WriteLine("VIRTMethod2 has NOT been overridden");
}
}

public class FinalClass : VIRTExample{
public override void VIRTMethod2(){
System.Console.WriteLine("VIRTMethod2 has been overridden");
}
}
A class can have many mains, how does this work?
Only one of them is run, that is the one marked (public) static, e.g:

public static void Main(string[] args){
// TODO: Add code to start application here
}
private void Main(string[] args, int i){
}
Does an object need to be made to run main?
No
Write a hello world console application.
using System;
namespace Console1{
class Class1{
[STAThread] // No longer needed
static void Main(string[] args){
Console.WriteLine("Hello world");
}

}

}
What are the two return types for main?
void and int
What is a reference parameter?
Reference parameters reference the original object whereas value parameters make a local copy and do not affect the original.
What is an out parameter?
An out parameter allows an instance of a parameter object to be made inside a method. Reference parameters must be initialised but out gives a reference to an uninstanciated object.
Write code to show how a method can accept a varying number of parameters.
using System;

namespace Console1{
class Class1
{
static void Main(string[] args){
ParamsMethod(1,"example");
ParamsMethod(1,2,3,4);
Console.ReadLine();
}

static void ParamsMethod(params object[] list){
foreach (object o in list){
Console.WriteLine(o.ToString());
}
}
}
}
What is an overloaded method?
An overloaded method has multiple signatures that are different.
What is recursion?
Recursion is when a method calls itself.
What is a constructor?
A constructor performs initialisation for an object (including the struct type) or class.
If I have a constructor with a parameter, do I need to explicitly create a default constructor?
Yes
What is a destructor?
A C# destuctor is not like a C++ destructor. It is actually an override for Finalize(). This is called when the garbage collector discovers that the object is unreachable. Finalize() is called before any memory is reclaimed.
Can you use access modifiers with destructors?
No
What is a delegate?
A delegate in C# is like a function pointer in C or C++. A delegate is a variable that calls a method indirectly, without knowing its name. Delegates can point to static or/and member functions. It is also possible to use a multicast delegate to point to multiple functions.
Write some code to use a delegate.
//Member function with a parameter

using System;
namespace Console1
{
class Class1{
delegate void myDelegate(int parameter1);

static void Main(string[] args){
MyClass myInstance = new MyClass();
myDelegate d = new myDelegate(myInstance.AMethod);
d(1); // <--- Calling function without knowing its name.
Test2(d);
Console.ReadLine();
}

static void Test2(myDelegate d){
d(2); // <--- Calling function without knowing its name.
}
}

class MyClass{
public void AMethod(int param1){
Console.WriteLine(param1);
}
}
}
What is a delegate useful for?
The main reason we use delegates is for use in event driven programming.
What is a value type and a reference type?
A reference type is known by a reference to a memory location on the heap.

A value type is directly stored in a memory location on the stack. A reference type is essentially a pointer, dereferencing the pointer takes more time than directly accessing the direct memory location of a value type.
Name 5 built in types.
Bool, char, int, byte, double
string is an alias for what?
System.String
Is string Unicode, ASCII, or something else?
Unicode
Strings are immutable, what does this mean?
Any changes to that string are in fact copies.
Name a few string properties.
trim, tolower, toupper, concat, copy, insert, equals, compare.
What is boxing and unboxing?
Converting a value type (stack->heap) to a reference type (heap->stack), and vise-versa.
Write some code to box and unbox a value type.
// Boxing
int i = 4;
object o = i;
// Unboxing
i = (int) o;
What is a heap and a stack?
here are 2 kinds of heap – 1: a chunk of memory where data is stored and 2: a tree based data structure. When we talk about the heap and the stack we mean the first kind of heap. The stack is a LIFO data structure that stores variables and flow control information. Typically each thread will have its own stack.
What is a pointer?
A pointer is a reference to a memory address.
What does new do in terms of objects?
Initializes an object.
How do you dereference an object?
Set it equal to null.
In terms of references, how do == and != (not overridden) work?
They check to see if the references both point to the same object.
What is a struct?
Unlike in C++ a struct is not a class – it is a value type with certain restrictions. It is usually best to use a struct to represent simple entities with a few variables. Like a Point for example which contains variables x and y.
Describe 5 numeric value types ranges.
sbyte -128 to 127
byte 0 – 255
short -32,768 to 32,767
int -2,147,483,648 to 2,147,483,647
ulong 0 to 18,446,744,073,709,551,615
What is the default value for a bool?
false
Write code for an enumeration.
public enum animals {Dog=1,Cat,Bear};
Write code for a case statement.
switch (n){
case 1:
x=1;
break;
case 2:
x=2;
break;
default:
goto case 1;
}
Is a struct stored on the heap or stack?
Stack
Can a struct have methods?
Yes
What is explicit vs. implicit conversion?
When converting from a smaller numeric type into a larger one the cast is implicit. An example of when an explicit cast is needed is when a value may be truncated.
Give examples of both explicit and implicit conversion.
// Implicit
short shrt = 400;
int intgr = shrt;

// Explicit
shrt = (short) intgr;
Can assignment operators be overloaded directly?
No
What do operators is and as do?
as acts is like a cast but returns a null on conversion failure. Is comares an object to a type and returns a boolean.
What is the difference between the new operator and modifier?
The new operator creates an instance of a class whereas the new modifier is used to declare a method with the same name as a method in one of the parent classes.
Explain sizeof and typeof.
typeof obtains the System.Type object for a type and sizeof obtains the size of a type.
Contrast ++count vs. count++
Some operators have temporal properties depending on their placement. E.g.

double x;
x = 2;
Console.Write(++x);
x = 2;

Console.Write(x++);
Console.Write(x);

Returns
323
What are the names of the three types of operators?
Unary, binary, and conversion.
An operator declaration must include a public and static modifier, can it have other modifiers?
No
Can operator parameters be reference parameters?
No
Arithmetic:
Logical (boolean and bitwise):
String concatenation:
Increment, decrement:
Shift:
Relational:
Assignment:
Member access:
Indexing:
Cast:
Conditional:
Delegate concatenation and removal:
Object creation:
Type information:
Overflow exception control:
Indirection and Address:
Arithmetic: +
Logical (boolean and bitwise): &
String concatenation: +
Increment, decrement: ++
Shift: >>
Relational: ==
Assignment: =
Member access: .
Indexing: []
Cast: ()
Conditional: ?:
Delegate concatenation and removal: +
Object creation: new
Type information: as
Overflow exception control: checked
Indirection and Address: *
What does operator order of precedence mean?
Certain operators are evaluated before others. Brackets help to avoid confusion.
What is special about the declaration of relational operators?
Relational operators must be declared in pairs.
What operators cannot be overloaded?
=, ., ?:, ->, new, is, sizeof, typeof
What is an exception?
A runtime error.
Can C# have multiple catch blocks?
Yes
Can break exit a finally block?
No
Can Continue exit a finally block?
No
What are expression and declaration statements?
Expression – produces a value e.g. blah = 0
Declaration – e.g. int blah;
A block contains a statement list {s1;s2;} what is an empty statement list?
{;}
What is a dangling else?
if (n>0)
if (n2>0)
Console.Write("Dangling Else")
else
Is switch case sensitive?
Yes
Write some code for a for loop.
for (int i = 1; i <= 5; i++)
Console.WriteLine(i);
Can you increment multiple variables in a for loop control?
Yes
for (int i = 1; j = 2;i <= 5 ;i++ ;j=j+2)
Write some code for a while loop.
int n = 1;
while (n < 6){
Console.WriteLine("Current value of n is {0}", n);
n++;
}
Write some code for do… while.
int x;
int y = 0;

do{
x = y++;
Console.WriteLine(x);
}

while(y < 5);
Write some code for a custom collection class.
using System;
using System.Collections;

public class Items : IEnumerable{
private string[] contents;

public Items(string[] contents){
this.contents = contents;
}

public IEnumerator GetEnumerator(){
return new ItemsEnumerator(this);
}

private class ItemsEnumerator : IEnumerator{
private int location = -1;
private Items i;

public ItemsEnumerator(Items i){
this.i = i;
}

public bool MoveNext(){
if (location < i.contents.Length - 1){
location++;
return true;
}else{
return false;
}
}

public void Reset(){
location = -1;
}

public object Current{
get{return i.contents[location];}
}
}

static void Main(){
// Test
string[] myArray = {"a","b","c"};
Items items = new Items(myArray);

foreach (string item in items){
Console.WriteLine(item);
}

Console.ReadLine();
}
}
Describe Jump statements: break, continue, and goto.
Break terminates a loop or switch. Continue jumps to the next iteration of an enclosing iteration statement. Goto jumps to a labelled statement.
How do you declare a constant?
public const int c1 = 5;
What is the default index of an array?
0
What is array rank?
The dimension of the array.
Can you resize an array at runtime?
No
Does the size of an array need to be defined at compile time.
No
Write some code to implement a multidimensional array.
int[,] b = {{0, 1}, {2, 3}, {4, 5}, {6, 7}, {8, 9}};
Write some code to implement a jagged array.
// Declare the array of two elements:
int[][] myArray = new int[2][];

// Initialize the elements:
myArray[0] = new int[5] {1,3,5,7,9};
myArray[1] = new int[4] {2,4,6,8};
What is an ArrayList?
A data structure from System.Collections that can resize itself.
Can an ArrayList be ReadOnly?
Yes
Write some code that uses an ArrayList.
ArrayList list = new ArrayList();
list.Add("Hello");
list.Add("World");
Can properties have an access modifier?
Yes
Can properties hide base class members of the same name?
Yes
What happens if you make a property static?
They become class properties.
Can a property be a ref or out parameter?
A property is not classified as a variable – it can’t be ref or out parameter.
What is an accessor?
An accessor contains executable statements associated with getting or setting properties.
Can an interface have properties?
Yes
What is early and late binding?
Late binding is using System.object instead of explicitly declaring a class (which is early binding).
What is polymorphism?
Polymorphism is the ability to implement the same operation many times. Each derived method implements the operation inherited from the base class in its own way.
What is a nested class?
A class declare within a class.
What is a namespace?
A namespace declares a scope which is useful for organizing code and to distinguish one type from another.
Can nested classes use any of the 5 types of accessibility?
Yes
Can base constructors can be private?
Yes
object is an alias for what?
System.Object
What is reflection?
Reflection allows us to analyze an assembly’s metadata and it gives us a mechanism to do late binding.
What namespace would you use for reflection?
System.Reflection
What does this do?
Public Foo() : this(12, 0, 0)
Calls another constructor in the list
Do local values get garbage collected?
They die when they are pulled off the stack (go out of scope).
Is object destruction deterministic?
No
Describe garbage collection (in simple terms).
Garbage collection eliminates uneeded objects.
1. the new statement allocates memory for an object on the heap.
2. When no objects reference the object it may be removed from the heap (this is a non deterministic process).
3. Finalize is called just before the memory is released.
What is the using statement for?
The using statement defines a scope at the end of which an object will be disposed.
How do you refer to a member in the base class?
To refer to a member in the base class use:return base.NameOfMethod().
Can you derive from a struct?
No
Does C# supports multiple inheritance?
No
The C# keyword ‘int’ maps to which .NET type?
System.Int32
Which of these string definitions will prevent escaping on backslashes in C#?

1. string s = #”n Test string”;
2. string s = “’n Test string”;
3. string s = @”n Test string”;
4. string s = “n Test string”;
3. string s = @”n Test string”;
Which of these statements correctly declares a two-dimensional array in C#?

1.int[,] myArray;
2.int[][] myArray;
3.int[2] myArray;
4.System.Array[2] myArray;
1.int[,] myArray;
If a method is marked as protected internal who can access it?

1. Classes that are both in the same assembly and derived from the declaring class.
2. Only methods that are in the same class as the method in question.
3. Internal methods can be only be called using reflection.
4. Classes within the same assembly, and classes derived from the declaring class.
4. Classes within the same assembly, and classes derived from the declaring class.
What is boxing?

1) Encapsulating an object in a value type.
2) Encapsulating a copy of an object in a value type.
3) Encapsulating a value type in an object.
4) Encapsulating a copy of a value type in an object.
4) Encapsulating a copy of a value type in an object.
All classes derive from what?
System.Object
Is constructor or destructor inheritance explicit or implicit? What does this mean?
Constructor or destructor inheritance is explicit…. Public Extended : base()à this is called the constructor initializer.
Can different assemblies share internal access?
No
Can you inherit from multiple interfaces?
Yes
In terms of constructors, what is the difference between: public MyDerived() : base() an public MyDerived() in a child class?
Nothing
Can abstract methods override virtual methods?
Yes
What keyword would you use for scope name clashes?
this
Can you have nested namespaces?
Yes
What are attributes?
Attributes are declarative tags which can be used to mark certain entities (like methods for example).
Name 3 categories of predefined attributes.
COM Interop, Transaction, Visual Designer Component Builder