Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hello Every One,
I am having One ResultClass in my project.
I am creating the object of that class
XML
<pre lang="c#">ResultClass r=new ResultClass();</pre>

one more thing i am doing is

<pre lang="cs">
//m_ResultList is temp resultclass object
ResultClass m_RecedResult = (ResultContainer)m_ResultList[0];
               m_ResultList.RemoveAt(0);</pre>


now i am not understanding the difference between this both statement.what is difference between new keyword and type of obj passing.
Posted
Comments
Sergey Alexandrovich Kryukov 9-Mar-14 1:03am    
The question does not make sense. This is the same as to ask "what is the difference between newborn and elementary school?". :-)
Forget about "difference"; you won't be able to explain what it is supposed to mean. Ask what do you fail to understand in first and second case, exactly.

And could you tell us: if you have no idea on the very basics, how did you managed to become an author of 10 answers on this forum? I don't want to review them, but I would not trust you as an expert... Please, feel some responsibility for your answers...

—SA

In first case, you create an instance of some class using the class constructor and assign a reference to the variable. In second case, you try to get a reference to the instance which was created (instantiated) some time ago, with type case. Note that it can be unsuccessful and throw one or another exception. First, a collection might be empty, then indexing at zero would be out if index range, and the actual runtime type of the collection element might be different, that would throw a type cast exception.

If you did not understand such elementary things, you should stop doing what you are doing and read some book/documentation on programming, .NET and the language, otherwise you won't get anything exception frustration.

—SA
 
Share this answer
 
new :
the new keyword can be used as an operator, a modifier, or a constraint.

new Operator : Used to create objects and invoke constructors.
Animal an = new Animal();

new Modifier : Used to hide an inherited member from a base class member.
public class BaseC<br />
{<br />
public int x;<br />
public void Invoke() { }<br />
}<br />
public class DerivedC : BaseC<br />
{<br />
new public void Invoke() { }<br />
}


new Constraint : Used to restrict types that might be used as arguments for a type parameter in a generic declaration.
class ItemFactory<t> where T : new()<br />
{<br />
public T GetNewItem()<br />
{<br />
    return new T();<br />
}<br />
}</t>



typeof :
Used to obtain the System.Type object for a type. A typeof expression takes the following form:
System.Type type = typeof(int);

You could have find more info on MSDN. :)

-KR
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-Mar-14 1:17am    
About the "new" modifier: Not to "hide"! "new" is only used to avoid compilation warning on hiding. You just tell to the compiler: "yes, I know that I'm doing such a bad thing as hiding, but in this case I do it intentionally". Nothing else. Maybe you need to read the documentation and understand this feature yourself.

I don't understand why you added all those points irrelevant to the question (incorrect question, for sure; nevertheless...) but still failed to say anything on the second case of OP's "comparison"...

—SA
Krunal Rohit 9-Mar-14 1:19am    
Yeah question doesn't make sense. But as OP is not cleared with these basics, I have addressed him with some basics.

-KR
Sergey Alexandrovich Kryukov 9-Mar-14 1:25am    
I know, but you did it with mistakes...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900