Written by Riley Perry from Distributed Development
OK – here we go,
the answers to the first 173.
1. Name 10 C# keywords.
abstract, event, new, struct,
explicit, null, base, extern, object, this
2. What is public accessibility?
There are no access restrictions.
3. What is protected accessibility?
Access is restricted to types derived
from the containing class.
4. What is internal accessibility?
A member marked internal is only accessible from files within the
same assembly.
5. What is protected internal accessibility?
Access is restricted to types derived
from the containing class or from files within the same assembly.
6. What is private accessibility?
Access is restricted to within the
containing class.
7. What is the default accessibility for a class?
internal for a top level class,
private for a nested one.
8. What is the default accessibility for members of an
interface?
public
9. What is the default accessibility for members of a
struct?
private
10. Can the members of an interface be private?
No.
11. Methods must declare a return type, what is the keyword used
when nothing is returned from the method?
void
12. Class methods to should be marked with what keyword?
static
13. 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");
}
}
14. 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)
{
}
15. Does an object need to be made to run main?
No
16. 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");
}
}
}
17. What are the two return types for main?
void and int
18. What is a reference parameter?
Reference parameters reference the original object
whereas value parameters make a local copy and do not affect the original. Some
example code is shown:
using System;
namespace Console1
{
class
Class1
{
static void Main(string[] args)
{
TestRef
tr1 = new TestRef();
TestRef
tr2 = new TestRef();
tr1.TestValue
= "Original value";
tr2.TestValue
= "Original value";
int tv1 = 1;
int tv2 = 1;
TestRefVal(ref tv1, tv2, ref
tr1, tr2);
Console.WriteLine(tv1);
Console.WriteLine(tv2);
Console.WriteLine(tr1.TestValue);
Console.WriteLine(tr2.TestValue);
Console.ReadLine();
}
static public void TestRefVal(ref int tv1Parm,
int tv2Parm,
ref TestRef tr1Parm,
TestRef
tr2Parm)
{
tv1Parm = 2;
tv2Parm
= 2;
tr1Parm.TestValue
= "New value";
tr2Parm.TestValue
= "New value";
}
}
}
class TestRef
{
public
string TestValue;
}
The output for this is:
2
1
New value
New value
19. 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.
20. 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());
}
}
}
}
21. What is an overloaded method?
An overloaded method has multiple signatures that are
different.
22. What is recursion?
Recursion is when a method calls itself.
23. What is a constructor?
A constructor performs initialisation for an object
(including the struct type) or class.
24. If I have a constructor with a parameter, do I need to
explicitly create a default constructor?
Yes
25. 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.
26. Can you use access modifiers with destructors?
No
27. 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.
28. Write some code to use a delegate.
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);
}
}
}
using System;
namespace Console1
{
class
Class1
{
delegate void
myDelegate(int parameter1);
static void
AStaticMethod(int param1)
{
Console.WriteLine(param1);
}
static void Main(string[] args)
{
MyClass
myInstance = new MyClass();
myDelegate
d = null;
d +=
new myDelegate(myInstance.AMethod);
d +=
new myDelegate(AStaticMethod);
d(1);
//both functions will be run.
Console.ReadLine();
}
}
class
MyClass
{
public void AMethod(int param1)
{
Console.WriteLine(param1);
}
}
}
29. What is a delegate useful for?
The main reason we use delegates is
for
use in event driven programming.
30. What is an event?
See 32
31. Are events synchronous of asynchronous?
Asynchronous
32. Events use a publisher/subscriber model. What is that?
Objects publish events to which other
applications subscribe. When the publisher
raises an event all subscribers to that event are notified.
33. Can a subscriber subscribe to more than one publisher?
Yes, also - here's some code for a publisher with multiple subscribers.
using System;
namespace Console1
{
class
Class1
{
delegate void
myDelegate(int parameter1);
static event
myDelegate myEvent;
static void
AStaticMethod(int param1)
{
Console.WriteLine(param1);
}
static void Main(string[] args)
{
MyClass
myInstance = new MyClass();
myEvent
+= new myDelegate(myInstance.AMethod);
myEvent
+= new myDelegate(AStaticMethod);
myEvent(1);
//both functions will be run.
Console.ReadLine();
}
}
class
MyClass
{
public void AMethod(int param1)
{
Console.WriteLine(param1);
}
}
}
Another example:
using System;
using System.Threading;
namespace EventExample
{
public
class Clock
{
public delegate void TwoSecondsPassedHandler(object
clockInstance, TimeEventArgs time);
//The clock publishes an event that others subscribe to
public event
TwoSecondsPassedHandler TwoSecondsPassed;
public void Start()
{
while(true)
{
Thread.Sleep(2000);
//Raise event
TwoSecondsPassed(this, new
TimeEventArgs(1));
}
}