Difference between in class and constructor initialisation [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 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.

Related

Use of protected keyword in c#? [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 1 year ago.
Improve this question
I want to understand the use of "protected" keyword in the following code (line 3).
public class PlayerData
{
static protected PlayerData instance;
static public PlayerData Instance { get { return instance; } }
public int health;
}
public class GameManager
{
PlayerData.Instance.health = 10;
}
A private field is not accessible from a child class.
A protected field can be.
If the field is protected, we can inherit from this class and use this field instead of the property.
This avoids the use of the getter which is a method, and that is a crazy time consuming via a CPU PROC CALL and RET using the CPU STACK to return the reference.
Thus it is more speed optimized to use the field instance instead of teh property Instance because we directly use the reference without the need of a method call that will slow down the process and the current thread, thus the game.
Approximately ~5x faster, way to speak vaguely, for each usage.
But that said, we must be carefull to not change the underlying object instance, unless we have a good reason. This field could be read-only, but it is not, who knows why. Perhaps to be able to change the object... either it is just an oversight, or this ref is assigned outside a constructor, or can be reassigner at any time.

What exactly does new object() do in c# [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 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.

new keyword with -1 in C# [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
This is the first time I saw this kind of declaration. I haven't find much information in Google. In one of the existing WPF Applications, where user try to create a new Window he instantiates the class like below. What "-1" means ? What exactly it do ?
ProductViewModel viewModel = new ProductViewModel(-1);
It's just a parameter for the constructor... When you create a class, you can specify zero (default empty constructor is created for you if you don't create at least one) or more constructors. In the constructor you usually set the variables for the class to work with. -1 usually means that the value isn't set (like a default value), but I believe in that scenario it would be better to create a constructor with an optional parameter like this:
public class ProductViewModel
{
// this is our modified constructor
public ProductViewModel(int productId = -1)
{
// do something with the values, probably set some internal field
}
}
A constructor is called always when you create an instance of a class using new keyword.
Based on the comment, the constructor had parameters declared like this:
params object[] args. It's just a fancy syntax for saying I take variable number of parameters. You can learn more about params here on C# reference: Params keyword
The depends on the code existing in ProductViewModel. I think this -1 indicates that the programmer intended to create a new Product. This -1 shall be the ProductId. If there is an Id that is greather than 0 then the details to the product will be loaded. If the Id is -1 then a new production should be created.
Thats is my idea about what this constructor parameter will do.

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.

Polymorphism in c# [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 9 years ago.
Improve this question
I have a problem with a class that is to spawn instances containing one out of about 30 objects of different types (I call it out_out_of_many_types_of_subcontract in the code snippet below).
class supercontract
{
void supercontract (float date, one_out_of_many_types_of_subcontract subcontract)
{
stuff....
}
}
Is there any way of declaring a semi-generic variable or must I (1) resort to polymorphism between constructors or (2) casting an object as a certain type with a block of (else)if clauses?
Cheers!
I would make them all implement a single interface.
interface IContract{}
class AContract: IContract {...}
Even if the interface is empty you can limit what types could be passed to your method.

Categories