Found 2628 Articles for Csharp

What is the System.Reflection.Module in C#?

Chandu yadav
Updated on 21-Jun-2020 14:18:33

205 Views

The System.Reflection namespace contains classes that allow you to obtain information about the application and to dynamically add types, values, and objects to the application.It has a module constructor that initializes a new instance of the Module class. A module is a portable executable file that has one or more classes and interfaces.Let us see an example of System.Reflection in C# −Exampleusing System; using System.Reflection; [AttributeUsage(AttributeTargets.All)] public class HelpAttribute : System.Attribute {    public readonly string Url;    public string Topic // Topic is a named parameter {       get {          return ... Read More

How to define character constants in C#?

Chandu yadav
Updated on 21-Jun-2020 13:50:27

195 Views

Character literals are enclosed in single quotes. For example, 'x' and can be stored in a simple variable of char type. A character literal can be a plain character (such as 'x'), an escape sequence (such as '\t'), or a universal character (such as '\u02C0').Let us see an example how to define a character constant in C# −using System; namespace Demo {    class Program {       static void Main(string[] args) {          Console.WriteLine("Welcome!\t");          Console.WriteLine("This is it!");          Console.ReadLine();       }    } }Above, we ... Read More

Variable Arguments (Varargs) in C#

Arjun Thakur
Updated on 21-Jun-2020 13:59:58

4K+ Views

Use the param keyword to get the variable arguments in C#.Let us see an example to multiply integers. We have used params keyword to accept any number of int values −static int Multiply(params int[] b)The above allows us to find multiplication of numbers with one as well as two int values. The fllowing calls the same function with multiple values −int mulVal1 = Multiply(5); int mulVal2 = Multiply(5, 10);Let us see the complete code to understand how variable arguments work in C# −Exampleusing System; class Program {    static void Main() {       int mulVal1 = Multiply(5); ... Read More

How to create a Directory using C#?

Samual Sam
Updated on 21-Jun-2020 14:00:14

250 Views

To create, move and delete directories in C#, the System.IO.Directory class has methods.Firstly, import the System.IO namespace.Now, use the Director.CreateDirectory() method to create a directory in the specified path −string myDir = @"D:\NEW"; if (!Directory.Exists(myDir)) {    Directory.CreateDirectory(myDir); }In the same way, you can create a sub-directory −string mysubdir = @"C:\NEW\my\"; Directory.CreateDirectory(mysubdir);

Value Type vs Reference Type in C#

Ankith Reddy
Updated on 21-Jun-2020 14:01:19

5K+ Views

Value Type and Reference, both are types in C# −Value TypeValue type variables can be assigned a value directly. They are derived from the class System.ValueType. The value types directly contain data. When you declare an int type, the system allocates memory to store the value.Value Type variables are stored in the stack.Examples are int, char, and float, which stores numbers, alphabets, and floating point numbers, respectively.Reference TypeIt refers to a memory location. Using multiple variables, the reference types can refer to a memory location. If the data in the memory location is changed by one of the variables, the ... Read More

How to create a Dictionary using C#?

karthikeya Boyini
Updated on 21-Jun-2020 14:02:04

157 Views

Dictionary is a collection of keys and values in C#. Dictionary is included in the System.Collection.Generics namespace.To create a dictionary, you first need to set it and add the key and values. Here we have added 5 keys with values to a Dictionary. We have set its keys and value type as int.IDictionary d = new Dictionary(); d.Add(1, 44); d.Add(2, 34); d.Add(3, 66); d.Add(4, 47); d.Add(5, 76);The following is the complete code −Exampleusing System; using System.Collections.Generic; public class Demo {    public static void Main() {       IDictionary d = new Dictionary();       d.Add(1, 44); ... Read More

How to count the number of items in a C# list?

George John
Updated on 21-Jun-2020 13:35:06

577 Views

Use the Array.Count property in C# to count the number of items in a list in C# −Set the listList myList = new List() {    "electronics",    "clothing",    "appliances",    "accessories" };Now count the number of items in a list in C# −myList.CountThe following is the complete code to count the number of items in a list −Exampleusing System; using System.Collections.Generic; class Program {    static void Main() {       List myList = new List() {          "electronics",          "clothing",          "appliances",          "accessories"       };       Console.WriteLine(myList.Count);    } }

What is the difference between function overriding and method hiding in C#?

Samual Sam
Updated on 21-Jun-2020 13:36:43

519 Views

OverridingUnder overriding, you can define a behavior that's specific to the subclass type, which means a subclass can implement a parent class method based on its requirement.Let us see an example of abstract classes that implements Overriding −Exampleusing System; namespace PolymorphismApplication {    abstract class Shape {       public abstract int area();    }    class Rectangle: Shape {       private int length;       private int width;       public Rectangle( int a = 0, int b = 0) {          length = a;       ... Read More

How to copy a section of an array into another array in C#?

Chandu yadav
Updated on 21-Jun-2020 13:38:56

207 Views

The Array.Copy() method in C# is used to copy section of one array to another array.The following is the syntax −Array.Copy(src, dest, length);Here,src = array to be copieddest = destination arraylength = how many elements to copyThe following is an example showing the usage of Copy(,,) method of array class in C# −Exampleusing System; class Program {    static void Main() {       int[] arrSource = new int[4];       arrSource[0] = 24;       arrSource[1] = 33;       arrSource[2] = 9;       arrSource[3] = 45;       int[] arrTarget = new int[3];       Array.Copy(arrSource, arrTarget, 3);       Console.WriteLine("Destination Array ...");       foreach (int value in arrTarget) {          Console.WriteLine(value);       }    } }

How to convert a 2D array into 1D array in C#?

karthikeya Boyini
Updated on 21-Jun-2020 13:42:39

2K+ Views

Set a two-dimensional array and a one-dimensional array −int[, ] a = new int[2, 2] {{1, 2}, {3, 4} }; int[] b = new int[4];To convert 2D to 1D array, set the two dimensional into one-dimensional we declared before −for (i = 0; i < 2; i++) {    for (j = 0; j < 2; j++) {       b[k++] = a[i, j];    } }The following is the complete code to convert a two-dimensional array to one-dimensional array in C# −Exampleusing System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Program {    class twodmatrix { ... Read More

Advertisements