Click here to Skip to main content
15,902,813 members
Please Sign up or sign in to vote.
2.00/5 (4 votes)
See more:
Can anyone explain this codin step by step?

Here is the coding :

C#
using System;

delegate string StrMod(string str);

class MainClass {

  static string replaceSpaces(string a) {
    Console.WriteLine("replaceSpaces");
    return a;
  }

  static string removeSpaces(string a) {
    Console.WriteLine("removeSpaces");
    return a;
  }

  static string reverse(string a) {
    Console.WriteLine("reverseSpaces");
    return a;
  }

  public static void Main() {
    StrMod strOp = new StrMod(replaceSpaces);
    string str;

    str = strOp("This is a test.");

    strOp = new StrMod(removeSpaces);
    str = strOp("This is a test.");

    strOp = new StrMod(reverse);
    str = strOp("This is a test.");
  }
}
Posted
Updated 29-Nov-11 21:15pm
v2

Do you have any idea how much work explaining code line by line is?
Every single line needs a paragraph of explanation! For example:
int next = r.Next();

Create a new variable called "next" which can hold a integer value. From the previously declared Random instance "r", call the "Next" method to get a new random number, and assign it to the "next" variable.

Can you imagine how long it would take us to explain even a very short code fragment like your example, line by line?

No. It is not going to happen. If you have a specific problem, then ask a question about it. But think first - would you want to sit down for 45 minutes and type up a line-by-line description for no good reason?
 
Share this answer
 
Comments
sathiyak 30-Nov-11 3:23am    
ok....understood...
I'll give you a couple of ideas by pointing you to the interesting parts in the code fragment you posted:

C#
using System;
// Here a type is declared that is something akin to a pointer to
// 
  // This function does not really replace spaces, but only writes output to the console and returns
  // the original string. 
  static string replaceSpaces(string a) {
    Console.WriteLine("replaceSpaces");
    return a;
  }

  // This is ^__b style="color:red"__^a function that returns a string and takes a string as a parameter</b__^
  // This function does not really remove spaces, but only writes output to the console and returns
  // the original string.
  static string removeSpaces(string a) {
    Console.WriteLine("removeSpaces");
    return a;
  }

  // This is ^__b style="color:red"__^a function that returns a string and takes a string as a parameter
  // This function does not really reverse a string, but only writes output to the console and
  // returns the original string.
  static string reverse(string a) {
    Console.WriteLine("reverseSpaces");
    return a;
  }

  public static void Main() {
    // After this line ^__b__^strOp can be called like a function that returns a string and takes a string as a parameter.
    // The dummy function ^__b__^replaceSpaces will be called.
    StrMod strOp = new StrMod(replaceSpaces); 
    string str;
    str = strOp("This is a test.");
    
    // After this line ^__b>strOp can be called like a function that returns a string and takes a string as a parameter.
    // The dummy function ^__b>removeSpaces will be called.
    strOp = new StrMod(removeSpaces);
    str = strOp("This is a test.");

    // After this line ^__b>strOp can be called like a function that returns a string and takes a string as a parameter.
    // The dummy function ^__b>reverse will be called.
    strOp = new StrMod(reverse);
    str = strOp("This is a test.");
  }
}


Regards,

—MRB
 
Share this answer
 
v2
Comments
fjdiewornncalwe 30-Nov-11 9:33am    
+5. Very nice considering the potential to do too much like Griff explains.
Manfred Rudolf Bihy 30-Nov-11 10:41am    
It was an attempt to make OP see the pattern behind the code. That's the reason I used the "fancy" colouring ;).
Thanks for the vote!
BillWoodruff 30-Nov-11 12:44pm    
+5 A very generous response to a question which reeks of "homework assignment."
I'm sure many people here could, but why would they?
You've made no attempt to explain what part of it you don't understand, or why you need to.

It smacks to me of a "Please do my homework for me" question...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900