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

How to implement "default(T)" for a given Type

Hi,

I'm trying to implement the functionality of the generic expression,
"default(T)", but for a given instance of the Type class.

The ideal would be if the Type class had a method like "object
Type.MakeDefaultInstance()" -- but it doesn't.

Here's my attempt, but it seems rather kludgy. Is there a better way?

static public object MakeDefaultInstance(Type type)
{
if (type.IsValueType)
return Activator.CreateInstance(type);
else
return null;
}

Thanks,
Jeff Brown
Jun 15 '06 #1
1 1764
Here's my attempt, but it seems rather kludgy. Is there a better way?


Really? I think it's rather nice and clean, and probably the way I'd
do it.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jun 16 '06 #2

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

Similar topics

0
by: Eagle Tsui | last post by:
Dear all I just installed VS2005 Team Suite. I have a problem in environment setting. Can I change the environement setting that I mean is change environment setting from "Project Management...
13
by: DaKoadMunky | last post by:
I recently came across some code in a template that default constructed an object of type T to pass to another function... SomeFunction(T()); The code that instantiates that template specifies...
1
by: MS | last post by:
I have an ASP.NET based site, and the main application is stored under a virtual directory called "Main". So in order to view my site, a browser would have to visit www.mydomain.com/Main/. But...
5
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was...
2
by: Justin Drerup | last post by:
I'm tryng to return a custom object that contains a collection of MembershipUsers through a web service however I receive the following error when trying to return the object through a web method:...
0
by: John Paul. A | last post by:
Hi, I want to get the port number of "Default Web Site" from IIS. Can you please tell me how to get it using the namespace System.DirectoryServices. Thanks, John
4
by: Michael Bray | last post by:
Lets say I want to get the default value for a system type... how would I implement the following function: public object DefaultValueOfType(Type t) { return ????; } I want to be able to...
1
by: Rob | last post by:
I am doing a simple setup deployment.... everything is fine but the program installs to the Program Files\Default Company Name.... I hear that is derived from the "Manufacturer".... Where do...
1
by: GD | last post by:
I am trying to restart IIS service programmatically from an application in the same machine. It works fine except that the "Default Web Site" in IIS Manager is not started. I wonder if there is any...
1
by: Simon van Beek | last post by:
Dear reader, Under | Tools | Options | the form Options will open. Under the tab you find the field "Default database folder". By opening the form this field shows on my pc I:\. I can't...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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: 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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.