Found 2745 Articles for Csharp

Accessing Attributes and Methods in C#

karthikeya Boyini
Updated on 19-Jun-2020 08:12:55

225 Views

An attribute is a declarative tag that is used to convey information to runtime about the behaviors of various elements like classes, methods, structures, enumerators, assemblies etc. in your program. To set an attribute −[attribute(positional_parameters, name_parameter = value, ...)] ElementHere, the name of the attribute and values come inside [ ] positional parameters allow you to specify information.ExampleThe following is an example to access attribute and methods in C# −Live Demo#define DEBUG using System; using System.Diagnostics; public class Demo {    [Conditional("DEBUG")]    public static void Message(string str) {       Console.WriteLine(str);    } } class Test {    static ... Read More

Are arrays zero indexed in C#?

Samual Sam
Updated on 19-Jun-2020 08:13:38

701 Views

Yes, arrays zero indexed in C#. Let us see how −If the array is empty, it has zero elements and has length 0.If the array has one element in 0 indexes, then it has length 1.If the array has two elements in 0 and 1 indexes, then it has length 2.If the array has three elements in 0, 1 and 2 indexes, then it has length 3.The following states that an array in C# begins with index 0 −/* begin from index 0 */ for ( i = 0; i < 5; i++ ) {    n[ i ] = ... Read More

Assertions in C#

karthikeya Boyini
Updated on 19-Jun-2020 08:14:00

1K+ Views

Assert statements are an effective way to catch program logic errors at runtime. It has two arguments −A boolean expression for a true condition, andWhat to display in case of false.Assertions are useful in large and complex programs to quickly flush out errors that generally arise when the code is modified. Avoid using any function call inside the assert method.You need to be sure that whatever code you add inside an Assert should not change the output if it is removed. This is when you implement Debug. Assert in your program.To implement it, you can use a temporary variable −int ... Read More

ArrayList in C#

Samual Sam
Updated on 19-Jun-2020 07:49:26

481 Views

A resizable implementation of the List interface is called ArrayList. It is a non-generic type of collection in C# that dynamically resizes.Let us see how to initialize ArrayList in C# −ArrayList arr= new ArrayList();Add an element like the below-given code snippet −ArrayList arr1 = new ArrayList(); arr1.Add(120); arr1.Add(160);Let us see the complete example to implement ArrayList in C# −ExampleLive Demousing System; using System.Collections; public class MyClass {    public static void Main() {       ArrayList arr1 = new ArrayList();       arr1.Add(120);       arr1.Add(160);       ArrayList arr2 = new ArrayList();   ... Read More

Beginning C# programming with Hello World

karthikeya Boyini
Updated on 19-Jun-2020 07:51:01

336 Views

The following is a simple “Hello World” program in C# programming −ExampleLive Demousing System; namespace MyHelloWorldApplication {    class MyDemoClass {       static void Main(string[] args) {          // display text          Console.WriteLine("Hello World");          // display another text          Console.WriteLine("Welcome!");          Console.ReadKey();       }    } }OutputHello World Welcome!Let us see now what all it includes −using System − the using keyword is used to include the System namespace in the program.namespace declaration − A namespace is a collection of classes. The ... Read More

Array Declarations in C#

Samual Sam
Updated on 19-Jun-2020 07:54:08

160 Views

The array is a collection of variables of the same type. They are stored contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.SyntaxTo declare an array in C# −type[] arrayName;Here, type − is the datatype of the array in C#.arrayName − The name of the array[ ] − Specify the size of the array.ExampleLet us see an example to understand how to declare an array in C# −Live Demousing System; namespace MyApplication {    class MyClass {       static void Main(string[] args) {          // ... Read More

Anonymous Methods in C#

karthikeya Boyini
Updated on 19-Jun-2020 08:03:04

191 Views

Pass a code block as a delegate parameter in C#, using the Anonymous methods in C#. Anonymous methods are the methods without a name, just the body.This is how you can declare Anonymous methods −delegate void DemoMethod(int n); ... DemoMethod dm = delegate(int a) {    Console.WriteLine("Our Anonymous Method: {0}", a); };As shown above, the following is the body of the anonymous method −Console.WriteLine("Our Anonymous Method: {0}", a);ExampleYou can try to run the following code to implement Anonymous methods in C# −Live Demousing System; delegate void Demo(int n); namespace MyDelegate {    class TestDelegate {       static int ... Read More

array-of- arrays double [][] in C#?

Samual Sam
Updated on 19-Jun-2020 07:31:53

652 Views

Arrays of arrays in C# are known as Jagged Arrays. To declare jagged arrays, use the double [ ][ ].Let us now declare them −int [][] marks;Now, let us initialize it, wherein marks are arrays of 5 integers −int[][] marks = new int[][]{new int[]{ 90, 95 }, new int[]{ 89, 94 }, new int[]{ 78, 87 }, new int[]{ 76, 68 }, new int[]{ 98, 91 } };ExampleLet us now see the complete example of jagged arrays in C# and learn how to implement it −Live Demousing System; namespace MyApplication {    class MyDemoClass {       static void ... Read More

Access Modifiers in C#

karthikeya Boyini
Updated on 19-Jun-2020 07:32:31

391 Views

Access Modifiers specifies the scope of variable and functions in C#. The following are the access modifiers used provided by C#:PublicThe public modifier sets no restriction on the access of members.ProtectedAccess limited to the derived class or class definition.InternalThe Internal access modifier access within the program that has its declaration.Protected internalIt has both the access specifiers provided by protected and internal access modifiers.PrivateLimited only inside the class in which it is declared. The members specified as private cannot be accessed outside the class.ExampleLet us see an example of protected access modifier, accessing the protected members −Live Demousing System; namespace MySpecifiers ... Read More

Asynchronous programming in C# using Async and Await keyword

Samual Sam
Updated on 19-Jun-2020 07:33:17

2K+ Views

Asynchronous programming in C# is an efficient approach towards activities blocked or access is delayed. If an activity is blocked like this in a synchronous process, then the complete application waits and it takes more time. The application stops responding. Using the asynchronous approach, the applications continue with other tasks as well.The async and await keywords in C# are used in async programming. Using them, you can work with .NET Framework resources, .NET Core, etc.  Asynchronous methods defined using the async keyword are called async methods.An application with a GUI, check the content of the queue and if an unprocessed ... Read More

Advertisements