473,385 Members | 1,445 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,385 software developers and data experts.

Generics Question.

This is just a thought, may be its not possible , but is it possible to
use generics in method declarations as return type arguments..

for example

public <T> Load(T value)
{
//method body
}

this would specify that the type passed would be type returned...

so at compile time, the compiler would do a static type checking to see
if there are compilation issues...
for exampple
Address addr = DataStore.Load(typeof(Address));


Dec 19 '05 #1
8 1182
Hello!

Yes, you can use the type as the return type for the method and have the
compiler check for conversion errors.

You may also use the "where" keyword to access the interface of the type in
the method body.

public T Load(T value) where T : CustomType, ICustomInterface
{
//method body

return value;
}

--
With regards
Anders Borum / SphereWorks
Microsoft Certified Professional (.NET MCP)
Dec 19 '05 #2
Ashish,

Yes, it is, you would declare it like this:

public T Load<T>(T value)

However, you could not call it like you would. You would have to do
this to call it:

Address addr = DataStore.Load<Address>(addrInstance);

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ashish" <as*****@thisisjunk.com> wrote in message
news:Og**************@TK2MSFTNGP10.phx.gbl...
This is just a thought, may be its not possible , but is it possible to
use generics in method declarations as return type arguments..

for example

public <T> Load(T value)
{
//method body
}

this would specify that the type passed would be type returned...

so at compile time, the compiler would do a static type checking to see if
there are compilation issues...
for exampple
Address addr = DataStore.Load(typeof(Address));

Dec 19 '05 #3
Nicholas, please contact me privately by e-mail.

Thanks in advance.

--
With regards
Anders Borum / SphereWorks
Microsoft Certified Professional (.NET MCP)
Dec 19 '05 #4
"Ashish" <as*****@thisisjunk.com> a écrit dans le message de news:
Og**************@TK2MSFTNGP10.phx.gbl...

| This is just a thought, may be its not possible , but is it possible to
| use generics in method declarations as return type arguments..
|
| for example
|
| public <T> Load(T value)
| {
| //method body
| }
|
| this would specify that the type passed would be type returned...

The above code will not compile as you are not specifying a return type. You
need to change it but...

| so at compile time, the compiler would do a static type checking to see
| if there are compilation issues...
|
| for exampple
|
| Address addr = DataStore.Load(typeof(Address));

IF this is what you want to achieve, then you need to do something more like
this :

public T Load<T>()
{
// method body
}

Used like this :

Address addr = DataStore.Load<Address>();

You do not need to pass any parameters to the function, just substitute the
generic parameter to strictly type the method.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Dec 19 '05 #5
thanks guys,
that was exactly what i was looking for !

Joanna Carter [TeamB] wrote:
"Ashish" <as*****@thisisjunk.com> a écrit dans le message de news:
Og**************@TK2MSFTNGP10.phx.gbl...

| This is just a thought, may be its not possible , but is it possible to
| use generics in method declarations as return type arguments..
|
| for example
|
| public <T> Load(T value)
| {
| //method body
| }
|
| this would specify that the type passed would be type returned...

The above code will not compile as you are not specifying a return type. You
need to change it but...

| so at compile time, the compiler would do a static type checking to see
| if there are compilation issues...
|
| for exampple
|
| Address addr = DataStore.Load(typeof(Address));

IF this is what you want to achieve, then you need to do something more like
this :

public T Load<T>()
{
// method body
}

Used like this :

Address addr = DataStore.Load<Address>();

You do not need to pass any parameters to the function, just substitute the
generic parameter to strictly type the method.

Joanna

Dec 20 '05 #6
Ahsish... The general form is:

// generic method to invoke
public interface IInvoke<R,P>
{
R Invoke(P p);
}
public delegate R DInvoke<R,P>(P p);

// generic collection
public class JALGenericCollection<T,R,P> : Disposable, IInvoke<R,P>
where T : IDisposable, IInvoke<R,P>

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Dec 20 '05 #7
Nicholas Paldino [.NET/C# MVP] wrote:
Ashish,

Yes, it is, you would declare it like this:

public T Load<T>(T value)

However, you could not call it like you would. You would have to do
this to call it:

Address addr = DataStore.Load<Address>(addrInstance);

Hope this helps.


No, you certainly *can* call as if there was no template. The following
works just fine (not useful, but an example that just came to my mind):

class Program
{
static double ToDoubleInvar<T>(T val) where T : IConvertible
{
return
val.ToDouble(System.Globalization.CultureInfo.Inva riantCulture);
}

static void Main(string[] args)
{
Console.WriteLine(ToDoubleInvar("3.0"));
Console.WriteLine(ToDoubleInvar(3f));
}
}

So you will be able to call it like:

Address addr = DataStore.Load(addrInstance);

Just my 2c
Stefan
Dec 20 '05 #8
Ashish,
As the other have shown you can define a method with a parameterized return
type.

Yes you can define a generic function where the parameter is only used for
the return type.

public T doSomething<T where T:new()>()

However! You need to supply the type parameter when you call the method
directly, something like:

object o = doSomething<object>();
MemoryStream m = doSomething<MemoryStream>();

However! it "violates" an FxCop rule as its "ambiguous". The compiler is not
able to use Type Inference to figure out the type parameter...

Here is a thread that discusses it:

http://groups.google.com/group/micro...4e46ca8f99b798

Personally I find in the case of GetCustomAttribute (as the thread shows) it
makes sense as the type parameter is encapsulating the downcast, plus the
type parameter is used to "do work".

--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Ashish" <as*****@thisisjunk.com> wrote in message
news:Og**************@TK2MSFTNGP10.phx.gbl...
| This is just a thought, may be its not possible , but is it possible to
| use generics in method declarations as return type arguments..
|
| for example
|
| public <T> Load(T value)
| {
| //method body
| }
|
| this would specify that the type passed would be type returned...
|
| so at compile time, the compiler would do a static type checking to see
| if there are compilation issues...
|
|
| for exampple
|
|
| Address addr = DataStore.Load(typeof(Address));
|
|
|
|
Dec 23 '05 #9

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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.