Is 'this.' a useful property? [closed] - c#

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
couldn't find any article about it. So.. is using '*this.*Chart1..' in asp.net, c# useful? Any time savings or why and when should I use it?
Thanks

It's really not ASP.NET-specific at all. It's just part of C#.
Some people suggest that you should always use it to indicate that you're referring to an instance member, as opposed to a static member or a local variable.
Personally I only use it when the qualification is required for disambiguation, e.g.
public Person(string name)
{
this.name = name;
}
Assuming you're in a situation where it doesn't affect the meaning of the code (i.e. where you're not disambiguating), it will have absolutely no effect on the generated IL, so there's no performance harm or benefit.
Note that in the rare case where you want to call an extension method on the current object, you need to use this as well. For example:
public class Foo<T> : IEnumerable<T>
{
// Implementation omitted
public int CountDistinct()
{
return this.Distinct().Count(); // this is required here
}
}

Three common uses for this, as per MSDN:
To qualify members hidden by similar names.
To pass an object as a parameter to other methods
To declare indexers
Refer to documentation for examples.

Related

C#, .net, general programming architecture. GetType or Enum : Which is better? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
Which do you think is better..
if (thing.GetType() == typeof(thisthing))
{
//do stuff for this type of thing.
}
Or give the objects an Enum property
if (thing.TypeName == NamesEnum.thisthing)
{
//do stuff for this type of thing.
}
Neither of these is a particularly extensible or maintainable approach.
It's typically better to design this directly into a virtual method in your type hierarchy, and just call the method. This allows other classes to override and provide custom functionality as appropriate.
In your case, the thisthing type (which should be named ThisThing if you want to follow .NET naming conventions) would just have a DoStuff method which could be virtual if needed, and call it.
It depends:
if (thing.TypeName == NamesEnum.thisthing)
will run more performant then GetType() as, this a simple comparison of 2 numeric values.
But:
if (thing.GetType() == typeof(thisthing))
is more more "flexible": when you do some refactoring, change type name or whatever, this condition will still work.
But will fail in condition, if 2 types belong to 2 different assemblies, instead in first case this still will be matched as equal,as you campare not types but just enum values.
In short, there is no best approach, just most suitable for your needs.
IF you are working with basic types that don't have sub types...
your first example can be shortened nicely to
if (thing is typeof(thisthing))
Depends on the situation really. If you get to a lot of different types you're going to want a switch statement at some point, so I would say option 2 if there are going to be a lot of types.

What are the practical usage of Cloning [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
Can anyone give me a practical usage where clone is used, I understand how its done and what are the types in it, but I dont see its practical usage. Also I read it somewhere that it is used to save the state of the object, but I still think that rarely the entire state of object would be so important.
If you have an object y encapsulated (for instance it is private) within object X, but you provide an accessor method (such as getY()) then you risk returning the reference to your private object and that's not good. You don't want client code to be able to get the reference to it. So instead you might copy or clone the object within getY(). As mentioned though copying is probably preferred vs. clone -- see Effective Java item 11.
Here is the example code:
public class X {
private Date y = new Date();
// other code here
public Date getY() {
//this could be bad:
//return y;
//this is good:
return new Date(y);
}
Sometimes you load your object properties to a form then bind each property to a (textbox,combobox, ...), after you do changes to the (textbox, checkbox,...) even if you cancel you can't get the original values, so cloning is the best option,
clone your object then bind, if you want to cancel the original object is always unchanged, this is just one case

best use for Regions and Inner Regions [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I have see alot of old code and from a lot of developers.
#region #endregion
can be helpful some times but if is use well.
how is the best way to use it to keep my code organized and easy to read?
It doesn't mean anything special if that's what you're asking. The only special effect it has is within Visual Studio for code folding.
If you have a large class that performs a few tasks, it may be best to region it out by separating your properties, different groups of methods, interface implementations, and whatever else you think may be important. There are no strict rules.
class MyReallyBigClass : IAwesome, INotAsAwesome
{
#region Public Properties
public string Test { get; set; }
// ..
#endregion
#region IAwesome Implementation
public void IAwesome.BeAwesome()
{
// ..
}
public int IAwesome.AwesomeLevel()
{
// ..
}
#endregion
#region INotAsAwesome Implementation [[...]]
#region Internal Fields
private int _whatever;
// ..
#endregion
}
Of course in practice, you wouldn't really get a class so large that you'd need to separate it out, but I normally do find myself using it around properties and interface implementations at the least.
It's purely to aid readability in the IDE. It's effectively stripped out on compilation.
That said, I tend to defer to Microsoft's style guidelines with regards to usage: grouping methods, properties, constructors, etc. - rarely, if ever, inside a method, and never between a brace construct (if, for, etc) and the opening brace.

Would you make this method Static or not? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
During a code review I presented a method quickly to the team that I had made static and one person agreed that there was no reason for it to not be static and a person disagreed saying that he would not make it static because it wasn't necessary and just to be on the safe side for future modifications and testing.
So I did quite a bit of research and obviously it's a specialized case but I would like to know what you would do in this situation and why?
(Its basically a helper method I call from a few different methods, a very low traffic page. More for my knowledge and learning on Static.)
private IEnumerable<Category> GetCategoryByID(int id, Context context)
{
var categoryQuery = from selectAllProc in context.SelectAll_sp()
where selectAllProc.CategoryID == id
select selectAllProc;
return categoryQuery;
}
Making private methods static is a form of micro-optimization; the method call is slightly faster. But the difference is almost too small to be meaningful.
Generally speaking, you should mark a method static when it:
Doesn't interact in any way with instance members, and
You would like to have the ability to call it without instantiating the class, as in Class.Method()
Ordinarily, methods like your example would go into their own static helper class, if they are used in more than one place.
If I were you I would ask my self the following questions.
Is it something which is related to type or instance of type?
If the answer is yes, I would be slightly inclined to make it static else, make it non static.
If you can give us some more information, the community can come up with some good options.
The first remark that comes to my mind is that by declaring this method static and possibly using it in multiple places in your code you are introducing a Service-locator kind of dependency.
As far as I know the main problem with it is that implicit dependencies are introduced, i.e. they can't be inferred by looking at method signatures.
As a consequence it can be much harder to assess the impact of a modification of your static method on the rest of your system.

What are good resources for learning about generics? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
Can anybody help me where to start and what are the essential things to learn about collections (non-generics) and generics?
Defining Collections
Collections in C#
An introduction to C# Generics
I also recommend the following book which has pretty much all the details you could want on Generics in .NET 2.0 onwards, including Generic classes, methods, delegates and constraints, how they differ from C++ templates, and the generics in the BCL.
.NET 2.0 Generics
For a really brief explanation: Regular collections store objects. The system doesn't know what kind of object is stored, so you have to cast them to the desired type when you work with them. Generic collections declare what kind of object is being put in at the time you create it. Then you always know what is there. it's like the difference between an object array and a String array.
I would definitely check out the list of links on the page PK posted for a more thorough understanding.
1) Classes can be defined with a generic type.
public class MyClass<TClass>
2) The types can be constrained using this syntax.
where TClass: struct
3) Methods also can gave generic types.
public TMethod ConvertTo<TMethod>()
4) Full Example
public class MyClass<TClass> where TClass: struct
{
private TClass _Instance;
public MyClass(TClass instance)
{
_Instance = instance;
}
public TMethod ConvertTo<TMethod>()
{
return (TMethod)Convert.ChangeType(_Instance, typeof(TMethod));
}
}

Categories