Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9270273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:19:40+00:00 2026-06-18T15:19:40+00:00

If we had a class, say MyClass , when instantiating an object of that

  • 0

If we had a class, say MyClass, when instantiating an object of that class, what is the difference between these two:

Object obj=new MyClass();
MyClass obj2=new MyClass();

I know that in .NET, all classes derive from System.Object, so that in this case, obj and obj2 variables just contain references to the objects.

However, is there any difference?

And if so, what is it, when using these two ways of instantiating objects?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T15:19:41+00:00Added an answer on June 18, 2026 at 3:19 pm

    Supposing your class had a property called Name, if you used this,

    Object obj = new MyClass();
    

    and tried to use:

    obj.Name = "George";
    

    This would be a compiler error, because the Object class does not have a Name property. This, however, would be fine:

    MyClass obj2 = new MyClass();
    obj2.Name = "George";
    

    because the obj2 is a MyClass. In short, in order to access a variable’s class members (properties, methods, variables), that variable has to be of a type that has those members, or it needs to be cast to the necessary class:

    ((MyClass)obj).Name = "George";
    

    However, constantly casting variables in order to get at their members is not good, as it requires extra CPU cycles and can lead to InvalidCastExceptions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I had a class SuperClass and two subclasses SubClassA and SubClassB that inherit
Say you had two classes A and B. If the relationship between is has-a
This is probably a very simple question. Say I had a class that computed
I have an application that uses CoreData. I previously had a class named Marker
i had created a class with two properties one of datetime datatype and other
I have a class that had an inline member, but I later decided that
Is it possible for a PHP object instance to destroy/unset itself? Say I had
I had enum class, say enum class Enum{ var1, var2; } Now I want
Let's say I have the following class. MyClass<T> { public void MyMethod(T a, List<T>
Say class A had method do(); and class B had field data; . I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.