473,399 Members | 3,888 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,399 software developers and data experts.

How do you pass a null argument to a generic method?

public void SomeGenericClassConstructor(T item){}

Very simple question: How do I pass in a null value as the argument?
Generics require something different than what I'm use to.

Thanks,
Lee
Feb 10 '07 #1
4 10171
I think maybe you have to do something like this:

T dummy = default(T);
SomeGenericMethod(dummy);

--
Lee
Feb 10 '07 #2
>I think maybe you have to do something like this:

T dummy = default(T);
SomeGenericMethod(dummy);
Or add the class constraint to T.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Feb 10 '07 #3
On Feb 9, 4:43 pm, "Lee Grissom" <lee dot grissom at quest dot com>
wrote:
public void SomeGenericClassConstructor(T item){}

Very simple question: How do I pass in a null value as the argument?
Generics require something different than what I'm use to.
If I understand the question correctly, the problem is that if you
just write "null", the compiler can't figure out which version of the
method to call, because null can be any reference type. You can
specify the type explicitly instead:

SomeGenericClassConstructor<string>(null);

Jesse

Feb 11 '07 #4
On Feb 9, 4:43 pm, "Lee Grissom" <lee dot grissom at quest dot com>
wrote:
public void SomeGenericClassConstructor(T item){}

Very simple question: How do I pass in a null value as the argument?
Generics require something different than what I'm use to.
I'm not sure that I understand your question.

Do you mean that this is a method within a generic class:

public class SomeGenericClass<T>
{
public void SomeGenericClass(T item) { }
}

? If this is so, then there is no problem passing null to the
constructor. Of course, you have to instantiate the generic class to a
particular class, first:

SomeGenericClass<stringmyInstance = new
SomeGenericClass<string>(null);

Or, are you talking about a generic method, in which case I think that
your syntax was wrong:

public void SomeGenericClassConstructor<T>(T item) { }

In that case, you can still pass null as the argument, but you have to
instantiate the method:

SomeGenericClassConstructor<string>(null);

Or was it neither of these?

Feb 12 '07 #5

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

Similar topics

110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
38
by: Radde | last post by:
HI all, Whats the difference b/w pass by ref and pass by pointer in C++ when ur passing objects as args.. Cheers..
6
by: LuB | last post by:
Hi, I wanted to use the most efficient argument passing method. I was always taught that its best to pass (const SomeObject& obj) if possible .... but in this case, I can't pass a const param...
6
by: aurgathor | last post by:
Howdy, How do I pass some function a generic comparison function? I figured out one non-generic case, but since this code got parameter declarations in two places, it's obviously not generic....
5
by: Tee | last post by:
Hi, When I have a function like: private string xyz(string Text) I can call it without passing in a string with xyz("") or xyz(string.Empty) but if the paramater needed is an array like:...
21
by: vmsgman | last post by:
Here is a code sample ... int blah = ReadFile( defArray, defFileName, w, h); // Read File Contents into memory array and return for processing public int ReadFile( ref ushort nArray, string...
13
by: andrea | last post by:
Sorry for the stupid question, I know, but sometimes is necessary starts from the basic. I don't know how to pass the result of a method generated from a DAL class to a BL class returning the...
9
by: mps | last post by:
I want to define a class that has a generic parameter that is itself a generic class. For example, if I have a generic IQueue<Tinterface, and class A wants to make use of a generic class that...
24
by: =?Utf-8?B?U3dhcHB5?= | last post by:
Can anyone suggest me to pass more parameters other than two parameter for events like the following? Event: Onbutton_click(object sender, EventArgs e)" Event handler: button.Click += new...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.