Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Extension Methods only visible and accessible within one class ("private")

Is it possible, in C#, to create extension methods on a class but restrict visibility/accessibility within a class? (e.g. Extension Method A on class M is only accessible within class Z)

Example:

class A
{
     String foo = "";
     String bar = foo.MakeMillionaire("arg");
}

In above example I want the extension method "MakeMillionaire" extending the String class only to be visible and accessible within class A. Can I do this somehow by defining the extension method in a static class within class A?

Edit: Trying a regular nested class yields "Error: Extension methods must be defined in a top level static class".

like image 514
Alex Avatar asked Sep 13 '09 02:09

Alex


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

Extension methods can only be defined in a static non-generic outer (non-nested) class.

What I usually do in such scenarios is make a separate static internal class in a different namespace in the same file, then include that namespace only in that file.

It would still be visible to other classes in that assembly; the only way to avoid that is to move the consuming class (class A in your example) to its own assembly, which you probably don't want to do.

like image 108
SLaks Avatar answered Oct 20 '22 02:10

SLaks


Declare your Extension Methods in a separate namespace, and you can include that namespace in specific files that you want to use them. Then, declare ClassA (the class you want to use your extension methods in) in a separate file, and use that namespace at the top of ClassA.cs. That way, only that class will have access to those extension methods.

Edit:

Something like the following

namespace Extension {
    public static class ExtensionMethods {
        public static string EnumValue(this MyEnum e) {
            switch (e) {
                case MyEnum.First:
                    return "First Friendly Value";
                case MyEnum.Second:
                    return "Second Friendly Value";
                case MyEnum.Third:
                    return "Third Friendly Value";
            }
            return "Horrible Failure!!";
        }
    }
}

ClassA.cs:

using Extension;

public class ClassA{
    //Work your magic here, using the EnumValue Extension method
    //wherever you want
}

ClassB.cs

public class ClassB{
    //EnumValue is not a valid Extension Method here.
}
like image 44
Mark Carpenter Avatar answered Oct 20 '22 04:10

Mark Carpenter