Click here to Skip to main content
15,888,590 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello I would like to create a class in C# with some functions extending char class.
Example we have some methods like char.IsUpper char.IsLower and I need to create some additional methods and functions in order to be able to use it every time I write source code for any application it's need.
I know that I can't inherit from a type. Could anyone guide me how to achieve that? Any help will be much appreciated. Thank you so much indvanced.

What I have tried:

class Character : System.Char
    {

    }
Posted

You are correct in stating that you cannot directly inherit from value types like 'char' or 'int'. You can however create extension methods to add new functionality to these types by using a static class, for example, 'CharacterExtensions'. Inside this class, define static extension methods that take the 'char' type as the first parameter and the 'this' modifier. As an example -
C#
public static class CharacterExtensions
{
    public static bool IsVowel(this char c)
    {
        return "AEIOUaeiou".IndexOf(c) != -1;
    }

    public static bool IsConsonant(this char c)
    {
        return !c.IsVowel() && char.IsLetter(c);
    }
}


In the above I have defined two extension methods - 'IsVowel' and 'IsConsonant'. These methods can be called on any 'char' instance as if they were instance methods of the char type. You can use this as in -
char letter = 'a';
bool isVowel = letter.IsVowel(); //Returns true...
bool isConsonant = letter.IsConsonant(); //Returns false...

letter = 'z';
isVowel = letter.IsVowel(); //Returns false...
isConsonant = letter.IsConsonant(); //Returns true...


You can now add similar to your static class to be used when needed.
 
Share this answer
 
Comments
Aggeliki Asimakopoulou 29-Mar-24 7:44am    
Thank you so much, I have two questions. First question: How does the system recognizes IsVowel is a methond of char? Because letter is a char? Does it come the argument this char c? And why do we use "this" in argument declaration?
And second question: Is there a way to be compilied through an assembly to include it to library in order to use it for any application that we write source code depending on the needs of course?
Thank you so much!!!
Andre Oosthuizen 29-Mar-24 8:56am    
You're welcome.

The system recognizes 'IsVowel' as a method of char because of the way extension methods are designed - MS Learn | Extension Methods[^]

When you call 'letter.IsVowel()', your compiler will do the following -

It looks for an instance method named 'IsVowel' in the char type. Since char is a value type and has no instance methods other than those inherited from 'System.ValueType', it doesn't find an instance method named 'IsVowel'.
It will then look for a static method named 'IsVowel' in the same class or a static extension method that has this char as the first parameter and is defined in the current scope or a referenced namespace as I did in the 'CharacterExtensions' class.
If it finds the 'IsVowel' extension method in the 'CharacterExtensions' class, the first parameter will be this char c.
The 'this' keyword in the parameter list of an extension method is what enables the compiler to treat the method as an instance method (see link above) when called on an instance of the extended type (in this case, char).

When you use 'letter.IsVowel()', the compiler translates it to a static method call. The 'this' keyword is thus used to specify that the first parameter of your extension method represents the instance of the type being extended. It allows the extension method to be called using the instance method syntax (letter.IsVowel()), while your compiler treats it as a static method call (CharacterExtensions.IsVowel(letter)), trust it all makes sense as I am no teacher. :)

2) Yes, you can compile into a separate DLL and then reference that assembly in any application where you need to use those extension methods.
Aggeliki Asimakopoulou 29-Mar-24 9:27am    
Thank you so much, for all the explation and for the link too. A thought came into my mind, that all this procedure that the interpreter looks firstly to the char type and secondly to the extended class may take a long time procedure, could we use async await to extended class methods in order to gain execution time? Or would we able to avoid looking at the char type and force the interpreter to look immediately at the extended method? That's very interesting.
Ralf Meier 31-Mar-24 17:33pm    
+5
Andre Oosthuizen 1-Apr-24 4:09am    
Thank you Ralf!
C#
using static System.Char;
public class Character
{
    //building off of Accepted solution
    const string Vowels = "AEIOUaeiou";

    //the field represented by the implicit operators
    protected char value;
    public static implicit operator char(Character @char)
    {
        return @char.value;
    }
    public static implicit operator Character(char @char)
    {
        return new Character() { value = @char };
    }
    public bool IsVowel => Vowels.IndexOf(this) != -1;
    public bool IsConsonant => !(this.IsVowel) && IsLetter(this);
}
 
Share this answer
 
v6
Comments
Dave Kreskowiak 30-Mar-24 14:43pm    
Did you want to explain what this does so the OP has a clue or what? Because as it stands right now, this is an unexplained code snippet that does not answer the OP's question in any way whatsoever.
Aggeliki Asimakopoulou 1-Apr-24 4:35am    
I don't understand what @ does and I don't understand how does it work? May you explain me please?
Thank you so much.
charles henington 1-Apr-24 12:05pm    
The @ sign just allows you to use a reserved keyword as a variable name, no different than _char. The implicit operators allow you to use the Character class as if it implements char and will represent the value held in the value field. You can read more on implicit and explicit operators at https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/user-defined-conversion-operators
charles henington 4-Apr-24 23:23pm    
Updated. Hope that helps.
Aggeliki Asimakopoulou 5-Apr-24 10:40am    
Yes it does! I am just not very familiar with this type of code. Thank you so much!!!

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