473,587 Members | 2,505 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

can we have a nullable field in the struct

hello,

can we have a nullable type field in the struct ?
struct Employee
{
nullable<string firstName =null;
}
we can't initialize a field in a struct so is there any possibility
that we can have a nullable field in the struct.
can anyone please help me out regarding this

Thanks,
James
Nov 11 '08 #1
4 2183
James,

You can't use string as the type parameter for Nullable<Tbecau se there
is a constraint making it a structure. This makes sense, since reference
types can be null.

If you just make it a string, by default, the field will be null when
the structure is constructed. You don't need to initialize it, you just
have to declare it.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"James" <bo************ *@gmail.comwrot e in message
news:9a******** *************** ***********@i20 g2000prf.google groups.com...
hello,

can we have a nullable type field in the struct ?
struct Employee
{
nullable<string firstName =null;
}
we can't initialize a field in a struct so is there any possibility
that we can have a nullable field in the struct.
can anyone please help me out regarding this

Thanks,
James
Nov 11 '08 #2
xc

hi Nicholas,

"You can't use string as the type parameter for Nullable<Tbecau se there
is a constraint making it a structure"

then, when it should be good to use Nullable<T>,
I think for classes there is no need at all to use Nullable<T>

thanks, Carlos


Nov 11 '08 #3
int
byte
etc

those are value types (structs) whereas string is not.

--
Pete
====
http://mrpmorris.blogspot.com
http://www.capableobjects.com
Nov 11 '08 #4
Carlos,

That's basically what I was saying. T must be a value type when using
it in Nullable<T>. For reference types (like string), it makes no sense
because you can just set it to null anyways.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"xc" <d22wrote in message news:Om******** *****@TK2MSFTNG P05.phx.gbl...
>
hi Nicholas,

"You can't use string as the type parameter for Nullable<Tbecau se there
is a constraint making it a structure"

then, when it should be good to use Nullable<T>,
I think for classes there is no need at all to use Nullable<T>

thanks, Carlos


Nov 11 '08 #5

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

Similar topics

3
32275
by: Sakoulakis | last post by:
Hi, I was just reading about nullable types and I like that feature very much. What dissapointed me is that nullable string is not supported. Imagine a field (of type sting) in a database which is not assigned. I agree with nullable only for value types but in my eyes string is kind of a mix between a value type and a reference type.
10
1822
by: John Wood | last post by:
I was just looking at an article about using nullable value types. (value types that can effectively have no value and not be set). The syntax is to append a question-mark to the value type in the declaration, eg: int? age; I don't like that much, I think it would be much more consistent to use a new keyword, such as "nullable". But...
5
4669
by: Narshe | last post by:
How can I check if a type is nullable? If a type is created like bool? = null;, then how can you tell if the type is nullable as opposed to not? You have to cast from bool to bool? and such, so I'll need to know if a type is nullable. -Josh
3
2099
by: rubikzube* | last post by:
Hi. I'm trying to write a snippet of code to detect if a variable is a nullable<t> struct, and I can't figure out the right syntax or if such a thing is even possible... Below are the results that I got when I attempted to perform some simple tests. Nullable<int> i = 32; bool isNullableClass = i is Nullable; // false
8
10699
by: Sam Kong | last post by:
Hello, I want to define a generic class which should accept only nullable types or reference types. What's the best way to costrain it? --------- class MyClass<T>{ ...
1
1531
by: Jon Davis | last post by:
Are nullable primatives stored on the stack, i.e. as a struct, or on the heap as a reference type? Jon
5
2946
by: =?Utf-8?B?emlubw==?= | last post by:
in .net 2.0, the class (myClass) contains a property defined as: private _creationDate as As Nullable(Of DateTime) Public Property CreationDate() As Nullable(Of DateTime) Get If _creationDate.HasValue Then Return _creationDate Else Return Nothing End If End Get
8
2013
by: Tony Johansson | last post by:
Hello! Jon skeet answer this question in a previous mail for several days ago if nullable type is a reference or a value type? With the following answer. It's a struct - otherwise there'd be relatively little value in having it instead of having explicit access to the boxed types. But what does the answer actually mean?
1
1302
by: shapper | last post by:
Hello, Is there a list of which data types are nulable? I know bool is: bool? published; But what about DateTime, Double, etc ... I know string is not ...
0
7849
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...
0
8347
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...
1
7973
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...
0
6626
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5394
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2358
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
1
1454
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1189
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...

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.