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 8 years ago.
Improve this question
This is basically a a design question:
I am rewriting an application in C# which is basically written in C++. C++ has this nice concept of Header files which will gold a lot of declared constant values for the consuming file.
However, we do not have Header files in C#. I may have two options
Create a class which will hold a lot of constant values for me(No so standard)
Store values in XML (Standard-But involves a lot of parsing hassle)
Which is a better solution? Is there any other solution that I may not know of?
Personally i'd use a static class and place all the values in there.
public static class Constants
{
public const int Ten = 10;
public const int Twenty = 20;
....
}
EDIT
As #JonSkeet suggested, it's better if you store these values in classes they pertain to, however, that might not always be possible.
Related
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 3 years ago.
Improve this question
Does anyone know how to convert the C# getter and setter to a java-like getter and setter pattern using the Rider IDE?
Convert this:
public Transform List
{
get { return list; }
set { list = value; }
}
to this
public Transform GetList() { return this.list; }
public SomeClass SetList(Transform list) { this.list = list; return SomeClass; }
This would be usedful for chaining setters in a fluent builder pattern.
A distinct non answer: stop wasting "double" your time!
C# isn't Java. Fighting a tool to fight the native idiomatic constructs of your target language, that is likely double pointless.
Source code is written to be read by humans. And good source code never surprises its readers. An experienced c# programmer will look at your Java like getters and setters and can only wonder: "why is he polluting these classes with those strange methods, instead of using c# property support".
Beyond that, you might want to read https://en.m.wikipedia.org/wiki/Uniform_access_principle to understand why the c# properties are actually a better approach than Java fields with getter/setter pairs!
Or as they said 2 thousand years ago: when you come to Rome, do like the Romans do! If you don't want to do like the Romans do, stay away from Rome, or c# in your specific case.
OP is probably coming from the java world. In the Menu(Intellij) Java IDE ->Code->Generate
In the generate menu, "Getter and Setter" is the 4th Option.
In the C# world(Rider), properties are used to expose selected fields.
Jetbrains Recommends generating Properties rather than the Java getter-setter way.
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 3 years ago.
Improve this question
void MyFunc(int var)
{
// Some Code
}
void MyFunc(List<int> varList)
{
// Some Code
}
What is the performance of parameter passing to these two functions?
There answer is, there should be little difference.
The first is allocating an int and copying the value type of a int,
The second is allocating an reference and copying a reference (which for all intents-and-purposes is an uint / ulong)
There is no appreciable difference.
However, the bigger problem is why you are care about these micro-optimisations, i think you are over thinking this. You can always test this for your self. Either look at the jitted asm, or download BenchmarkDotNet and run a performance test
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 4 years ago.
Improve this question
I have just learned get set principles in C# and I wonder whether there is any interest of using the same principle for methods.
If I understand well, get and set are used for variables. But it could be possible to apply the same principle for methods. For instance:
private int _GiveMultiply()
{
int a = ...
int b = ...
return c = a*b;
}
public int GiveMultiply
{
get { return _GiveMultiply(); }
}
But is there any kind of interest to do such a thing ?
For example is there a risk to use a public function that can be prevented using such a process ?
The answer is: it depends. I'll try to help you to reformulate your question: does it make any sense to return a function rather than computed result from another fuction? Then I would say: definitely yes (let me know if you'd like to know ehy, I'll update this post). But the example you showed does not return a fuction, it just wraps it into yet another fuction, which is useless. The only exception is various kinds of abstract method patterns, where you might have public function with some predefined logic and call to the abstract/virtual fuctions; rarely they do have same name, then indeed wrapping sort of works.
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 8 years ago.
Improve this question
Short and sweet (hopefully), is there a specific reason not to use the this keyword when writing getters and setters in C#? I know the typical format, and the one I've always used, is:
public void SetDay(int _day) { day = _day; }
public int GetDay()
{
return day;
}
Recently though, I've been learning Java, and in several of the books I've been using I've seen it written instead like this:
public void SetDay(int _day) { this.day = _day; }
public int GetDay()
{
return this.day;
}
So basically, is there a reason to avoid doing it the same way in C#? Will it cause any problems or errors, or is it a valid approach and really just a matter of personal preference. I'm wondering because, while I know the this in C# is understood, explicitly using the this keyword seems like it would aid in eliminating a bit extra ambiguity, which personally is always a good thing.
Thank you!
There's not much to it really.
You can do it, and you can avoid it. I think it's quite obvious when you're in the getter/setter that you're talking about the object you're in, so I've never used it there.
Also, it seems like Resharper will suggest it's redundant, and gray it out.
If you find it to be of use for you (readability wise), by all means, use it. Otherwise, it'll save you five keystrokes (about a second?) every time you implement a getter by hand ... :)
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 8 years ago.
Improve this question
Ill start with an example or two. Take the following sample class:
class Sample
{
private object _someObject;
public Sample(object someobject)
{
_someObject = someobject;
// If I then wanted to pass someobject to a method within the constructor,
// is it better to use the field version or the parameter version. Example:
SomeMethod(someobject);
// OR
SomeMethod(_someObject);
}
}
Additionally, I have just finished the book titled "Efficient C#" by Bill Wagner and would like to know if there are any more books out there with a similar format as this one.
I am interested in knowing why I should write code the way it is written (More efficient IL for example)
Thanks in advance guys :)
It makes no difference, they're all references to the same object.