473,396 Members | 1,970 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Generics Question

Hello,

I may be mis understanding some of the uses of generics so this example may
not be valid, but here goes.

I have a method that I wont to be able to return a string or byte array, it
declared as :

public T Decrypt<T>(byte[] cipherText, byte[] optionalEntropy)

how do I know what type T is, I want todo something like :

if (T is string )
{
return (T)(object)Encoding.ASCII.GetString(plainText);
}
else
{
return (T)(object)plainText;
}
Also, I want to limit T to string or byte[], can this be done.

Steve
Oct 20 '05 #1
1 944

"Steve Drake" <Steve@_NOSPAM_Drakey.co.uk> wrote in message
news:OA**************@TK2MSFTNGP09.phx.gbl...
Hello,

I may be mis understanding some of the uses of generics so this example
may not be valid, but here goes.

I have a method that I wont to be able to return a string or byte array,
it declared as :

public T Decrypt<T>(byte[] cipherText, byte[] optionalEntropy)

how do I know what type T is, I want todo something like :

if (T is string )
{
return (T)(object)Encoding.ASCII.GetString(plainText);
}
else
{
return (T)(object)plainText;
}


The typeof operator can be used on generic type parameters, meaning
typeof(T) == typeof(string) if the parameter is string.
Also, I want to limit T to string or byte[], can this be done.


No, you'd probably be best off writing a common method that does all the
grunt work and a companion method that converts it to a string:

byte[] Decrypt(byte[] cipherText, byte[] optionalEntropy);
string[] DecryptString(byte[] cipherText, byte[] optionalEntropy)
{
byte[] plainText = Decrypt(cipherText,optionalEntropy);
return Encoding.ASCII.GetString(plainText);
}

This is probably easier for the end user to understand and doesn't give you
any issues when someone passes System.Windows.Forms.ListBox as the type
parameter.
Oct 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Fred | last post by:
Is there a way to use generics with vs.net 2003?? I was searching around and I saw gyro but does it works with vs.net 2003?? if it does how am I suppouse to install it. If it doesnt is there...
27
by: Bernardo Heynemann | last post by:
How can I use Generics? How can I use C# 2.0? I already have VS.NET 2003 Enterprise Edition and still can´t use generics... I´m trying to make a generic collection myCollection<vartype> and...
12
by: Michael S | last post by:
Why do people spend so much time writing complex generic types? for fun? to learn? for use? I think of generics like I do about operator overloading. Great to have as a language-feature, as...
13
by: Luc Vaillant | last post by:
I try to compare to values of generic value type T in a generic class as follow: public class C<T> where T : struct { private T value1; private T value2; C(T value1, T value2) {
1
by: Peter Kirk | last post by:
Hi I have never used generics before, and I was wondering if the following sort of use was acceptable/normal for a method: public IList<IPerson> GetPersons() { IList<IPerson> personList =...
18
by: riftimes | last post by:
Hello, would you help me to find totorials with examples about generics and Dictionary thank you.
11
by: hammad.awan_nospam | last post by:
Hello, I'm wondering if it's possible to do the following with Generics: Let's say I have a generic member variable as part of a generic class like this: List<DLinqQuery<TDataContext>>...
1
by: Kevin S. Goff | last post by:
Hi, all, Hopefully this will make sense: I have 2 classes that implement the same generic interface. public interface IAgingReport<T> { T GetAgingReport(DateTime dAsOfDate); }
7
by: SpotNet | last post by:
Hello NewsGroup, Reading up on Generics in the .NET Framework 2.0 using C# 2005 (SP1), I have a question on the application of Generics. Knowingly, Generic classes are contained in the...
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.