C
C#2y ago
Stalk

❔ hi everyone what

i need help in c# what is get and set , and what is a args, and sender? any body can help me!
9 Replies
ero
ero2y ago
get and set are "accessors" for properties. you can individually assign logic to them to verify input and output for them. you can also omit one or the other, or give one a different accessibility $structure
MODiX
MODiX2y ago
namespace Namespace;

[Attribute]
public class Class
{
public string PublicField;
private bool _privateField;
protected double protectedField;

public int PublicProperty { get; set; }

public Class() {} // Constructor

public void Method(int parameter)
{
var localVariable = parameter;
}
}
namespace Namespace;

[Attribute]
public class Class
{
public string PublicField;
private bool _privateField;
protected double protectedField;

public int PublicProperty { get; set; }

public Class() {} // Constructor

public void Method(int parameter)
{
var localVariable = parameter;
}
}
For C# versions older than 10, see $StructureOld
ero
ero2y ago
a property could be for example;
private int _intProperty;
public int IntProperty
{
get
{
return _intProperty; // this will return _intProperty's current value
}
set
{
if (value <= 100) // `value` here is the actual value the user passed in when they did `IntProperty = 42` (42)
{
_intProperty = value;
}
else
{
// don't allow values larger than 100
throw new ArgumentOutOfRangeException();
}
}
}
private int _intProperty;
public int IntProperty
{
get
{
return _intProperty; // this will return _intProperty's current value
}
set
{
if (value <= 100) // `value` here is the actual value the user passed in when they did `IntProperty = 42` (42)
{
_intProperty = value;
}
else
{
// don't allow values larger than 100
throw new ArgumentOutOfRangeException();
}
}
}
you can also do
public int IntProperty { get; private set; }
public int IntProperty { get; private set; }
this won't have any validation of the input or output, this is called an auto-property but it's only assignable from the current class it's in args is usually a string[] or an object[] (string array, object array) that gets passed into the entry point of the program (the Main) method. it doesn't need to be there, but it can. for console apps, the args will be all additional information passed into the execution of your program $mains
MODiX
MODiX2y ago
The possible signatures for Main are
public static void Main() { }
public static int Main() { }
public static void Main(string[] args) { }
public static int Main(string[] args) { }
public static async Task Main() { }
public static async Task<int> Main() { }
public static async Task Main(string[] args) { }
public static async Task<int> Main(string[] args) { }
public static void Main() { }
public static int Main() { }
public static void Main(string[] args) { }
public static int Main(string[] args) { }
public static async Task Main() { }
public static async Task<int> Main() { }
public static async Task Main(string[] args) { }
public static async Task<int> Main(string[] args) { }
public is not required (can be any accessibility). Top-level statements are compiled into a Main method and will use an appropriate signature depending on whether args is referenced, the await operator is used and/or an explicit return statement is used. https://docs.microsoft.com/en-US/dotnet/csharp/fundamentals/program-structure/main-command-line
Main() and command-line arguments
Learn about Main() and command-line arguments. The 'Main' method is the entry point of an executable program.
ero
ero2y ago
for example, a console app can be started with YourProgram.exe arg1 arg2 the resulting args array will be a string[] where item 0 is "arg1" and item 1 is "arg2" sender (in the context of a winforms app) is the object which caused the invocation (raising, execution) of the event
Stalk
Stalk2y ago
no i mean like
void OnSliderValueChanged(object sender, ValueChangedEventArgs args)
void OnSliderValueChanged(object sender, ValueChangedEventArgs args)
ero
ero2y ago
yes in this example, sender will be the Slider which raised the OnSliderValueChanged event
Stalk
Stalk2y ago
thanks
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.