|
You can't do that. There's simply no way to express it. You could do it if TestClass1 and TestClass2 both extend some base class or implement some interface that TestClass3 does not, but you can't express a restriction "must be subtype of TypeA or of TypeB".
Here's what the standard[^] has to say about where:
We can supply an optional list of constraints for each type parameter. A constraint indicates a requirement
that a type shall fulfill in order to be accepted as a type argument. (For example, it might have to implement
a given interface or be derived from a given base class.) A constraint is declared using the word where,
followed by a type parameter and colon (: ), followed by a comma-separated list of constraints, which can
include a class type, interface types, other type parameters, the reference type constraint “class”, the value
type constraint “struct”, and the constructor constraint “new()”
The only thing that can do is express one or several constraints that must all be fulfilled.
|
|
|
|
|
As Harold says, you can't do that: you can declare an extension method that extends a single class (or interface) only, you can't "list" classes and expect it to work.
One way to do it, is to declare an empty interface and set the extension method to restrict to just those classes:
public interface IAllowThese { }
public static class ExtensionTest
{
public static String AAAAAAA<T>(this T x) where T : IAllowThese
{
return "Extension Method Test";
}
}
public class TestClass1 : IAllowThese { }
public class TestClass2 : IAllowThese { }
public class TestClass3 { }
You can then do this:
TestClass1 test1 = new TestClass1();
TestClass2 test2 = new TestClass2();
TestClass3 test3 = new TestClass3();
Console.WriteLine(test1.AAAAAAA());
Console.WriteLine(test2.AAAAAAA());
Console.WriteLine(test3.AAAAAAA());
And only the final one will give a compilation error.
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
That I get all puffed up like a fugu-fish in mating display, awarding myself a succulent lagniappe of self-esteem-bloat: thinking I was clever to be first out the gate; and, lo, the bird of time was on the wing thirty-four minutes before me, yet my sluggard browser revealed not the shadows of your wings.
And, now, alack, alas, I whimper: back in the sewer again; hearing the madding crowd snicker as they say: "look: a me-too."
I can't understand this long delay in forum messages being posted when other parts of CP seem to be updated instantly.
“But I don't want to go among mad people,” Alice remarked.
“Oh, you can't help that,” said the Cat: “we're all mad here. I'm mad. You're mad.”
“How do you know I'm mad?” said Alice.
“You must be," said the Cat, or you wouldn't have come here.” Lewis Carroll
|
|
|
|
|
Don't fret about it: it happened to you this time, It'll happen to me next time!
It's good to know we were thinking on the same wavelength: that validates the whole idea in a way.
I think all the hamsters are in transit to Puppy Bowl[^] which does slow the site down.
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
One solution would be to make Classes you want to use your extension inherit from a common Interface; Classes that don't inherit from that Interface will then throw a compile-time exception if you try and use the extension with them. Consider:
public interface UseAAAAAAA {}
public static class ExtensionTest
{
public static String AAAAAAA<T>(this T x) where T : UseAAAAAAA
{
return "Extension Method Test Class: " + typeof(T).Name;
}
}
public class Test1 : UseAAAAAAA {}
public class Test2 : UseAAAAAAA {}
public class Test3 {} Test:
Test1 test1 = new Test1();
Test2 test2 = new Test2();
Test3 test3 = new Test3();
Console.WriteLine(test1.AAAAAAA());
Console.WriteLine(test2.AAAAAAA());
“But I don't want to go among mad people,” Alice remarked.
“Oh, you can't help that,” said the Cat: “we're all mad here. I'm mad. You're mad.”
“How do you know I'm mad?” said Alice.
“You must be," said the Cat, or you wouldn't have come here.” Lewis Carroll
|
|
|
|
|
BillWoodruff wrote: inherit from a common Interface
A minor correction: you implement an interface, you don't inherit from it.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi Richard, I fall-on-my-face in abject shame, righteously corrected, good Sir ! You gotta admit, though, that using an empty Interface in this way is kind of ... kinky.
“But I don't want to go among mad people,” Alice remarked.
“Oh, you can't help that,” said the Cat: “we're all mad here. I'm mad. You're mad.”
“How do you know I'm mad?” said Alice.
“You must be," said the Cat, or you wouldn't have come here.” Lewis Carroll
|
|
|
|
|
The trick is not to be "writing" mulitple extension methods but to utilize operator overloading which still shares all of the common code.
private static void HelpFooBar<T>(T t) where T :baseclass
public static void FooBar(this RealObject o){
HelpFooBar(o);
}
public static void FooBar(this AnotehrObject o){
HelpFooBar(o);
}
Also, as previously mentioned. Class inheritance is the best way.
Even further if you "own" the classes just create a common interface.
|
|
|
|
|
hello
Console Application c# & use shape & color
|
|
|
|
|
If you really want to use shapes and colours, then a Console application is not the best solution: it is possible to draw shapes and so forth with a console app, but it's generally a lot more trouble than it's worth, since the console does not support and graphics methods directly and ASCII art is a PITA! Seriously, I'd look at a WinForms application as a minimum for anything more complex that basic text input / output (and even then, I'd probably want almost zero input with that.
Colours you can do: http://www.dotnetperls.com/console-color[^]
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
Your question is nota question. We really have no context information at all and no description of what you're really trying to do.
The other answer you got is about the best you can hope for given the complete lack of detail in your question. Keep this in mind when you ask questions in the future.
|
|
|
|
|
I am sorry brother, dint understand your question. It looked more like a puzzle. 
|
|
|
|
|
Given the lack of clarity in your question, the closest answer I can give you is this - a console output to draw a rectangle in a different colour using standard text characters.
public void PrintRectangle()
{
Console.BackgroundColor = ConsoleColor.Green;
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine("+----+");
Console.WriteLine("| |");
Console.WriteLine("| |");
Console.WriteLine("| |");
Console.WriteLine("| |");
Console.WriteLine("+----+");
Console.ResetColor();
}
|
|
|
|
|
I'm new to C#,am using form application, how i can store image into database and retrieve the image from database .
|
|
|
|
|
That's only been documented about 2,000,000 times on the web. All it takes is a little searching[^].
modified 30-Jan-14 15:57pm.
|
|
|
|
|
Hello friend,
It is the approach that matters: I agree to What ever Sir,Dave Kreskowiak just said. Now you want to ask "can i insert an image inside an SQL table?".
For that you need to think:
Step1: What is the datatype that you need to assign in the SQL table which can store images?
Or
Can i store images in an SQL table?.
Step2: When you get that data type, you need to find out, which querry can i use to do that?
Step3: HOw do i integrate it with c#?
Or
How can i create an "SQL Querry in C#"?
For that you need to think, write down your doubts and start searching. This is applicable even to me. When ever we get some task we write it down in our notepad.
Think about the prerequisites. Formulate a code.
After writting that code. If we are stuck somewhere we try to do brainstorming with our freinds. If none of them are able to solve the problem, then we come to code project and seek expert guidance.
The main thing is to start off. You havent taken any efforts to do so.
Thanks.
|
|
|
|
|
Nice approach to help beginners. +5. If you teach them how to think for themselves they will learn much quicker.
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
|
|
|
|
|
Yes Sir, thats true.
But they also need to start learning and reading. Every one wants short cuts. Well, thanks for supporting me.
When ever i say something like this people make fun, but eventually they follow the same path.
People ask: How to convert a datetime to string? and they get the answer by google searching.
But they dont think : why can i use ToString() on all the types, why can i not use ToByte()? is that type even present?
Self assessment is lacking these days Sir. I believe one must always carry a pen and a notebook. Comes handy while learning things.
When ever you get an exception write it down and search for it. There are many things .
I hope we speak again so that you enlighten me with your knowledge.
Thanks for supporting.
|
|
|
|
|
|
I want to develop a .net windows application with c# code behind to check the problems of a video file. If i give a video file as input to the tool, it should detect the problems of the video (like audio clarity, picture clarity, video struck etc.,).
Please help me.
|
|
|
|
|
This is not a trivial project; you will need to do some research into the subject, and how to identify and fix such issues.
Veni, vidi, abiit domum
|
|
|
|
|
So just how are you going to check the "audio quality" or the "video quality"?? are you checking the metadata properties of the video for certain values or are you actually trying to get the code to "listen" or "look at" the audio and video? If it's the later, good luck! You're going to need it because that is FAR from a simple thing to do.
|
|
|
|
|
|
Quote: Please help me.
Ok, what research have you done so far?
What about reading something related to Signal Processing, Sampling etc?
Did you start with:
1> Playing a video file(any file) in a form application on a button click(this may or may not be a correct approach, but something to start off with).
at least start with something. No task is impossible. I know it is tough. If one way fails there are multiple ways. At least start something.
|
|
|
|
|
Rahul VB wrote: No task is impossible
Really? I want to see you push a dead elephant to the top of Mount Everest, only yourself, with no lifting equipment.
Get started. I'll wait here.
|
|
|
|