using System;
class Factorial {
// This is a recursive function.
public int factR(int n) {
int result;
if(n==1) return 1;
result = factR(n-1) * n;
return result;
}
}
public class Recursion {
public static void Main() {
Factorial f = new Factorial();
Console.WriteLine("Factorials using recursive method.");
Console.WriteLine("Factorial of 3 is " + f.factR(3));
Console.WriteLine("Factorial of 4 is " + f.factR(4));
Console.WriteLine("Factorial of 5 is " + f.factR(5));
Console.WriteLine();
2) What is command line argument used within main in C#? What does it look like? I am a visual learner, so it would be great if there are pictures or something for illustration. here is the code:
class TestClass
{
static void Main(string[] args)
{
// Display the number of command line arguments:
System.Console.WriteLine(args.Length);
}
}
3) So far I am on the static constructor and method, but I already find it confusing to understand. As far as I know, you cannot use static members, methods and so on with instance method and variables. YOU can only use them separately or if you want to use them together, you must create an object. So for instance,
public static void staticMeth (MyClass ob) {
ob.NonStaticMeth();
} // This one is ok since we already have an object named ob and NonStaticMeth is an instance method.
However, I don't really understand this code here because public CountInst is a instance constructor and not a static constructor but how come that works? Isn't that the static and the instance have to work independently and separately and they cannot be used together without the object being qualified?
// Use a static field to count instances.
using System;
class CountInst {
static int count = 0;
// increment count when object is created
public CountInst() {
count++;
}
// decrement count when object is destroyed
~CountInst() {
count--;
}
public static int getcount() {
return count;
}
}
public class CountDemo {
public static void Main() {
CountInst ob;
for(int i=0; i < 10; i++) {
ob = new CountInst();
Console.WriteLine("Current count: " +
CountInst.getcount());
}
}
}

New Topic/Question
Reply




MultiQuote





|