Use of protected keyword in c#? [closed] - c#

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.

Related

Best way to implement this? [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
Hi I'm a unity developer working on a bar manger game and I'm just wondering on the best way to implement a drinks system where it sets the value of the drink, number of servings in the barrel, name of the drink price of the whole barrel.
Here is some code I was working on before:
This is the interface method
public interface IDrinkSystem
{
string SetDrinkName(string nameToSet);
float SetDrinkValue(float drinkValue);
int AmmountOfServingsInBarrel(int Servings);
float PriceOfBarrel(float price);
}
This is the class method of doing it
public class DrinkSystem
{
public void NewDrink(string drinkName, float drinkValue, int barrelServings, float barrelPrice)
{
// Have getters and setters for all values in separate methods
}
}
What is the best way for making it easy to expand and at a push can I make an array of the NewDrink to store all the drinks i have or is there abetter way of doing this.
Use ScriptableObjects, as if they were files.
For every type of drink, have a ScriptableObject which you can fill in from Unity easily (even your artist can do this), and then at runtime, you load them into objects of a single DrinkEntity class which loads these values.
This way you don't end up with dozens or hundreds of "DrinkSystemBeer"/"DrinkSystemWhiskey"/etc classes, but you can still keep all your code clean and pattern-friendly.
IMPORTANT: NEVER operate with the ScriptableObjects directly. Simply load them into a DrinkEntity on its constructor. Treat ScriptableObject as if they were xml or json files from which you read your data.

How to clean a class correcly [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 5 years ago.
Improve this question
I have a class with functions to read data files. When read the class stores this datas in public properties.
After that I write the content of the puplic properties into a database.
Then it repeates. This means I read again datas with this class.
Before it repeates I want to clean the class.
What is the best way to do it?
Is it better to clear the content of the public properties manually or is it better to dispose the class. Maybe this is the more elegant way to do it?
Let's assume your class looks similar to:
public class MyClass
{
public double IntField { get; set; }
public string StringField { get; set; }
}
And your application something like:
public class MyApp
{
public void Execute() {
var myClass = new MyClass();
// set properties
myClass.IntField = 123;
myClass.StringField = "Hello";
// save to DB
}
}
Each time your application runs, it will run the Execute method which will create a new instance of your class. This way all class properties will be empty or "clean" as you call it.
create new method , name it let say "clear_data" , call it before load data from function that do it . also may be you will need call it from destructor to prevent memory leaks (in case you have pointers that point to some allocated memory ).
What about Memento pattern? Might be useful for you:
https://www.codeproject.com/Articles/186184/Memento-Design-Pattern
Or, as already pointed out, you have 3 other good possibilities:
- Create a method Clear() that sets properties to default value,
- If you have some unmanaged resources, you can use IDisposable interface,
- Create static readonly property holding Empty instance of the class and reassign it. But I would go with Memento/Clear way.

Making an object public static vs passing it around [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 5 years ago.
Improve this question
I am writing a game in Monogame framework and well
there are some objects that have only 1 instance
(but that does not necessarily mean that there should be only 1
instance) during the lifespan of an application by default,
like for example ContentManager.
Now what's bugging me is that I am not sure what is a better practice and why:
To make that object public static and access it from other classes
to use that static instance to load stuff
or
Pass that object as an argument to other classes constructor and use it that way
Just to add another option, you can use the singleton pattern to restrict the creation of new classes, like a GameManager. This pattern comes with cons and pros, so you need to analyze your requirements.
public final class Singleton {
private static final Singleton INSTANCE = new Singleton();
private Singleton() {}
public static Singleton getInstance() {
return INSTANCE;
}
}
The simplest way to implement the implement the pattern is to have a class with a private constructor, a static parameter for your instance and a method to return the instance.
https://en.wikipedia.org/wiki/Singleton_pattern

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.

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.

Categories