Is it OK to use a public variable in C# if it is readonly?
Tag : chash , By : Luciano Campos
Date : March 29 2020, 07:55 AM
seems to work fine Since he didn't answer (yet) and no one else referenced this yet: There is a great article on this topic by Jon Skeet amending his book C# in depth (give credits to Jon):
|
C# public variable as writeable inside the class but readonly outside the class
Tag : chash , By : Suresh S
Date : March 29 2020, 07:55 AM
To fix the issue you can do I have a .Net C# class where I need to make a variable public. I need to initialize this variable within a method (not within the constructor). However, I don't want the variable to be modifieable by other classes. Is this possible? , Don't use a field - use a property: class Foo
{
public string Bar { get; private set; }
}
class Foo
{
[CompilerGenerated]
private string <Bar>k__BackingField;
public string Bar
{
[CompilerGenerated]
get
{
return this.<Bar>k__BackingField;
}
[CompilerGenerated]
private set
{
this.<Bar>k__BackingField = value;
}
}
}
|
Is it better to have a public readonly object, identical local readonly objects per class, or to directly reference a li
Tag : chash , By : Cesar Sanz
Date : March 29 2020, 07:55 AM
hope this fix your issue I suppose it depends on your design. If this is just a simple, one-off application, I'd go with your first example. You have a single viewport, none of your classes are dependent on the GraphicsDevice or however you decide to manage that viewport. Storing the single object there and referencing it in your code will be fast; it's just a field reference so they don't come much faster than that. Calling GraphicsDevice.Viewport every time, especially since it's a property means you'll be running a property getter method every single time you want to access it.
|
trying to expose a class member to be public and readonly or a public constant
Date : March 29 2020, 07:55 AM
To fix this issue i am stuck with a simple variable assignment , the only condintion to make it littl complex for me is that i need the struct values to be private , so they wont be modified alswhwere , Use Properties public static int CustomersTid { get { return CustomersMeta.TableID; } }
public static string CustomersTblName { get { return CustomersMeta.TableName; } }
public static int TimesTid { get { return TimesMeta.TableID; } }
public static string TimesTblName { get { return TimesMeta.TableName; } }
|
Using public readonly fields for immutable structs instead of private field/public getter pairs
Tag : chash , By : Jarques
Date : March 29 2020, 07:55 AM
will be helpful for those in need In pure C# without reflection, there are few reasons to avoid read-only fields in your case, and I probably would opt for read-only fields myself. Most of the general advantages of properties don't really apply here. That said... Anything that uses reflection to get a list of properties, and acts on those properties, will not work with fields (whether read-only or not) without modification.
|