nullable

Can I create a generic function that accepts a Nullable<something>?

I'm working on a method that will accept a StreamWriter, either a string or a nullable value, and a length, and write the value to the StreamWriter, padded to the length. If the value is null, I want to write spaces. I want to do something like the following oversimplified example, which is just for demonstration purposes. public void F...

WPF : Nullable ComboBox

I want to have an empty item in the comboBox to allow the user to "Unselect" and keep the comboBox empty (Null value). How can I do that? ...

What is the memory footprint of a Nullable<T>

An int (Int32) has a memory footprint of 4 bytes. But what is the memory footprint of: int? i = null; and : int? i = 3; Is this in general or type dependent? ...

Boxing / Unboxing Nullable Types - Why this implementation ?

Extract from CLR via C# on Boxing / Unboxing value types ... On Boxing: If the nullable instance is not null, the CLR takes the value out of the nullable instance and boxes it. In other words a Nullable < Int32 > with a value of 5 is boxed into a boxed-Int32 with a value of 5. On Unboxing: Unboxing is simply the act of obtaining a refe...

Can a WCF service contract have a nullable input parameter?

I have a contract defined like this: [OperationContract] [WebGet(UriTemplate = "/GetX?myStr={myStr}&myX={myX}", BodyStyle = WebMessageBodyStyle.Wrapped)] string GetX(string myStr, int? myX); I get an exception: [InvalidOperationException: Operation 'GetX' in contract 'IMyGet' has a query variable named 'myX' of type 'System.Nullable1[...

Working with Nullable<'T> in F#

I'm wondering what others have come up with for dealing with Nullable<'T> in F#. I want to use Nullable<'T> on data types so that serialization works properly (i.e., doesn't write out F# option type to XML). But, I don't want my code stuck dealing with the ugliness of dealing with Nullable<'T> directly. Any suggestions? Is it better to...

Why is a Nullable<T> not a valid Custom Attribute Parameter when T is?

If I have an enum like this public enum Hungry { Somewhat, Very, CouldEatMySocks } and a custom attribute like this public class HungerAttribute : Attribute { public Hungry HungerLevel { get; set; } public Hungry? NullableHungerLevel { get; set; } } I can do this [Hunger(HungerLevel = Hungry.CouldEatMySocks)] p...

SubSonic3 Linq query generates "IS NOT NULL" instead of "IS NULL"

here is my linq query: var test = from m in db.Members where m.UserId == null select m.Id; test.ToList(); UserId is a nullable Guid field on the members table that corresponds to the ASP.NET membership table aspnet_member. I am unable to generate a query through subsonic that will select where the userid IS null, only where ...

Overloading a Method to Support Reference Types and Nullable Types

I have an extension method that I would like to overload so it can handle both reference types and nullable value types. When I try to do this, however, I get "Member with the same signature is already declared." Can C# not use the where qualifier on my generic methods to distinguish them from each other? The obvious way to make this w...

Iterating through a Enumerable collection with nullable types

Hey, I'm trying to iterate over an Enumerable collection of mix of nullable types However I want to compare the nullable type with a intrinsic type such as string or decimal. i.e. Here is a snippet <% foreach (PropertyInfo prop in this.Columns) { %> <td> <% var typeCode = Type.GetTypeCode(prop.PropertyType)...

Set property Nullable<> by reflection

I try to set a Nullable<> property dynamicly. I Get my property ex : PropertyInfo property = class.GetProperty("PropertyName"); // My property is Nullable<> at this time So the type could be a string or int I want to set my property by reflection like property.SetValue(class,"1256",null); It's not working when my property is a Nu...

Hibernate default joining for nullable many-to-one

Hi all, I have a hibernate mapping like this in a ProductDfn class @ManyToOne( fetch = FetchType.LAZY, optional = true ) @JoinColumn( name = "productTypeFk", nullable = true ) public ProductType getProductType() { return productType; } Note that the relationship is defined as optional (and the column is nullable). When doing HQL...

How to exclude null properties when using XmlSerializer

I'm serializing a class like this public MyClass { public int? a { get; set; } public int? b { get; set; } public int? c { get; set; } } All of the types are nullable because I want minimal data stored when serializing an object of this type. However, when it is serialized with only "a" populated, I get the following xml ...

Coding practices for C# Nullable type

I have never used nullable types in my C# code. Now I have decided to change my coding practice by introducing nullable types in my code. What are the major changes in the coding practices should be made while making a transition from normal datatypes to nullable data-types in case of application programming? What are the areas that sh...

C# newbie with DateTime variable I want to set to null

I have an output data class with a DateTime variable. I want to clear that to a null value in a loader class but the compiler complains with: Cannot convert null to 'System.Data.Time' because it is a non-nullable value type. I understand that, but if I change the type to DateTime? creating the nullable type wrapper I get: No overload ...

Performance surprise with "as" and nullable types

I'm just revising chapter 4 of C# in Depth which deals with nullable types, and I'm adding a section about using the "as" operator, which allows you to write: object o = ...; int? x = o as int?; if (x.HasValue) { ... // Use x.Value in here } I thought this was really neat, and that it could improve performance over the C# 1 equiva...

How to use SqlBulkCopy with nullable columns

I’m having an issue using SqlBulkCopy when nullable columns are involved. It sounds like SqlBulkCopy doesn't know how to deal with nullable columns and throws an illegal size error when it encounters a zero length column. The error is "Received an invalid column length from the bcp client..." I’m wondering what the best practice is for ...

Binding to a Nullable<DateTime> control property

We have a custom control that has a "Value" property of type System.Nullable (aka System.DateTime?). We have an object with a "Received" property of the same type. When we try to bind the control to the object, the following InvalidCastException is thrown: Invalid cast from 'System.DateTime' to 'System.Nullable`1[[System.DateTime, msc...

StructureMap "WithCtorArg" "EqualTo" and nullable types

StructureMap doesn't like passing in Nullable types as constructor arguments. Is there a reason for this? Is there a way to get this to work? [TestMethod] public void Demo() { ObjectFactory.Initialize(x => x.ForRequestedType<TestClass>() .TheDefault.Is.OfConcreteType<TestClass>() .WithCtorArg("param1").EqualT...

assign nullable objects for returning IQueryable

I am returning IQueryable<Customer> to the other method for some querying operations. The return method looks like: return from cust in _dbCustList select new Customer { CustomerId = cust.Customer_Id, FirstName= cust.First_Name, LastName= cust.Last_Na...