What exactly does new object() do in c# [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have had trouble wrapping my head around this and it seems to have lack luster documentation.
For example, this code:
private static readonly Object obj = new Object();
Can someone parse through this code and explain what is happening here. What exactly are the properties of this new object that was created? Why create an object this way?

You create a new oject with the type of Object. In most cases a statement like this is used for locking purpuse, see https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/lock-statement for more information.

It simply creates an object of a type Object, which is the base type for all of C# reference types.
I has 4 methods:
ToString()
GetHashCode()
GetType()
Equals()
Every class derives form Object, so it has all of the methods above.
Moreover, 3 of these methods are virtual (so you can override them):
ToString()
GetHashCode()
Equals()
It's sometimes used for locking as Isitar mentioned in his answer.

Object obj
that declares a variable of type Object
= new Object();
the equals sign is assignment, the new operator creates a reference to a new instance of class Object and the portion of Object() default initializes it.
it will have the default properties of an object.

Related

Difference between in class and constructor initialisation [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to understand in the code below
class x
{
int a=3;
...
other class members
}
class y
{
int a;
public y()
{
a=3;
}
...
other class members
}
What is the difference between these two initialisation methods?
Also does it have anything to do with static classes?
Under the C++11 standard, we can supply an in-class initializer for a
data member. When we create objects, the in-class initializers will be
used to initialize the data members. Members without an initializer
are default initialized.
Your first example uses an in-class initializer, while your second example only initializes a within the default constructor.
Say you have another constructor z, which takes some parameters but does not initialize data member a. Then upon calling z,
If you use in-class initializer, it will be used to set a = 3.
If you only initialize a in your default constructor, then a will be uninitialized.

anonymous type object or dynamic [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Assuming we have a function:
public IEnumerable<object> GetViewModel(int id) {
var records = LoadRecordById(id);
var result = (from record in records
select new {
field1 = record.data1,
field2 = record.data2,
....
}).ToArray();
return result;
}
We can return it like IEnumerable<object> and like IEnumerable<dynamic>. The question is: what to use and why? what advantages/disadvantages ?
You have an anonymous type right now.
If you cast it to object, you will have no way to get the fields out. You cannot cast it back to an anonymous object (since you don't know what it was) and you can't access the properties from object.
If you cast it to dynamic, you can access the fields as you normally would, but you will lose all type safety. It will determine the type at compile time, which means you have access to the types generated at compile time, but if you type something wrong you will get a compilation error at runtime.
Best thing to do is to create a class that has the properties you need and return a list of that class. Then you will get the properties, and will get the benefits of static typing.
You should use an interface or a base class. But to answer your question:
dynamic foo = "Some string";
foo.ToUpper(); // this works fine
foo.Something(); // compiles, but runtime error
object bar = "Some string";
bar.ToUpper(); // does not compile

Casting object to an interface return null [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an object from a class which returns an interface. but when I cast the object to that interface it returns null. what is the problem?
public partial class CompanyConfigurations : SgConfigurationControl, `IWizardControl`
CompanyConfigurations c
c as IWizardControl
The as operator returns null if the instance you want to cast, does not implement the specified interface, or does not inherit from the specified base class.
It's all clearly stated in MSDN: http://msdn.microsoft.com/en-us/library/cscsdfbt.aspx
Offcourse, if the instance that you want to cast is not assigned to an instance of a class (in other words, if the variable is null, then casting the variable using the as operator will return null as well offcourse.

Number of copies of instance method and fields? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Just being curious.
If I create 2 objects of a particular class, then 2 instances of the instance methods and the instance variables are present in the memory for that particular class?
EDIT: I tried with static and for sure, only one instance of the members were there but not sure with instance variables though.
If I create 2 objects of a particular class, then 2 instances of the instance methods and the instance variables are present in the memory for that particular class?
Instance fields - yes. Instance methods (including property accessors), no. Code (both instance and static) is shared among all instances.
Note that static classes will create an additional type for each generic parameter used, and each of those types will share one set of static variables, so for example:
// for example only, not intended to be a perfect singleton implementation
public class Singleton<T> where t : new()
{
private static T _Instance;
public T Instance()
{
return _Instance ?? (_Instance = new T());
}
}
Singleton<Class1> and Singleton<Class2> will each have a different object in memory for _Instance.

Generate Runtime type based on propertyinfo[] [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I have got a list of PropertyInfo, now i need to either populate a new object with these propertyinfos and there values or generic a type runtime containing these properties so i can create a new instance of the object based on my runtime created type.
I cannot create a new instance of an object based on an earlier type because i just filtered out my collection based properties (this is due to serialization of an object; long story).
How can i achieve this?
I think TypeBuilder will suit your needs.
On the bottom of the page you will see a clear example.
Also if you need to build methods use Expression Trees instead of ILGenerator.
Here's a few options:
Use TypeBuilder to create a runtime type. Use DefineMethodOverride to implement the get/set methods of the properties and return the interface type (with the runtime implementation).
Use one of the many Mock frameworks. They basically do the plumbing for you; the result is the same.

Categories