// Passing By Output
// Passing by output is similar to Reference params. Except that Output params transmit data
// out of the method, not accept data in it. Passing by Output technic defined by "out" keyword.
// So you will use "out" keyword for this purpose.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NewSandbox
{
class Program
{
static void YourMethod(out int x, out int y)
{
x = 4;
y = 6;
}
static void Main(string[] args)
{
int x, y;
YourMethod(out x, out y);
Console.WriteLine(x); //output will be 4
Console.WriteLine(y); //output will be 6
Console.ReadKey();
}
}
}
English etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
English etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
20 Şubat 2018 Salı
(2/3) Passing argument by Reference
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
static void YourMethod(ref int m)
{
m = m * m;
}
static void Main(string[] args)
{
int number = 5;
YourMethod(ref number); // Now the "number" variable has affected
Console.WriteLine(number); //output will be 25. NOT 5.
Console.ReadKey();
}
}
}
(1/3) Passing argument by Value
// 1) Passing By Value
// C# creates a copy of the given variable's value and put it
// into the method's parameter. Ergo you can do any changes you
// want on the parameter within the method without making any
// changes on the argument. I.e you are changing only the "Copy
// of the Argument's Value". Not given argument or argument's value.
// -By default, C# will use -By Value- technic to passing the arguments.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NewSandbox
{
class Program
{
static void YourMethod(int m){
m = m*m;
}
static void Main(string[] args)
{
int number = 5;
YourMethod(number); //output will be 5
}
}
}
// Brief explanation: The method will operate on the VALUE, not the ACTUAL VARIABLE.
14 Aralık 2017 Perşembe
passing by Reference & Output together in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NewSandbox
{
class Program
{
static void YourMethod(ref int x, out int y)
{
x = 4;
y = 6;
}
static void Main(string[] args)
{
int x =100, y = 200;
YourMethod(ref x, out y);
Console.WriteLine(x); //output will be 4
Console.WriteLine(y); //output will be 6
Console.ReadKey();
}
}
}
22 Kasım 2017 Çarşamba
What is Encapsulation & 'Private' Access Modifier
We have created a property named “Age” without a field and
encapsulated it with "private" access modifier to restrict
directly access on it. So, the "Age" property is unreachable
from the outside of the given class (The class that we gonna
use in this example is “Person”). Thats what "private" access
modifiers do. This process is called "Encapsulation".
It's a meaningful identifier since we are "encapsulating"
the given property and hiding it from the outer-space.
With the encapsulation in this example, you are not able to
change the "Age" property by directly addressing it. For example:
Person p1 = new Person();
p1.Age = 33;
/*That will not work as expected.
If we could use the "public" access modifier, that expression
could work properly. Lets see another example:
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
class Person{
private int Age = 30;
public void IncAge(int n){
Age += n;
}
public void DecAge(int n){
Age -= n;
}
public int GetAge(){
return Age;
}
}
static void Main(){
Person p1 = new Person();
p1.DecAge(4);
Console.WriteLine(p1.GetAge());
p1.IncAge(10);
Console.WriteLine(p1.GetAge());
p1.Age = 23;
/*
As you can see, this line will cause an error too since
"Age" Property's access modifier has set to "Private".
It can’t be reached from this Main Method or any other
methods in your project for reasons that i have wrote above.
*/
}
}
}
Overloading a Method (misuse)
// When overloading methods, methods' parameters'
// definitions must be differ from each other.
// You cannot overload these methods for example:
int NewMethod(int number);
int NewMethod(int number2);
int NewMethod(int number3);
// These declarations will result an error.
21 Kasım 2017 Salı
Overloading Sample (string a, int b)
We'll declare a method that has string and double parameters in this example.
When you overloading methods, the methods will be called based on the
argument's type. A double argument will call the method which gets a "double"
inside it. An integer argument will match with a method which gets a "int" inside it.
Thats the point of the Overloading. Lets see it in example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoloLearn
{
class Program
{
static void Main()
{
Console.WriteLine("You are in Main Method.");
Console.WriteLine("---");
Enter(5);
Console.WriteLine("---");
Enter(5.2);
Console.WriteLine("---");
New("A text here. The number is:", 666);
}
static void Enter(int n)
{
Console.WriteLine("You are in Enter Method with INT parameter");
Console.WriteLine(n);
}
static void Enter(double n)
{
Console.WriteLine("You are in Enter Method with DOUBLE parameter");
Console.WriteLine(n);
}
static void New(string st, double s)
{
Console.WriteLine("You are in 'New' Method with 2 parameter: string, double");
Console.WriteLine(st + s);
}
}
}
Kaydol:
Kayıtlar (Atom)