C
C#•3y ago
Down

beginner help

Hi so im new and i need to present in code this things: Defining a class, Field vs Property Calling functions from the base class Constructors Static function Static field I know how to make few of them but still would be nice if you show me
15 Replies
x0rld
x0rld•3y ago
it looks like a homework to do 👀
Down
DownOP•3y ago
Not exactly a homework but yeah its school thing Can i get help with it there ?
x0rld
x0rld•3y ago
you will not learn if you just get the solution what makes you struggle ?
Down
DownOP•3y ago
The Field vs property and constructors the most The calling functions from base all i struggle with is when should i use base(something) and when base.something
x0rld
x0rld•3y ago
a property is a field with a getter/setter define
public class MyClass
{
// this is a field. It is private to your class and stores the actual data.
private string _myField;

// this is a property. When accessed it uses the underlying field,
// but only exposes the contract, which will not be affected by the underlying field
public string MyProperty
{
get
{
return _myField;
}
set
{
_myField = value;
}
}

// This is an AutoProperty (C# 3.0 and higher) - which is a shorthand syntax
// used to generate a private field for you
public int AnotherProperty { get; set; }
}
public class MyClass
{
// this is a field. It is private to your class and stores the actual data.
private string _myField;

// this is a property. When accessed it uses the underlying field,
// but only exposes the contract, which will not be affected by the underlying field
public string MyProperty
{
get
{
return _myField;
}
set
{
_myField = value;
}
}

// This is an AutoProperty (C# 3.0 and higher) - which is a shorthand syntax
// used to generate a private field for you
public int AnotherProperty { get; set; }
}
Down
DownOP•3y ago
O okay now i get it so they have Like built in get and set methods
x0rld
x0rld•3y ago
yeah and for example if you want to tranform your instance of a class in json you need to have some properties cause it doesn't detect field just use them all the time 😄
Down
DownOP•3y ago
Okay How about the constructors and base keyword
x0rld
x0rld•3y ago
the contructor is a normal method but it return a new instance of your object and you have to call it like your class name
Down
DownOP•3y ago
Okay and i can have many of them right
x0rld
x0rld•3y ago
yeah you can have multiple constructors
Down
DownOP•3y ago
Okay okay Now this one
x0rld
x0rld•3y ago
i've always used the base(x) I don't really have exeperience with inheritance in c#
Down
DownOP•3y ago
Oh okay
x0rld
x0rld•3y ago
as I see the base(something) is only to call the parent constructor

Did you find this page helpful?