Found 2628 Articles for Csharp

Write a C# program to check if a number is prime or not

Samual Sam
Updated on 20-Jun-2020 10:09:09

3K+ Views

To calculate whether a number is prime or not, we have used a loop and within that on every iteration, we have an if statement to find that the remainder is equal to 0, between the number itself.for (int i = 1; i

Write a C# program to check if a number is divisible by 2

Samual Sam
Updated on 20-Jun-2020 09:54:42

2K+ Views

To check if a number is divisible by2 or not, you need to first find the remainder.If the remainder of the number when it is divided by 2 is 0, then it would be divisible by 2.Let’s say our number is 10, we will check it using the following if-else −// checking if the number is divisible by 2 or not if (num % 2 == 0) {    Console.WriteLine("Divisible by 2 "); } else {    Console.WriteLine("Not divisible by 2"); }The following is an example to find whether the number is divisible by 2 or not −Example Live Demousing System; ... Read More

How to use ‘is’ operator in C#?

karthikeya Boyini
Updated on 20-Jun-2020 09:56:17

110 Views

The "is" operator in C# checks whether the run-time type of an object is compatible with a given type or not.The following is the syntax −expr is typeHere, expr is the expression and type is the name of the typeThe following is an example showing the usage of is operator in C# −Example Live Demousing System; class One { } class Two { } public class Demo {    public static void Test(object obj) {       One x;       Two y;       if (obj is One) {          Console.WriteLine("Class One");   ... Read More

How to use ‘as’ operator in C#?

Samual Sam
Updated on 20-Jun-2020 09:57:12

241 Views

The "as" operator perform conversions between compatible types. It is like a cast operation and it performs only reference conversions, nullable conversions, and boxing conversions. The as operator can't perform other conversions, such as user-defined conversions, which should instead be performed by using cast expressions.The following is an example showing the usage of as operation in C#. Here ‘as’ is used for conversion:string s = obj[i] as string;Try to run the following code to work with ‘as’ operator in C# −Example Live Demousing System; public class Demo {    public static void Main() {       object[] obj = ... Read More

Write a C# program to calculate a factorial using recursion

karthikeya Boyini
Updated on 20-Jun-2020 09:58:04

2K+ Views

Factorial of a number is what we are finding using a recursive function checkFact () in the below example −If the value is 1, it returns 1 since Factorial is 1 −if (n == 1) return 1;If not, then the recursive function will be called for the following iterations if you want the value of 5!Interation1: 5 * checkFact (5 - 1); Interation2: 4 * checkFact (4 - 1); Interation3: 3 * checkFact (3 - 1); Interation4: 4 * checkFact (2 - 1);To calculate a factorial using recursion, you can try to run the following code which ... Read More

Write a C# program to check if a number is Palindrome or not

Samual Sam
Updated on 20-Jun-2020 09:11:43

842 Views

First, find the reverse of the string to check if a string is a palindrome or not −Array.reverse()Now use the equals() method to match the original string with the reversed. If the result is true, that would mean the string is Palindrome.Let us try the complete example. Here, our string is “Madam”, which is when reversed gives the same result −Example Live Demousing System; namespace Demo {    class Program {       static void Main(string[] args) {          string string1, rev;          string1 = "Madam";          char[] ch = string1.ToCharArray(); ... Read More

Clone() method in C#

karthikeya Boyini
Updated on 20-Jun-2020 09:12:10

597 Views

The Clone() method in C# is used to create a similar copy of the array.Let us see an example to clone an array using the Clone() method −Example Live Demousing System; class Program {    static void Main() {       string[] arr = { "one", "two", "three", "four", "five" };       string[] arrCloned = arr.Clone() as string[];       Console.WriteLine(string.Join(", ", arr));       // cloned array       Console.WriteLine(string.Join(", ", arrCloned));       Console.WriteLine();    } }Outputone, two, three, four, five one, two, three, four, fiveAbove, we have a string array −string[] ... Read More

Comments in C#

Samual Sam
Updated on 20-Jun-2020 09:13:35

151 Views

Comments are used for explaining the code. Compilers ignore the comment entries. The multiline comments in C# programs start with /* and terminate with the characters */ as shown below.Multi-line comments/* The following is a multi-line comment In C# /*The /*...*/ is ignored by the compiler and it is put to add comments in the program.Single line comments// variable int a = 10;The following is a sample C# program showing how to add single-line as well as multi-line comments −Example Live Demousing System; namespace HelloWorldApplication {    class HelloWorld {       static void Main(string[] args) {     ... Read More

Classes vs Structures in C#

Samual Sam
Updated on 20-Jun-2020 09:26:22

575 Views

In C#, a structure is a value type data type. It helps you to make a single variable hold related data of various data types. The struct keyword is used for creating a structure.When you define a class, you define a blueprint for a data type.The following are the differences between classes and structures in C# −Classes are reference types and structs are value typesUnlike classes, structures cannot inherit other structures or classes.Structures cannot be used as a base for other structures or classes.When you create a struct object using the New operator, it gets created and the appropriate constructor ... Read More

Class in C#

karthikeya Boyini
Updated on 20-Jun-2020 09:45:26

123 Views

Blueprint for a data type is what you can call a class in C#. Objects are instances of a class. The methods and variables that constitute a class are called members of the class.ExampleThe following is the general form of a class in C# − class class_name {    // member variables     variable1;     variable2;    ...     variableN;    // member methods     method1(parameter_list) {       // method body    }     method2(parameter_list) {       // method body    }    ...     methodN(parameter_list) {       ... Read More

Advertisements