#Extension Method exercise problem [C#]
6 messages · Page 1 of 1 (latest)
That's the C# track, right?
This concept exercise Log Analysis is about "extension methods", a feature of C# that allows you to add new functions to a class that has been defined elsewhere.
It's explained at the beginning of the page:
About Extension Methods
Extension methods allow adding methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. They are defined as static methods but are called by using instance method syntax. Their first parameter is preceded by the this modifier, and specifies which type the method operates on, and are brought into scope at the namespace level.
public static int WordCount(this string str)
{
return str.Split().Length;
}
"Hello World".WordCount();
// => 2
You can see that the example defines an extension method WordCount for the built-in type string, without modifying the original class.
In the same way the tasks 1-4 of the exercise want you to define four extension methods for string, too.
the error message says "LogAnalysisTests.cs(11,62): error CS1501: No overload for method 'SubstringAfter' takes 1 arguments" but the assignment states: "Implement the (static) LogAnalysis.SubstringAfter() extension method, that takes in some string delimiter and returns the substring after the delimiter.". So now I am just simply confused as to what I am supposed to do
Take a look at the example for task 1:
var log = "[INFO]: File Deleted.";
log.SubstringAfter(": "); // => returns "File Deleted."
The function operates on two strings: the log ("[INFO]: File Deleted.") and the substring (: "). Therefore your SubstringAfter needs two parameters, this string str and string substring.
oh ok thanks! Ill give it another try
Extension Method exercise problem [C#]