Found 2628 Articles for Csharp

What is method overloading in C#?

karthikeya Boyini
Updated on 20-Jun-2020 15:38:14

2K+ Views

Two or more than two methods having the same name but different parameters is what we call method overloading in C#.Method overloading in C# can be performed by changing the number of arguments and the data type of the arguments.Let’s say you have a function that prints multiplication of numbers, then our overloaded methods will have the same name but different number of arguments −public static int mulDisplay(int one, int two) { } public static int mulDisplay(int one, int two, int three) { } public static int mulDisplay(int one, int two, int three, int four) { }The following is an ... Read More

How to pass pointers as parameters to methods in C#?

Samual Sam
Updated on 20-Jun-2020 15:39:11

2K+ Views

To pass pointers as parameters to methods, refer the below steps −Firstly, crate a function swap with unsafe modifier.public unsafe void swap(int* p, int *q) {    int temp = *p;    *p = *q;    *q = temp; }Now under static void main, add the value for the first and second variable, set pointers for both of them.Display the values of the variables and then call the swap() method shown above. The method swaps the values and displays the result −public unsafe static void Main() {    Program p = new Program();    int var1 = 10;    int ... Read More

What are pointers in C#?

Arjun Thakur
Updated on 20-Jun-2020 15:38:39

5K+ Views

Pointer is a variable whose value is the address of another variable i.e., the direct address of the memory location.The syntax of a pointer is −type *var-name;The following is how you can declare a pointer type −double *z; /* pointer to a double */C# allows using pointer variables in a function of code block when it is marked by the unsafe modifier. The unsafe code or the unmanaged code is a code block that uses a pointer variable.The following is our module showing how to declare and use a pointer variable. We have used unsafe modifier here −static unsafe void ... Read More

How to pass parameters to a method in C#?

karthikeya Boyini
Updated on 20-Jun-2020 15:40:16

149 Views

To pass parameters to a method in C#, let us see how to pass parameters by value. In this mechanism, when a method is called, a new storage location is created for each value parameter.The values of the actual parameters are copied into them. Hence, the changes made to the parameter inside the method have no effect on the argument.Here is the example showing how to pass parameters to a method −Example Live Demousing System; namespace Demo {    class NumberManipulator {       public void swap(int x, int y) {          int temp;     ... Read More

How to calculate fractional power using C#?

Ankith Reddy
Updated on 20-Jun-2020 15:24:02

389 Views

To calculate fractional power in C#, use the Math.Pow method.The following sets 5 to the power 3.7 −double res = Math.Pow(5, 3.7);The following is the complete example showing how to calculate fractional power in C# −Example Live Demousing System; class Program {    static void Main() {       double res = Math.Pow(5, 3.7);       Console.WriteLine("Result = {0}", res);       Console.ReadLine();    } }OutputResult = 385.646164200006

How to call custom methods in C#?

Samual Sam
Updated on 20-Jun-2020 15:25:24

257 Views

To define a custom method in C#, use the following syntax − (Parameter List) {    Method Body }To call a custom method, try to run the following code. It has the checkPalindrome() method which is called to checker whether the binary representation is Palindrome or not −Example Live Demousing System; public class Demo {    public static long funcReverse(long num) {       long myRev = 0;       while (num > 0) {          myRev = 1;       }       return myRev;    }    public static ... Read More

How to calculate Power of a number using recursion in C#?

George John
Updated on 20-Jun-2020 15:26:30

981 Views

To calculate power of a number using recursion, try the following code.Here, if the power is not equal to 0, then the function call occurs which is eventually recursion −if (p!=0) {    return (n * power(n, p - 1)); }Above, n is the number itself and the power reduces on every iteration as shown below −Example Live Demousing System; using System.IO; public class Demo {    public static void Main(string[] args) {       int n = 5;       int p = 2;       long res;       res = power(n, p);       Console.WriteLine(res);    }    static long power (int n, int p) {       if (p!=0) {          return (n * power(n, p - 1));       }       return 1;    } }Output25

How to calculate the Size of Folder using C#?

karthikeya Boyini
Updated on 20-Jun-2020 15:26:47

3K+ Views

To calculate the size of a folder in C#, use the Directory.EnumerateFiles Method and get the files.To get the sub- directories, use the EnumerateDirectories method. Our folder is set using DirectoryInfo class −DirectoryInfo info = new DirectoryInfo(@"D:/new");Now find the size −long totalSize = info.EnumerateFiles().Sum(file => file.Length); For the directories, use −info.EnumerateDirectories()Other manipulations you can perform on Directories in C# are:MethodDescriptionCreateDirectory(String)Creates all directories and subdirectories in the specified path unless they already exist.CreateDirectory (String, DirectorySecurity)Creates all the directories in the specified path, unless the already exist, applying the specified Windows security.Delete(String)Deletes an empty directory from a specified path.Delete(String, Boolean)Deletes the specified ... Read More

How to use C# FileStream class?

Samual Sam
Updated on 20-Jun-2020 15:27:34

199 Views

A stream for file operations such as read and write is provided by the FileStream class.Create an object like thisFileStream fstream = new FileStream("d:ew.txt", FileMode.OpenOrCreate);Above we have used FileMode.OpenOrCreate so that the file or opened or created if it does not already exist.The following is n example showing how to use the FileStream class in C# −using System; using System.IO; public class Demo {    public static void Main(string[] args) {       FileStream fstream = new FileStream("d:ew.txt", FileMode.OpenOrCreate);       // write into the file       fstream.WriteByte(90);       // close the file       fstream.Close();    } }

What are the different access specifiers in C#.NET?

Chandu yadav
Updated on 20-Jun-2020 15:27:58

4K+ Views

The following are the access specifiers supported by C#.NET −Public Access SpecifierIt allows a class to expose its member variables and member functions to other functions and objects.Private Access SpecifierPrivate access specifier allows a class to hide its member variables and member functions from other functions and objects. Only functions of the same class can access its private members.Protected Access SpecifierProtected access specifier allows a child class to access the member variables and member functions of its base class.Internal Access SpecifierInternal access specifier allows a class to expose its member variables and member functions to other functions and objects in ... Read More

Advertisements