C
C#β€’2y 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β€’2y ago
it looks like a homework to do πŸ‘€
Down
Downβ€’2y ago
Not exactly a homework but yeah its school thing Can i get help with it there ?
x0rld
x0rldβ€’2y ago
you will not learn if you just get the solution what makes you struggle ?
Down
Downβ€’2y 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β€’2y 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
Downβ€’2y ago
O okay now i get it so they have Like built in get and set methods
x0rld
x0rldβ€’2y 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
Downβ€’2y ago
Okay How about the constructors and base keyword
x0rld
x0rldβ€’2y 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
Downβ€’2y ago
Okay and i can have many of them right
x0rld
x0rldβ€’2y ago
yeah you can have multiple constructors
Down
Downβ€’2y ago
Okay okay Now this one
x0rld
x0rldβ€’2y ago
i've always used the base(x) I don't really have exeperience with inheritance in c#
Down
Downβ€’2y ago
Oh okay
x0rld
x0rldβ€’2y ago
as I see the base(something) is only to call the parent constructor