Implement an interface while calling a method (inline) [duplicate] - c#

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.

Related

Is it possible to create indexer in helper class c# [duplicate]

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.

In C# when using generics, and using the constraint where T : new() can the new() operator ever have a parameter? [duplicate]

This question already has answers here:
Is there a generic constructor with parameter constraint in C#?
(9 answers)
Closed 1 year ago.
I am trying to do something like this:
where DataTableLoader2 is a generic helper class
public static class DataTableLoader2 <T> where T : class, new (StringComparer)
Thanks for any help offered....
K
No, it can't. The purpose of new() is simply to enforce that T must have a default, parameterless constructor. If you want to enforce a generic having some common initialization then you should do it through either a base class, or an interface.

How to set interface implementation in generic interface? [duplicate]

This question already has answers here:
How can I use interface as a C# generic type constraint?
(11 answers)
Closed 3 years ago.
I would like to write generic interface
interface IFoo<TYPE>{}
But also I would like to restrict this TYPE like this (for example)
interface IFoo<TYPE: MyAnotherClass>{}
So, it is means that I don't want that user will pass any TYPE that don't implement MyAnotherClass
How to achieve this behavior in C#?
P.S. In Java(for example) it is possible...
interface IFoo<T> where T : MyAnotherClass {
}

Is it necessary to explicitly inherit from object? [duplicate]

This question already has answers here:
Redundant to inherit from Object in C#?
(7 answers)
Closed 7 years ago.
In this MSDN example, the class explicitly inherits from Object:
class Point: Object {
// ...
}
Is explicitly inheriting from Object ever necessary? That is, is it not equivalent to the following?
class Point: Object {
// ...
}
No, it is implicitly inherited. This is why every class in C# has a .ToString() and .Equals().
https://msdn.microsoft.com/en-us/library/vstudio/system.object%28v=vs.100%29.aspx
Object class: "This is the ultimate base class of all classes in the .NET Framework; it is the root of the type hierarchy."
This type of inheritance where everything is derived from a single class is called a "unified type system"

How to extend the string class in c# to change its behavior? [duplicate]

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

Categories

Resources