473,883 Members | 2,233 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.MakeDefaul tInstance()" -- but it doesn't.

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

static public object MakeDefaultInst ance(Type type)
{
if (type.IsValueTy pe)
return Activator.Creat eInstance(type) ;
else
return null;
}

Thanks,
Jeff Brown
Jun 15 '06 #1
1 1783
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
1099
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 Settings" to "General Development Settings". Thank you
13
2290
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 T as an int. Proper program behavior relies on that "default constructed" int being zero. That lead me to the following example...
1
1293
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 if a browser just visits www.mydomain.com, he will NOT see my site (since all the content is under the Main subdirectory). Currently, I use a simple ASP redirect script in the root directory that will redirect the user to the Main subdirectory. But...
5
19608
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 having a problem in the calling program. I searched online and found suggestions that I return an Array instead so I modified my code (below) to return an Array instead of an ArrayList. Now I get the message when I try to run just my webservice...
2
3856
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: You must implement a default accessor on System.Web.Security.MembershipUserCollection because it inherits from ICollection. The error appears when trying to load the WebService.asmx file. The code builds fine.
0
1134
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
6693
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 call it like this:
1
12242
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 you change this ? How do you get there ? Thanks !
1
2088
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 way to start it programmatically. Thanks. GD
1
9638
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 close the form because then the message pups up "MS Office Access can't change the work directory to I:\ " If I change it to D:\ I can close the form but by opening the form again the field is still showing I:\.
0
9945
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9798
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10766
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10863
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10422
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7978
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6007
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4622
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3241
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.