-1
public string Name
{
    get
    {
        return this.name;
    }
    set
    {
        this.name = value;
    }
}

here this is equivalent to

public string Name{ get; set; } right? 

similarly for the class variable

public NewsItem ns { get { return this.DataContext as  NewsItem; } }

and

public NewsItem ns{get;}

are they same???? if not what will the default return value in public NewsItem ns{get;}

and what does that this.DataContext means in

public NewsItem ns { get { return this.DataContext as  NewsItem; } }

what will be the return value if the code is like this

public NewsItem ns { get { return this.DataContext} }

Thanks in Advance

Os Snehith Ab
  • 67
  • 1
  • 7

2 Answers2

0

difference between public string Name{ get; set; } and public string Name{ get { return name; }...

In C# 3.0 and later, (the previous one)auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors. They also enable client code to create objects. And the last one is a normal property that is a member that provides a flexible mechanism to read, write, or compute the value of a private field.

public NewsItem ns { get { return this.DataContext as NewsItem; } }

The DataContext for a FrameworkElement. A common use of a data context is when a FrameworkElement uses the {Binding} markup extension and participates in data binding. If you want to DataContext could be regarded as NewsItem. you need pass NewsItem instance to DataContext, when page initialize.

public TestPage()
{
    this.InitializeComponent();
    this.DataContext = new NewsItem();
}

what will be the return value if the code is like this public NewsItem ns { get { return this.DataContext} }

please refer to above description, if you call ns get method it will return a NewsItem instance that was evaluated current DataContext.

Nico Zhu
  • 32,367
  • 2
  • 15
  • 36
  • Zhu Hai, if this. data context is not assigned initially and what would be the return values for these ``` public NewsItem ns { get { return this.DataContext ; } } ``` ``` public NewsItem ns { get ; } ``` – Os Snehith Ab Sep 30 '21 at 11:50
  • if you have not assigned value for DataContext, it will return null when you call NewsItem get method. – Nico Zhu Oct 01 '21 at 13:42
0

Your first Name property returns the value of the name field. The second is an auto-implented property that returns the value of a compiler generated field value that you don't see in your code. They are not equivalent.

public NewsItem ns { get { return this.DataContext as NewsItem; } } returns the current value of the DataContext property if it has been set to a NewsItem value. Otherwise it returns null.

public NewsItem ns {get;} returns the current value of the ns property which will be null unless you have explicitly set it to some value somewhere. So they are not the same.

So to answer your question, the default value of the DataContext property is null so you have to set it in order to be able to bind to some property of it.

It's not set for you by the framework, unless it's inherited from a parent element in the visual tree.

mm8
  • 163,881
  • 10
  • 57
  • 88