This question already has answers here:
C# extend indexer?
(6 answers)
Closed 1 year ago.
I am intrested in creating custom indexer for char[,]. It seems to me that it is prohibited, but I am wondering if there is an oportunity. The code that could have solved the problem is:
public static class GeneratorHelpers
{
public static char int[Vector2D position] (this char[,] field)
{
return field[position.X, position.Y];
}
}
The above code does not compile.
Currently, there is no such things as extension properties, operators or indexers in the C# language. The most common pattern for this would be just to take additional parameters to an extension method.
Related
This question already has answers here:
Can anonymous class implement interface?
(9 answers)
Closed 6 years ago.
In Java, assuming that class A is an interface/abstract class, I can do the following:
callMethod(new A(){
public void myFunc(){
System.out.println("test");
}
});
How can I achieve the same shortcut effect in C# without having to declare a class seperately .
Thanks
You can't.
C# does not allow you to implement interfaces on Anonymous Types.
This question already has answers here:
Add new property to string class C# [duplicate]
(3 answers)
Closed 7 years ago.
I don't like the way string behaves and there are a few other things I would like to change.
It appears string cannot be extended because it is a sealed class.
Is there another way? I could copy the source code and make my own class but then it wouldn't be compatible with string, or could I make it compatible?
You could use extension methods to extend String. The link below explains extension methods and has an example of how to add a WordCount() function to String.
https://msdn.microsoft.com/en-us/library/bb383977.aspx
This question already has answers here:
labeling a group of members as private/public in c#
(3 answers)
Closed 9 years ago.
Is there a way in C# to declare a group of variables and methods as private or public like in C++ (example below). I am just trying to avoid typing a million "public"s and "private"s.
class Foo
{
private:
int Alpha;
string Dog;
public:
bool Bites;
bool Bad;
}
I keep getting an error in C# and have exhausted my internet search abilities. Thanks
No. You need to specify visibility for each member.
private is default for members, so it is safe to omit it (unless your coding guidelines tell you must specify). More details/links - Default visibility for C# classes and members (fields, methods, etc)?
This question already has answers here:
Can I add extension methods to an existing static class?
(18 answers)
Closed 9 years ago.
How Can I Make Extension Method For System.IO.Path Class what i mean that i need something like below:
Path.GetExtension(sFilePath)
i want to make method:
Path.GetMimeType(sFilePath)
Extension Method:
public static string GetMIMEType(this Path sPath,string sFilePath)
{
string sExtension = Path.GetExtension(sFilePath).ToLowerInvariant();
if (sExtension.Length > 0 && dicMIMETypes.ContainsKey(sExtension.Remove(0, 1)))
{
return dicMIMETypes[sExtension.Remove(0, 1)];
}
return "unknown/unknown";
}
but when compile above code method get error ('System.IO.Path': static types cannot be used as parameters).
Thanks All,
Path is static, and therefore you cannot create an extension method for it. Extension methods require an instance of an object.
This feature is currently not available. you can add extension methods only to an Instance not to Class itself(static).
Feature request already given to Microsoft Static extension methods in C# 4.0
it is not implemented and They may consider it in future or may not.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Check if a class is derived from a generic class
How can I test if a type if some kind of List<>? Eg. as the three classes below are.
public class Words : List<string>
{
}
public class Numbers : List<double>
{
}
public class BigWords : Words
{
}
I tried
typeof(List<DateTime>).IsSubclassOf(typeof(List<>))
typeof(Words).IsSubclassOf(typeof(List<>))
typeof(Numbers).IsSubclassOf(typeof(List<>))
typeof(BigWords).IsSubclassOf(typeof(List<>))
But that returned False four times. I expected True for each.
I expect this question has been answered else where, but I failed to find it, because I found the language of inheritance, polymorphism and generics can be confusing.
Look at Type.IsAssignableFrom method.