I am trying to extend the System.String
and add one more static field String.Space
which represents a single space " "
just like how Microsoft implemented String.Empty
in the namespace Using System;
. I am not able to implement this extension please help.
I wanted to implement a kind of field extension...
wanted the result like
5 Answers
Answers 1
You could have a static class
like so;
public static class Strings { public const string Space = " "; }
And use it in your code like;
FullName = FirstName + Strings.Space + MiddleName + Strings.Space + LastName;
However, the ultimate goal is to make the code maintainable, be reasonably efficient and reduce the likelihood of errors. With that in mind, it's best to reduce the number of concatenations and supply just the values that are needed. To that end a simple format string would be a cleaner approach; (pre C#6);
FullName = string.Format("{0} {1} {2}", FirstName, MiddleName, LastName);
And if C#6+;
FullName = $"{FirstName} {MiddleName} {LastName}";
Why would you use those last two approaches? Well, it comes down to separation of concerns. Leave the string formatting to string.Format()
and simply supply the values you need. You reduce the likelihood of errors by having fewer components in the mix, decrease maintenance overhead by the same and increase efficiency by reducing the number of strings involved to perform the concatenation.
Answers 2
Unfortunately the framework (v4.7 at the time of this writing) does not currently support extension properties/fields. See a good write up of this here "Why No Extension Properties?"
Which means you are limited to using an extension method. One such example:
namespace System.String { public static class Extensions { public static string Space(this string s) { return " "; } } }
Extension methods require an instance of an object. So it can be used as such:
// Returns " " var space = "".Space();
Or
// Returns " " var space = String.Empty.Space();
This falls into one of those "It's not pretty, but it works" categories.
Answers 3
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace StringFunctions.Extensions { public static class StringExtensions { public static readonly string Space = " "; public static string Spacemethod(this string name) { return string.Concat(name," "); } } }
in the place use the Alias name of class String. Like
using String = StringFunctions.Extensions.StringExtensions;
small string it's reserved word, so, it's throw Error.if you getting String.Space w=it's work entire places of class, other string function call like string.Empty
Your functions call String.Space
like this
using String = StringFunctions.Extensions.StringExtensions; namespace StringFunctions { class Program { static void Main(string[] args) { var t = String.Space; } } }
Answers 4
I was able to do the following. You'll get a warning, but it compiles.
namespace System { public static partial class String { public static string Space = " "; } }
To call:
string sp = String.Space;
Answers 5
There are different variations and opinions from others between constants and not. As far as attempting to hijack/add functionality onto the String
class itself, I would tend to not do that.
Instead, create your own string helpers class.
public static class Strings { public const string Space = @" "; }
Or
public static class Strings { public static string Space => @" "; }
Then the usage is like
//Usage public class Test { public void Go() { string m = Strings.Space; } }
0 comments:
Post a Comment