1

Properties

 public class student
{
    public int id { get; set; }
    public mark mark { get; set; }
}
public class mark
{
    public int value { get; set; }
}

I'm creating object as below

student x=new student();
x.id=1 --> default value 0

but when i access x.mark.value it throwing exception(x.mark is null)

Doesn't initiate by default? any reason?

balaji
  • 1,090
  • 1
  • 17
  • 26

7 Answers7

4

The default value for an automatic property is default(T), where T is the type. Since the default for any reference type is null, mark is null. Value types can never be null and, as you have noticed, default(int) is 0.

Ed S.
  • 115,705
  • 20
  • 165
  • 244
  • @balaji: I don't know what you mean by that, but typically you would *construct* your type in your *constructor*, i.e., `mark = new mark();` – Ed S. Jan 31 '14 at 08:11
  • Value types can be set to null if required Forexample Nullable or int? – slash shogdhe Jan 31 '14 at 08:29
  • @slashshogdhe: Value types can *never* be null. You can of course create a wrapper with properly overloaded operators to simulate such a thing. Just because `Nullable` exists for such a purpose doesn't mean you are actually setting a value type to `null`. – Ed S. Jan 31 '14 at 08:32
3

Default value for all reference types is null not 0. You should initialize it in your constructor:

public student()
{
   mark = new mark();
}

When you try to access mark.value without initializing it you are trying to access a null object's property which is cause the NullReferenceException:

null.value = NullReferenceException!

See the documentation and this question for more details: What is a NullReferenceException and how do I fix it?

Community
  • 1
  • 1
Selman Genç
  • 94,267
  • 13
  • 106
  • 172
1

class is reference type, and all reference types resides in heap, their memory is allocated when you create object of it using new. So if you doesn't create new object i.e. you don't instantiate that class it will hold null i.e. no address is assigned to that class.

A.T.
  • 18,248
  • 7
  • 39
  • 59
0

Variables in C# automatically take on their default values (which can be found by calling default on them - for example default(int).

The reason your ID variable is being intialised to zero is because the default of an int is 0.

The reason your Mark variable is being intialised to nullis because the default of any class is null.

Liath
  • 9,054
  • 9
  • 48
  • 78
0

Default value for reference types is null reference. Make your mark a struct if you want the behavior you described:

public struct mark
{
    public int value { get; set; }
}

student x = new student();
x.id = 1;
x.mark.value = 5; // No exception thrown

As an alternative, just new your mark from within the constructor:

public student()
{
    this.mark = new mark();
}

Hope this helps.

volpav
  • 5,024
  • 17
  • 27
0

You are referring to the object instance mark which you do not initialize in your student class. There are many options available for you. If you insist that they both should be classes, create a instance of Mark in default constructor of Student

public class Student
{
    public Student()
    {
        this.mark = new Mark();
    }

    public int id { get; set; }
    public Mark mark { get; set; }
}
public class Mark
{
    public int value { get; set; }
}
Davlet D
  • 1,880
  • 1
  • 12
  • 14
0

What you don't see, i guess, is that you are initializing student in fact, but not mark at all! While initializing student by using

student s = new student();
s.id = 1;

All your properties would look like this:

  • id = 1
  • mark = null

If you would extend your constructor for student-class like this:

public class student
{
    public student() {
        this.mark = new mark();
    }

    public int id { get; set; }
    public mark mark { get; set; }
}

You would reach your goal for a default value of 0 when calling

x.mark.value;

while your properties would look like this:

  • id = 1
  • mark = 0

i hope this might help you!

Matthias R.
  • 423
  • 2
  • 14