This question already has answers here:
Calling the base constructor in C#
(10 answers)
Closed 7 years ago.
namespace ClientApp
{
[Service(Exported = false)]
class RegistrationIntentService : IntentService
{
static object locker = new object();
public RegistrationIntentService() : base("RegistrationIntentService") { }
In the snippet above, the constructor extends a base("RegistrationIntentService"), what is this doing?
I found this example at:
https://developer.xamarin.com/guides/cross-platform/application_fundamentals/notifications/android/remote_notifications_in_android/
Does it mean the contructor is the same as the RegistrationIntentService constructor?
This is just calling IntentService's constructor with a string parameter "RegistrationIntentService". It is a C# language keyword and is not Xamarin specific.
If the base class of an object does not have a default constructor, you must provide parameters to a base constructor in the derived class's constructor using this syntax.
See the MSDN documentation on the base keyword for some examples.
Related
This question already has answers here:
Bodyless constructor in non-abstract C# class
(2 answers)
Closed 6 years ago.
I saw a code similar to what mentioned below :
public class SomeClass : AnotherClass
{
public SomeClass();
.
.
.
.
}
So what is the meaning of the following line of code in this, what does it do ?
public SomeClass();
Code I am referring to.
that is the constructor of the class SomeClass, and you can use it (call it) everytime you need to create a new object (AKA instance) of the class SomeClass
take a look at how to use a constructor (Thanks to CrowCoder for the comment) and if required take a look at the constructors doc
Instance constructors are used to create and initialize any instance
member variables when you use the new expression to create an object
of a class. To initialize a static class, or static variables in a
non-static class, you must define a static constructor. For more
information, see Static Constructors (C# Programming Guide).
What you are referencing is called a Constructor. Constructors have the same name as the class, and they are used to initialize data members of the new object.
Please reference.
Whenever a class or struct is created, its constructor is called. A class or struct may have multiple constructors that take different arguments. Constructors enable the programmer to set default values, limit instantiation, and write code that is flexible and easy to read.
If you saw code similar to what you mentioned, then honestly that would cause a compile time error, since the syntax for that default constructor (taking 0 parameters) is incorrect.
Needs to be:
public SomeClass()
{
//initialize members;
}
If you retrieved the code that you just posted a picture of, by hitting Go To Definition of the class, then it will only show declarations, and not implementation.
This question already has answers here:
C# Interfaces. Implicit implementation versus Explicit implementation
(13 answers)
Closed 9 years ago.
I was always using interfgaces similarly to abstract classes - just to make sure all objects will have consistent external methods etc.
But from MSDN, I do not get that:
interface ISampleInterface
{
void SampleMethod();
}
class ImplementationClass : ISampleInterface
{
// Explicit interface member implementation:
void ISampleInterface.SampleMethod()
{
// Method implementation.
}
static void Main()
{
// Declare an interface instance.
ISampleInterface obj = new ImplementationClass();
// Call the member.
obj.SampleMethod();
}
}
Why there is explicitly mentioned interface name in the method declaration and why it does not work without it?
Also why is the interface being instantionated, shouldn't be just instance of class implementing it?
Why there is explicitly mentioned interface name in the method
declaration and why it does not work without it?
Sometimes it is handy to make your interface explicitly implemented. See this other Stack Overflow questions answer: https://stackoverflow.com/questions/408415/why-explicit-interface-implementation
Also why is the interface being instantionated, shouldn't be just
instance of class implementing it?
They instanciate the ImplementationClass but downcast it to the ISampleInterface Thus preventing themselves from touching non-interface contract guaranteed methods/properties.
This question already has answers here:
Passing arguments to C# generic new() of templated type
(16 answers)
Closed 9 years ago.
I am making a base class from which other classes can be derived.
public class BaseClass<T> where T
{
public BaseClass()
{
TClassObject = new T("SomeText"); // Error here
}
public T TClassObject { get; set; }
}
'T': cannot provide arguments when creating an instance of a variable type.
What I am missing here.
From MSDN:
The new constraint specifies that any type argument in a generic class declaration must have a public parameterless constructor.
So it needs to be parameterless. You may want to look at Activator.CreateInstance
http://msdn.microsoft.com/en-us/library/system.activator.createinstance.aspx
The where T : new() constraint states that T must have a parameterless constructor. Your code is calling into a constructor that takes a string parameter, and it isn't guaranteed that your T will have such a constructor.
It is not possible in C# to create a constraint on a specific constructor signature. If you need this functionality, you're better off using something like one of the answers in this thread.
This question already has answers here:
Is it possible to have a private class?
(4 answers)
Closed 9 years ago.
Whats the difference between "Class" only and "Private Class" declaration in C#?
If these are nested classes, there's no difference:
namespace Foo
{
public class Outer
{
private class ExplicitlyPrivate {}
class ImplicitlyPrivate {}
}
}
Type members always default to being private.
If it's a top-level class, then you can't make it private - but the default is internal:
namespace Foo
{
class ClassIsInternalByDefault {}
}
When you declare a class without specifying an accessibility modifier it will default to the lowest accessibility possible.
More practically, specifying private when private is not permissible can result in a compilation error.
A simple answer is to say that a private class is meant to protect attributes within that class from being changed by any external classes, other than during construction of the program. A normal "class", well, doesn't have that protection.
It is a form of ENCAPSULATION.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does âTâ mean in C#?
What does <T> denote in C#
I have searched about the question, but because I do not have any idea about that syntax, I could not search well enough. Well, after the short explanation my question is :
While I was looking to a particle effect library I meet this :
[Serializable]
public class DPSF<Particle, Vertex> : IDPSFParticleSystem
where Particle : global::DPSF.DPSFParticle, new()
where Vertex : struct, global::DPSF.IDPSFParticleVertex
{
// constructors, methods, parameters here ...
}
What does this notation mean in blankets (<...>) and how it is used ?
They are generic types that you can pass into a class definition:
A simple example:
public class Test<T>
{
public T yourVariable;
}
Test<int> intClass = new Test();
Test<string> stringClass = new Test();
intClass.yourVariable // would be of type int
stringClass.yourVariable // would be of type string
Where T is the type you want (i.e. another class, a string, integer, anything generic)
where Particle : global::DPSF.DPSFParticle - means that the Particle object must inherit from DPSF.DPSFParticle
It means that DPSF class is a generic class. Particle and Vertex between <> are type parameters. It means that DPSF gets two types as parameters and serves as a generic class.
Take a look at here: MSDN: Generics
EDIT
Where keyword allows you to restrict type parameters. where:class means parameter must be a class in order this generic class to work.