Unfamiliar C# syntax for class instantiation / initialization - DataContext?

Not familiar with this syntax.  Is this instantiating an instance of the class?  Why is there no "new" keyword?

InspectionsByTasksViewModel vm = (DataContext as InspectionsByTasksViewModel);

Open in new window

Tom KnowltonWeb developerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chinmay PatelChief Technology NinjaCommented:
Hi Tom,

If you would post the entire code snippet it would have been easier to explain. By the looks of it, this is not initialization, this is casting.

It will try to cast the DataContext as a type of InspectionsByTasksViewModel.

Biggest advantage or disadvantage of as keyword is, that the as operator is like a cast operation. But, if the conversion isn't possible, as returns null instead of raising an exception.

kaufmed 👽Commented:
By the looks of it, this is not initialization,
Well, vm is being initialized...it's just being initialized based on what you described  ;)
Tom KnowltonWeb developerAuthor Commented:
You're correct; I did not give much surrounding code or context.  Here is a screenshot of an example where this is taking place.

I should have mentioned that this was inside of a XAML application, and apparently "DataContext" belongs to the FrameworkElement, which apparently is part of XAML (I'm brand new to XAML).

I've just never seen initialization of a class instance done via casting?

more context
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Chinmay PatelChief Technology NinjaCommented:
I am not sure if you have used VB.Net or some other language but VB.Net does have a similar operator

kaufmed 👽Commented:
InspectionsByTasksViewModel vm is just the declaration of a variable. A variable can point to any previously instantiated data, or the data can be "new'd" up inline. In your case, the variable is used to hold whatever is in the DataContext--maybe that's a InspectionsByTasksViewModel and maybe it's LibraryBook...who knows? DataContext isn't strongly typed, which means it can hold anything. The author of the code is using the safe-cast operator as to ensure that no exception is encountered. You can see in the very next line the author checks that vm is not equal to null. This is due to the behavior of as--as Chinmay already mentioned:  If the thing on the left of as can be cast as the thing on the right of as, then the cast succeeds; otherwise, null is assigned to the variable on the left of the equals sign.

See:  https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/as

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Tom KnowltonWeb developerAuthor Commented:
Thank you!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.