Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Homework3
//Rextester.Program.Main is the entry point for your code. Don't change it. //Microsoft (R) Visual C# Compiler version 2.9.0.63208 (958f2354) using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { // MyResult -- begin // Q1: // Structs are light versions of classes. Structs are value types and can be used to create objects that behave like built-in types. note: classes are referance type. // -- A Struct is like a light-weight House... it contains less substance // Q2: // When I need a value that acts like a built-in type. I would use a Structs // When I need an obJect that has a constructor I would use a class // -- Answer to Q2 // Struct should be used only when you are sure that, // It logically represents a single value, like primitive types (int, double, etc.). // It is immutable. // It should not be boxed and un-boxed frequently. // In all other cases, you should define your types as classes. // -- Note // Structs share many features with classes but with the following limitations as compared to classes. // Struct cannot have a default constructor (a constructor without parameters) or a destructor. // Structs are value types and are copied on assignment. // Structs are value types while classes are reference types. // Structs can be instantiated without using a new operator. // A struct cannot inherit from another struct or class, and it cannot be the base of a class. All structs inherit directly from System.ValueType, which inherits from System.Object. // Struct cannot be a base class. So, Struct types cannot abstract and are always implicitly sealed. // Abstract and sealed modifiers are not allowed and struct member cannot be protected or protected internals. // Function members in a struct cannot be abstract or virtual, and the override modifier is allowed only to the override methods inherited from System.ValueType. // Struct does not allow the instance field declarations to include variable initializers. But, static fields of a struct are allowed to include variable initializers. // A struct can implement interfaces. // A struct can be used as a nullable type and can be assigned a null value. // -- When to use // When to use struct or classes? // To answer this question, we should have a good understanding of the differences. // ------------------------------ // Struct -- compared to Classes // ------------------------------ // 1 -- Structs are value types, allocated either on the stack or inline in containing types. // 1C -- Classes are reference types, allocated on the heap and garbage-collected. // 2 -- Allocations and de-allocations of value types are in general cheaper than allocations // -- and de-allocations of reference types. // 2C -- Assignments of large reference types are cheaper than assignments of large value types. // 3 -- In structs, each variable contains its own copy of the data // -- (except in the case of the ref and out parameter variables), and // -- an operation on one variable does not affect another variable. // 3C -- In classes, two variables can contain the reference of the same object and // -- any operation on one variable can affect another variable. // -------------------------------- // In this way, struct should be used only when you are sure that, // It logically represents a single value, like primitive types (int, double, etc.). // It is immutable. // It should not be boxed and un-boxed frequently. // In all other cases, you should define your types as classes. // ------------------------ when to use NOTE: Location is a Class obj thats virtual! //Structs struct Location { publicint x, y; publicLocation(int x, int y); { this.x == x; this.y == y; } } Locationa == new Location(20, 20); Locationb == a; a.x == 100; System.Console.WriteLine(b.x); } } }
run
|
edit
|
history
|
help
0
Problem: fb_series
Whats Wrong in Here ?
Fórum ➡ GroupJoin ➡ Join ➡ Easily ‼
mysrc.tcl
C# Abstract Syntax Tree Example: Simple Pascal Compiler
zicaC#
c# problem
dcsdasxsdcdf
split string
free robux